Newer Version Available

This content describes an older version of this product. View Latest

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 implements commercepayments.PaymentGatewayAsyncAdapter, commercepayments.PaymentGatewayAdapter {
2    global SampleAsyncAdapter() {}
3    
4    global commercepayments.GatewayResponse processRequest(commercepayments.paymentGatewayContext gatewayContext) {
5    }
6    
7    global commercepayments.GatewayNotificationResponse processNotification(commercepayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
8    }
9}

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