chore(core-flows): order transactions (#8568)
What: - Add order transaction when Cart is completed and payment is refunded
This commit is contained in:
committed by
GitHub
parent
24985cf89d
commit
96bdf3e2c6
@@ -34,7 +34,9 @@ export const completeCartWorkflowId = "complete-cart"
|
||||
*/
|
||||
export const completeCartWorkflow = createWorkflow(
|
||||
completeCartWorkflowId,
|
||||
(input: WorkflowData<CompleteCartWorkflowInput>): WorkflowResponse<OrderDTO> => {
|
||||
(
|
||||
input: WorkflowData<CompleteCartWorkflowInput>
|
||||
): WorkflowResponse<OrderDTO> => {
|
||||
const cart = useRemoteQueryStep({
|
||||
entry_point: "cart",
|
||||
fields: completeCartFields,
|
||||
@@ -44,7 +46,7 @@ export const completeCartWorkflow = createWorkflow(
|
||||
|
||||
const paymentSessions = validateCartPaymentsStep({ cart })
|
||||
|
||||
authorizePaymentSessionStep({
|
||||
const payment = 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,
|
||||
@@ -94,7 +96,7 @@ export const completeCartWorkflow = createWorkflow(
|
||||
}).config({ name: "final-cart" })
|
||||
)
|
||||
|
||||
const cartToOrder = transform({ input, cart }, ({ cart }) => {
|
||||
const cartToOrder = transform({ cart, payment }, ({ cart, payment }) => {
|
||||
const allItems = (cart.items ?? []).map((item) => {
|
||||
return prepareLineItemData({
|
||||
item,
|
||||
@@ -133,6 +135,15 @@ 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,
|
||||
@@ -147,6 +158,7 @@ export const completeCartWorkflow = createWorkflow(
|
||||
shipping_methods: shippingMethods,
|
||||
metadata: cart.metadata,
|
||||
promo_codes: promoCodes,
|
||||
transactions,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { CreateOrderTransactionDTO } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const addOrderTransactionStepId = "add-order-transaction"
|
||||
/**
|
||||
* This step creates an order transaction.
|
||||
*/
|
||||
export const addOrderTransactionStep = createStep(
|
||||
addOrderTransactionStepId,
|
||||
async (data: CreateOrderTransactionDTO, { container }) => {
|
||||
const service = container.resolve(ModuleRegistrationName.ORDER)
|
||||
|
||||
const created = await service.addTransactions(data)
|
||||
|
||||
return new StepResponse(created, created.id)
|
||||
},
|
||||
async (id, { container }) => {
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
|
||||
const service = container.resolve(ModuleRegistrationName.ORDER)
|
||||
|
||||
await service.deleteTransactions(id)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./add-order-transaction"
|
||||
export * from "./archive-orders"
|
||||
export * from "./cancel-fulfillment"
|
||||
export * from "./cancel-order-change"
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
import { PaymentEvents } from "@medusajs/utils"
|
||||
import { MathBN, PaymentEvents } from "@medusajs/utils"
|
||||
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 { refundPaymentStep } from "../steps/refund-payment"
|
||||
|
||||
export const refundPaymentWorkflowId = "refund-payment-workflow"
|
||||
@@ -23,6 +26,35 @@ export const refundPaymentWorkflow = createWorkflow(
|
||||
) => {
|
||||
const payment = refundPaymentStep(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 }) => {
|
||||
return {
|
||||
order_id: orderPayment.id,
|
||||
amount: MathBN.mult(
|
||||
input.amount ?? payment.raw_amount ?? payment.amount,
|
||||
-1
|
||||
),
|
||||
currency_code: payment.currency_code,
|
||||
reference_id: payment.id,
|
||||
reference: "refund",
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
addOrderTransactionStep(orderTransactionData)
|
||||
})
|
||||
|
||||
emitEventStep({
|
||||
eventName: PaymentEvents.REFUNDED,
|
||||
data: { id: payment.id },
|
||||
|
||||
Reference in New Issue
Block a user