Newer Version Available

This content describes an older version of this product. View Latest

SaleNotification Class

When a payment gateway sends a notification for a sale payment, the payment gateway adapter creates the SaleNotification object to store information about notification.

Namespace

CommercePayments

Usage

SaleNotification is used in asynchronous payment gateway adapters. Specify the CommercePayments namespace when creating an instance of this class. The constructor of this class takes no arguments. For example: commercePayments.SaleNotification saleNotification = new commercepayments.SaleNotification();

Example

1global commercepayments.GatewayNotificationResponse processNotification(commercepayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
2    commercepayments.PaymentGatewayNotificationRequest gatewayNotificationRequest = gatewayNotificationContext.getPaymentGatewayNotificationRequest();
3    Blob request = gatewayNotificationRequest.getRequestBody();
4    AdyenNotificationRequest notificationRequest = AdyenNotificationRequest.parse(request.toString().replace('currency', 'currencyCode'));
5    List < AdyenNotificationRequest.NotificationItems > notificationItems = notificationRequest.notificationItems;
6    AdyenNotificationRequest.NotificationRequestItem notificationRequestItem = notificationItems[0].NotificationRequestItem;
7
8    Boolean success = Boolean.valueOf(notificationRequestItem.success);
9    String pspReference = notificationRequestItem.pspReference;
10    String eventCode = notificationRequestItem.eventCode;
11    Double amount = notificationRequestItem.amount.value;
12    String reason = notificationRequestItem.reason;
13    Datetime eventDate = notificationRequestItem.eventDate;
14
15    commercepayments.NotificationStatus notificationStatus = null;
16    if (success) {
17        notificationStatus = commercepayments.NotificationStatus.Success;
18    } else {
19        notificationStatus = commercepayments.NotificationStatus.Failed;
20    }
21
22    commercepayments.BaseNotification notification = null;
23    if ('AUTHORISATION'.equals(eventCode) && amount > 0) {
24        notification = new commercepayments.SaleNotification();
25        notification.setGatewayReferenceNumber(pspReference);
26    } else {
27        system.debug('handling unknown event : ' + eventCode);
28        commercepayments.GatewayNotificationResponse unknownEventResponse = new commercepayments.GatewayNotificationResponse();
29        unknownEventResponse.setStatusCode(200);
30        unknownEventResponse.setResponseBody(Blob.valueOf('[not allowed]'));
31        return unknownEventResponse;
32    }
33
34    notification.setStatus(notificationStatus);
35    notification.setAmount(amount / 100);
36    notification.setGatewayResultCodeDescription(reason);
37    notification.setGatewayDate(eventDate);
38
39    commercepayments.NotificationSaveResult saveResult = commercepayments.NotificationClient.record(notification);
40
41    commercepayments.GatewayNotificationResponse gnr = new commercepayments.GatewayNotificationResponse();
42    if (saveResult.isSuccess()) {
43        gnr.setStatusCode(200);
44    } else {
45        gnr.setStatusCode(400);
46    }
47    gnr.setResponseBody(Blob.valueOf(saveResult.toString()));
48    return gnr;
49}

SaleNotification Methods

The following are methods for SaleNotification.

setAmount(amount)

Sets the amount for the sale payment.

Signature

public void setAmount(Double amount)

Parameters

amount
Type: Double

Return Value

Type: void

setGatewayAvsCode(gatewayAvsCode)

Sets the AVS (address verification system) result code information that the gateway returned. Maximum length of 64 characters.

Signature

public void setGatewayAvsCode(String gatewayAvsCode)

Parameters

gatewayAvsCode
Type: String
Used to verify the address mapped to a payment method when the payments platform requests tokenization from the payment gateway.

Return Value

Type: void

setGatewayDate(gatewayDate)

Sets the date that the sale occurred. Some gateways don’t send this value.

Signature

public void setGatewayDate(Datetime gatewayDate)

Parameters

gatewayDate
Type: Datetime

Return Value

Type: void

setGatewayMessage(gatewayMessage)

Sets error messages that the gateway returned for the sale request. Maximum length of 255 characters.

Signature

public void setGatewayMessage(String gatewayMessage)

Parameters

gatewayMessage
Type: String

Return Value

Type: void

setGatewayReferenceDetails(gatewayReferenceDetails)

Sets additional data that you can use for the sale payment. You can use any data that isn’t normalized in financial entities. This field has a maximum length of 1000 characters and can store data as JSON or XML.

Signature

public void setGatewayReferenceDetails(String gatewayReferenceDetails)

Parameters

gatewayReferenceDetails
Type: String

Return Value

Type: void

setGatewayReferenceNumber(gatewayReferenceNumber)

Sets the unique gateway reference number for the transaction that the gateway returned. Maximum length of 255 characters.

Signature

public void setGatewayReferenceNumber(String gatewayReferenceNumber)

Parameters

gatewayReferenceNumber
Type: String

Return Value

Type: void

setGatewayResultCode(gatewayResultCode)

Sets a gateway-specific result code. The code may be mapped to a Salesforce-specific result code. Maximum length of 64 characters.

Signature

public void setGatewayResultCode(String gatewayResultCode)

Parameters

gatewayResultCode
Type: String

Return Value

Type: void

setGatewayResultCodeDescription(gatewayResultCodeDescription)

Sets a description of the gateway-specific result code that a payment gateway returned. Maximum length of 1000 characters.

Signature

public void setGatewayResultCodeDescription(String gatewayResultCodeDescription)

Parameters

gatewayResultCodeDescription
Type: String
Description of the gateway’s result code. Use this field to learn more about why the gateway returned a certain result code.

Return Value

Type: void

setId(id)

Sets the ID of a notification sent by the payment gateway.

Signature

public void setId(String id)

Parameters

id
Type: String

Return Value

Type: void

setSalesforceResultCodeInfo(salesforceResultCodeInfo)

Sets the Salesforce-specific result code information. Payment gateways have many response codes for payment calls. Salesforce uses the result code information to map payment gateway codes to a predefined set of standard Salesforce result codes.

Signature

public void setSalesforceResultCodeInfo(commercepayments.SalesforceResultCodeInfo salesforceResultCodeInfo)

Parameters

salesforceResultCodeInfo
Type: commercepayments.SalesforceResultCodeInfo

Return Value

Type: void

setStatus(status)

Sets the notification status value on the notification object.

Signature

public void setStatus(commercepayments.NotificationStatus status)

Parameters

status
Type: commercepayments.NotificationStatus

Return Value

Type: void