Create
1 min
To create a Subscription in Stripe, use the action “CreateSubscription” and pass the Subscription data in the RequestJSON under “subscriptions.”
Note: You can only create one record at a time.
Here is an example of creating a Subscription where we are setting every possible field(See Subscription - Stripe for all the available fields). The response will return the response back from Stripe, which includes the Subscription ID (internalId) plus the newly created Salesforce Subscription ID (salesforceId).
Map<String, Object> subscriptionData = new Map<String, Object>();
subscriptionData.put('processorCustomerId', 'processorCustomerId');
subscriptionData.put('collectionMethod', 'charge_automatically');
subscriptionData.put('processorPaymentMethodId', 'processorPaymentMethodId');
List<object> items = new List<object>();
Map<String, Object> itemData = new Map<String, Object>();
itemData.put('processorPriceId', 'processorPriceId');
itemData.put('quantity', 2);
items.add(itemData);
subscriptionData.put('items', items);
// Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('subscriptions', new List<Object>{subscriptionData});
String reqJSON = JSON.serialize(requestJSONMap);
Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version','1.0');
reqObj.put('action','CreateSubscription');
reqObj.put('processorId', 'processorId');
reqObj.put('requestJSON',reqJSON);
// Placing a request to Breadwinner Global API
Map<String,Object> resp = bw_payments.BreadwinnerPaymentsAPI.call(reqObj);
System.debug(resp);