From 1f6ee0fc00024c25d6628e6531097f93f54f8a1b Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Tue, 6 Jul 2021 10:03:34 +0200 Subject: [PATCH 1/2] fix: paypal order --- .../src/api/routes/hooks/paypal.js | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js b/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js index 03fa6d3276..72a6a95bef 100644 --- a/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js +++ b/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js @@ -40,15 +40,40 @@ export default async (req, res) => { const orderService = req.scope.resolve("orderService") await manager.transaction(async (m) => { - const order = await orderService - .withTransaction(m) - .retrieveByCartId(cartId) - .catch((_) => undefined) + const cart = await cartService.withTransaction(m).retrieve(cartId) - if (!order) { - await cartService.withTransaction(m).setPaymentSession(cartId, "paypal") - await cartService.withTransaction(m).authorizePayment(cartId) - await orderService.withTransaction(m).createFromCart(cartId) + switch (cart.type) { + case "swap": { + const swap = await swapService + .withTransaction(m) + .retrieveByCartId(cartId) + .catch((_) => undefined) + + if (swap && swap.confirmed_at === null) { + await cartService + .withTransaction(m) + .setPaymentSession(cartId, "paypal") + await cartService.withTransaction(m).authorizePayment(cartId) + await swapService.withTransaction(m).registerCartCompletion(swap.id) + } + break + } + + default: { + const order = await orderService + .withTransaction(m) + .retrieveByCartId(cartId) + .catch((_) => undefined) + + if (!order) { + await cartService + .withTransaction(m) + .setPaymentSession(cartId, "paypal") + await cartService.withTransaction(m).authorizePayment(cartId) + await orderService.withTransaction(m).createFromCart(cartId) + } + break + } } }) From fca29cc5cc0d6f20d01fada7445d32da85291cd8 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 9 Jul 2021 10:41:19 +0200 Subject: [PATCH 2/2] fix: support for hook completion of swap carts --- packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js b/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js index 72a6a95bef..8803ccb6b5 100644 --- a/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js +++ b/packages/medusa-payment-paypal/src/api/routes/hooks/paypal.js @@ -22,7 +22,8 @@ export default async (req, res) => { } try { - const authId = req.body.resource.id + const body = req.body + const authId = body.resource.id const auth = await paypalService.retrieveAuthorization(authId) const order = await paypalService.retrieveOrderFromAuth(auth) @@ -37,6 +38,7 @@ export default async (req, res) => { const manager = req.scope.resolve("manager") const cartService = req.scope.resolve("cartService") + const swapService = req.scope.resolve("swapService") const orderService = req.scope.resolve("orderService") await manager.transaction(async (m) => {