1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 generatePublishableKey,
6 generateStoreHeaders,
7 } from "../../../../helpers/create-admin-user"
8 import { getProductFixture } from "../../../../helpers/fixtures"
9 import { createOrderSeeder } from "../../fixtures/order"
10 import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
11
12 jest.setTimeout(60000)
13
14 medusaIntegrationTestRunner({
15 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
16 let storeHeaders
17 let storeHeadersWithCustomer
18 beforeAll(async () => {
19 const container = getContainer()
20 const publishableKey = await generatePublishableKey(container)
21 storeHeaders = generateStoreHeaders({ publishableKey })
22 await createAdminUser(dbConnection, adminHeaders, container)
23 const result = await createAuthenticatedCustomer(api, storeHeaders, {
24 first_name: "tony",
25 last_name: "stark",
26 email: "tony@stark-industries.com",
27 })
28
29 storeHeadersWithCustomer = {
30 headers: {
31 ...storeHeaders.headers,
32 authorization: `Bearer ${result.jwt}`,
33 },
34 }
35
36 await dbUtils.snapshot()
37 })
38
39 describe("POST /store/payment-collections/:id/payment-sessions", () => {
40 let region
41 let product
42 let cart
43 let shippingProfile
44 let salesChannel
45
46 beforeEach(async () => {
47 region = (
48 await api.post(
49 "/admin/regions",
50 {
51 payment_providers: ["pp_system_default"],
52 name: "United States",
53 currency_code: "usd",
54 countries: ["us"],
55 },
56 adminHeaders
57 )
58 ).data.region
59
60 salesChannel = (
61 await api.post("/admin/sales-channels", { name: "Web" }, adminHeaders)
62 ).data.sales_channel
63
64 shippingProfile = (
65 await api.post(
66 `/admin/shipping-profiles`,
67 { name: "Test", type: "default" },
68 adminHeaders
69 )
70 ).data.shipping_profile
71
72 product = (
73 await api.post(
74 "/admin/products",
75 getProductFixture({
76 title: "test",
77 status: "published",
78 shipping_profile_id: shippingProfile.id,
79 variants: [
80 {
81 title: "Test variant",
82 manage_inventory: false,
83 prices: [
84 {
85 amount: 150,
86 currency_code: "usd",
87 rules: { region_id: region.id },
88 },
89 ],
90 },
91 ],
92 }),
93 adminHeaders
94 )
95 ).data.product
96 })
97
98 it("should create a session when the customer is a guest", async () => {
99 cart = (
100 await api.post(
101 "/store/carts",
102 {
103 region_id: region.id,
104 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
105 sales_channel_id: salesChannel.id,
106 },
107 storeHeaders
108 )
109 ).data.cart
110
111 const paymentCollection = (
112 await api.post(
113 `/store/payment-collections`,
114 {
115 cart_id: cart.id,
116 },
117 storeHeaders
118 )
119 ).data.payment_collection
120
121 await api.post(
122 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
123 { provider_id: "pp_system_default" },
124 storeHeaders
125 )
126
127 // Adding a second payment session to ensure only one session gets created
128 const {
129 data: { payment_collection },
130 } = await api.post(
131 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
132 { provider_id: "pp_system_default" },
133 storeHeaders
134 )
135
136 expect(payment_collection.payment_sessions).toEqual([
137 expect.objectContaining({
138 currency_code: "usd",
139 provider_id: "pp_system_default",
140 status: "pending",
141 amount: 150,
142 }),
143 ])
144 })
145
146 it("should create a session when the customer is authenticated", async () => {
147 cart = (
148 await api.post(
149 "/store/carts",
150 {
151 region_id: region.id,
152 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
153 sales_channel_id: salesChannel.id,
154 },
155 storeHeadersWithCustomer
156 )
157 ).data.cart
158
159 const paymentCollection = (
160 await api.post(
161 `/store/payment-collections`,
162 { cart_id: cart.id },
163 storeHeadersWithCustomer
164 )
165 ).data.payment_collection
166
167 const {
168 data: { payment_collection },
169 } = await api.post(
170 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
171 { provider_id: "pp_system_default" },
172 storeHeadersWithCustomer
173 )
174
175 // TODO: This does not create an account holder as the system payment provider does not support it
176 // Create a custom system payment provider that supports it or add account holder support to the system payment provider
177 // This test will pass through the account holder creation step though, which is what the test is checking for
178 expect(payment_collection.payment_sessions).toEqual([
179 expect.objectContaining({
180 currency_code: "usd",
181 provider_id: "pp_system_default",
182 status: "pending",
183 amount: 150,
184 }),
185 ])
186 })
187 })
188
189 describe("POST /admin/payment-collections/:id/payment-sessions", () => {
190 let order
191
192 beforeEach(async () => {
193 const container = getContainer()
194 const result = await createOrderSeeder({ api, container })
195 order = result.order
196 })
197
198 it("should create a payment session via the admin endpoint", async () => {
199 const paymentCollection = (
200 await api.post(
201 "/admin/payment-collections",
202 { order_id: order.id, amount: 100 },
203 adminHeaders
204 )
205 ).data.payment_collection
206
207 const {
208 data: { payment_collection },
209 } = await api.post(
210 `/admin/payment-collections/${paymentCollection.id}/payment-sessions`,
211 { provider_id: "pp_system_default" },
212 adminHeaders
213 )
214
215 expect(payment_collection.payment_sessions).toEqual([
216 expect.objectContaining({
217 provider_id: "pp_system_default",
218 status: "pending",
219 amount: 100,
220 }),
221 ])
222 })
223 })
224 },
225 })