Repository URL to install this package:
|
Version:
2.0.0-rc.6 ▾
|
'use strict';
const fetch = require('fetch-everywhere');
const {
errorHandler,
responseHandler
} = require('./helpers/fetch');
/**
* Create a subscription for a user.
*
* @param {string} accessToken - The user's access-token
* @param {number} planId - The numerical plan id to setup for the user
* @param {object} subscription - the subscription information
* @param {boolean} subscription.autoRenewal
* @param {string} [subscription.locale]
* @param {number} [subscription.units]
* @param {boolean} [subscription.trial]
* @param {Array} [subscription.billingInfo]
* @param {object} [subscription.billingInfo.billingAddress]
* @param {string} [subscription.billingInfo.billingAddress.country]
* @returns {Promise<object>}
*/
function createSubscriptionForUser(accessToken, planId = 25, subscription = {}) {
if (typeof accessToken !== 'string') {
throw new TypeError(`accessToken param must be a string, got ${typeof accessToken}`);
}
const url = `${process.env.SVC_BILLING_BASE_URL}/subscriptions/plans/${planId}/subscribe`;
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
Accept: 'application/json'
};
const subscriptionDefaults = {
trial: true,
autoRenewal: true,
units: 1,
locale: 'en-US',
billingInfo: [{
billingAddress: {
country: 'CH'
}
}]
};
return fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({ ...subscriptionDefaults,
...subscription
})
}).then(responseHandler).catch(errorHandler);
}
module.exports = createSubscriptionForUser;
//# sourceMappingURL=createSubscriptionForUser.js.map