According to a report by Statista, global e-commerce sales are expected to reach 8.1 trillion dollars by 2026, up from $3.53 trillion in 2019. With this growth comes an increased demand for secure and efficient payment processing.

The e-commerce industry is growing, and businesses are looking for ways to improve payment processing and enhance the customer experience. Payment gateway integration with Salesforce can streamline payment processing and improve cash flow management.

In this article, we’ll discuss how to integrate the Cashfree payment gateway with Salesforce, including a problem statement, solution design, process diagram, implementation details, code snippet (high level), explanation module-wise, and snapshots of the entire Cashfree payment life cycle.

By following the steps outlined in this guide, businesses can achieve seamless and secure payment integration with Salesforce, providing a better customer experience and increasing sales.

Problem statement

  • Integrate Salesforce with Cashfree Payment Gateway (only for payment purposes). Based on the account or opportunity’s data, the payment link should be a trigger for the end user in Salesforce.
  • Once the end user receives an email from ABC Company, he or she should be able to pay the amount by clicking the button inside the email body.
  • Once the payment is made, ABC Company should be able to get information about the payment status inside the Salesforce Org, and the end user should automatically get an email with an INVOICE pdf attachment.

Solution design

  1. Identify the API methods: First, we need to identify the API methods provided by the Cashfree payment gateway that can be used to perform various payment-related operations such as payment creation, payment verification, payment refund, etc.
  2. Define data model: Based on the identified API methods, we can define a data model to store payment-related data such as payment ID, payment status, payment amount, payment date, etc.
  3. Create Apex classes: We can create Apex classes to interact with Cashfree payment gateway APIs and perform various payment-related operations such as creating payments, verifying payments, refunding payments, etc.
  4. Implement the integration: Once the Apex classes are created, we can implement the integration by calling the appropriate methods of the Apex classes from within Salesforce based on the payment-related operations needed.
  5. Test the integration: Finally, we must thoroughly test the integration to ensure it works as expected.

Process diagram

Implementation details – Salesforce integration with Payment Gateway

  1. Identify the API methods: We can refer to the Cashfree payment gateway documentation to identify the API methods that can be used for payment-related operations. Cashfree provides both REST and SDK-based APIs.
  2. Define the data model: We can create a custom object in Salesforce to store payment-related data. This object can have fields such as Payment ID (text), Payment Status (picklist), Payment Amount (currency), Payment Date (DateTime), etc.
  3. Create Apex classes: We can create two Apex classes, one for creating payments and another for verifying payments.
  4. The Apex class for creating payments can have methods to create a payment request, receive the payment response, and update the payment object with the payment details such as payment ID, payment status, payment amount, payment date, etc.
  5. The Apex class for verifying payments can have methods to verify the payment status based on the payment ID and update the payment object with the updated payment status.
  6. Implement the integration: We can create a custom Visualforce page or a Lightning component that provides the user interface to initiate payment-related operations, such as creating and verifying payments. This UI component can call the appropriate methods of the Apex classes created in Step 3 to perform the payment-related operations.
  7. Test the integration: We can create test data in Salesforce and perform various payment-related operations, such as creating payments, verifying payments, and refunding payments. We can also test the error handling scenarios, such as invalid payment requests, incorrect payment responses, etc.
  8. Invoice pdf: We can create an invoice pdf from opportunity data using the Visualforce page and send it to the end user through the Apex class.

Salesforce Payment Integration Code Snippet (High Level)  

  • Here is a high-level code snippet for creating a payment request using the Cashfree REST API:
HttpRequest req = new HttpRequest();
req.standpoint(' https://sandbox.cashfree.com/pg/links);
req.setMethod('POST');
          req.setHeader('Content-Type', 'application/json');
req.setHeader('X-Client-Id', '<YOUR_CLIENT_ID>');
req.setHeader('X-Client-Secret', '<YOUR_CLIENT_SECRET>');
req.setBody('{"link_Id":"23R0001","linkAmount":"100","linkCurrency":"INR","customerEmail":"customer@example.com","customerPhone":"9999999999","linkNote":"Test link","notifyUrl":"https://example.com/notify"}');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
  • This code sends a POST request to the Cashfree payment gateway API to create a payment request with the specified order details. The X-Client-Id and X-Client-Secret headers are used to authenticate the API request with the Cashfree payment gateway. The response body contains payment details such as payment ID, payment status, etc.

Explanation module-wise

Part 1:

  • Create a custom field called Cashfree Payment Status on the Opportunity object to store the payment status.
  • Create a custom field called payment link on the Opportunity object to store the payment link address.
  • Create a button on the Opportunity Record using the LWC component.
  • With a button click, retrieve the opportunity record’s data and call the apex class. 
  • The Apex class will perform an HTTP callout of the opportunity data using the post method. 
  • In response, we will get the payment link from a cash-free gateway.
  • Store this payment link on the opportunity record and send this link to the end client using email alerts and lightning flows. 
  • Once the payment link is sent, create a payment-related record automatically to track the payment history. 

Part 2:

  • Create a button to check whether the payment is done or not on the payment-related record using the LWC component.
  • With a click of a button, it will call the apex class.
  • The Apex class will perform an HTTP callout using Payment ID, checking the payment status of the CashFree gateway. 
  • If payment is made, update the status field of Opportunity’s record. 
  • Send the invoice pdf and save it to Opportunity under Notes and Attachment using the Visualforce page and Apex Trigger.

Snapshots of the entire CashFree Payment life cycle

  1. “Send Payment Link” button in Opportunity Object

Payment link: Modal with recipient details

  1. An email with the payment link and details triggers the client.
  1. After successful payment completion, a confirmation email will be sent to the client.
  1. Invoice Template format sent to Client from Salesforce

Additional information 

Salesforce merchant services 

Salesforce Merchant Services is a platform that helps businesses manage payments and customer information. It makes payment processing more secure and efficient.

Cashfree Payment Gateway is a service businesses can use to process customer payments. It accepts various payment methods like credit cards, debit cards, and net banking.

  • By integrating Cashfree Payment Gateway with Salesforce Merchant Services, businesses can manage their payments and customer information directly from the Salesforce platform.
  • This integration makes payment processing faster and more accurate, which means fewer errors and less time spent on manual data entry.
  • The integration also provides advanced fraud prevention features to ensure secure transactions.
  • With Cashfree Payment Gateway and Salesforce Merchant Services integration, businesses can offer their customers a wide range of payment options, making it easier and more convenient for customers to make payments.
  • This integration can help businesses increase sales and improve customer satisfaction by providing a hassle-free payment experience.

Quick summary 

  • The e-commerce industry is expanding, and companies are searching for ways to improve payment processing and customer experience.
  • Payment gateway integration with Salesforce can help you enhance cash flow management and streamline payment processing.
  • Cashfree Payment Gateway may be coupled with Salesforce to provide a seamless and secure payment integration, resulting in a better customer experience and higher sales.
  • The integration can be completed by finding the Cashfree API functions, building a data model, generating Apex classes, implementing the integration, and extensively testing.
  • A custom Salesforce object can be developed to hold payment-related data, and a custom Visualforce page or Lightning component can be created to offer the user experience for payment-related tasks.
  • Payment-related processes such as making payments, verifying payments, and refunding payments can be carried out using the built Apex classes.
  • An Apex class can generate an invoice PDF using opportunity data and send it to the end user.
  • A high-level code snippet for making a payment request using the Cashfree REST API is supplied, which entails submitting a POST request to the Cashfree payment gateway API to build a payment request with the requested order data.