UNPKG

@dbbs/strapi-stripe-payment

Version:
109 lines (108 loc) 4.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const strapi_1 = require("@strapi/strapi"); const http_errors_1 = __importDefault(require("http-errors")); const validationSchemas_1 = require("../validationSchemas"); const helpers_1 = require("../helpers"); exports.default = strapi_1.factories.createCoreController('plugin::stripe-payment.subscription', ({ strapi }) => ({ async createCheckoutSession(ctx) { const { quantity, planId, organizationName, organizationId } = ctx.request.body; const { user } = ctx.state; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.createCheckoutSessionSchema, { quantity, planId, organizationName, organizationId }); const checkoutSessionLink = await strapi .plugin('stripe-payment') .service('subscription') .createCheckoutSession({ userId: user.id, ...validatedParams }); ctx.send({ url: checkoutSessionLink }); }, async getSubscriptionById(ctx) { const { id } = ctx.params; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.getSubscriptionByIdSchema, { id }); const subscription = await strapi .plugin('stripe-payment') .service('subscription') .getSubscriptionById(validatedParams); if (!subscription) { throw new http_errors_1.default.NotFound(`Subscription with ID ${id} not found`); } ctx.send(subscription); }, async getMySubscription(ctx) { const { user } = ctx.state; const subscription = await strapi .plugin('stripe-payment') .service('subscription') .getMySubscription({ userId: user.id }); ctx.send(subscription); }, async getSubscriptions(ctx) { const subscriptions = await strapi.plugin('stripe-payment').service('subscription').getSubscriptions(); ctx.send(subscriptions); }, async cancelSubscription(ctx) { const { id } = ctx.params; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.pauseSubscriptionSchema, { id }); const result = await strapi.plugin('stripe-payment').service('subscription').cancelSubscription(validatedParams); if (!result) { throw new http_errors_1.default.NotFound(`Subscription with ID: ${id} not found`); } ctx.send(result); }, async pauseSubscription(ctx) { const { id } = ctx.params; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.pauseSubscriptionSchema, { id }); const subscription = await strapi .plugin('stripe-payment') .service('subscription') .pauseSubscription(validatedParams); if (!subscription) { throw new http_errors_1.default.NotFound(`Subscription with ID: ${id} not found`); } ctx.send(subscription); }, async resumeSubscription(ctx) { const { id } = ctx.params; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.resumeSubscriptionSchema, { id }); const subscription = await strapi .plugin('stripe-payment') .service('subscription') .resumeSubscription(validatedParams); if (!subscription) { throw new http_errors_1.default.NotFound(`Subscription with ID: ${id} not found`); } ctx.send(subscription); }, async updateStripeSubscription(ctx) { const { id } = ctx.params; const { quantity, planId } = ctx.request.body; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.updateSubscriptionSchema, { id, quantity, planId }); const subscription = await strapi .plugin('stripe-payment') .service('subscription') .updateStripeSubscription(validatedParams); if (!subscription) { throw new http_errors_1.default.NotFound(`Subscription with ID: ${id} not found`); } ctx.send(subscription); }, async resubscribe(ctx) { const { id } = ctx.params; const validatedParams = await (0, helpers_1.validateWithYupSchema)(validationSchemas_1.resubscribeSchema, { id }); const result = await strapi.plugin('stripe-payment').service('subscription').resubscribe(validatedParams); if (!result) { throw new http_errors_1.default.NotFound(`Subscription with ID ${id} not found`); } ctx.send(result); } }));