Merge pull request #308 from medusajs/fix/paypal-complete-order
fix(medusa-payment-paypal): fixes bug where swap carts completed by webhook resulted in regular orders
This commit is contained in:
@@ -22,7 +22,8 @@ export default async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const authId = req.body.resource.id
|
const body = req.body
|
||||||
|
const authId = body.resource.id
|
||||||
const auth = await paypalService.retrieveAuthorization(authId)
|
const auth = await paypalService.retrieveAuthorization(authId)
|
||||||
|
|
||||||
const order = await paypalService.retrieveOrderFromAuth(auth)
|
const order = await paypalService.retrieveOrderFromAuth(auth)
|
||||||
@@ -37,18 +38,44 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
const manager = req.scope.resolve("manager")
|
const manager = req.scope.resolve("manager")
|
||||||
const cartService = req.scope.resolve("cartService")
|
const cartService = req.scope.resolve("cartService")
|
||||||
|
const swapService = req.scope.resolve("swapService")
|
||||||
const orderService = req.scope.resolve("orderService")
|
const orderService = req.scope.resolve("orderService")
|
||||||
|
|
||||||
await manager.transaction(async (m) => {
|
await manager.transaction(async (m) => {
|
||||||
const order = await orderService
|
const cart = await cartService.withTransaction(m).retrieve(cartId)
|
||||||
.withTransaction(m)
|
|
||||||
.retrieveByCartId(cartId)
|
|
||||||
.catch((_) => undefined)
|
|
||||||
|
|
||||||
if (!order) {
|
switch (cart.type) {
|
||||||
await cartService.withTransaction(m).setPaymentSession(cartId, "paypal")
|
case "swap": {
|
||||||
await cartService.withTransaction(m).authorizePayment(cartId)
|
const swap = await swapService
|
||||||
await orderService.withTransaction(m).createFromCart(cartId)
|
.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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user