diff --git a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts index b531012a5a22b..294d64eda69c3 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts @@ -46,7 +46,7 @@ export const completeCartWorkflow = createWorkflow( const paymentSessions = validateCartPaymentsStep({ cart }) - const payment = authorizePaymentSessionStep({ + authorizePaymentSessionStep({ // We choose the first payment session, as there will only be one active payment session // This might change in the future. id: paymentSessions[0].id, @@ -96,7 +96,7 @@ export const completeCartWorkflow = createWorkflow( }).config({ name: "final-cart" }) ) - const cartToOrder = transform({ cart, payment }, ({ cart, payment }) => { + const cartToOrder = transform({ cart }, ({ cart }) => { const allItems = (cart.items ?? []).map((item) => { return prepareLineItemData({ item, @@ -135,15 +135,6 @@ export const completeCartWorkflow = createWorkflow( .map((adjustment) => adjustment.code) .filter((code) => Boolean) as string[] - const transactions = [ - { - amount: payment.raw_amount ?? payment.amount, - currency_code: payment.currency_code, - reference: "payment", - reference_id: payment.id, - }, - ] - return { region_id: cart.region?.id, customer_id: cart.customer?.id, @@ -158,7 +149,6 @@ export const completeCartWorkflow = createWorkflow( shipping_methods: shippingMethods, metadata: cart.metadata, promo_codes: promoCodes, - transactions, } }) diff --git a/packages/core/core-flows/src/payment/workflows/capture-payment.ts b/packages/core/core-flows/src/payment/workflows/capture-payment.ts index ae6ea0dafef24..5897903371800 100644 --- a/packages/core/core-flows/src/payment/workflows/capture-payment.ts +++ b/packages/core/core-flows/src/payment/workflows/capture-payment.ts @@ -4,8 +4,11 @@ import { WorkflowData, WorkflowResponse, createWorkflow, + transform, + when, } from "@medusajs/workflows-sdk" -import { emitEventStep } from "../../common" +import { emitEventStep, useRemoteQueryStep } from "../../common" +import { addOrderTransactionStep } from "../../order/steps/add-order-transaction" import { capturePaymentStep } from "../steps/capture-payment" export const capturePaymentWorkflowId = "capture-payment-workflow" @@ -23,6 +26,32 @@ export const capturePaymentWorkflow = createWorkflow( ): WorkflowResponse => { const payment = capturePaymentStep(input) + const orderPayment = useRemoteQueryStep({ + entry_point: "order_payment_collection", + fields: ["order.id"], + variables: { payment_collection_id: payment.payment_collection_id }, + list: false, + }) + + when({ orderPayment }, ({ orderPayment }) => { + return !!orderPayment?.order?.id + }).then(() => { + const orderTransactionData = transform( + { input, payment, orderPayment }, + ({ input, payment, orderPayment }) => { + return { + order_id: orderPayment.order.id, + amount: input.amount ?? payment.raw_amount ?? payment.amount, + currency_code: payment.currency_code, + reference_id: payment.id, + reference: "capture", + } + } + ) + + addOrderTransactionStep(orderTransactionData) + }) + emitEventStep({ eventName: PaymentEvents.CAPTURED, data: { id: payment.id }, diff --git a/packages/core/core-flows/src/payment/workflows/refund-payment.ts b/packages/core/core-flows/src/payment/workflows/refund-payment.ts index 829917092a26e..c51a0856b8aa1 100644 --- a/packages/core/core-flows/src/payment/workflows/refund-payment.ts +++ b/packages/core/core-flows/src/payment/workflows/refund-payment.ts @@ -38,9 +38,9 @@ export const refundPaymentWorkflow = createWorkflow( }).then(() => { const orderTransactionData = transform( { input, payment, orderPayment }, - ({ input, payment }) => { + ({ input, payment, orderPayment }) => { return { - order_id: orderPayment.id, + order_id: orderPayment.order.id, amount: MathBN.mult( input.amount ?? payment.raw_amount ?? payment.amount, -1