The refund object is a child of the payment object.


Refund Records can now be created directly in Salesforce (from V2.20.4 - Winter 23) which means that you can create them using a flow, manually or via a batch class.

When creating a refund record you must populate the following fields:  

  • Payment  - lookup to the payment record to which the refund relates and with a status of ‘collected from customer’
  • Payment method - this must be the same as the original payment method on the payment
  • Amount - this cannot be more than the original payment amount - you can partially refund, but not process more than was originally taken

In order to process a Refund, the following must be true on the record:

  • Status equals “New” or ”Failed” 
  • The Refund Date should be today or less than today 
  • The associated Payment record should have the status ‘Collected From Customer’
  • The Record Type of the original payment should be 'Inbound'

Processing Refunds can be done in the following ways’:

  • From the payment record to which the refund relates
  • From the refund tab by selecting the required records (up to 200 at a time)
  • Directly from the refund record
  • Via a batch class
  • Via a scheduled job  


Required Permissions:


Permission Set

Details

Asperato Refund

Refund - Read, Create, Edit

Automating the Refund Process


If you want to schedule the Refund job to process refund records automatically, or even create and process refund records automatically, then you can choose to do the following steps.


The apex job to process Refunds is a separate job to the existing Asperato Repeat Payments scheduled job. It will need to be created and scheduled separately.


Scheduling the refund job to process all refund records with a status of ‘New’ can be done in one of the following two ways:


Schedule from within Apex Classes > Schedule Apex:

Job Name: AspertoRefundsJob

Apex Class: ProcessRefundsSchedule

Select Frequency as required

If you require the job to run more than once a day, schedule from within Developer Console using the apex code below (set your own cron expression as per frequency required)

String cronExpression ='0 0 * * * ?'; 

String refundsJob = System.schedule('Asperato Refunds Job', cronExpression, new

asp04.ProcessRefundsSchedule());

System.debug(' Asperato ONE repeat payment job scheduled with id ' +

refundsJob);



The following code can be used to create and process Refund records via the batch class. 


This is done by using the Payment id and Amount, then processing the Refund records which are in the status ‘New’. To Select Refund records via the batch job with a status of ‘Failed’ the record id of the failed records must be passed directly into the batch, they won’t be picked up automatically.  Failed records can alternatively be submitted via the List View or via the Record itself once the reason for failure has been addressed.


The Refund records will only be picked up with the Refund Date is today or less, and the Payment Status is ‘Collected from Customer’ 




Map<Id, Decimal> paymentIdsToRefundAmountMap = new Map<Id, Decimal>();

paymentIdsToRefundAmountMap.put(payment.Id, 20);

BatchSendRefundByPayment refundBatch= new

BatchSendRefundByPayment(paymentIdsToRefundAmountMap);

Database.executeBatch(refundBatch);






Using a batch class to process Refunds can be done with the following code (the same conditions apply to this as with processing refunds in other ways)




BatchSendRefunds refundBatch = new BatchSendRefunds(new List<Id>

{refundPayment.Id});

//Change the id from List of Refund Ids

Database.executeBatch(refundBatch);