Batch Apex Callouts to Stripe

Some customers use batch apex to make callouts to Stripe. This is supported for Product, Price, Coupon, Subscription, Subscription Item, Subscription Schedules, and Stripe Invoices. This functionality is available in conjunction with Salesforce's Change Data Capture feature.


  1. Optional: Add a batch apex class to your org that will work with the Change Data Capture feature. (This code will be dependent on your business use case. Use your in-house development team to decide what works best for your org.)


Some use cases use declarative means to identify new Stripe Billing objects. The apex code snippet below is only an example. Your setup may differ.

Example of a batch apex class:

public with sharing class SendingToStripeBatch implements Database.Batchable<SObject>, Database.Stateful, Database.AllowsCallouts {
    Set<Id> recordIds;
    Integer batchStep = 1;
    Id priceId;
    Id productId;
    Decimal quantity;
    Id subscriptionId;
    public SendingToStripeBatch(Set<Id> recordIds, Integer batchStep) {
        this.recordIds = recordIds;
        this.batchStep = batchStep;
    public SendingToStripeBatch(Id subscriptionId, Id priceId, Id productId, Decimal quantity, Integer batchStep) {
        this.subscriptionId = subscriptionId;
        this.priceId = priceId;
        this.productId = productId;
        this.quantity = quantity;
        this.batchStep = batchStep;
    public Database.QueryLocator start(Database.BatchableContext BC) {
        String query;
        if (this.batchStep == 1) {
            query = 'SELECT Id FROM Product2 WHERE Id IN :recordIds';
        } else if (this.batchStep == 2) {
            query = 'SELECT Id FROM bt_stripe__Plan2__c WHERE Id IN :recordIds';
        } else if (this.batchStep == 3) {
            query = 'SELECT Id FROM bt_stripe__Coupon2__c WHERE Id IN :recordIds';
        } else if (this.batchStep == 4) {
            query = 'SELECT Id FROM bt_stripe__Subscription2__c WHERE Id IN :recordIds';
        } else if (this.batchStep == 5) {
            query = 'SELECT Id FROM bt_stripe__Sales_Document__c WHERE Id IN :recordIds';
        } else if (this.batchStep == 6) {
            query = 'SELECT Id FROM bt_stripe__Subscription2__c WHERE Id = :subscriptionId';
        return Database.getQueryLocator(query);

    public void execute(Database.BatchableContext BC, List<SObject> scope) {
        if (this.batchStep >= 1 && this.batchStep < 6) {
            for (SObject record :(List<SObject>)scope) {
                record.put('bt_stripe__Push_To_Stripe__c', false);
            update scope;
            for (SObject record :(List<SObject>)scope) {
                record.put('bt_stripe__Push_To_Stripe__c', true);
            update scope;
        } else if (this.batchStep == 6) {
            bt_stripe__Subscription_Item__c si = new bt_stripe__Subscription_Item__c();
            si.bt_stripe__Product__c = productId;
            si.bt_stripe__Subscription__c = subscriptionId;
            si.bt_stripe__Plan__c = priceId;
            si.bt_stripe__Quantity__c = quantity;
            insert si;
    public void finish(Database.BatchableContext BC) {

  1. Add the object you would like to use to the Change Data Capture setting in Setup.
  1. Navigate to Setup -> Custom Settings -> Blackthorn Pay - Trigger Settings -> Enable Change Data Capture = TRUE.
  2. Create the records you would like sent to Stripe.
  3. Optional: Execute apex code that will tell the Change Data Capture logic which records to send to Stripe.


The code snippet below is only an example of a way to call the apex class from step 1. Your use case may differ and may use other declarative means for sending new records to Stripe.

Example of code used in an Execute Anonymous window of the developer console:

Set<Id> recordIds = new Set<Id>{recordId1, recordId2, recordId3, recordId4};
SendingToStripeBatch batch = new SendingToStripeBatch(new Map<Id, SObject>([SELECT Id, bt_stripeTest__Push_To_Stripe__c FROM Product2 WHERE Id IN :recordIds]).keySet(), 1);

Once the steps above have been configured and the code executed you will notice the related records are sent to Stripe.

Did this page help you?