Newer Version Available
SaleRequest Class
Namespace
Usage
This class holds all the required details about a sale request. Gateway adapters read the fields of this class object while constructing a sale JSON request thatis sent to the payment gateway. The object of this class is made available through commercepayments.paymentGatewayContext by calling getPaymentRequest().
Example
1private String buildSaleRequest(commercepayments.SaleRequest saleRequest) {
2 String currencyIso = saleRequest.currencyIsoCode;
3 commercepayments.SaleApiPaymentMethodRequest paymentMethod = saleRequest.paymentMethod;
4 if (currencyIso == null) {
5 currencyIso = UserInfo.getDefaultCurrency();
6 }
7
8 JSONGenerator jsonGeneratorInstance = JSON.createGenerator(true);
9 jsonGeneratorInstance.writeStartObject();
10 jsonGeneratorInstance.writeStringField('merchantAccount', '{!$Credential.Username}');
11 jsonGeneratorInstance.writeStringField('reference', String.valueOf(Datetime.now().getTime()) + String.valueOf(Math.random()).substring(2, 8));
12 jsonGeneratorInstance.writeFieldName('amount');
13 jsonGeneratorInstance.writeStartObject();
14 jsonGeneratorInstance.writeStringField('value', String.ValueOf((saleRequest.amount * 100.0).intValue()));
15 jsonGeneratorInstance.writeStringField('currency', currencyIso);
16 jsonGeneratorInstance.writeEndObject();
17
18 jsonGeneratorInstance.writeFieldName('paymentMethod');
19 jsonGeneratorInstance.writeStartObject();
20
21 String shopperReference;
22 String type = 'scheme';
23
24 if (saleRequest.paymentMethodData != null) {
25 String token = saleRequest.paymentMethodData.get('gatewayToken');
26 String paymentMethodType = saleRequest.paymentMethodData.get('paymentMethodType');
27 shopperReference = saleRequest.paymentMethodData.get('gatewayReference');
28
29 if ('us_bank_account'.equals(paymentMethodType)) {
30 type = 'ach';
31 } else if ('sepa_debit'.equals(paymentMethodType)) {
32 type = 'sepadirectdebit';
33 } else if ('au_becs_debit'.equals(paymentMethodType)) {
34 type = 'directdebit_AU';
35 } else if ('bacs_debit'.equals(paymentMethodType)) {
36 type = 'directdebit_GB';
37 }
38
39 jsonGeneratorInstance.writeStringField('type', type);
40 jsonGeneratorInstance.writeStringField('storedPaymentMethodId', token);
41 }
42
43 jsonGeneratorInstance.writeEndObject();
44 jsonGeneratorInstance.writeStringField('shopperInteraction', 'ContAuth');
45 jsonGeneratorInstance.writeStringField('recurringProcessingModel', 'UnscheduledCardOnFile');
46 jsonGeneratorInstance.writeStringField('shopperReference', shopperReference);
47
48 jsonGeneratorInstance.writeNumberField('captureDelayHours', 0);
49 jsonGeneratorInstance.writeEndObject();
50
51 return jsonGeneratorInstance.getAsString();SaleRequest Constructors
The following are constructors for SaleRequest.
SaleRequest(amount)
Signature
global SaleRequest(Double amount)
Parameters
- amount
- Type: Double
- Amount of the sale request.
SaleRequest Properties
The following are properties for SaleRequest.
paymentMethod
Signature
global commercepayments.SaleApiPaymentMethodRequest paymentMethod {get; set;}
Property Value
paymentMethodData
This field is populated when SaleInput specifies a saved payment method. Accessible using paymentMethodData on SaleRequest. The map contains these fields from SavedPaymentMethod: GatewayToken, Type, GatewayReference, and StandardEntryCode for direct gateway interaction without querying the database.
Signature
public Map<String,String> paymentMethodData {get; set;}
Property Value
Type: Map<String,String>
submittedByMerchant
Signature
public Boolean submittedByMerchant {get; set;}
Property Value
Type: Boolean
SaleRequest Methods
The following are methods for SaleRequest.
equals(obj)
Signature
global Boolean equals(Object obj)
Parameters
- obj
- Type: Object
Return Value
Type: Boolean
hashCode()
Signature
global Integer hashCode()
Return Value
Type: Integer