fix(core-flows): add order transaction on capture (#8591)
This commit is contained in:
committed by
GitHub
parent
19d30df624
commit
3d3fd9e500
@@ -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,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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<PaymentDTO> => {
|
||||
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 },
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user