From cc5af67c483163095e6b3373f3794bf356ae504a Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Sun, 11 May 2025 18:45:44 +0200 Subject: [PATCH] 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 --- .../admin/payment-sessions.spec.ts | 79 +++++++++++++++++-- .../workflows/create-payment-session.ts | 30 ++++--- 2 files changed, 92 insertions(+), 17 deletions(-) diff --git a/integration-tests/http/__tests__/payment-collection/admin/payment-sessions.spec.ts b/integration-tests/http/__tests__/payment-collection/admin/payment-sessions.spec.ts index 2e2b083dec..c87caadacf 100644 --- a/integration-tests/http/__tests__/payment-collection/admin/payment-sessions.spec.ts +++ b/integration-tests/http/__tests__/payment-collection/admin/payment-sessions.spec.ts @@ -6,18 +6,31 @@ import { generateStoreHeaders, } from "../../../../helpers/create-admin-user" import { getProductFixture } from "../../../../helpers/fixtures" +import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" -jest.setTimeout(30000) +jest.setTimeout(60000) medusaIntegrationTestRunner({ testSuite: ({ dbConnection, getContainer, api }) => { let storeHeaders - + let storeHeadersWithCustomer beforeEach(async () => { const container = getContainer() const publishableKey = await generatePublishableKey(container) storeHeaders = generateStoreHeaders({ publishableKey }) 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", () => { @@ -25,16 +38,29 @@ medusaIntegrationTestRunner({ let product let cart let shippingProfile + let salesChannel beforeEach(async () => { region = ( await api.post( "/admin/regions", - { name: "United States", currency_code: "usd", countries: ["us"] }, + { + name: "United States", + currency_code: "usd", + countries: ["us"], + }, adminHeaders ) ).data.region + salesChannel = ( + await api.post( + "/admin/sales-channels", + { name: "Web" }, + adminHeaders + ) + ).data.sales_channel + shippingProfile = ( await api.post( `/admin/shipping-profiles`, @@ -67,20 +93,21 @@ medusaIntegrationTestRunner({ adminHeaders ) ).data.product + }) + it("should create a session when the customer is a guest", 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, }, storeHeaders ) ).data.cart - }) - it("should create a payment session", async () => { const paymentCollection = ( await api.post( `/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, + }), + ]) + }) }) }, }) diff --git a/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts b/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts index 8f18d7b0ee..4e19fef24c 100644 --- a/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts +++ b/packages/core/core-flows/src/payment-collection/workflows/create-payment-session.ts @@ -3,21 +3,21 @@ import { CustomerDTO, PaymentSessionDTO, } from "@medusajs/framework/types" +import { isPresent, Modules } from "@medusajs/framework/utils" import { - WorkflowData, - WorkflowResponse, createWorkflow, parallelize, transform, when, + WorkflowData, + WorkflowResponse, } from "@medusajs/framework/workflows-sdk" import { createRemoteLinkStep, useRemoteQueryStep } from "../../common" import { - createPaymentSessionStep, createPaymentAccountHolderStep, + createPaymentSessionStep, } from "../steps" import { deletePaymentSessionsWorkflow } from "./delete-payment-sessions" -import { isPresent, Modules } from "@medusajs/framework/utils" /** * The data to create payment sessions. @@ -122,16 +122,22 @@ export const createPaymentSessionsWorkflow = createWorkflow( ) }) - const accountHolderInput = { - provider_id: input.provider_id, - context: { - // The module is idempotent, so if there already is a linked account holder, the module will simply return it back. - account_holder: existingAccountHolder, - customer: paymentCustomer, - }, - } + const accountHolderInput = transform( + { existingAccountHolder, input, paymentCustomer }, + (data) => { + return { + provider_id: data.input.provider_id, + 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) + return { paymentCustomer, accountHolder } })