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): add order transaction on capture #8591

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -158,7 +149,6 @@ export const completeCartWorkflow = createWorkflow(
shipping_methods: shippingMethods,
metadata: cart.metadata,
promo_codes: promoCodes,
transactions,
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading