同期ゲートウェイアダプタの作成
同期支払設定では、Salesforce 支払プラットフォームがトランザクション情報をゲートウェイに送信し、最終的なトランザクションの状況が含まれるゲートウェイ応答を待機します。Salesforce では、ゲートウェイでトランザクションが成功した場合のみトランザクションを作成します。
同期ゲートウェイアダプタでは、PaymentGatewayAdapter インターフェースを実装します。このトピックでは、PaymentGatewayAdapter に続いて支払プラットフォームと支払ゲートウェイ間の通信の大半に貢献している processRequest クラスを見ていくことで、非同期アダプタのサンプルを分析していきます。
PaymentGatewayAdapter
すべての非同期ゲートウェイでは、PaymentGatewayAdapter インターフェースを実装する必要があります。すべての PaymentGatewayAdapter では、processRequest メソッドを実装する必要があります。
1global with sharing class SampleAdapter implements commercepayments.PaymentGatewayAdapter {
2 global SampleAdapter() {}
3
4 global commercepayments.GatewayResponse processRequest(commercepayments.paymentGatewayContext gatewayContext) {
5 }
6}最初の支払要求の処理
支払プラットフォームは支払 API 要求を受信すると、さらに評価を行うために要求をゲートウェイアダプタに渡します。アダプタは、同期支払フローの最初のステップを表す processRequest メソッドをコールして要求評価プロセスを開始します。processRequest 実装は 3 つの部分に分けることができます。
最初に、ゲートウェイで理解できる支払要求オブジェクトを作成します。
1commercepayments.RequestType requestType = gatewayContext.getPaymentRequestType();
2if (requestType == commercepayments.RequestType.Capture) {
3 req.setEndpoint('/pal/servlet/Payment/v52/capture');
4 body = buildCaptureRequest((commercepayments.CaptureRequest)gatewayContext.getPaymentRequest());
5} else if (requestType == commercepayments.RequestType.ReferencedRefund) {
6 req.setEndpoint('/pal/servlet/Payment/v52/refund');
7 body = buildRefundRequest((commercepayments.ReferencedRefundRequest)gatewayContext.getPaymentRequest());
8}次に、アダプタが要求を支払ゲートウェイに送信します。
1req.setBody(body);
2req.setMethod('POST');
3commercepayments.PaymentsHttp http = new commercepayments.PaymentsHttp();
4HttpResponse res = null;
5try {
6 res = http.send(req);
7} catch(CalloutException ce) {
8 commercepayments.GatewayErrorResponse error = new commercepayments.GatewayErrorResponse('500', ce.getMessage());
9 return error;
10}1if ( requestType == commercepayments.RequestType.Capture) {
2 // Refer to the end of this doc for sample createCaptureResponse implementation
3 response = createCaptureResponse(res);
4} else if ( requestType == commercepayments.RequestType.ReferencedRefund) {
5 response = createRefundResponse(res);
6}
7return response;