Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core-flows): capture before order created #9980

Merged
merged 15 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-lamps-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/core-flows": patch
"@medusajs/medusa": patch
---

Create Order before payment capture
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 @@ -15,6 +16,41 @@ export const processPaymentWorkflow = createWorkflow(
entity: "payment",
fields: ["id"],
filters: { payment_session_id: input.data?.session_id },
}).config({
name: "payment-query",
})

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

const cartPaymentCollection = useQueryStep({
entity: "cart_payment_collection",
fields: ["cart_id"],
filters: {
payment_collection_id:
paymentSessionResult.data[0].payment_collection_id,
},
}).config({
name: "cart-payment-query",
})

when({ cartPaymentCollection }, ({ cartPaymentCollection }) => {
return !!cartPaymentCollection.data.length
}).then(() => {
completeCartWorkflow
.runAsStep({
input: {
id: cartPaymentCollection.data[0].cart_id,
},
})
.config({
continueOnPermanentFailure: true, // Continue payment processing even if cart completion fails
})
})

when({ input }, ({ input }) => {
Expand All @@ -31,8 +67,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
12 changes: 2 additions & 10 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 @@ -29,7 +26,7 @@ export default async function paymentWebhookhandler({
const input = event.data

if (
(input.payload.rawData as unknown as SerializedBuffer).type === "Buffer"
(input.payload?.rawData as unknown as SerializedBuffer)?.type === "Buffer"
) {
input.payload.rawData = Buffer.from(
(input.payload.rawData as unknown as SerializedBuffer).data
Expand All @@ -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
Loading