Skip to content

Commit

Permalink
fix(core-flows): create order before payment capture
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Nov 7, 2024
1 parent 1a0882c commit 00eb61e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 50 deletions.
2 changes: 0 additions & 2 deletions packages/core/core-flows/src/payment/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from "./capture-payment"
export * from "./on-payment-processed"
export * from "./process-payment"
export * from "./refund-payment"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WebhookActionResult } from "@medusajs/types"
import { PaymentActions } from "@medusajs/utils"
import { createWorkflow, when } from "@medusajs/workflows-sdk"
import { completeCartWorkflow } from "../../cart/workflows/complete-cart"
import { useQueryStep } from "../../common/steps/use-query"
import { authorizePaymentSessionStep } from "../steps"
import { capturePaymentWorkflow } from "./capture-payment"
Expand All @@ -17,6 +18,31 @@ export const processPaymentWorkflow = createWorkflow(
filters: { payment_session_id: input.data?.session_id },
})

const paymentSessionResult = useQueryStep({
entity: "payment_session",
fields: ["payment_collection_id"],
filters: { id: input.data?.session_id },
})

const cartPaymentCollection = useQueryStep({
entity: "cart_payment_collection",
fields: ["cart_id"],
filters: {
payment_collection_id:
paymentSessionResult.data[0].payment_collection_id,
},
})

when({ cartPaymentCollection }, ({ cartPaymentCollection }) => {
return !!cartPaymentCollection.data.length
}).then(() => {
completeCartWorkflow.runAsStep({
input: {
id: cartPaymentCollection.data[0].cart_id,
},
})
})

when({ input }, ({ input }) => {
return (
input.action === PaymentActions.SUCCESSFUL && !!paymentData.data.length
Expand All @@ -31,8 +57,12 @@ export const processPaymentWorkflow = createWorkflow(
})

when({ input }, ({ input }) => {
// Authorize payment session if no Cart is linked to the payment
// When associated with a Cart, the complete cart workflow will handle the authorization
return (
input.action === PaymentActions.AUTHORIZED && !!input.data?.session_id
!cartPaymentCollection.data.length &&
input.action === PaymentActions.AUTHORIZED &&
!!input.data?.session_id
)
}).then(() => {
authorizePaymentSessionStep({
Expand Down
10 changes: 1 addition & 9 deletions packages/medusa/src/subscribers/payment-webhook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
onPaymentProcessedWorkflow,
processPaymentWorkflow,
} from "@medusajs/core-flows"
import { processPaymentWorkflow } from "@medusajs/core-flows"
import {
IPaymentModuleService,
ProviderWebhookPayload,
Expand Down Expand Up @@ -49,11 +46,6 @@ export default async function paymentWebhookhandler({
await processPaymentWorkflow(container).run({
input: processedEvent,
})

// We process the intended side effects of payment processing separately.
await onPaymentProcessedWorkflow(container).run({
input: processedEvent,
})
}

export const config: SubscriberConfig = {
Expand Down

0 comments on commit 00eb61e

Please sign in to comment.