@wepublish/api
Version:
API core for we.publish.
137 lines • 5.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePaymentProvider = void 0;
const tslib_1 = require("tslib");
const client_1 = require("@prisma/client");
const body_parser_1 = tslib_1.__importDefault(require("body-parser"));
class BasePaymentProvider {
constructor(props) {
var _a;
this.remoteManagedSubscription = false;
this.id = props.id;
this.name = props.name;
this.offSessionPayments = props.offSessionPayments;
this.incomingRequestHandler = (_a = props.incomingRequestHandler) !== null && _a !== void 0 ? _a : body_parser_1.default.json();
}
updateRemoteSubscriptionAmount(props) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return;
});
}
cancelRemoteSubscription(props) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return;
});
}
createRemoteInvoice(props) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return;
});
}
updatePaymentWithIntentState({ intentState, paymentClient, paymentsByID, invoicesByID, subscriptionClient, userClient, invoiceClient }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const payment = yield paymentsByID.load(intentState.paymentID);
// TODO: should we overwrite already paid/canceled payments
if (!payment)
throw new Error(`Payment with ID ${intentState.paymentID} not found`);
const updatedPayment = yield paymentClient.update({
where: { id: payment.id },
data: {
state: intentState.state,
paymentData: intentState.paymentData,
intentData: payment.intentData,
intentSecret: payment.intentSecret,
intentID: payment.intentID,
invoiceID: payment.invoiceID,
paymentMethodID: payment.paymentMethodID
}
});
if (!updatedPayment) {
throw new Error('Error while updating Payment');
}
// get invoice and subscription joins out of the payment
const invoice = yield invoicesByID.load(payment.invoiceID);
if (!invoice || !invoice.subscriptionID) {
throw new Error(`Invoice with ID ${payment.invoiceID} does not exist`);
}
const subscription = yield subscriptionClient.findUnique({
where: {
id: invoice.subscriptionID
},
include: {
periods: true
}
});
if (!subscription) {
throw new Error(`Subscription with ID ${invoice.subscriptionID} does not exist`);
}
const invoicePeriod = subscription.periods.find(period => period.invoiceID === invoice.id);
if (!invoicePeriod) {
throw new Error(`Invoice with ID ${invoice.id} has no period!`);
}
// Mark invoice as paid
if (intentState.state === client_1.PaymentState.paid) {
yield invoiceClient.update({
where: { id: invoice.id },
data: {
paidAt: new Date()
}
});
yield subscriptionClient.update({
where: { id: invoice.subscriptionID },
data: {
paidUntil: invoicePeriod.endsAt
}
});
}
// update payment provider
if (intentState.customerID && payment.invoiceID) {
yield this.updatePaymentProvider(userClient, subscription, intentState.customerID);
}
return updatedPayment;
});
}
/**
* adding or updating paymentProvider customer ID for user
*
* @param userClient
* @param subscription
* @param customerID
* @private
*/
updatePaymentProvider(userClient, subscription, customerID) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!subscription) {
throw new Error('Empty subscription within updatePaymentProvider method.');
}
const user = yield userClient.findUnique({
where: {
id: subscription.userID
},
include: {
paymentProviderCustomers: true
}
});
if (!user)
throw new Error(`User with ID ${subscription.userID} does not exist`);
yield userClient.update({
where: {
id: user.id
},
data: {
paymentProviderCustomers: {
deleteMany: {
paymentProviderID: this.id
},
create: {
paymentProviderID: this.id,
customerID
}
}
}
});
});
}
}
exports.BasePaymentProvider = BasePaymentProvider;
//# sourceMappingURL=payment-provider.js.map