Skip to main content

Payment Method Assignment

Overview

Payment Method Tokenization is the first step in the payments lifecycle. To use a Payment Method it must be assigned to a Chargent Order. Chargent Orders are central to payments lifecycle management.

Prerequisites

There are multiple prerequisite resources that must be created before managing Payment Method assignment to a Chargent Order.

           Object           Definition
Chargent OrderChargent Orders are central objects to payments lifecycle management.
Payment MethodPayment Methods are instruments that may be used to make or take payment.
Payment GatewayPayment Gateways track all the configuration settings that Chargent will use to proccess the payments and send requests to the gateway.

Assign Payment Method to Chargent Order

Apex Example
try {
// Retrieve the last Chargent Order
ChargentOrders__ChargentOrder__c lastCreatedChargentOrder = [SELECT Id, ChargentOrders__Payment_Method_Default__c FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Payment_Descriptor__c LIKE 'Chargent Test%' ORDER BY CreatedDate DESC LIMIT 1];

try {
// Retrieve the last Chargent Payment Method
ChargentBase__Payment_Method__c lastCreatedPaymentMethod = [SELECT Id FROM ChargentBase__Payment_Method__c WHERE ChargentBase__First_Name__c = 'Chargent' AND ChargentBase__Last_Name__c = 'Test' ORDER BY CreatedDate DESC LIMIT 1];

// assocoiate Payment Method to Chargent Order
lastCreatedChargentOrder.ChargentOrders__Payment_Method_Default__c = lastCreatedPaymentMethod.Id;

// Update the Order
try {
update lastCreatedChargentOrder;
System.debug('Chargent Order updated successfully.');
} catch (Exception e) {
System.debug('Error updating Chargent Order: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error retrieving last created Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error retrieving last created Chargent Order: ' + e.getMessage());
}

Unassign Payment Method to Chargent Order

Apex Example
try {
// Retrieve the last Chargent Order
ChargentOrders__ChargentOrder__c lastCreatedChargentOrder = [SELECT Id, ChargentOrders__Payment_Method_Default__c FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Payment_Descriptor__c LIKE 'Chargent Test%' ORDER BY CreatedDate DESC LIMIT 1];

// assocoiate Payment Method to Chargent Order
lastCreatedChargentOrder.ChargentOrders__Gateway__c = null;
lastCreatedChargentOrder.ChargentOrders__Payment_Method_Default__c = null;

// Update the Order
try {
update lastCreatedChargentOrder;
System.debug('Chargent Order updated successfully.');
} catch (Exception e) {
System.debug('Error updating Chargent Order: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error retrieving last created Chargent Order: ' + e.getMessage());
}

Multi-Gateway Tokenization

When a Payment Method has been tokenized across multiple gateways, changing the gateway on the Chargent Order will determine where to send the next transaction using the Token associated with the Payment Method.

Apex Example
try {
// Retrieve the last Chargent Order
ChargentOrders__ChargentOrder__c lastCreatedChargentOrder = [SELECT Id, ChargentOrders__Payment_Method_Default__c FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Payment_Descriptor__c LIKE 'Chargent Test%' ORDER BY CreatedDate DESC LIMIT 1];

try {
// Get payment gateway to use
String paymentGatewayName = 'Braintree Test';
ChargentBase__Gateway__c paymentGateway = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name = :paymentGatewayName ORDER BY CreatedDate DESC LIMIT 1];

// assign Gateway to Chargent Order
lastCreatedChargentOrder.ChargentOrders__Gateway__c = paymentGateway.Id;

// Update the Order
try {
update lastCreatedChargentOrder;
System.debug('Chargent Order updated successfully.');
} catch (Exception e) {
System.debug('Error updating Chargent Order: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error retrieving the Gateway: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error retrieving last created Chargent Order: ' + e.getMessage());
}