@agnostack/next-shopify
Version:
Please contact agnoStack via info@agnostack.com for any questions
70 lines • 3.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePaymentConfig = exports.ensureBilling = void 0;
const shopify_api_1 = require("@shopify/shopify-api");
const shared_1 = require("../../shared");
const ensureBilling = (paymentConfig_1, ...args_1) => __awaiter(void 0, [paymentConfig_1, ...args_1], void 0, function* (paymentConfig, { shopify, session, forceBilling } = {}) {
const { shop, accessToken } = (0, shared_1.ensureObject)(session);
const { name: plan, test, config, required } = (0, shared_1.ensureObject)(paymentConfig);
if (!required) {
return undefined;
}
const isTest = (0, shared_1.isTrue)(test);
const hasValidBilling = (0, shared_1.isTrue)((0, shared_1.stringNotEmpty)(shop) &&
(0, shared_1.stringNotEmpty)(accessToken) &&
(0, shared_1.stringNotEmpty)(plan) &&
(0, shared_1.objectNotEmpty)(config) &&
Object.values(shared_1.BILLING_INTERVALS).includes(config === null || config === void 0 ? void 0 : config.interval));
if (!hasValidBilling) {
throw new shopify_api_1.BillingError({
message: 'Invalid billing configuration',
});
}
try {
// NOTE: forceBilling ensures while in "review" mode that we rebill each time (or for local testing if sent in manually)
if ((0, shared_1.isTrue)(forceBilling) || !(0, shared_1.isTrue)(yield shopify.billing.check({ plans: [plan], isTest, session }))) {
return shopify.billing.request({ plan, isTest, session });
}
return undefined;
}
catch (error) {
console.error('Failed to verify or initialize payment', error);
throw new shopify_api_1.BillingError({
message: 'Failed to verify or initialize payment',
});
}
});
exports.ensureBilling = ensureBilling;
// eslint-disable-next-line arrow-body-style
const generatePaymentConfig = (paymentConfig, { SHOPIFY_RUN_MODE, appTitle: _appTitle, data: { appName, debugMode, environment, } = {}, }) => {
if ((0, shared_1.objectEmpty)(paymentConfig) || (0, shared_1.stringEmpty)(environment)) {
return undefined;
}
const { name, price, currency = 'USD', type = shared_1.BILLING_INTERVALS.ONE_TIME, } = paymentConfig;
const appTitle = name || _appTitle || appName;
const isProduction = (environment === 'production');
const notProduction = !isProduction;
const billingName = `${appTitle}${notProduction ? ` - ${environment}` : ''} - ${(0, shared_1.unsnakecase)(type)}`;
return {
debugMode,
required: (isProduction || debugMode || (['cli', 'review'].includes(SHOPIFY_RUN_MODE))),
test: (notProduction || debugMode || (SHOPIFY_RUN_MODE === 'review')),
name: billingName,
config: {
interval: type,
amount: price,
currencyCode: currency,
},
};
};
exports.generatePaymentConfig = generatePaymentConfig;
//# sourceMappingURL=billing.js.map