Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/subscriptions-api-connector / api / subscriptionApi.js
Size: Mime:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.buildHeaders = buildHeaders;
exports.createSubscription = createSubscription;
exports.changeSubscriptionPlan = changeSubscriptionPlan;
exports.fetchSubscription = fetchSubscription;
exports.fetchAllSubscriptions = fetchAllSubscriptions;
exports.validateSubdomain = validateSubdomain;
exports.validateVatId = validateVatId;
exports.saveAndValidateBillingInfo = saveAndValidateBillingInfo;
exports.changeAutoRenewal = changeAutoRenewal;

var _fetchEverywhere = _interopRequireDefault(require("fetch-everywhere"));

var _fetch = require("../helpers/fetch.js");

var _converter = _interopRequireDefault(require("../helpers/converter"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var SUBDOMAIN_ENDPOINT = '/np/utils/validateUrlPrefix';
/**
 * Only adds truthy values to the headers object.
 *
 * @method
 * @private
 * @param {string} authToken Http Authorization bearer token to be sent on the request headers
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 */

function buildHeaders() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      authToken = _ref.authToken,
      contentType = _ref.contentType,
      accept = _ref.accept,
      experimentId = _ref.experimentId;

  return _objectSpread({}, Boolean(authToken) && {
    Authorization: "Bearer ".concat(authToken)
  }, Boolean(experimentId) && {
    'billing-experiment-key': experimentId
  }, {
    'Content-Type': contentType || 'application/json',
    Accept: accept || 'application/json'
  });
}
/**
 * Creates a subscription for the user.
 * @method
 * @public
 * @param {string} planId The id of the subscription plan
 * @param {SubscriptionPayload} subscriptionPayload The data of the subscription to create
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */


function createSubscription(authToken, planId, subscriptionPayload, experimentId, options) {
  var headers = buildHeaders({
    authToken: authToken,
    experimentId: experimentId
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/plans/").concat(planId, "/subscribe"), {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(subscriptionPayload)
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler);
}
/**
 * Updates the user's subscription.
 * @method
 * @public
 * @param {string} planId The id of the subscription plan
 * @param {SubscriptionPayload} subscriptionPayload The data of the subscription to create
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {string} experimentId Id of an AB test experiment defined in the MongoDB for svc-billing
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */


function changeSubscriptionPlan(authToken, planId, subscriptionPayload, experimentId, options) {
  var headers = buildHeaders({
    authToken: authToken,
    experimentId: experimentId
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/plans/").concat(planId, "/change"), {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(subscriptionPayload)
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler);
}
/**
 * Fetches a user subscription.
 * @method
 * @public
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */


function fetchSubscription(authToken, options) {
  var headers = buildHeaders({
    authToken: authToken
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint), {
    headers: headers
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler).then(function (response) {
    return response && response.document && response.document.length > 0 ? response.document[0] : null;
  }).then(_converter["default"]);
}
/**
 * Fetches all user subscriptions, including those that expired.
 * @method
 * @public
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */


function fetchAllSubscriptions(authToken, options) {
  var headers = buildHeaders({
    authToken: authToken
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/list"), {
    headers: headers
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler).then(function (response) {
    return response && response.document ? response.document : [];
  }).then(function (subscriptions) {
    return subscriptions.map(_converter["default"]);
  });
}
/**
 * Checks whether a subdomain is available.
 * Note: this method still uses the NP API.
 * @method
 * @public
 * @param {string} subdomain The subdomain to validate
 * @param {string} locale The user's locale
 * @param {string} mandatorId The id of the user's mandator
 */


function validateSubdomain(subdomain, locale, mandatorId) {
  var headers = buildHeaders({
    contentType: 'application/x-www-form-urlencoded'
  });
  var details = {
    urlPrefix: subdomain,
    locale: locale,
    mandatorId: mandatorId
  };
  var formData = Object.keys(details).map(function (key) {
    return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(details[key]));
  }).join('&');
  return (0, _fetchEverywhere["default"])(SUBDOMAIN_ENDPOINT, {
    method: 'POST',
    headers: headers,
    body: formData
  })["catch"](_fetch.errorHandlerMonolith).then(_fetch.responseHandlerMonolith);
}
/**
 * Checks whether a VAT ID is valid.
 * @method
 * @public
 * @param {string} vatId The VAT ID to check for validity
 * @param {string} country The Country provided by the user
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {Object} options Additional options
 * @param {string} options.endpoint The endpoint's base URL
 */


function validateVatId(authToken, vatId, country, options) {
  var headers = buildHeaders({
    authToken: authToken
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/validate/").concat(country, "/").concat(vatId), {
    headers: headers
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler).then(function (response) {
    return response.document ? response.document.valid : false;
  });
}
/**
 * Saves billing info and checks if vat is valiid
 * @method
 * @public
 * @param {string} authToken Http Authorization bearer token to send with requests to billing API
 * @param {Object} billing information
 * @param {string} options.endpoint The endpoint's base URL
 */


function saveAndValidateBillingInfo(authToken, billingInfo, options) {
  var headers = buildHeaders({
    authToken: authToken
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/billingInfo"), {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(billingInfo)
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler).then(function (response) {
    return response.document ? response.document.valid : false;
  });
}
/**
 * Sets the autorenwal state of the given subscription
 *
 * @param {string} subscriptionId
 * @param {boolean} autoRenewalOn
 */


function changeAutoRenewal(authToken, subscriptionId, autoRenewalOn, options) {
  var autoRenewalState = autoRenewalOn ? 'ON' : 'OFF';
  var headers = buildHeaders({
    authToken: authToken
  });
  return (0, _fetchEverywhere["default"])("".concat(options.endpoint, "/").concat(subscriptionId, "/autorenewal/").concat(autoRenewalState), {
    method: 'PUT',
    headers: headers,
    body: JSON.stringify({})
  })["catch"](_fetch.errorHandler).then(_fetch.responseHandler);
}