@wepublish/api
Version:
API core for we.publish.
123 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StripeCheckoutPaymentProvider = void 0;
const tslib_1 = require("tslib");
const client_1 = require("@prisma/client");
const api_1 = require("../../../../utils-api/src");
const stripe_1 = tslib_1.__importDefault(require("stripe"));
const payment_provider_1 = require("./payment-provider");
function mapStripeCheckoutEventToPaymentStatue(event) {
switch (event) {
case 'requires_payment_method':
case 'requires_confirmation':
case 'requires_action':
return client_1.PaymentState.requiresUserAction;
case 'processing':
return client_1.PaymentState.processing;
case 'succeeded':
return client_1.PaymentState.paid;
case 'canceled':
return client_1.PaymentState.canceled;
default:
return null;
}
}
class StripeCheckoutPaymentProvider extends payment_provider_1.BasePaymentProvider {
constructor(props) {
super(props);
this.stripe = new stripe_1.default(props.secretKey, {
apiVersion: '2020-08-27'
});
this.webhookEndpointSecret = props.webhookEndpointSecret;
}
getWebhookEvent(body, signature) {
return this.stripe.webhooks.constructEvent(body, signature, this.webhookEndpointSecret);
}
webhookForPaymentIntent(props) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const signature = props.req.headers['stripe-signature'];
const event = this.getWebhookEvent(props.req.body, signature);
if (!event.type.startsWith('checkout.session')) {
return {
status: 200,
message: `Skipping handling ${event.type}`
};
}
const session = event.data.object;
const intentStates = [];
switch (event.type) {
case 'checkout.session.completed': {
const intent = yield this.stripe.paymentIntents.retrieve(session.payment_intent);
const state = mapStripeCheckoutEventToPaymentStatue(intent.status);
if (state !== null && session.client_reference_id !== null) {
intentStates.push({
paymentID: session.client_reference_id,
paymentData: JSON.stringify(intent),
state
});
}
}
}
return {
status: 200,
paymentStates: intentStates
};
});
}
createIntent(props) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!props.successURL)
throw new Error('SuccessURL is not defined');
if (!props.failureURL)
throw new Error('FailureURL is not defined');
const session = yield this.stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: props.invoice.items.map(item => ({
price_data: {
currency: 'chf',
product_data: {
name: item.name
},
unit_amount: item.amount
},
quantity: item.quantity
})),
mode: 'payment',
success_url: props.successURL,
cancel_url: props.failureURL,
client_reference_id: props.paymentID,
customer_email: props.invoice.mail
});
if (session.amount_total === null) {
throw new Error('Error amount_total can not be null');
}
(0, api_1.logger)('stripeCheckoutPaymentProvider').info('Created Stripe checkout session with ID: %s for paymentProvider %s', session.id, this.id);
if (session.url === null) {
throw new Error('session url can not be null');
}
return {
intentID: session.id,
intentSecret: session.url,
intentData: JSON.stringify(session),
state: client_1.PaymentState.submitted
};
});
}
checkIntentStatus({ intentID }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const session = yield this.stripe.checkout.sessions.retrieve(intentID);
const state = session.payment_status === 'paid' ? client_1.PaymentState.paid : client_1.PaymentState.requiresUserAction;
if (!session.client_reference_id) {
(0, api_1.logger)('stripePaymentProvider').error('Stripe checkout session with ID: %s for paymentProvider %s returned with client_reference_id', session.id, this.id);
throw new Error('empty paymentID');
}
return {
state,
paymentID: session.client_reference_id,
paymentData: JSON.stringify(session)
};
});
}
}
exports.StripeCheckoutPaymentProvider = StripeCheckoutPaymentProvider;
//# sourceMappingURL=stripe-checkout-payment-provider.js.map