fix(core-flows): use transform as input to account holder step (#12430)

* fix(core-flows): use transform as input to account holder step

* chore: add a simple test

* chore: add comment

* chore: Fix tests

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: olivermrbl <oliver@mrbltech.com>
This commit is contained in:
authored and GitHub committed 2025-05-11 18:45:44 +02:00
1 parent 6aa1ebdee2
commit cc5af67c48
2 files changed
+92 -17

No files matched your search

@@ -6,18 +6,31 @@ import {
generateStoreHeaders, generateStoreHeaders,
} from "../../../../helpers/create-admin-user" } from "../../../../helpers/create-admin-user"
import { getProductFixture } from "../../../../helpers/fixtures" import { getProductFixture } from "../../../../helpers/fixtures"
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
jest.setTimeout(30000) jest.setTimeout(60000)
medusaIntegrationTestRunner({ medusaIntegrationTestRunner({
testSuite: ({ dbConnection, getContainer, api }) => { testSuite: ({ dbConnection, getContainer, api }) => {
let storeHeaders let storeHeaders
let storeHeadersWithCustomer
beforeEach(async () => { beforeEach(async () => {
const container = getContainer() const container = getContainer()
const publishableKey = await generatePublishableKey(container) const publishableKey = await generatePublishableKey(container)
storeHeaders = generateStoreHeaders({ publishableKey }) storeHeaders = generateStoreHeaders({ publishableKey })
await createAdminUser(dbConnection, adminHeaders, container) await createAdminUser(dbConnection, adminHeaders, container)
const result = await createAuthenticatedCustomer(api, storeHeaders, {
first_name: "tony",
last_name: "stark",
email: "tony@stark-industries.com",
})
storeHeadersWithCustomer = {
headers: {
...storeHeaders.headers,
authorization: `Bearer ${result.jwt}`,
},
}
}) })
describe("POST /admin/payment-collections/:id/payment-sessions", () => { describe("POST /admin/payment-collections/:id/payment-sessions", () => {
@@ -25,16 +38,29 @@ medusaIntegrationTestRunner({
let product let product
let cart let cart
let shippingProfile let shippingProfile
let salesChannel
beforeEach(async () => { beforeEach(async () => {
region = ( region = (
await api.post( await api.post(
"/admin/regions", "/admin/regions",
{ name: "United States", currency_code: "usd", countries: ["us"] }, {
name: "United States",
currency_code: "usd",
countries: ["us"],
},
adminHeaders adminHeaders
) )
).data.region ).data.region
salesChannel = (
await api.post(
"/admin/sales-channels",
{ name: "Web" },
adminHeaders
)
).data.sales_channel
shippingProfile = ( shippingProfile = (
await api.post( await api.post(
`/admin/shipping-profiles`, `/admin/shipping-profiles`,
@@ -67,20 +93,21 @@ medusaIntegrationTestRunner({
adminHeaders adminHeaders
) )
).data.product ).data.product
})
it("should create a session when the customer is a guest", async () => {
cart = ( cart = (
await api.post( await api.post(
"/store/carts", "/store/carts",
{ {
region_id: region.id, region_id: region.id,
items: [{ variant_id: product.variants[0].id, quantity: 1 }], items: [{ variant_id: product.variants[0].id, quantity: 1 }],
sales_channel_id: salesChannel.id,
}, },
storeHeaders storeHeaders
) )
).data.cart ).data.cart
})
it("should create a payment session", async () => {
const paymentCollection = ( const paymentCollection = (
await api.post( await api.post(
`/store/payment-collections`, `/store/payment-collections`,
@@ -115,6 +142,48 @@ medusaIntegrationTestRunner({
}), }),
]) ])
}) })
it("should create a session when the customer is authenticated", async () => {
cart = (
await api.post(
"/store/carts",
{
region_id: region.id,
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
sales_channel_id: salesChannel.id,
},
storeHeadersWithCustomer
)
).data.cart
const paymentCollection = (
await api.post(
`/store/payment-collections`,
{ cart_id: cart.id },
storeHeadersWithCustomer
)
).data.payment_collection
const {
data: { payment_collection },
} = await api.post(
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
{ provider_id: "pp_system_default" },
storeHeadersWithCustomer
)
// TODO: This does not create an account holder as the system payment provider does not support it
// Create a custom system payment provider that supports it or add account holder support to the system payment provider
// This test will pass through the account holder creation step though, which is what the test is checking for
expect(payment_collection.payment_sessions).toEqual([
expect.objectContaining({
currency_code: "usd",
provider_id: "pp_system_default",
status: "pending",
amount: 150,
}),
])
})
}) })
}, },
}) })
@@ -3,21 +3,21 @@ import {
CustomerDTO, CustomerDTO,
PaymentSessionDTO, PaymentSessionDTO,
} from "@medusajs/framework/types" } from "@medusajs/framework/types"
import { isPresent, Modules } from "@medusajs/framework/utils"
import { import {
WorkflowData,
WorkflowResponse,
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when, when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { createRemoteLinkStep, useRemoteQueryStep } from "../../common" import { createRemoteLinkStep, useRemoteQueryStep } from "../../common"
import { import {
createPaymentSessionStep,
createPaymentAccountHolderStep, createPaymentAccountHolderStep,
createPaymentSessionStep,
} from "../steps" } from "../steps"
import { deletePaymentSessionsWorkflow } from "./delete-payment-sessions" import { deletePaymentSessionsWorkflow } from "./delete-payment-sessions"
import { isPresent, Modules } from "@medusajs/framework/utils"
/** /**
* The data to create payment sessions. * The data to create payment sessions.
@@ -122,16 +122,22 @@ export const createPaymentSessionsWorkflow = createWorkflow(
) )
}) })
const accountHolderInput = { const accountHolderInput = transform(
provider_id: input.provider_id, { existingAccountHolder, input, paymentCustomer },
context: { (data) => {
// The module is idempotent, so if there already is a linked account holder, the module will simply return it back. return {
account_holder: existingAccountHolder, provider_id: data.input.provider_id,
customer: paymentCustomer, context: {
}, // The module is idempotent, so if there already is a linked account holder, the module will simply return it back.
} account_holder: data.existingAccountHolder,
customer: data.paymentCustomer,
},
}
}
)
const accountHolder = createPaymentAccountHolderStep(accountHolderInput) const accountHolder = createPaymentAccountHolderStep(accountHolderInput)
return { paymentCustomer, accountHolder } return { paymentCustomer, accountHolder }
}) })