Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Builder Examples for Payment Gateway Adapters

The final sections of a payment gateway adapter should define how the adapter creates requests and responses. The implementation of these classes can vary widely based on your gateway and platform requirements. We’ve provided several generics examples for review.

Example

  • buildCaptureRequest:
    1private String buildCaptureRequest(commercepayments.CaptureRequest captureRequest) {
    2   Boolean IS_MULTICURRENCY_ORG = UserInfo.isMultiCurrencyOrganization();
    3    QueryUtils qBuilderForAuth = new QueryUtils(PaymentAuthorization.SObjectType);
    4    qBuilderForAuth.getSelectClause().addField('GatewayRefNumber', false);
    5    qBuilderForAuth.setWhereClause(' WHERE Id =' + '\'' + captureRequest.paymentAuthorizationId + '\'');
    6    PaymentAuthorization authObject = (PaymentAuthorization)Database.query(qBuilderForAuth.buildSOQL())[0];
    7
    8    JSONGenerator jsonGeneratorInstance = JSON.createGenerator(true);
    9    jsonGeneratorInstance.writeStartObject();
    10    jsonGeneratorInstance.writeStringField('merchantAccount', '{!$Credential.Username}');
    11    jsonGeneratorInstance.writeStringField('originalReference', authObject.GatewayRefNumber);
    12
    13    jsonGeneratorInstance.writeFieldName('modificationAmount');
    14    jsonGeneratorInstance.writeStartObject();
    15    jsonGeneratorInstance.writeStringField('value', String.ValueOf((captureRequest.amount * 100.0).intValue()));
    16    jsonGeneratorInstance.writeEndObject();
    17
    18    jsonGeneratorInstance.writeEndObject();
    19    return jsonGeneratorInstance.getAsString();
    20}

Example

  • createCaptureResponse:
    1private commercepayments.GatewayResponse createCaptureResponse(HttpResponse response) {
    2    Map<String, Object> mapOfResponseValues = (Map
    3                        <String, Object>) JSON.deserializeUntyped(response.getBody());
    4    Integer statusCode = response.getStatusCode();
    5    String responceValue = (String)mapOfResponseValues.get('response');
    6    if(statusCode == 200) {
    7        system.debug('Response - success - Capture received');
    8       commercepayments.CaptureResponse captureResponse = new commercepayments.CaptureResponse();
    9        captureResponse.setAsync(true); // Very important to treat this as an asynchronous transaction
    10        captureResponse.setGatewayReferenceNumber((String)mapOfResponseValues.get('pspReference'));
    11        captureResponse.setSalesforceResultCodeInfo(new commercepayments.SalesforceResultCodeInfo(commercepayments.SalesforceResultCode.Success));
    12        return captureResponse;
    13    } else {
    14        system.debug('Response - error - Capture not received by Gateway');
    15        String message = (String)mapOfResponseValues.get('message');
    16        commercepayments.GatewayErrorResponse error = new commercepayments.GatewayErrorResponse(String.valueOf(statusCode), message);
    17        return error;
    18    }
    19}