@lomray/microservice-payment-stripe
Version:
Stripe payment microservice based on NodeJS & inverted json.
103 lines (99 loc) • 4.44 kB
JavaScript
;
var tslib = require('tslib');
var microserviceNodejsLib = require('@lomray/microservice-nodejs-lib');
var transaction = require('../../entities/transaction.js');
var extractIdFromStripeInstance = require('../../helpers/extract-id-from-stripe-instance.js');
var messages = require('../../helpers/validators/messages.js');
/**
* Application fee webhook handlers
*/
class ApplicationFee {
/**
* @constructor
*/
constructor(manager) {
this.manager = manager;
this.transactionRepository = manager.getRepository(transaction);
}
/**
* Handles application fee refund updated
*/
handleApplicationFeeRefundUpdated(event, sdk) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const { fee } = event.data.object;
const applicationFeeId = extractIdFromStripeInstance(fee);
const transactions = yield this.transactionRepository.find({
applicationFeeId,
});
if (!transactions.length) {
throw new microserviceNodejsLib.BaseException({
status: 500,
message: messages.getNotFoundMessage('Failed to update refunded application fees. Debit or credit transaction'),
payload: { eventName: event.type, applicationFeeId },
});
}
const { amount, amount_refunded: refundedApplicationFeeAmount } = yield sdk.applicationFees.retrieve(applicationFeeId);
/**
* @TODO: create helper for updates check
*/
let isUpdated = false;
transactions.forEach((transaction) => {
if (transaction.fee !== amount) {
throw new microserviceNodejsLib.BaseException({
status: 500,
message: `Handle webhook event "${event.type}" occur. Transaction fee is not equal to Stripe application fee`,
payload: { eventName: event.type, transactionFee: transaction.fee, feeAmount: amount },
});
}
if (transaction.params.refundedApplicationFeeAmount !== refundedApplicationFeeAmount) {
transaction.params.refundedApplicationFeeAmount = refundedApplicationFeeAmount;
isUpdated = true;
}
});
if (!isUpdated) {
return;
}
yield this.transactionRepository.save(transactions);
});
}
/**
* Handles application fee refunded
*/
handleApplicationFeeRefunded(event) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const { id: applicationFeeId, amount, amount_refunded: refundedApplicationFeeAmount, } = event.data.object;
const transactions = yield this.transactionRepository.find({
applicationFeeId,
});
if (!transactions.length) {
throw new microserviceNodejsLib.BaseException({
status: 500,
message: messages.getNotFoundMessage('Failed to update refunded application fees. Debit or credit transaction'),
payload: { eventName: event.type, applicationFeeId },
});
}
let isUpdated = false;
/**
* @TODO: move out to separate helper (e.g. class calculation)
*/
transactions.forEach((transaction) => {
if (transaction.fee !== amount) {
throw new microserviceNodejsLib.BaseException({
status: 500,
message: `Handle webhook event "${event.type}" occur. Transaction fee is not equal to Stripe application fee`,
payload: { eventName: event.type, transactionFee: transaction.fee, feeAmount: amount },
});
}
if (transaction.params.refundedApplicationFeeAmount !== refundedApplicationFeeAmount) {
transaction.params.refundedApplicationFeeAmount = refundedApplicationFeeAmount;
isUpdated = true;
}
});
if (!isUpdated) {
return;
}
yield this.transactionRepository.save(transactions);
});
}
}
module.exports = ApplicationFee;