This document defines the Batch which is responsible to capture the recurring payment and create Payment Transaction record in Salesforce.
🛠️ Batch Job: BatchRecurringPaymentProcess
-
🔍 Automatically processes scheduled recurring payments by capturing the due amount using the linked payment method.
🔍 How It Works
-
Fetch Records
Picks all Recurring Payment records where the Next Payment Due Date is today. -
Attempt Payment Capture
-
Captures the Payment Due amount using the linked Payment Method.
-
Creates a Payment Transaction record linked to the Recurring Payment record.
-
-
Success Handling
If the payment is successfully captured:-
✅
Next Payment Due Date→ Recalculated using the set frequency (e.g., weekly, monthly) -
✅
Last Payment Success Date→ Updated to current date -
✅
Payment Due→ Reset to0.00
-
-
Failure Handling
If the payment fails:-
❌
Payment Due→ Current due + Failed amount -
❌
Next Payment Due Date→ Still calculated forward -
❌
Last Payment Attempt Date→ Updated to today
-
📝 Fields Updated in Recurring Payment Record
|
Field |
Description |
|---|---|
|
|
Set to the next scheduled date based on recurrence frequency |
|
|
Date when the last successful payment was captured |
|
|
Amount pending for the next payment; updated on failure |
|
|
Date on which the payment was last attempted (success or fail) |
🧾 Output
-
✅ A new Payment Transaction record is created and linked to the recurring payment.
-
🔗 Transaction details include gateway response, amount, and status.
⏱️ Scheduling
-
This batch can be scheduled via Apex Scheduler for daily execution to ensure timely payment attempts.
💻 Execution Script
👉 Run batch without filter (uses metadata-based query):
Database.executeBatch(new BatchRecurringPaymentProcess(), 1);
👉 Run batch with a custom SOQL query:
String sQuery = 'SELECT Id, cm_finance__Amount__c, cm_finance__Active__c, cm_finance__Next_Payment_Due__c, cm_finance__Next_Payment_Due_Date__c, cm_finance__Parent_Record_Id__c, cm_finance__Payment_Gateway__c, cm_finance__Payment_Gateway__r.Name, cm_finance__Payment_Gateway__r.cm_finance__Payment_Gateway__c, cm_finance__Payment_Method__c, cm_finance__Start_Date__c, cm_finance__End_Date__c, cm_finance__Payment_Method__r.cm_finance__Holder_Full_Name__c, cm_finance__Payment_Method__r.cm_finance__Card_Expiration_Month__c, cm_finance__Payment_Method__r.cm_finance__Card_Expiration_Year__c, cm_finance__Payment_Method__r.RecordType.Name, cm_finance__Payment_Method__r.cm_finance__Encrypted_Token__c, cm_finance__Payment_Method__r.cm_finance__Payment_Mode__c, cm_finance__Payment_Method__r.cm_finance__Location_User_Id__c, cm_finance__Recurrence_Frequency__c, cm_finance__Scheduled_Date__c, cm_finance__Billing_City__c, cm_finance__Billing_Country__c, cm_finance__Billing_Line1__c, cm_finance__Billing_Name__c, cm_finance__Billing_Postal_Code__c, cm_finance__Billing_State__c FROM cm_finance__Recurring_Payment__c WHERE cm_finance__Active__c = true';
Database.executeBatch(new BatchRecurringPaymentProcess(sQuery), 1);
✅ Benefits
📅 Automates recurring billing logic
💳 Ensures accurate tracking of payment attempts
📊 Reduces manual collection overhead
🔁 Captures missed payments in future cycles automatically