fix: account for lowest currency unit in paypal (#761)

* fix: account for lowest currency unit in paypal

* fix: currency rounding
This commit is contained in:
Sebastian Rindom
2021-11-10 17:19:35 +01:00
committed by GitHub
parent 75c74656fb
commit b30a2af94d

View File

@@ -1,7 +1,15 @@
import _ from "lodash" import _ from "lodash"
import { humanizeAmount, zeroDecimalCurrencies } from "medusa-core-utils"
import PayPal from "@paypal/checkout-server-sdk" import PayPal from "@paypal/checkout-server-sdk"
import { PaymentService } from "medusa-interfaces" import { PaymentService } from "medusa-interfaces"
function roundToTwo(num, currency) {
if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
return `${num}`
}
return num.toFixed(2)
}
class PayPalProviderService extends PaymentService { class PayPalProviderService extends PaymentService {
static identifier = "paypal" static identifier = "paypal"
@@ -101,7 +109,10 @@ class PayPalProviderService extends PaymentService {
custom_id: cart.id, custom_id: cart.id,
amount: { amount: {
currency_code: currency_code.toUpperCase(), currency_code: currency_code.toUpperCase(),
value: (amount / 100).toFixed(2), value: roundToTwo(
humanizeAmount(amount, currency_code),
currency_code
),
}, },
}, },
], ],
@@ -193,7 +204,10 @@ class PayPalProviderService extends PaymentService {
value: { value: {
amount: { amount: {
currency_code: currency_code.toUpperCase(), currency_code: currency_code.toUpperCase(),
value: (cart.total / 100).toFixed(2), value: roundToTwo(
humanizeAmount(cart.total, currency_code),
currency_code
),
}, },
}, },
}, },
@@ -254,7 +268,10 @@ class PayPalProviderService extends PaymentService {
request.requestBody({ request.requestBody({
amount: { amount: {
currency_code: payment.currency_code.toUpperCase(), currency_code: payment.currency_code.toUpperCase(),
value: (amountToRefund / 100).toFixed(2), value: roundToTwo(
humanizeAmount(amountToRefund, payment.currency_code),
payment.currency_code
),
}, },
}) })