Skip to main content

Payment Refund

Overview

Payment Refund will refund an amount to a previous Payment Authorization Capture transaction.

Prerequisites

There is one prerequisite resource that must be created before requesting a payment refund.

           Object           Definition
Chargent TransactionTransactions record the results of the requests made to the payment gateway on the Chargent Order. Transactions are also used to make additional requests in the payments lifecycle.

Refund Types

There are two types of payment refunds.

TypeDescription
Full RefundInput up to the full charge amount to perform a full refund.
Partial RefundInput a lesser value than the charge amount to perform a partial refund

Create Full Payment Refund

Apex Example
try {
// Get the last Chargent Order to use
ChargentOrders__ChargentOrder__c lastChargentOrder = [SELECT Id, ChargentOrders__Account__c, ChargentOrders__Payment_Method_Default__c FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Payment_Descriptor__c LIKE 'Chargent Test%' ORDER BY CreatedDate DESC LIMIT 1];
Id lastChargentOrderId = lastChargentOrder.Id;

try {
// Get the last authorization transaction
ChargentOrders__Transaction__c lastSaleTransaction = [SELECT Id FROM ChargentOrders__Transaction__c WHERE ChargentOrders__Order__c = :lastChargentOrderId ORDER BY CreatedDate DESC LIMIT 1];
Id lastSaleTransactionId = lastSaleTransaction.Id;

try {
Map<String, Object> paymentRefundMap = new Map<String, Object>();
paymentRefundMap.put('ObjectId', lastSaleTransactionId);

Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('data', paymentRefundMap);
actionsMap.put('action', 'Refund');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Refund create error.');
} else {
// Success
System.debug('Payment Refund created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Refund: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Transaction: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Chargent Order: ' + e.getMessage());
}

Create Partial Payment Refund

Apex Example
try {
// Get the last Chargent Order to use
ChargentOrders__ChargentOrder__c lastChargentOrder = [SELECT Id, ChargentOrders__Account__c, ChargentOrders__Payment_Method_Default__c FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Payment_Descriptor__c LIKE 'Chargent Test%' ORDER BY CreatedDate DESC LIMIT 1];
Id lastChargentOrderId = lastChargentOrder.Id;

try {
// Get the last authorization transaction
ChargentOrders__Transaction__c lastSaleTransaction = [SELECT Id FROM ChargentOrders__Transaction__c WHERE ChargentOrders__Order__c = :lastChargentOrderId ORDER BY CreatedDate DESC LIMIT 1];
Id lastSaleTransactionId = lastSaleTransaction.Id;

try {
Map<String, Object> paymentRefundMap = new Map<String, Object>();
paymentRefundMap.put('ObjectId', lastSaleTransactionId);
paymentRefundMap.put('CustomTransactionType', 'PartialRefund');
paymentRefundMap.put('Amount', '50.00');

Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('data', paymentRefundMap);
actionsMap.put('action', 'Refund');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Refund create error.');
} else {
// Success
System.debug('Payment Refund created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Partial Payment Refund: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Transaction: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Chargent Order: ' + e.getMessage());
}

Learn More

Learn more about Payment Refunds.