@agnostack/next-shopify
Version:
Please contact agnoStack via info@agnostack.com for any questions
93 lines • 5.62 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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleAppBilling = void 0;
const verifyd_1 = require("@agnostack/verifyd");
const utils_1 = require("../../utils");
const shared_1 = require("../../../shared");
const TERMINAL_STATUSES = [
'EXPIRED',
'DECLINED',
'CANCELLED'
];
// NOTE: typically handling: "/api/webhooks/app/billing"
const handleAppBilling = (serverRuntimeConfig, data) => {
const { returnResponse = true } = data !== null && data !== void 0 ? data : {};
return (req, res) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
const handleResponse = (body, statusCode = 200) => {
if (returnResponse) {
return res.status(statusCode).json(body);
}
return { statusCode, body };
};
try {
const { 'x-shop-domain': shopDomain, 'x-shopify-shop-domain': shopifyShopDomain, 'x-shopify-shop': shop = shopifyShopDomain !== null && shopifyShopDomain !== void 0 ? shopifyShopDomain : shopDomain, } = (_a = req.headers) !== null && _a !== void 0 ? _a : {};
if ((0, shared_1.stringEmpty)(shop)) {
return handleResponse({ handler: 'billing', error: 'Missing shop' });
}
const { shopify, loadSession, updateSession, getVerifiedClients } = yield (0, utils_1.getShopifyHelpers)(serverRuntimeConfig, { shop });
// TODO: explore getVerificationHelpers?
const rawBody = yield (0, verifyd_1.ensureRawBody)(req);
const _e = yield shopify.webhooks.validate({
rawBody,
rawRequest: req,
rawResponse: res,
}), { valid } = _e, webhookCheck = __rest(_e, ["valid"]);
if (valid) {
try {
let message = 'ignored';
const [eventType, { status } = {}] = Object.entries((0, shared_1.safeParse)(rawBody || '{}'))[0];
if ((eventType === shared_1.BILLING_EVENT_TYPES.SUBSCRIPTION) && TERMINAL_STATUSES.includes(status)) {
const { Graphql } = (_b = yield getVerifiedClients()) !== null && _b !== void 0 ? _b : {};
const installationSubscriptionsResponse = yield Graphql.request(shared_1.APP_QUERY.INSTALLATION_SUBSCRIPTIONS);
const installationSubscriptions = (_d = (_c = installationSubscriptionsResponse === null || installationSubscriptionsResponse === void 0 ? void 0 : installationSubscriptionsResponse.data) === null || _c === void 0 ? void 0 : _c.currentAppInstallation) === null || _d === void 0 ? void 0 : _d.activeSubscriptions;
// NOTE this ensure all subscriptions are cancelled when in terminal status
yield Promise.all((0, shared_1.ensureArray)(installationSubscriptions).map((subscription) => (Graphql.request(shared_1.APP_QUERY.CANCEL_APP_SUBSCRIPTION, {
variables: {
id: subscription === null || subscription === void 0 ? void 0 : subscription.id,
},
}))));
message = 'deleted';
}
if ((serverRuntimeConfig.SHOPIFY_RUN_MODE === 'review') && !TERMINAL_STATUSES.includes(status)) {
const { id: sessionId } = yield loadSession();
// NOTE: billingStatus is a custom field we add when we're in review mode to handle reauth billing correctly
yield updateSession(sessionId, { billingStatus: status });
}
return handleResponse({ handler: 'billing', message });
}
catch (error) {
return handleResponse({ handler: 'billing', error: error === null || error === void 0 ? void 0 : error.message });
}
}
console.warn('Unable to validate billing webhook', webhookCheck);
return handleResponse({ handler: 'billing', error: 'Unable to validate billing webhook' });
}
catch (error) {
console.error('Error handling billing', error);
return handleResponse({ handler: 'billing', error: 'Error handling billing' });
}
});
};
exports.handleAppBilling = handleAppBilling;
//# sourceMappingURL=billing.js.map