AuthorizationRequest Class
Sends information about an authorization request to a gateway adapter
during a service call. This class extends the BaseRequest class and inherits all its methods.
Namespace
Usage
This class contains information about a transaction
authorization request. The gateway adapter reads fields from this class while
constructing an authorization JSON request to send to the payment gateway. An object of
this class is available by calling getPaymentRequest() in the PaymentGatewayContext Class.
Example
Creating a buildAuthRequest class to store information about the authorization request.
private String buildAuthRequest(commercepayments.AuthorizationRequest authRequest) {
// Multiply amount by 100.0 to convert to cents
String requestBody = createRequestBody(String.ValueOf((authRequest.amount*100.0).intValue()), authRequest);
return requestBody;
private String createRequestBody(String amount, commercepayments.AuthorizationRequest authRequest) {
JSONGenerator jsonGeneratorInstance = JSON.createGenerator(true);
String currencyIso = authRequest.currencyIsoCode;
commercepayments.AuthApiPaymentMethodRequest paymentMethod = authRequest.paymentMethod;
commercepayments.GatewayErrorResponse error;
// Write data to the JSON string.
jsonGeneratorInstance.writeStartObject();
jsonGeneratorInstance.writeStringField('merchantAccount', '{!$Credential.Username}');
jsonGeneratorInstance.writeStringField('reference', authRequest.comments == null ? 'randomstring' : authRequest.comments);
if(currencyIso == null) {
currencyIso = UserInfo.getDefaultCurrency();
}
jsonGeneratorInstance.writeFieldName('amount');
jsonGeneratorInstance.writeStartObject();
jsonGeneratorInstance.writeStringField('value', amount);
jsonGeneratorInstance.writeStringField('currency', currencyIso);
jsonGeneratorInstance.writeEndObject();
commercepayments.CardPaymentMethodRequest cardPaymentMethod;
if(paymentMethod != null) {
cardPaymentMethod = paymentMethod.cardPaymentMethod;
if (cardPaymentMethod != null) {
if (cardPaymentMethod.CardCategory != null) {
if (commercepayments.CardCategory.CreditCard == cardPaymentMethod.CardCategory) {
jsonGeneratorInstance.writeFieldName('card');
jsonGeneratorInstance.writeStartObject();
if (cardPaymentMethod.cvv != null)
jsonGeneratorInstance.writeStringField('cvc', String.ValueOf(cardPaymentMethod.cvv));
if (cardPaymentMethod.cardholdername != null)
jsonGeneratorInstance.writeStringField('holderName', cardPaymentMethod.cardholdername);
if (cardPaymentMethod.cardnumber != null)
jsonGeneratorInstance.writeStringField('number', cardPaymentMethod.cardnumber);
if (cardPaymentMethod.expiryMonth != null && cardPaymentMethod.expiryYear != null ) {
String expMonth = ((String.ValueOf(cardPaymentMethod.expiryMonth)).length() == 1 ? '0' : '') + String.ValueOf(cardPaymentMethod.expiryMonth);
jsonGeneratorInstance.writeStringField('expiryMonth', expMonth);
jsonGeneratorInstance.writeStringField('expiryYear', String.ValueOf(cardPaymentMethod.expiryYear));
}
jsonGeneratorInstance.writeEndObject();
} else {
//Support for other card type
}
} else {
throw new SampleValidationException('Required Field Missing : CardCategory');
}
} else {
throw new SampleValidationException('Required Field Missing : CardPaymentMethod');
}
} else {
throw new SampleValidationException('Required Field Missing : PaymentMethod');
}
jsonGeneratorInstance.writeEndObject();
return jsonGeneratorInstance.getAsString();
}
AuthorizationRequest Constructors
The following are constructors for AuthorizationRequest.
AuthorizationRequest(amount)
Constructor for building the amount in an authorization request. This
constructor is intended for test usage and throws an exception if used outside of the Apex
test context.
Signature
global AuthorizationRequest(Double amount)
Parameters
- amount
- Type: Double
- The amount of the authorization.
AuthorizationRequest Properties
The following are properties for AuthorizationRequest.
AuthorizationRequest Methods
The following are methods for AuthorizationRequest.
equals(obj)
Maintains the integrity of lists of type AuthorizationRequest by determining the equality of external
objects in a list. This method is dynamic and based on the equals method in
Java.
Signature
global Boolean equals(Object obj)
Parameters
- obj
- Type: Object
- External object whose key is to be validated.
Return Value
Type: Boolean
hashCode()
Maintains the integrity of lists of type AuthorizationRequest by determining the uniqueness of the
external object in a list.
Signature
global Integer hashCode()
Return Value
Type: Integer