Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payment-stripe): update payment intent amount unit parsers #179

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions microservices/payment-stripe/src/services/payment-gateway/stripe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Log } from '@lomray/microservice-helpers';
import { BaseException, Microservice } from '@lomray/microservice-nodejs-lib';
import Event from '@lomray/microservices-client-api/constants/events/payment-stripe';
import fromSmallestUnit from '@lomray/microservices-client-api/helpers/parsers/from-smallest-unit';
import toSmallestUnit from '@lomray/microservices-client-api/helpers/parsers/to-smallest-unit';
import { validate } from 'class-validator';
import StripeSdk from 'stripe';
Expand Down Expand Up @@ -1098,7 +1099,14 @@ class Stripe extends Abstract {
const { id: paymentMethodCardId } = chargeCard;

// Get parsed entity cost
const entityUnitCost = this.toSmallestCurrencyUnit(entityCost);
const entityUnitCost = toSmallestUnit(entityCost);

if (!entityUnitCost) {
throw new BaseException({
status: 500,
message: 'Failed to calculate entity cost unit amount.',
});
}

// Calculate not-tax transaction fees
const {
Expand Down Expand Up @@ -1181,40 +1189,36 @@ class Stripe extends Abstract {
const stripePaymentIntent: StripeSdk.PaymentIntent = await this.sdk.paymentIntents.create({
...(title ? { description: title } : {}),
metadata: {
// Original float entity cost
entityCost,
stripeFee: this.fromSmallestCurrencyUnit(stripeFeeUnit),
platformFee: this.fromSmallestCurrencyUnit(platformUnitFee),
receiverExtraFee: this.fromSmallestCurrencyUnit(receiverAdditionalFee),
senderExtraFee: this.fromSmallestCurrencyUnit(senderAdditionalFee),
receiverExtraRevenue: this.fromSmallestCurrencyUnit(extraReceiverUnitRevenue),
cardId: paymentMethodCardId,
fee: this.fromSmallestCurrencyUnit(collectedFeeUnit),
feesPayer,
receiverRevenue: this.fromSmallestCurrencyUnit(receiverUnitRevenue),
baseFee: this.fromSmallestCurrencyUnit(baseFeeUnit),
senderPersonalFee: this.fromSmallestCurrencyUnit(senderPersonalFeeUnit),
receiverPersonalFee: this.fromSmallestCurrencyUnit(receiverPersonalFeeUnit),
senderId: senderCustomer.userId,
cardId: paymentMethodCardId,
receiverId: receiverUserId,
// Original float entity cost
entityCost,
stripeFee: fromSmallestUnit(stripeFeeUnit)!,
platformFee: fromSmallestUnit(platformUnitFee)!,
receiverExtraFee: fromSmallestUnit(receiverAdditionalFee)!,
senderExtraFee: fromSmallestUnit(senderAdditionalFee)!,
receiverExtraRevenue: fromSmallestUnit(extraReceiverUnitRevenue)!,
receiverRevenue: fromSmallestUnit(receiverUnitRevenue)!,
baseFee: fromSmallestUnit(baseFeeUnit)!,
senderPersonalFee: fromSmallestUnit(senderPersonalFeeUnit)!,
receiverPersonalFee: fromSmallestUnit(receiverPersonalFeeUnit)!,
fee: fromSmallestUnit(collectedFeeUnit)!,
...(entityId ? { entityId } : {}),
...(title ? { description: title } : {}),
...(tax?.id ? { taxCalculationId: tax?.id } : {}),
...(Object.keys(sharedTaxData).length !== 0 ? { ...sharedTaxData } : {}),
...(taxAutoCalculateFeeUnit
? { taxAutoCalculateFee: this.fromSmallestCurrencyUnit(taxAutoCalculateFeeUnit) }
? { taxAutoCalculateFee: fromSmallestUnit(taxAutoCalculateFeeUnit) }
: {}),
...(taxFeeUnit ? { taxFee: this.fromSmallestCurrencyUnit(taxFeeUnit) } : {}),
...(taxFeeUnit ? { taxFee: fromSmallestUnit(taxFeeUnit) } : {}),
...(tax?.transactionAmountWithTaxUnit
? {
taxTransactionAmountWithTax: this.fromSmallestCurrencyUnit(
tax?.transactionAmountWithTaxUnit,
),
taxTransactionAmountWithTax: fromSmallestUnit(tax?.transactionAmountWithTaxUnit),
}
: {}),
...(tax?.totalAmountUnit
? { taxTotalAmount: this.fromSmallestCurrencyUnit(tax?.totalAmountUnit) }
: {}),
...(tax?.totalAmountUnit ? { taxTotalAmount: fromSmallestUnit(tax?.totalAmountUnit) } : {}),
},
payment_method_types: [StripePaymentMethods.CARD],
confirm: true,
Expand Down