PaymentGatewayAsyncAdapter Interface

Implement the interface to allow customers to process payments asynchronously.

Namespace

CommercePayments

Usage

Implementing an asynchronous adapter also requires the processNotification method from the GatewayNotificationResponse class.

Example

1global with sharing class SampleAsyncAdapter 
2        implements commercepayments.PaymentGatewayAsyncAdapter, 
3        commercepayments.PaymentGatewayAdapter {
4        
5        global SampleAsyncAdapter() {
6        }
7        
8        global commercepayments.GatewayResponse processRequest(
9        commercepayments.paymentGatewayContext gatewayContext) {
10        }
11        
12        global commercepayments.GatewayNotificationResponse processNotification(
13        commercepayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
14        }
15        }

PaymentGatewayAsyncAdapter Methods

The following are methods for PaymentGatewayAsyncAdapter.

processNotification(paymentGatewayNotificationContext)

Entry point for processing notifications from payment gateways.

Signature

global commercepayments.GatewayNotificationResponse processNotification(commercepayments.PaymentGatewayNotificationContext var1)

Parameters

paymentGatewayNotificationContext
Type: PaymentGatewayNotificationContext
The PaymentGatewayNotificationContext object wraps all the information related to a gateway notification.

Return Value

Type: GatewayNotificationResponse

When the payment gateway sends a notification to the payments platform, the platform responds with a GatewayNotificationResponse indicating whether the platform succeeded or failed at receiving the notification.

PaymentGatewayAsyncAdapter Example Implementation

This is a sample implementation of the commercepayments.PaymentGatewayAsyncAdapter interface.

1global with sharing class AdyenAdapter implements commercepayments.PaymentGatewayAsyncAdapter, commercepayments.PaymentGatewayAdapter {
2    global AdyenAdapter() {}
3    
4    global commercepayments.GatewayResponse processRequest(commercepayments.paymentGatewayContext gatewayContext) {
5    }
6    
7    global commercepayments.GatewayNotificationResponse processNotification(commercepayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
8    }
9}
10
11commercepayments.RequestType requestType = gatewayContext.getPaymentRequestType();
12if (requestType == commercepayments.RequestType.Capture) {
13    req.setEndpoint('/pal/servlet/Payment/v52/capture');
14    body = buildCaptureRequest((commercepayments.CaptureRequest)gatewayContext.getPaymentRequest());
15} else if (requestType == commercepayments.RequestType.ReferencedRefund) {
16    req.setEndpoint('/pal/servlet/Payment/v52/refund');
17    body = buildRefundRequest((commercepayments.ReferencedRefundRequest)gatewayContext.getPaymentRequest());
18}
19
20req.setBody(body);
21req.setMethod('POST');
22commercepayments.PaymentsHttp http = new commercepayments.PaymentsHttp();
23HttpResponse res = null;
24try {
25    res = http.send(req);
26} catch(CalloutException ce) {
27    commercepayments.GatewayErrorResponse error = new commercepayments.GatewayErrorResponse('500', ce.getMessage());
28    return error;
29}
30
31if ( requestType == commercepayments.RequestType.Capture) {
32    response =  createCaptureResponse(res);
33} else if ( requestType == commercepayments.RequestType.ReferencedRefund) {
34    response =  createRefundResponse(res);
35}
36return response;
37
38commercepayments.PaymentGatewayNotificationRequest notificationRequest = gatewayNotificationContext.getPaymentGatewayNotificationRequest();
39Blob request = notificationRequest.getRequestBody();
40Map<String, Object> jsonReq = (Map<String, Object>)JSON.deserializeUntyped(request.toString());
41List<Object> notificationItems = (List<Object>)jsonReq.get('notificationItems');
42Map<String, Object> notificationRequestItem =
43    (Map<String, Object>)((Map<String, Object>)notificationItems[0]).get('NotificationRequestItem');
44Boolean success = Boolean.valueOf(notificationRequestItem.get('success'));
45String pspReference = (String)notificationRequestItem.get('pspReference');
46String eventCode = (String)notificationRequestItem.get('eventCode');
47Double amount = (Double)((Map<String, Object>)notificationRequestItem.get('amount')).get('value');
48
49commercepayments.NotificationStatus notificationStatus = null;
50if (success) {
51    notificationStatus = commercepayments.NotificationStatus.Success;
52} else {
53    notificationStatus = commercepayments.NotificationStatus.Failed;
54}
55commercepayments.BaseNotification notification = null;
56if ('CAPTURE'.equals(eventCode)) {
57    notification = new commercepayments.CaptureNotification();
58} else if ('REFUND'.equals(eventCode)) {
59    notification = new commercepayments.ReferencedRefundNotification();
60}
61notification.setStatus(notificationStatus);
62notification.setGatewayReferenceNumber(pspReference);
63notification.setAmount(amount);
64
65commercepayments.NotificationSaveResult saveResult = commercepayments.NotificationClient.record(notification);
66
67commercepayments.GatewayNotificationResponse gnr = new commercepayments.GatewayNotificationResponse();
68if (saveResult.isSuccess()) {
69    system.debug('Notification accepted by platform');
70} else {
71    system.debug('Errors in the result '+ Blob.valueOf(saveResult.getErrorMessage()));
72}
73gnr.setStatusCode(200);
74gnr.setResponseBody(Blob.valueOf('[accepted]'));
75return gnr;