Sample Code: Submit Payment

There are different ways to integrate with the payment functionality of Payment Center depending on your needs. If you have an application outside Salesforce and you want to use Payment Center to process payments for you, you can use the Payment Center REST API. If you simply wanted a button to process payments inside Salesforce, or a payment link on an email, this may be the one for you. But what if you have your own custom payment page (could be a Visualforce page) and you simply want to call an Apex class to process payment? You can do that by calling our global classes.

Below is a sample code utilizing our global class to submit payment to the default processor.



public with sharing class PaymentCenterSampleCodes {
    
    public static void payNow() {
        
        // formulate payment request, data here will normally come from your own interface
        fw1__Payment__c payment = formulatePaymentRequest();
        
        // submit payment to processor (the actual callout happens here)
        fw1.ProcessorResponseModel response = doPayment(payment);
        
        // parse response
        if (response.IsSuccessful) {
            // payment has been processed successfully by the processor and a payment record is created in Salesforce
            System.debug('Payment ID in Salesforce: ' + response.ReferenceId);
        }
        else {
            // payment has failed, no payment record has been created in Salesforce
            System.debug('Payment Failure Reason: ' + response.Message);
        }
        
    }
    
    
    private static fw1__Payment__c formulatePaymentRequest() {
        
        fw1__Payment__c payment = new fw1__Payment__c();
        
        payment.fw1__Payment_Processor__c = getDefaultPaymentProcessor();
        payment.fw1__API_Payment__c = true;
        payment.fw1__Payment_Method__c = 'Credit Card';
        payment.fw1__Type__c = 'Charge'; // possible values: Charge/Authorization
        payment.fw1__Payment_Date__c = System.Now();
        
        // THE FOLLOWING DATA WOULD NORMALLY COME FROM YOUR INTERFACE
        
        payment.Name = 'Sample payment'; // any name to identify the payment
        payment.fw1__Amount__c = 10.00; // amount to charge
        payment.fw1__Email_Currency_Symbol__c = '$';
        payment.fw1__Credit_Card_Type__c = 'Visa';
        payment.fw1__First_Name__c = 'Joe';
        payment.fw1__Last_Name__c = 'Smith';
        payment.fw1__Credit_Card_Number__c = '4111111111111111';
        payment.fw1__Expiry_Month__c = '01';
        payment.fw1__Expiry_Year__c = '2018';
        payment.fw1__Billing_Street__c = '12345 sepulveda st';
        payment.fw1__Billing_City__c = 'Torrance';
        payment.fw1__Billing_State__c = 'CA';
        payment.fw1__Billing_Zip__c = '90505';
        payment.fw1__Billing_Country__c = 'US';
        payment.fw1__Email__c = 'test@yourcompany.com'; // optional, if populated a receipt will be sent to this email
        
        // You can optionally attach the payment to an account, contact or opportunity by populating the fields below
        
        // payment.fw1__Account__c = 
        // payment.fw1__Contact__c = 
        // payment.fw1__Opportunity__c = 
        
        return payment;
    }
    

    private static String getDefaultPaymentProcessor() {
        String defaultPaymentProcessor = '';
        
        fw1__Payment_Processor__c pProcessor = new fw1__Payment_Processor__c();
        try {
            pProcessor = [SELECT Name
                            FROM fw1__Payment_Processor__c
                           WHERE fw1__Use_As_Default__c = true
                           LIMIT 1];
            defaultPaymentProcessor = pProcessor.Name;
        } catch (Exception e) {
            System.Debug('There is no default Payment Processor: ' + e);
            try {
                pProcessor = [SELECT Name
                                FROM fw1__Payment_Processor__c
                               LIMIT 1];
                defaultPaymentProcessor = pProcessor.Name;
            } catch (Exception ex) {
                System.Debug('There are no Payment Processors.' + ex);
            }
        }
        return defaultPaymentProcessor;
    }
    
    
    private static fw1.ProcessorResponseModel doPayment(fw1__Payment__c payment) {
        fw1.ProcessorResponseModel response = new fw1.ProcessorResponseModel();
        
        response.IsSuccessful = fw1.PaymentProcessor.doPayment(payment);
        response.ReferenceId = payment.Id;
        response.Message = payment.fw1__Payment_Result__c;
        response.ErrorType = payment.fw1__Error_Type__c;
        response.ErrorCode = payment.fw1__Error_Code__c;
        
        return response;
    }
}


To run the code, follow the steps below:

  1. Install Payment Center in your development or sandbox org.
  2. Create a new Apex class and copy and paste the code above.
  3. The sample code uses a test credit card number, so ensure that your payment processor is in test mode. More info here.
  4. Execute the sample code in this form:
    PaymentCenterSampleCodes.payNow();
  5. If successful, a test payment record will be created in the Payment object.
  6. The actual global class that does the payment is this:
    fw1.PaymentProcessor.doPayment(payment);

    The rest of the code are “prep” code in order to formulate the payment request to be passed to the global class.

Test Class

When creating your test class, there are setup records that need to be created in order for the global class to run error-free. Here is an example of a test class for the sample code above:


@isTest
public class PaymentCenterSampleCodeTests {

    public static testMethod void testPayNow() {
        
        // SETUP RECORD - add Payment Center Settings
        fw1__Payment_center_setting__c setting = new fw1__Payment_center_setting__c();
        setting.Name = 'Default Settings';
        insert setting;
        
        // SETUP RECORD - add processor
        fw1.PackagedData.restoreStripeData();
        
        PaymentCenterSampleCodes.payNow();
    }

}

You will need to add the setup records at the top of your test class before calling the global class.

 

 

Was this article helpful?

Related Articles

Comments are closed.

  • About Us

    Kulturra.com builds enterprise apps on the Force.com platform. We have created some of the most popular apps on the AppExchange, and are being used by over 2000+ Salesforce users and continues to grow.

  • Our Culture

    As a company, we believe in simple. So we build simple applications out of complex processes. We also provide unparalleled customer support because we truly believe that our customers' success is our success. Read more on what our customers say about us.
  • Check Our Customer Reviews

    2015sf_Partner_Available_On_appexchange_RGB_Gray_Border
Copyright © 2018 Kulturra.com, Inc.
21151 S. Western Ave., Suite 215, Torrance, CA 90501, USA
MSA
All trademarks, trade names and logos appearing on the site are the property of their respective owners.
^ Back to Top