chore(core-flows): order transactions (#8568)

What:

- Add order transaction when Cart is completed and payment is refunded
This commit is contained in:
Carlos R. L. Rodrigues
2024-08-12 16:06:58 -03:00
committed by GitHub
parent 24985cf89d
commit 96bdf3e2c6
15 changed files with 140 additions and 32 deletions

View File

@@ -0,0 +1,9 @@
---
"medusa-test-utils": patch
"create-medusa-app": patch
"@medusajs/core-flows": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---
Order transactions

View File

@@ -148,7 +148,7 @@ medusaIntegrationTestRunner({
tax_total: 0.89,
discount_total: 1.1,
discount_tax_total: 0.11,
original_total: 70.9,
original_total: 60.9,
original_tax_total: 1,
item_total: 50,
item_subtotal: 50,

View File

@@ -33,10 +33,12 @@
"winston": "^3.9.0"
},
"devDependencies": {
"@medusajs/types": "^1.11.16",
"@types/chalk": "^2.2.0",
"@types/commander": "^2.12.2",
"@types/configstore": "^6.0.0",
"@types/inquirer": "^9.0.3",
"@types/node": "^20.12.11",
"@types/node-emoji": "^1.8.2",
"@types/pg": "^8.6.6",
"@types/uuid": "^9.0.1",

View File

@@ -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,
}
})

View File

@@ -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)
}
)

View File

@@ -1,3 +1,4 @@
export * from "./add-order-transaction"
export * from "./archive-orders"
export * from "./cancel-fulfillment"
export * from "./cancel-order-change"

View File

@@ -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 },

View File

@@ -35,6 +35,7 @@
"peerDependencies": {
"@medusajs/medusa": ">1.19",
"@medusajs/modules-sdk": "^1.12.10",
"@mikro-orm/core": "5.9.7",
"axios": "^0.28.0",
"express": "^4.18.3",
"get-port": "^5.1.0",

View File

@@ -1151,9 +1151,19 @@ export interface CreateOrderTransactionDTO {
order_id: string
/**
* The description of the transaction.
* The associated return's ID.
*/
description?: string
return_id?: string
/**
* The associated claim's ID.
*/
claim_id?: string
/**
* The associated exchange's ID.
*/
exchange_id?: string
/**
* The data model this transaction references.
@@ -1167,17 +1177,6 @@ export interface CreateOrderTransactionDTO {
*/
reference_id?: string
/**
* The internal note of the transaction.
*/
internal_note?: string | null
/**
* The user or customer that created this
* transaction.
*/
created_by?: string
/**
* The amount of the transaction.
*/
@@ -1187,11 +1186,6 @@ export interface CreateOrderTransactionDTO {
* The currency code of the transaction.
*/
currency_code: string
/**
* Holds custom data in key-value pairs.
*/
metadata?: Record<string, unknown> | null
}
/**

View File

@@ -272,11 +272,21 @@ export interface PaymentDTO {
*/
amount: BigNumberValue
/**
* The raw amount of the payment.
*/
raw_amount?: BigNumberValue
/**
* The authorized amount of the payment.
*/
authorized_amount?: BigNumberValue
/**
* The raw authorized amount of the payment.
*/
raw_authorized_amount?: BigNumberValue
/**
* The ISO 3 character currency code of the payment.
*/
@@ -337,11 +347,21 @@ export interface PaymentDTO {
*/
captured_amount?: BigNumberValue
/**
* The sum of the associated captures' raw amounts.
*/
raw_captured_amount?: BigNumberValue
/**
* The sum of the associated refunds' amounts.
*/
refunded_amount?: BigNumberValue
/**
* The sum of the associated refunds' raw amounts.
*/
raw_refunded_amount?: BigNumberValue
/**
* The associated captures.
*
@@ -356,6 +376,11 @@ export interface PaymentDTO {
*/
refunds?: RefundDTO[]
/**
* The ID of the associated payment collection.
*/
payment_collection_id: string
/**
* The associated payment collection.
*
@@ -553,6 +578,11 @@ export interface PaymentSessionDTO {
*/
authorized_at?: Date
/**
* The ID of the associated payment collection.
*/
payment_collection_id: string
/**
* The payment collection the session is associated with.
*

View File

@@ -329,7 +329,7 @@ describe("Total calculation", function () {
tax_total: 9.512770562770562,
discount_total: 17.5,
discount_tax_total: 1.65,
original_total: 124.5,
original_total: 109.97619047619048,
original_tax_total: 11.067099567099566,
item_total: 97.37,
item_subtotal: 100.9090909090909,
@@ -618,7 +618,7 @@ describe("Total calculation", function () {
tax_total: 10.08,
discount_total: 24.2,
discount_tax_total: 2.42,
original_total: 140.8,
original_total: 115.8,
original_tax_total: 12.5,
item_total: 85.8,
item_subtotal: 100,

View File

@@ -191,7 +191,7 @@ export function decorateCartTotals(
// TODO: Gift Card calculations
const originalTempTotal = MathBN.add(
subtotal,
itemsOriginalSubtotal,
shippingOriginalTotal,
originalTaxTotal
)

View File

@@ -2742,7 +2742,7 @@ moduleIntegrationTestRunner<ICartModuleService>({
tax_total: 0,
discount_total: 300,
discount_tax_total: 0,
original_total: 220,
original_total: 210,
original_tax_total: 0,
item_total: 200,
item_subtotal: 500,
@@ -2777,7 +2777,7 @@ moduleIntegrationTestRunner<ICartModuleService>({
precision: 20,
},
raw_original_total: {
value: "220",
value: "210",
precision: 20,
},
raw_original_tax_total: {

View File

@@ -50,9 +50,6 @@ function updateReturnItems(returnEntry, items) {
const data = items.find((i) => i.details.reference_id === item.item_id)
if (!data) return
console.log("ITEM: ", item)
console.log("DATA: ", data)
const receivedQuantity = MathBN.add(
item.received_quantity || 0,
data.details.quantity

View File

@@ -15468,11 +15468,13 @@ __metadata:
version: 0.0.0-use.local
resolution: "create-medusa-app@workspace:packages/cli/create-medusa-app"
dependencies:
"@medusajs/types": ^1.11.16
"@medusajs/utils": ^1.11.9
"@types/chalk": ^2.2.0
"@types/commander": ^2.12.2
"@types/configstore": ^6.0.0
"@types/inquirer": ^9.0.3
"@types/node": ^20.12.11
"@types/node-emoji": ^1.8.2
"@types/pg": ^8.6.6
"@types/uuid": ^9.0.1
@@ -22754,6 +22756,7 @@ __metadata:
peerDependencies:
"@medusajs/medusa": ">1.19"
"@medusajs/modules-sdk": ^1.12.10
"@mikro-orm/core": 5.9.7
axios: ^0.28.0
express: ^4.18.3
get-port: ^5.1.0