fix(core-flows): add order transaction on capture (#8591)
This commit is contained in:
@@ -46,7 +46,7 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
|
|
||||||
const paymentSessions = validateCartPaymentsStep({ cart })
|
const paymentSessions = validateCartPaymentsStep({ cart })
|
||||||
|
|
||||||
const payment = authorizePaymentSessionStep({
|
authorizePaymentSessionStep({
|
||||||
// We choose the first payment session, as there will only be one active payment session
|
// We choose the first payment session, as there will only be one active payment session
|
||||||
// This might change in the future.
|
// This might change in the future.
|
||||||
id: paymentSessions[0].id,
|
id: paymentSessions[0].id,
|
||||||
@@ -96,7 +96,7 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
}).config({ name: "final-cart" })
|
}).config({ name: "final-cart" })
|
||||||
)
|
)
|
||||||
|
|
||||||
const cartToOrder = transform({ cart, payment }, ({ cart, payment }) => {
|
const cartToOrder = transform({ cart }, ({ cart }) => {
|
||||||
const allItems = (cart.items ?? []).map((item) => {
|
const allItems = (cart.items ?? []).map((item) => {
|
||||||
return prepareLineItemData({
|
return prepareLineItemData({
|
||||||
item,
|
item,
|
||||||
@@ -135,15 +135,6 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
.map((adjustment) => adjustment.code)
|
.map((adjustment) => adjustment.code)
|
||||||
.filter((code) => Boolean) as string[]
|
.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 {
|
return {
|
||||||
region_id: cart.region?.id,
|
region_id: cart.region?.id,
|
||||||
customer_id: cart.customer?.id,
|
customer_id: cart.customer?.id,
|
||||||
@@ -158,7 +149,6 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
shipping_methods: shippingMethods,
|
shipping_methods: shippingMethods,
|
||||||
metadata: cart.metadata,
|
metadata: cart.metadata,
|
||||||
promo_codes: promoCodes,
|
promo_codes: promoCodes,
|
||||||
transactions,
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ import {
|
|||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
|
transform,
|
||||||
|
when,
|
||||||
} from "@medusajs/workflows-sdk"
|
} 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"
|
import { capturePaymentStep } from "../steps/capture-payment"
|
||||||
|
|
||||||
export const capturePaymentWorkflowId = "capture-payment-workflow"
|
export const capturePaymentWorkflowId = "capture-payment-workflow"
|
||||||
@@ -23,6 +26,32 @@ export const capturePaymentWorkflow = createWorkflow(
|
|||||||
): WorkflowResponse<PaymentDTO> => {
|
): WorkflowResponse<PaymentDTO> => {
|
||||||
const payment = capturePaymentStep(input)
|
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({
|
emitEventStep({
|
||||||
eventName: PaymentEvents.CAPTURED,
|
eventName: PaymentEvents.CAPTURED,
|
||||||
data: { id: payment.id },
|
data: { id: payment.id },
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ export const refundPaymentWorkflow = createWorkflow(
|
|||||||
}).then(() => {
|
}).then(() => {
|
||||||
const orderTransactionData = transform(
|
const orderTransactionData = transform(
|
||||||
{ input, payment, orderPayment },
|
{ input, payment, orderPayment },
|
||||||
({ input, payment }) => {
|
({ input, payment, orderPayment }) => {
|
||||||
return {
|
return {
|
||||||
order_id: orderPayment.id,
|
order_id: orderPayment.order.id,
|
||||||
amount: MathBN.mult(
|
amount: MathBN.mult(
|
||||||
input.amount ?? payment.raw_amount ?? payment.amount,
|
input.amount ?? payment.raw_amount ?? payment.amount,
|
||||||
-1
|
-1
|
||||||
|
|||||||
Reference in New Issue
Block a user