@dbbs/strapi-stripe-payment
Version:
Strapi integration plugin for Stripe payment system
51 lines (50 loc) • 2.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const http_errors_1 = __importDefault(require("http-errors"));
const strapi_1 = require("@strapi/strapi");
const helpers_1 = require("../helpers");
const enums_1 = require("../enums");
exports.default = strapi_1.factories.createCoreService('plugin::stripe-payment.purchase', ({ strapi }) => ({
async createCheckoutSession(params) {
const { userId } = params;
const { organizationName, customerId, plan } = await (0, helpers_1.validateAndFetchCheckoutSessionDetails)(strapi, params);
if (plan.type !== enums_1.PlanType.ONE_TIME) {
throw new http_errors_1.default.BadRequest('Cannot create purchase for a one-time plan');
}
const successUrl = strapi.config.get('server.stripe.successPaymentUrl');
const sessionParams = {
success_url: successUrl,
metadata: { organizationName, userId, planId: plan.id, quantity: 1 },
line_items: [{ price: plan.stripe_id, quantity: 1 }],
mode: 'payment',
...(customerId && { customer: customerId })
};
const session = await strapi.plugin('stripe-payment').service('stripe').checkout.sessions.create(sessionParams);
return session.url;
},
async getAllTransactions(params) {
const { organizationId } = params;
const organization = await strapi.query('plugin::stripe-payment.organization').findOne({
where: {
id: organizationId
},
populate: {
transactions: true
}
});
if (!organization) {
return null;
}
return strapi.query('plugin::stripe-payment.purchase').findMany({
where: {
organization: organization.id
},
populate: {
plan: true
}
});
}
}));