@dbbs/strapi-stripe-payment
Version:
Strapi integration plugin for Stripe payment system
48 lines (47 loc) • 2.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAndFetchCheckoutSessionDetails = validateAndFetchCheckoutSessionDetails;
const http_errors_1 = __importDefault(require("http-errors"));
async function validateAndFetchCheckoutSessionDetails(strapi, params) {
const { planId, quantity, organizationId } = params;
let organizationName;
let customerId;
if (params.organizationName) {
const organizationExisting = await strapi.query('plugin::stripe-payment.organization').count({
where: {
name: params.organizationName
}
});
if (organizationExisting) {
throw new http_errors_1.default.BadRequest(`Organization with name ${params.organizationName} already exists`);
}
organizationName = params.organizationName;
}
else {
const organizationById = await strapi.query('plugin::stripe-payment.organization').findOne({
where: { id: organizationId },
populate: { users: true, subscription: true }
});
if (!organizationById) {
throw new http_errors_1.default.NotFound(`Organization with id ${organizationId} not found`);
}
if (quantity < organizationById.users.length) {
throw new http_errors_1.default.BadRequest('Quantity cannot be less than the number of users in the organization');
}
if (quantity > organizationById.quantity) {
throw new http_errors_1.default.BadRequest("Quantity exceeds the organization's limit. Please increase the number of seats first.");
}
customerId = organizationById.customer_id;
organizationName = organizationById.name;
}
const plan = await strapi.query('plugin::stripe-payment.plan').findOne({
where: { id: planId }
});
if (!plan) {
throw new http_errors_1.default.NotFound('Plan not found');
}
return { organizationName, customerId, plan };
}