Payment Connect

How to Process Scheduled Payments via Batch

This document outlines the functionality of the BatchCaptureScheduledPayment batch process in Payment Connect, which is responsible for capturing pre-authorized scheduled payments.


🛠️ Batch Job: BatchCaptureScheduledPayment

  • Captures scheduled payments for transactions that were previously authorized and are due to be processed today.


🔍 How It Works

  1. Fetch Records
    Queries Payment Transaction records that meet all the following conditions:

    • Payment Date = today

    • Payment Status = Authorized

  2. Capture Attempt

    • Attempts to capture the authorized amount using the linked Payment Method.

    • Uses the originally authorized amount as the capture amount.

  3. Success Handling
    If the capture is successful:

    • Payment Status → Updated to Accepted/Captured

    • Captured Amount → Updated with the actual captured value

  4. Failure Handling
    If the capture fails:

    • Payment Status → Updated to Failed

    • Failure Reason (or error message field) → Populated with the gateway error response


🧠 Automation

  • This batch can be scheduled to run daily to automatically process all scheduled payments due that day.


💻 Execution Script

👉 Run batch without filter (uses metadata-based query):

Database.executeBatch(new BatchCaptureScheduledPayment(), 1); 

👉 Run batch with a custom SOQL query:

String sQuery = 'SELECT Id, cm_finance__Payment_Status__c, cm_finance__Payment_Mode__c, cm_finance__Transaction_Id__c, cm_finance__Payment_Gateway__c, cm_finance__Transaction_Status__c, cm_finance__Amount__c, cm_finance__Application_Fee__c, cm_finance__Payment_Method__r.cm_finance__Holder_Full_Name__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__Card_Expiration_Year__c, cm_finance__Payment_Method__r.cm_finance__Card_Expiration_Month__c, cm_finance__Payment_Gateway__r.cm_finance__Payment_Gateway__c, cm_finance__Payment_Gateway__r.Name, cm_finance__Payment_Method__r.cm_finance__Bank_Account_Owner_Type__c, cm_finance__Payment_Method__r.cm_finance__Bank_Account_Type__c , cm_finance__Payment_Method__r.cm_finance__Location_User_Id__c  FROM cm_finance__Payment_Transaction__c WHERE cm_finance__Payment_Status__c = \'Authorized\' and cm_finance__Payment_Date__c<=today'; 
Database.executeBatch(new BatchCaptureScheduledPayment(sQuery), 1); 

✅ Benefits

📅 Ensures timely processing of all authorized scheduled payments
💳 Automates capture logic and reduces manual effort
📈 Tracks successful vs. failed capture attempts for auditing
🔁 Maintains clean lifecycle for payment authorization → accepted→ settlement