Authorization Reversal Apex Class Implementation

The Authorization Reversal Service uses the AuthorizationReversalRequest and AuthorizationReversalResponse classes to manage the creation and storage of authorization reversal information. Implement these classes in your payment gateway adapter.
AuthorizationReversalRequest
Represents the authorization reversal request. Extends BaseRequest and inherits all its methods.
AuthorizationReversalRequest uses a constructor to build an authorization reversal request record in Salesforce. The AuthorizationReversalRequest constructor takes no arguments. You can invoke it as follows.
1CommercePayments.AuthorizationReversalRequest arr = new CommercePayments.AuthorizationReversalRequest();
If you want to build a sample authorization reversal, you can also invoke a constructor with arguments for the reversal amount and payment authorization ID. However, the constructor would only work for test usage and would throw an exception if used outside of the Apex test context.
1commercepayments.AuthorizationReversalRequest authorizationReversalRequest = 
2new commercepayments.AuthorizationReversalRequest(80, authObj.id);
AuthorizationReversalResponse
The payment gateway adapter sends this class as a response for an Authorization Reversal request type. Extends AbstractResponse and inherits its methods.
AuthorizationReversalResponse uses a constructor to build an authorization reversal request record in Salesforce. The AuthorizationReversalResponse constructor takes no arguments. You can invoke it as follows:
1CommercePayments.AuthorizationReversalResponse arp = new CommercePayments.AuthorizationReversalResponse();

Salesforce doesn't support bulk operations or custom fields in the authorization reversal process.

Note

Implementing Reversal Classes in Your Gateway Adapter

Add your reversal classes to your payment gateway adapter. We recommend adding AuthorizationReversal as a possible requestType value when calling processRequest on the gateway’s response.

1global commercepayments.GatewayResponse processRequest(commercepayments.paymentGatewayContext gatewayContext) {
2        commercepayments.RequestType requestType = gatewayContext.getPaymentRequestType();
3        commercepayments.GatewayResponse response;
4        
5        try {
6        //add conditions for other requestType values here
7        //..
8        else if (requestType == commercepayments.RequestType.AuthorizationReversal) {
9                response = createAuthReversalResponse((commercepayments.AuthorizationReversalRequest)gatewayContext.getPaymentRequest());}
10        
11        return response;

Then, add a class that sets the amount of the authorization reversal request, gateway information, and the Salesforce result code.

1global commercepayments.GatewayResponse createAuthReversalResponse(commercepayments.AuthorizationReversalRequest authReversalRequest) {
2        commercepayments.AuthorizationReversalResponse authReversalResponse = new commercepayments.AuthorizationReversalResponse();
3        if(authReversalRequest.amount!=null )
4        {
5            authReversalResponse.setAmount(authReversalRequest.amount);
6        }
7        else
8        {
9             throw new SalesforceValidationException('Required Field Missing : Amount');             
10        }
11   
12        system.debug('Response - success');
13        authReversalResponse.setGatewayDate(system.now());
14        authReversalResponse.setGatewayResultCode('00');
15        authReversalResponse.setGatewayResultCodeDescription('Transaction Normal');
16        //Replace 'xxxxx' with the gateway reference number.
17        authReversalResponse.setGatewayReferenceNumber('SF'+xxxxx);
18        authReversalResponse.setSalesforceResultCodeInfo(SUCCESS_SALESFORCE_RESULT_CODE_INFO);
19        return authReversalResponse;
20    }
Sample Apex Request
1String authorizationId = '0XcxXXXXXXXXXXXXXXX';
2ConnectApi.AuthorizationReversalRequest authorizationReversalRequest = new ConnectApi.AuthorizationReversalRequest();
3authorizationReversalRequest.amount = 1.0;
4authorizationReversalRequest.comments = 'Captured from custom action';
5authorizationReversalRequest.ipAddress = '192.162.10.3';
6authorizationReversalRequest.email = 'testuser@example.com';
7
8ConnectApi.AuthorizationReversalResponse authorizationReversalResponse = ConnectApi.Payments.reverseAuthorization(authorizationReversalRequest, authorizationId);
9String authReversalId = authorizationReversalResponse.paymentAuthAdjustment.id;
10System.debug(authorizationReversalResponse);
11System.debug(authReversalId);