From be443a809302840dd58d55ce5270d4a812956807 Mon Sep 17 00:00:00 2001 From: Josh Bowling Date: Tue, 5 Nov 2024 06:42:25 +0900 Subject: [PATCH] Billing: Add createSetupIntent function Change-type: minor --- src/models/billing.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/models/billing.ts b/src/models/billing.ts index 38a2aebed..f953faaa6 100644 --- a/src/models/billing.ts +++ b/src/models/billing.ts @@ -84,6 +84,7 @@ export interface BankAccountBillingInfo extends BillingInfo { export interface TokenBillingSubmitInfo { token_id: string; 'g-recaptcha-response'?: string; + token_type?: 'payment_method' | 'setup_intent'; } export interface BillingPlanInfo { @@ -268,6 +269,39 @@ const getBillingModel = function ( return body; }, + /** + * @summary Create a Stripe setup intent required for setting billing information + * @name createSetupIntent + * @public + * @function + * @memberof balena.models.billing + * + * @param {(String|Number)} organization - handle (string) or id (number) of the target organization. + * + * @fulfil {Object} - partial stripe setup intent object + * @returns {Promise} + * + * @example + * balena.models.billing.createSetupIntent(orgId).then(function(setupIntent) { + * console.log(setupIntent); + * }); + */ + createSetupIntent: async ( + organization: string | number, + ): Promise<{ + id: string; + client_secret: string; + }> => { + const orgId = await getOrgId(organization); + + const { body } = await request.send({ + method: 'POST', + url: `/billing/v1/account/${orgId}/setup-intent`, + baseUrl: apiUrl, + }); + return body; + }, + /** * @summary Update the current billing information * @name updateBillingInfo @@ -280,6 +314,7 @@ const getBillingModel = function ( * * @param {String} billingInfo.token_id - the token id generated for the billing info form * @param {(String|undefined)} [billingInfo.'g-recaptcha-response'] - the captcha response + * @param {(String|undefined)} [billingInfo.token_type] - token type * @fulfil {Object} - billing information * @returns {Promise} *