Newer Version Available
PaymentGatewayAsyncAdapter インターフェース
名前空間
使用方法
非同期アダプタを実装するには、GatewayNotificationResponse クラスの processNotification メソッドも必要です。
例
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 のメソッド
PaymentGatewayAsyncAdapter のメソッドは次のとおりです。
processNotification(paymentGatewayNotificationContext)
署名
global commercepayments.GatewayNotificationResponse processNotification(commercepayments.PaymentGatewayNotificationContext var1)
パラメータ
- paymentGatewayNotificationContext
- 型: PaymentGatewayNotificationContext
- PaymentGatewayNotificationContext オブジェクトは、ゲートウェイ通知に関連するすべての情報をラップします。
戻り値
型: GatewayNotificationResponse
支払ゲートウェイが支払プラットフォームに通知を送信すると、プラットフォームはその通知の受信に成功したか失敗したかを示す GatewayNotificationResponse で応答します。
PaymentGatewayAsyncAdapter の実装例
これは、commercepayments.PaymentGatewayAsyncAdapter インターフェースの実装例です。
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;