Payment Connect

Set up Recurring Payments

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

  1. Fetch Records
    Picks all Recurring Payment records where the Next Payment Due Date is today.

  2. Attempt Payment Capture

    • Captures the Payment Due amount using the linked Payment Method.

    • Creates a Payment Transaction record linked to the Recurring Payment record.

  3. 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 to 0.00

  4. 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

Next Payment Due Date

Set to the next scheduled date based on recurrence frequency

Last Payment Success Date

Date when the last successful payment was captured

Payment Due

Amount pending for the next payment; updated on failure

Last Payment Attempt Date

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