1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6 import { createOrderSeeder } from "../../fixtures/order"
7
8 jest.setTimeout(60000)
9
10 medusaIntegrationTestRunner({
11 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
12 let order
13 let adminUser
14
15 beforeAll(async () => {
16 const container = getContainer()
17 ;({ user: adminUser } = await createAdminUser(
18 dbConnection,
19 adminHeaders,
20 container
21 ))
22 const result = await createOrderSeeder({ api, container })
23 order = result.order
24
25 await dbUtils.snapshot()
26 })
27
28 describe("POST /admin/payment-collections/:id/mark-as-paid", () => {
29 const createCollection = async () => {
30 return (
31 await api.post(
32 "/admin/payment-collections",
33 { order_id: order.id, amount: 100 },
34 adminHeaders
35 )
36 ).data.payment_collection
37 }
38
39 const getCollectionPayments = async (collectionId) => {
40 const { data } = await api.get(
41 `/admin/orders/${order.id}?fields=id,*payment_collections.payments`,
42 adminHeaders
43 )
44 return data.order.payment_collections.find(
45 (pc) => pc.id === collectionId
46 ).payments
47 }
48
49 it("records the captured payment under the system provider by default", async () => {
50 const collection = await createCollection()
51
52 const { data } = await api.post(
53 `/admin/payment-collections/${collection.id}/mark-as-paid`,
54 { order_id: order.id },
55 adminHeaders
56 )
57
58 expect(data.payment_collection).toEqual(
59 expect.objectContaining({ status: "completed" })
60 )
61
62 const payments = await getCollectionPayments(collection.id)
63 expect(payments).toEqual([
64 expect.objectContaining({
65 provider_id: "pp_system_default",
66 amount: 100,
67 }),
68 ])
69 expect(payments[0].captured_at).toBeTruthy()
70 })
71
72 it("records the captured payment under the provider passed in provider_id", async () => {
73 const collection = await createCollection()
74
75 const { data } = await api.post(
76 `/admin/payment-collections/${collection.id}/mark-as-paid`,
77 { order_id: order.id, provider_id: "pp_system_default_2" },
78 adminHeaders
79 )
80
81 expect(data.payment_collection).toEqual(
82 expect.objectContaining({ status: "completed" })
83 )
84
85 const payments = await getCollectionPayments(collection.id)
86 expect(payments).toEqual([
87 expect.objectContaining({
88 provider_id: "pp_system_default_2",
89 amount: 100,
90 }),
91 ])
92 expect(payments[0].captured_at).toBeTruthy()
93 })
94
95 it("records the acting admin user on the capture", async () => {
96 const collection = await createCollection()
97
98 await api.post(
99 `/admin/payment-collections/${collection.id}/mark-as-paid`,
100 { order_id: order.id },
101 adminHeaders
102 )
103
104 const payments = await getCollectionPayments(collection.id)
105 const { data } = await api.get(
106 `/admin/payments/${payments[0].id}?fields=id,*captures`,
107 adminHeaders
108 )
109
110 expect(data.payment.captures).toEqual([
111 expect.objectContaining({ created_by: adminUser.id }),
112 ])
113 })
114 })
115 },
116 })