Salesforce PayPal Integration

Salesforce

In this session, we will show how you can easily connect the PayPal Payflow Pro payment gateway to Salesforce, in order to charge credit cards directly from Salesforce, either via embedded buttons.

Agenda:

  1. Purpose of Integration
  2. What is API?
  3. What is WebService?
  4. Live Demo
    1. Create a Paypal Account.
    2. Create Sandbox in PayPal Account.
    3. Paypal Connected App
    4. Paypal API
      1. Create Payment using Paypal Method
      2. Create Payment using Credit Card Method
      3. Create Order using API
      4. Create Invoice
      5. Send Invoice
      6. Create Product
      7. Create Product Subscription
  5. Object Structure of the Objects involved in Demo
  6. Walkthrough of how to set up the Integration and test API using Postman tool.
  7. Q & A

Here is the Link to Request Body that we used in our demo

https://docs.google.com/document/d/1JI1KuBE9vmfB5Ts6-msOdWINI7y_TkX0fFZpOHhCxi4/edit?usp=sharing

image copyrights to #ApexHours

Here is the recording of the session

Here is the PPT involved used for presentation

8 comments

  • Hey Amit,

    This was great session. I have few queries, when I am trying to implement.

    Even though I am able to hit the endpoint and getting Success Response, I am not able to see the Amount in the Activities for PayPal method. And one more thing, for Credit card, how we have create the CARD ID which I am not able to find anywhere.

    Appreciate your help on this

  • Hi Amit,
    I have been trying this integration, all the instances paypal method of payment is not working.
    I am getting success response and also I have paid, its redirecting to the URL which I have given. But I dont see that transaction in the Business Account.
    Do you have any idea what might be the reason?

    • Thanks,
      Here is the code for the same.
      public static List apiDetails(){

      String env = System.Label.PayPal_Environment;

      List paypalCred = new List();
      paypalCred = [Select Id, MasterLabel, Client_ID__c, Client_Secret__c,
      Token_URL__c, grant_type__c, Content_Type__c, Method__c From Paypal_API__mdt
      Where MasterLabel =:env Limit 1];
      return paypalCred;

      }

      public static void generateCredentials(){

      String env = System.Label.PayPal_Environment;

      List paypalCred = apiDetails();

      If(paypalCred != null && paypalCred.size() > 0){

      String clientId = paypalCred.get(0).Client_ID__c;
      String clientSecret = paypalCred.get(0).Client_Secret__c;
      String tokenURL = paypalCred.get(0).Token_URL__c;
      String grantType = paypalCred.get(0).grant_type__c;
      String contentType = paypalCred.get(0).Content_Type__c;
      String method = paypalCred.get(0).Method__c;

      /* Prepare Http Request to Get the Paypal Token Credentials */

      String dataToEncode = clientId+':'+clientSecret;
      Blob blobValue = Blob.valueOf(dataToEncode);
      String encodedData = EncodingUtil.base64Encode(blobValue);

      /*System.debug(' #### encodedData '+encodedData);*/

      String body = 'grant_type='+grantType;

      /* Prepare a Var for Error */
      String errorMessage = '';

      HttpRequest HttpReq = new HttpRequest();
      HttpResponse httpRes = new HttpResponse();

      HttpReq.setEndPoint(tokenURL);
      httpReq.setMethod(method);
      httpReq.setHeader('Authorization', 'Basic '+encodedData);
      httpReq.setHeader('Content-Type' , contentType);
      httpReq.setBody(body);
      httpReq.setTimeOut(120000);

      try{
      httpRes = (new Http()).send(httpReq);
      Integer statusCode = httpRes.getStatusCode();
      String responseBody = httpRes.getBody();
      if(statusCode == 200){
      /*System.debug(' responseBody '+responseBody);*/
      Map responseMap = (Map)JSON.deserializeUntyped(responseBody);
      String scope = (String)responseMap.get('scope');
      String access_token = (String)responseMap.get('access_token');
      String token_type = (String)responseMap.get('token_type');
      String app_id = (String)responseMap.get('app_id');
      Integer expires_in = (Integer)responseMap.get('expires_in');
      String nonce = (String)responseMap.get('nonce');
      DateTime experiesInTime = System.now().addSeconds(expires_in);

      /* Create the Metadata for the Token */
      String fullName = 'PayPal_TokenInfo.'+env;
      Map fieldValueMap = new Map();
      fieldValueMap.put('access_token__c', access_token);
      fieldValueMap.put('app_id__c', app_id);
      fieldValueMap.put('expires_in_seconds__c', expires_in);
      fieldValueMap.put('expires_in_time__c', experiesInTime);
      fieldValueMap.put('nonce__c', nonce);
      fieldValueMap.put('scope__c', scope);
      fieldValueMap.put('token_type__c', token_type);

      /* Create/Update the Metadata Record */
      CreateUpdateMetadataUtils.createUpdateMetadata(fullName, env, fieldValueMap);

      }else{
      System.debug(' Error Occured '+responseBody);
      errorMessage = 'Unexpected Error while communicating with PayPal API. '
      +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
      }
      }catch(System.Exception e){
      System.debug('#### Exception Excuted '+e.getStackTraceString()+' '+e.getMessage());
      if(String.valueOf(e.getMessage()).startsWith('Unauthorized endpoint')){
      errorMessage = 'Unauthorize endpoint: An Administer must go to Setup -> Administer -> Security Control ->'
      +' Remote Site Setting and add '+' '+ tokenURL+' Endpoint';
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
      }else{
      errorMessage = 'Unexpected Error while communicating with PayPal API. '
      +'Status '+httpRes.getStatus()+' and Status Code '+httpRes.getStatuscode();
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, errorMessage));
      }
      }
      }
      }

Leave a Reply