fix(medusa, medusa-payment-paypal): Add missing data in Payment Collection input (#3010)

This commit is contained in:
Adrien de Peretti
2023-01-13 16:26:39 +01:00
committed by GitHub
parent 8221e089b8
commit 142c8aa70f
8 changed files with 140 additions and 21 deletions

View File

@@ -0,0 +1,7 @@
---
"medusa-payment-klarna": patch
"medusa-payment-paypal": patch
"@medusajs/medusa": patch
---
fix(medusa): Payment collection should provider the region_id, total and id in the partial cart data

View File

@@ -41,6 +41,26 @@ describe("KlarnaProviderService", () => {
order_amount: 100, order_amount: 100,
}) })
}) })
it("creates Klarna order using new API", async () => {
const result = await klarnaProviderService.createPayment({
email: "",
context: {},
shipping_methods: [],
shipping_address: null,
id: "",
region_id: carts.frCart.region_id,
total: carts.frCart.total,
resource_id: "resource_id",
currency_code: carts.frCart.region.currency_code,
amount: carts.frCart.total
})
expect(result).toEqual({
order_id: "123456789",
order_amount: 100,
})
})
}) })
describe("retrievePayment", () => { describe("retrievePayment", () => {
@@ -152,6 +172,30 @@ describe("KlarnaProviderService", () => {
order_id: "123456789", order_id: "123456789",
}) })
}) })
it("returns updated Klarna order using new API", async () => {
result = await klarnaProviderService.updatePayment(
{
order_id: "123456789",
},
{
email: "",
context: {},
shipping_methods: [],
shipping_address: null,
id: "",
region_id: carts.frCart.region_id,
total: carts.frCart.total,
resource_id: "resource_id",
currency_code: carts.frCart.region.currency_code,
amount: carts.frCart.total
}
)
expect(result).toEqual({
order_id: "123456789",
})
})
}) })
describe("cancelPayment", () => { describe("cancelPayment", () => {

View File

@@ -5,7 +5,7 @@ import { PaymentService } from "medusa-interfaces"
class KlarnaProviderService extends PaymentService { class KlarnaProviderService extends PaymentService {
static identifier = "klarna" static identifier = "klarna"
constructor({ logger, shippingProfileService, totalsService }, options) { constructor({ logger, shippingProfileService }, options) {
super() super()
/** /**
@@ -42,15 +42,12 @@ class KlarnaProviderService extends PaymentService {
/** @private @const {ShippingProfileService} */ /** @private @const {ShippingProfileService} */
this.shippingProfileService_ = shippingProfileService this.shippingProfileService_ = shippingProfileService
/** @private @const {TotalsService} */
this.totalsService_ = totalsService
} }
async lineItemsToOrderLines_(cart) { async lineItemsToOrderLines_(cart) {
let order_lines = [] let order_lines = []
for (const item of cart.items) { for (const item of cart.items ?? []) {
// Withdraw discount from the total item amount // Withdraw discount from the total item amount
const quantity = item.quantity const quantity = item.quantity
@@ -67,7 +64,7 @@ class KlarnaProviderService extends PaymentService {
}) })
} }
if (cart.shipping_methods.length) { if (cart.shipping_methods?.length) {
const name = [] const name = []
let total = 0 let total = 0
let tax = 0 let tax = 0
@@ -103,7 +100,7 @@ class KlarnaProviderService extends PaymentService {
async cartToKlarnaOrder(cart) { async cartToKlarnaOrder(cart) {
let order = { let order = {
// Cart id is stored, such that we can use it for hooks // Cart id is stored, such that we can use it for hooks
merchant_data: cart.id, merchant_data: cart.resource_id ?? cart.id,
locale: "en-US", locale: "en-US",
} }
@@ -147,8 +144,8 @@ class KlarnaProviderService extends PaymentService {
} }
order.order_amount = total order.order_amount = total
order.order_tax_amount = tax_total - cart.gift_card_tax_total order.order_tax_amount = tax_total - cart.gift_card_tax_total ?? 0
order.purchase_currency = region.currency_code.toUpperCase() order.purchase_currency = region?.currency_code?.toUpperCase() ?? "SE"
order.merchant_urls = { order.merchant_urls = {
terms: this.options_.merchant_urls.terms, terms: this.options_.merchant_urls.terms,

View File

@@ -30,9 +30,9 @@
}, },
"scripts": { "scripts": {
"prepare": "cross-env NODE_ENV=production yarn run build", "prepare": "cross-env NODE_ENV=production yarn run build",
"build": "babel src -d . --ignore **/__tests__", "build": "babel src -d . --ignore **/(__tests__|__mocks__)",
"watch": "babel -w src --out-dir . --ignore **/__tests__", "watch": "babel -w src --out-dir . --ignore **/(__tests__|__mocks__)",
"test": "jest --passWithNoTests" "test": "jest --passWithNoTests src/**"
}, },
"peerDependencies": { "peerDependencies": {
"medusa-interfaces": "1.3.3" "medusa-interfaces": "1.3.3"

View File

@@ -1,8 +1,5 @@
import PayPalProviderService from "../paypal-provider" import PayPalProviderService from "../paypal-provider"
import { import { PayPalClientMock, PayPalMock, } from "../../__mocks__/@paypal/checkout-server-sdk"
PayPalMock,
PayPalClientMock,
} from "../../__mocks__/@paypal/checkout-server-sdk"
const TotalsServiceMock = { const TotalsServiceMock = {
getTotal: jest.fn().mockImplementation((c) => c.total), getTotal: jest.fn().mockImplementation((c) => c.total),
@@ -65,6 +62,40 @@ describe("PaypalProviderService", () => {
expect(result.id).toEqual("test") expect(result.id).toEqual("test")
}) })
it("creates paypal order using new API", async () => {
result = await paypalProviderService.createPayment({
id: "",
region_id: "fr",
total: 1000,
resource_id: "resource_id"
})
expect(PayPalMock.orders.OrdersCreateRequest).toHaveBeenCalledTimes(1)
expect(PayPalClientMock.execute).toHaveBeenCalledTimes(1)
expect(PayPalClientMock.execute).toHaveBeenCalledWith(
expect.objectContaining({
order: true,
body: {
intent: "AUTHORIZE",
application_context: {
shipping_preference: "NO_SHIPPING",
},
purchase_units: [
{
custom_id: "resource_id",
amount: {
currency_code: "EUR",
value: "10.00",
},
},
],
},
})
)
expect(result.id).toEqual("test")
})
}) })
describe("retrievePayment", () => { describe("retrievePayment", () => {
@@ -140,6 +171,41 @@ describe("PaypalProviderService", () => {
expect(result.id).toEqual("test") expect(result.id).toEqual("test")
}) })
it("updates paypal order using new API", async () => {
result = await paypalProviderService.updatePayment(
{ id: "test" },
{
id: "",
region_id: "fr",
total: 1000,
resource_id: "resource_id"
}
)
expect(PayPalMock.orders.OrdersPatchRequest).toHaveBeenCalledTimes(1)
expect(PayPalMock.orders.OrdersPatchRequest).toHaveBeenCalledWith("test")
expect(PayPalClientMock.execute).toHaveBeenCalledTimes(1)
expect(PayPalClientMock.execute).toHaveBeenCalledWith(
expect.objectContaining({
order: true,
body: [
{
op: "replace",
path: "/purchase_units/@reference_id=='default'",
value: {
amount: {
currency_code: "EUR",
value: "10.00",
},
},
},
],
})
)
expect(result.id).toEqual("test")
})
}) })
describe("capturePayment", () => { describe("capturePayment", () => {

View File

@@ -88,10 +88,10 @@ class PayPalProviderService extends PaymentService {
* @returns {object} the data to be stored with the payment session. * @returns {object} the data to be stored with the payment session.
*/ */
async createPayment(cart) { async createPayment(cart) {
const { region_id } = cart const { region_id, id, resource_id, total } = cart
const { currency_code } = await this.regionService_.retrieve(region_id) const { currency_code } = await this.regionService_.retrieve(region_id)
const amount = cart.total const amount = total
const request = new PayPal.orders.OrdersCreateRequest() const request = new PayPal.orders.OrdersCreateRequest()
request.requestBody({ request.requestBody({
@@ -101,7 +101,7 @@ class PayPalProviderService extends PaymentService {
}, },
purchase_units: [ purchase_units: [
{ {
custom_id: cart.id, custom_id: resource_id ?? id,
amount: { amount: {
currency_code: currency_code.toUpperCase(), currency_code: currency_code.toUpperCase(),
value: roundToTwo( value: roundToTwo(
@@ -216,7 +216,7 @@ class PayPalProviderService extends PaymentService {
*/ */
async updatePayment(sessionData, cart) { async updatePayment(sessionData, cart) {
try { try {
const { region_id } = cart const { region_id, total } = cart
const { currency_code } = await this.regionService_.retrieve(region_id) const { currency_code } = await this.regionService_.retrieve(region_id)
const request = new PayPal.orders.OrdersPatchRequest(sessionData.id) const request = new PayPal.orders.OrdersPatchRequest(sessionData.id)
@@ -228,7 +228,7 @@ class PayPalProviderService extends PaymentService {
amount: { amount: {
currency_code: currency_code.toUpperCase(), currency_code: currency_code.toUpperCase(),
value: roundToTwo( value: roundToTwo(
humanizeAmount(cart.total, currency_code), humanizeAmount(total, currency_code),
currency_code currency_code
), ),
}, },

View File

@@ -306,6 +306,8 @@ export default class PaymentCollectionService extends TransactionBaseService {
shipping_methods: [], shipping_methods: [],
shipping_address: null, shipping_address: null,
id: "", id: "",
region_id: payCol.region_id,
total: session.amount,
}, },
resource_id: payCol.id, resource_id: payCol.id,
currency_code: payCol.currency_code, currency_code: payCol.currency_code,
@@ -465,6 +467,8 @@ export default class PaymentCollectionService extends TransactionBaseService {
shipping_methods: [], shipping_methods: [],
shipping_address: null, shipping_address: null,
id: "", id: "",
region_id: payCol.region_id,
total: session.amount,
}, },
resource_id: payCol.id, resource_id: payCol.id,
currency_code: payCol.currency_code, currency_code: payCol.currency_code,

View File

@@ -665,6 +665,7 @@ export default class PaymentProviderService extends TransactionBaseService {
context.cart = data.cart context.cart = data.cart
context.amount = data.amount context.amount = data.amount
context.currency_code = data.currency_code context.currency_code = data.currency_code
context.resource_id = data.resource_id
Object.assign(context, cart) Object.assign(context, cart)
} }