allBlogsList

Redeeming Gift Certificates in Salesforce Commerce Cloud SFRA

Using Gift Certificates as a payment method in Salesorce Commerce Cloud SFRA Checkout

The Salesforce Commerce Cloud SFRA platform does not allow a customer to use Gift Certificates as a payment method by default. A little bit of coding work is required to enable this option on your eCommerce website. 

Introduction

In my last article (found here), I detailed how to sell Gift Certificates through a Salesforce Commerce Cloud (SFCC) Storefront Reference Architecture (SFRA) storefront. Now for the second half of the project: allowing customers to redeem those Gift Certificates during checkout.

Setup

There is some Business Manager (BM) configuration that needs to be completed before code is written. First the “Gift Certificate” payment method must be enabled under Merchant Tools -> Ordering -> Payment Methods. If you’d like to restrict the payment method to a specific group, country, or fund range, this is the place to do so.

You’ll also want to make sure the Payment Processor “BASIC_GIFT_CERTIFICATE” is assigned to this payment method. It should come this way out of the box, but it’s always good practice to check.

Code

If you already have a hooks.js file in your base cartridge, you’ll need to add the following lines to handle the payment processing for Gift Certificates.

Gift Certificate HookHooks.js

If you don’t have a hooks.js file in your base cartridge, you’ll want to add both the hooks.js file and a package.json file to point to the hooks.js file. More information about hooks can be found in the Infocenter.

For this implementation, the Gift Certificate addition area has been added near the billing form, but outside of the normal payment submission methods. The endpoint to process the Gift Certificate will be handled via AJAX and front end JavaScript.

In “CheckoutServices.js”, a new endpoint has been added to process the Gift Certificates. When accessed, the script first validates that the “GIFT_CERTIFICATE” payment method is enabled. After validation, the script runs the “Handle” hook for this payment method. The “Handle” hook makes sure the Gift Certificate has not been redeemed, has not expired, and has a balance available for use. The hook also makes sure there are no duplicate Gift Certificates added to the basket. Once we’re sure the Gift Certificate is valid and not duplicated, a new Payment Instrument is created for either the balance of the Gift Certificate, or the Order Total (whichever is appropriate). We then return the Gift Certificate Object to the previous script for further processing.

Gift Certificate HandleBASIC_GIFT_CERTIFICATE Handle.js

The next step is running the supplied “CalculatePaymentTransaction” function, with a few modifications. The gist of this function is to create a Payment Transaction on the Gift Certificate Payment Instrument. Presuming that completes with no errors, we return the Gift Certificate Code, Balance, and Order Total Balance to the front end for display on the page.

Gift Certificate CalculateCalculatePaymentTransaction.js

At this point, the customer can either use another Gift Certificate to pay the balance of the order, or can pay the balance with a different payment method.

Once the customer is ready to complete their order, the regular “CheckoutServices-PlaceOrder” function is run. As part of that function, the “Calculate Payment Transaction” function is run again, this time adding the Order Total Balance to the other Payment Instrument Transaction for later processing.

During the “Handle Payment” function, the “Authorize” hook for Gift Certificates is run. This hook redeems the Gift Certificate for the balance added to the Payment Instrument Transaction. If the redemption fails, the Gift Certificate is removed from the basket and the customer is prompted to use a different payment method. Once the Gift Certificate is redeemed, there is no way to “unredeem”, so it’s critical that this is one of the final steps before the order is finalized.

Gift Certificate AuthorizeBASIC_GIFT_CERTIFICATE Authorize.js

After the Gift Certificate is processed, the next Payment Instrument is authorized for the balance of the order, and the order will be finalized.

Post Processing

For this project, there are a few caveats that we had to take into account - most specifically the fact that customers can store “Loyalty” Gift Certificates on their account and use them via a “one-click” checkout function during checkout. After the Gift Certificate is redeemed, the code must be removed from the customer’s Profile custom attribute so the Gift Certificate is removed from their account and from the checkout area.  Additionally, the project calls for the Gift Certificates to have “Expiration Dates”, which is not an out of the box feature for Gift Certificates in Commerce Cloud. This was solved by adding a custom attribute on the Gift Certificate system object, and we populate that field with the appropriate date on Gift Certificate creation. A job runs nightly to disable all “expired” Gift Certificates.

Conclusion

Thankfully, all Business Manager functionality and available server functions have been left intact for Gift Certificate creation and redemption in Salesforce Commerce Cloud. The SFCC Developers also left helpful hints throughout the core SFRA codebase to help point any developer in the right directions of where to add additional code to support Gift Certificates. There are surely many other ways to enable this functionality, but I hope this helps point some developers in the right direction.