Newer Version Available
承認取消の Apex クラス実装
承認取消サービスは AuthorizationReversalRequest クラスと AuthorizationReversalResponse クラスを使用して、承認取消情報の作成と保存を管��します。これらのクラスを支払ゲートウェイアダプタに実装します。
- AuthorizationReversalRequest
- 承認取消要求を表します。BaseRequest を拡張し、そのメソッドのすべてを継承します。
- AuthorizationReversalRequest はコンストラクタを使用して、Salesforce で承認取消要求レコードを作成します。AuthorizationReversalRequest コンストラクタは引数を取りません。このコンストラクタは次のように呼び出すことができます。
-
1CommercePayments.AuthorizationReversalRequest arr = new CommercePayments.AuthorizationReversalRequest(); - 承認取消サンプルを作成する場合は、取消額と支払承認 ID を引数としてコンストラクタを呼び出すこともできます。ただし、コンストラクタはテスト目的でのみ機能し、Apex テストコンテキスト以外で使用された場合は例外が発生します。
-
1commercepayments.AuthorizationReversalRequest authorizationReversalRequest = 2new commercepayments.AuthorizationReversalRequest(80, authObj.id); - AuthorizationReversalResponse
- 支払ゲートウェイアダプタは、承認取消要求種別の応答としてこのクラスを送信します。AbstractResponse を拡張し、そのメソッドのすべてを継承します。
- AuthorizationReversalResponse はコンストラクタを使用して、Salesforce で承認取消要求レコードを作成します。AuthorizationReversalResponse コンストラクタは引数を取りません。このコンストラクタは次のように呼び出すことができます。
-
1CommercePayments.AuthorizationReversalResponse arp = new CommercePayments.AuthorizationReversalResponse();
ゲートウェイアダプタでの取消クラスの実装
取消クラスを支払ゲートウェイアダプタに追加します。ゲートウェイの応答で processRequest をコールするときに、使用可能な requestType 値として AuthorizationReversal を追加することをお勧めします。
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;その後、承認取消認要求の金額、ゲートウェイ情報、および Salesforce 結果コードを設定するクラスを追加します。
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 }
サンプル Apex 要求
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);