declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
34 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reqInvoiceCharge = void 0;
const helpful_errors_1 = require("helpful-errors");
const visualogic_1 = require("visualogic");
const DeclaredStripeInvoice_1 = require("../../domain/objects/DeclaredStripeInvoice");
const castToDeclaredStripeInvoice_1 = require("../cast/castToDeclaredStripeInvoice");
const getInvoice_1 = require("./getInvoice");
/**
* .what = requests that the customer be charged for the invoice
* .what.intent
* - attempts to .pay the invoice with the customers payment method
* - responds with failure via error or success via updated invoice state
*/
exports.reqInvoiceCharge = (0, visualogic_1.withLogTrail)(async (input, context) => {
// find the invoice
const invoiceFound = await (0, getInvoice_1.getInvoice)(input, context);
if (!invoiceFound)
throw new helpful_errors_1.BadRequestError('can not charge an invoice that does not exist', {
input,
invoiceFound,
});
// verify it is still in draft mode
if (invoiceFound.status === DeclaredStripeInvoice_1.DeclaredStripeInvoiceStatus.paid)
return invoiceFound; // if its already "paid", then this could be an idempotent retry, so just return it
if (invoiceFound.status !== DeclaredStripeInvoice_1.DeclaredStripeInvoiceStatus.open)
throw new helpful_errors_1.BadRequestError('can not charge an invoice that is not in open mode', { invoiceFound });
// otherwise, finalize it
const stripeInvoiceUpdated = await context.stripe.invoices.pay(invoiceFound.id);
return (0, castToDeclaredStripeInvoice_1.castToDeclaredStripeInvoice)(stripeInvoiceUpdated);
}, {
name: (0, visualogic_1.getResourceNameFromFileName)(__filename),
});
//# sourceMappingURL=reqInvoiceCharge.js.map