Create and Charge
1 min
To Create and Charge an Invoice in Stripe, use the action "CreateInvoice," collectionMethod as "charge_automatically," invoiceStatus as "CHARGE_CUSTOMER" and pass the Invoice data in the RequestJSON under "invoices."
Note: You can only create one record at a time.
The following is an example of Create and Charge an Invoice, where we are setting every possible field (See Invoice - Stripe for the available fields). The response will return the response from Stripe, which includes the new Stripe Invoice ID (internalId), plus the newly created Salesforce Invoice ID (salesforceId).
Map<String, Object> invoiceData = new Map<String, Object>();
invoiceData.put('processorCustomerId', 'processorCustomerId');
invoiceData.put('collectionMethod', 'charge_automatically');
invoiceData.put('invoiceStatus', 'CHARGE_CUSTOMER');
invoiceData.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);
invoiceData.put('items', items);
// Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('invoices', new List<Object>{invoiceData});
String reqJSON = JSON.serialize(requestJSONMap);
Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version','1.0');
reqObj.put('action','CreateInvoice');
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);