---
schema_version: "1"
id: cbb8ee63e8da0e14be60e0741249fcbbd65bf0e5570a6fad260089103caf9e87
title: Builder Examples for Payment Gateway Adapters
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_commercepayments_builder_examples.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_commercepayments_builder_examples.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: a8772acbbf86280121cb0001077e71d15e0f8f5d7aa56e4df655ce004d2175a2
description: 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.
---
# 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**:
    
    private String buildCaptureRequest(commercepayments.CaptureRequest captureRequest) {
       Boolean IS\_MULTICURRENCY\_ORG = UserInfo.isMultiCurrencyOrganization();
        QueryUtils qBuilderForAuth = new QueryUtils(PaymentAuthorization.SObjectType);
        qBuilderForAuth.getSelectClause().addField('GatewayRefNumber', false);
        qBuilderForAuth.setWhereClause(' WHERE Id =' + '\\'' + captureRequest.paymentAuthorizationId + '\\'');
        PaymentAuthorization authObject = (PaymentAuthorization)Database.query(qBuilderForAuth.buildSOQL(), AccessLevel.USER\_MODE)\[0\];
    
        JSONGenerator jsonGeneratorInstance = JSON.createGenerator(true);
        jsonGeneratorInstance.writeStartObject();
        jsonGeneratorInstance.writeStringField('merchantAccount', '{!$Credential.Username}');
        jsonGeneratorInstance.writeStringField('originalReference', authObject.GatewayRefNumber);
    
        jsonGeneratorInstance.writeFieldName('modificationAmount');
        jsonGeneratorInstance.writeStartObject();
        jsonGeneratorInstance.writeStringField('value', String.ValueOf((captureRequest.amount \* 100.0).intValue()));
        jsonGeneratorInstance.writeEndObject();
    
        jsonGeneratorInstance.writeEndObject();
        return jsonGeneratorInstance.getAsString();
    }

## Example

- **createCaptureResponse**:
    
    private commercepayments.GatewayResponse createCaptureResponse(HttpResponse response) {
        Map<String, Object\> mapOfResponseValues = (Map
                            <String, Object\>) JSON.deserializeUntyped(response.getBody());
        Integer statusCode = response.getStatusCode();
        String responceValue = (String)mapOfResponseValues.get('response');
        if(statusCode == 200) {
            system.debug('Response - success - Capture received');
           commercepayments.CaptureResponse captureResponse = new commercepayments.CaptureResponse();
            captureResponse.setAsync(true); // Very important to treat this as an asynchronous transaction
            captureResponse.setGatewayReferenceNumber((String)mapOfResponseValues.get('pspReference'));
            captureResponse.setSalesforceResultCodeInfo(new commercepayments.SalesforceResultCodeInfo(commercepayments.SalesforceResultCode.Success));
            return captureResponse;
        } else {
            system.debug('Response - error - Capture not received by Gateway');
            String message = (String)mapOfResponseValues.get('message');
            commercepayments.GatewayErrorResponse error = new commercepayments.GatewayErrorResponse(String.valueOf(statusCode), message);
            return error;
        }
    }