|
1
|
import { addGiftCardToCartWorkflow } from "@medusajs/loyalty-plugin/workflows" |
|
2
|
import { MedusaError, Modules, ProductStatus } from "@medusajs/framework/utils" |
|
3
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
generatePublishableKey, |
|
8
|
generateStoreHeaders, |
|
9
|
} from "../../../../helpers/create-admin-user" |
|
10
|
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" |
|
11
|
|
|
12
|
jest.setTimeout(60 * 1000) |
|
13
|
|
|
14
|
const giftCardPayload = { |
|
15
|
currency_code: "usd", |
|
16
|
value: 400, |
|
17
|
code: "TEST1", |
|
18
|
line_item_id: "lin_123", |
|
19
|
} |
|
20
|
|
|
21
|
medusaIntegrationTestRunner({ |
|
22
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
23
|
let customer |
|
24
|
let giftCard |
|
25
|
let product |
|
26
|
let storeHeaders |
|
27
|
let cart |
|
28
|
let cheapVariant, veryCheapVariant, expensiveVariant |
|
29
|
let region, salesChannel |
|
30
|
|
|
31
|
beforeAll(async () => { |
|
32
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
33
|
const publishableKey = await generatePublishableKey(getContainer()) |
|
34
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
35
|
|
|
36
|
product = ( |
|
37
|
await api.post( |
|
38
|
`/admin/products`, |
|
39
|
{ |
|
40
|
title: "test product 1", |
|
41
|
status: ProductStatus.PUBLISHED, |
|
42
|
options: [{ title: "size", values: ["large", "small"] }], |
|
43
|
variants: [ |
|
44
|
{ |
|
45
|
title: "expensive", |
|
46
|
manage_inventory: false, |
|
47
|
options: { |
|
48
|
size: "large", |
|
49
|
}, |
|
50
|
prices: [{ amount: 800, currency_code: "usd" }], |
|
51
|
}, |
|
52
|
{ |
|
53
|
title: "cheap", |
|
54
|
manage_inventory: false, |
|
55
|
options: { |
|
56
|
size: "large", |
|
57
|
}, |
|
58
|
prices: [{ amount: 400, currency_code: "usd" }], |
|
59
|
}, |
|
60
|
{ |
|
61
|
title: "very cheap", |
|
62
|
manage_inventory: false, |
|
63
|
options: { |
|
64
|
size: "large", |
|
65
|
}, |
|
66
|
prices: [{ amount: 200, currency_code: "usd" }], |
|
67
|
}, |
|
68
|
], |
|
69
|
}, |
|
70
|
adminHeaders |
|
71
|
) |
|
72
|
).data.product |
|
73
|
|
|
74
|
cheapVariant = product.variants.find((v) => v.title === "cheap") |
|
75
|
expensiveVariant = product.variants.find((v) => v.title === "expensive") |
|
76
|
veryCheapVariant = product.variants.find((v) => v.title === "very cheap") |
|
77
|
|
|
78
|
giftCard = ( |
|
79
|
await api.post( |
|
80
|
`/admin/gift-cards`, |
|
81
|
{ ...giftCardPayload }, |
|
82
|
adminHeaders |
|
83
|
) |
|
84
|
).data.gift_card |
|
85
|
|
|
86
|
salesChannel = ( |
|
87
|
await api.post( |
|
88
|
`/admin/sales-channels`, |
|
89
|
{ name: "test-sales-channel" }, |
|
90
|
adminHeaders |
|
91
|
) |
|
92
|
).data.sales_channel |
|
93
|
|
|
94
|
region = ( |
|
95
|
await api.post( |
|
96
|
"/admin/regions", |
|
97
|
{ |
|
98
|
payment_providers: ["pp_system_default"], |
|
99
|
name: "test-region", |
|
100
|
currency_code: "usd", |
|
101
|
}, |
|
102
|
adminHeaders |
|
103
|
) |
|
104
|
).data.region |
|
105
|
|
|
106
|
const { |
|
107
|
data: { cart: cart2 }, |
|
108
|
} = await api.post( |
|
109
|
`/store/carts`, |
|
110
|
{ |
|
111
|
region_id: region.id, |
|
112
|
sales_channel_id: salesChannel.id, |
|
113
|
items: [{ variant_id: expensiveVariant.id, quantity: 1 }], |
|
114
|
}, |
|
115
|
storeHeaders |
|
116
|
) |
|
117
|
|
|
118
|
cart = cart2 |
|
119
|
|
|
120
|
await dbUtils.snapshot() |
|
121
|
}) |
|
122
|
|
|
123
|
describe("POST /store/carts/:id/gift-cards", () => { |
|
124
|
it("should successfully add a gift card to a cart", async () => { |
|
125
|
const { |
|
126
|
data: { cart: cartWithGiftCard }, |
|
127
|
} = await api.post( |
|
128
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
129
|
{ code: giftCard.code }, |
|
130
|
storeHeaders |
|
131
|
) |
|
132
|
|
|
133
|
expect(cartWithGiftCard).toEqual( |
|
134
|
expect.objectContaining({ |
|
135
|
total: 400, |
|
136
|
original_total: 800, |
|
137
|
credit_line_total: 400, |
|
138
|
gift_cards: expect.arrayContaining([ |
|
139
|
expect.objectContaining({ id: giftCard.id }), |
|
140
|
]), |
|
141
|
credit_lines: expect.arrayContaining([ |
|
142
|
expect.objectContaining({ |
|
143
|
amount: 400, |
|
144
|
reference: "gift-card", |
|
145
|
reference_id: giftCard.id, |
|
146
|
}), |
|
147
|
]), |
|
148
|
}) |
|
149
|
) |
|
150
|
|
|
151
|
const giftCard2 = ( |
|
152
|
await api.post( |
|
153
|
`/admin/gift-cards`, |
|
154
|
{ ...giftCardPayload, code: "TEST-2" }, |
|
155
|
adminHeaders |
|
156
|
) |
|
157
|
).data.gift_card |
|
158
|
|
|
159
|
const { |
|
160
|
data: { cart: cartWithGiftCard2 }, |
|
161
|
} = await api.post( |
|
162
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
163
|
{ code: giftCard2.code }, |
|
164
|
storeHeaders |
|
165
|
) |
|
166
|
|
|
167
|
expect(cartWithGiftCard2).toEqual( |
|
168
|
expect.objectContaining({ |
|
169
|
total: 0, |
|
170
|
original_total: 800, |
|
171
|
credit_line_total: 800, |
|
172
|
gift_cards: expect.arrayContaining([ |
|
173
|
expect.objectContaining({ id: giftCard.id }), |
|
174
|
expect.objectContaining({ id: giftCard2.id }), |
|
175
|
]), |
|
176
|
credit_lines: expect.arrayContaining([ |
|
177
|
expect.objectContaining({ |
|
178
|
amount: 400, |
|
179
|
reference: "gift-card", |
|
180
|
reference_id: giftCard.id, |
|
181
|
}), |
|
182
|
expect.objectContaining({ |
|
183
|
amount: 400, |
|
184
|
reference: "gift-card", |
|
185
|
reference_id: giftCard2.id, |
|
186
|
}), |
|
187
|
]), |
|
188
|
}) |
|
189
|
) |
|
190
|
|
|
191
|
// Should not add gift card when total is 0 |
|
192
|
const giftCard3 = ( |
|
193
|
await api.post( |
|
194
|
`/admin/gift-cards`, |
|
195
|
{ ...giftCardPayload, code: "TEST-3" }, |
|
196
|
adminHeaders |
|
197
|
) |
|
198
|
).data.gift_card |
|
199
|
|
|
200
|
const { |
|
201
|
data: { cart: cartWithGiftCard3 }, |
|
202
|
} = await api.post( |
|
203
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
204
|
{ code: giftCard3.code }, |
|
205
|
storeHeaders |
|
206
|
) |
|
207
|
|
|
208
|
expect(cartWithGiftCard3.credit_lines).toHaveLength(2) |
|
209
|
expect(cartWithGiftCard3.gift_cards).toHaveLength(2) |
|
210
|
expect(cartWithGiftCard3).toEqual( |
|
211
|
expect.objectContaining({ |
|
212
|
total: 0, |
|
213
|
original_total: 800, |
|
214
|
credit_line_total: 800, |
|
215
|
gift_cards: expect.arrayContaining([ |
|
216
|
expect.objectContaining({ id: giftCard.id }), |
|
217
|
expect.objectContaining({ id: giftCard2.id }), |
|
218
|
]), |
|
219
|
credit_lines: expect.arrayContaining([ |
|
220
|
expect.objectContaining({ |
|
221
|
amount: 400, |
|
222
|
reference: "gift-card", |
|
223
|
reference_id: giftCard.id, |
|
224
|
}), |
|
225
|
expect.objectContaining({ |
|
226
|
amount: 400, |
|
227
|
reference: "gift-card", |
|
228
|
reference_id: giftCard2.id, |
|
229
|
}), |
|
230
|
]), |
|
231
|
}) |
|
232
|
) |
|
233
|
}) |
|
234
|
|
|
235
|
it("should throw error when adding a gift card that does not exist", async () => { |
|
236
|
const { response } = await api |
|
237
|
.post( |
|
238
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
239
|
{ code: "does-not-exist" }, |
|
240
|
storeHeaders |
|
241
|
) |
|
242
|
.catch((err) => err) |
|
243
|
|
|
244
|
expect(response.data.message).toEqual( |
|
245
|
"Gift card (does-not-exist) not found" |
|
246
|
) |
|
247
|
}) |
|
248
|
|
|
249
|
describe("validate hook", () => { |
|
250
|
let rejectGiftCards = false |
|
251
|
const hookCallback = jest.fn() |
|
252
|
|
|
253
|
// hook handlers cannot be deregistered, so the handler is registered |
|
254
|
// once and gated on a flag each test resets |
|
255
|
addGiftCardToCartWorkflow.hooks.validate((data) => { |
|
256
|
hookCallback(data) |
|
257
|
|
|
258
|
if (rejectGiftCards) { |
|
259
|
throw new MedusaError( |
|
260
|
MedusaError.Types.NOT_ALLOWED, |
|
261
|
"Gift cards are not accepted on this cart" |
|
262
|
) |
|
263
|
} |
|
264
|
}) |
|
265
|
|
|
266
|
afterEach(() => { |
|
267
|
rejectGiftCards = false |
|
268
|
hookCallback.mockClear() |
|
269
|
}) |
|
270
|
|
|
271
|
it("should run the hook with the cart and gift cards", async () => { |
|
272
|
await api.post( |
|
273
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
274
|
{ code: giftCard.code }, |
|
275
|
storeHeaders |
|
276
|
) |
|
277
|
|
|
278
|
expect(hookCallback).toHaveBeenCalledWith( |
|
279
|
expect.objectContaining({ |
|
280
|
input: { code: giftCard.code, cart_id: cart.id }, |
|
281
|
cart: expect.objectContaining({ id: cart.id }), |
|
282
|
giftCards: [expect.objectContaining({ id: giftCard.id })], |
|
283
|
}) |
|
284
|
) |
|
285
|
}) |
|
286
|
|
|
287
|
it("should reject the gift card when the hook throws", async () => { |
|
288
|
rejectGiftCards = true |
|
289
|
|
|
290
|
const { response } = await api |
|
291
|
.post( |
|
292
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
293
|
{ code: giftCard.code }, |
|
294
|
storeHeaders |
|
295
|
) |
|
296
|
.catch((err) => err) |
|
297
|
|
|
298
|
expect(response.data.message).toEqual( |
|
299
|
"Gift cards are not accepted on this cart" |
|
300
|
) |
|
301
|
|
|
302
|
const { |
|
303
|
data: { cart: cartAfterRejection }, |
|
304
|
} = await api.get( |
|
305
|
`/store/carts/${cart.id}?fields=+credit_line_total,*gift_cards`, |
|
306
|
storeHeaders |
|
307
|
) |
|
308
|
|
|
309
|
expect(cartAfterRejection.credit_lines).toHaveLength(0) |
|
310
|
expect(cartAfterRejection.gift_cards).toHaveLength(0) |
|
311
|
}) |
|
312
|
}) |
|
313
|
}) |
|
314
|
|
|
315
|
describe("DELETE /store/carts/:id/gift-cards", () => { |
|
316
|
let giftCard2 |
|
317
|
|
|
318
|
beforeEach(async () => { |
|
319
|
await api.post( |
|
320
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
321
|
{ code: giftCard.code }, |
|
322
|
storeHeaders |
|
323
|
) |
|
324
|
|
|
325
|
giftCard2 = ( |
|
326
|
await api.post( |
|
327
|
`/admin/gift-cards`, |
|
328
|
{ ...giftCardPayload, code: "TEST-2" }, |
|
329
|
adminHeaders |
|
330
|
) |
|
331
|
).data.gift_card |
|
332
|
|
|
333
|
await api.post( |
|
334
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
335
|
{ code: giftCard2.code }, |
|
336
|
storeHeaders |
|
337
|
) |
|
338
|
}) |
|
339
|
|
|
340
|
it("should successfully remove a gift card from a cart", async () => { |
|
341
|
const { |
|
342
|
data: { cart: cartWithGiftCard }, |
|
343
|
} = await api.delete( |
|
344
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
345
|
{ |
|
346
|
data: { |
|
347
|
code: giftCard.code, |
|
348
|
}, |
|
349
|
...storeHeaders, |
|
350
|
} |
|
351
|
) |
|
352
|
|
|
353
|
expect(cartWithGiftCard.gift_cards).toHaveLength(1) |
|
354
|
expect(cartWithGiftCard.credit_lines).toHaveLength(1) |
|
355
|
expect(cartWithGiftCard).toEqual( |
|
356
|
expect.objectContaining({ |
|
357
|
total: 400, |
|
358
|
original_total: 800, |
|
359
|
credit_line_total: 400, |
|
360
|
gift_cards: expect.arrayContaining([ |
|
361
|
expect.objectContaining({ id: giftCard2.id }), |
|
362
|
]), |
|
363
|
credit_lines: expect.arrayContaining([ |
|
364
|
expect.objectContaining({ |
|
365
|
amount: 400, |
|
366
|
reference: "gift-card", |
|
367
|
reference_id: giftCard2.id, |
|
368
|
}), |
|
369
|
]), |
|
370
|
}) |
|
371
|
) |
|
372
|
|
|
373
|
const { |
|
374
|
data: { cart: cartWithGiftCard2 }, |
|
375
|
} = await api.delete( |
|
376
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
377
|
{ |
|
378
|
data: { |
|
379
|
code: giftCard2.code, |
|
380
|
}, |
|
381
|
...storeHeaders, |
|
382
|
} |
|
383
|
) |
|
384
|
|
|
385
|
expect(cartWithGiftCard2).toEqual( |
|
386
|
expect.objectContaining({ |
|
387
|
total: 800, |
|
388
|
original_total: 800, |
|
389
|
credit_line_total: 0, |
|
390
|
gift_cards: [], |
|
391
|
credit_lines: [], |
|
392
|
}) |
|
393
|
) |
|
394
|
}) |
|
395
|
|
|
396
|
it("should throw error when adding a gift card that does not exist", async () => { |
|
397
|
const { response } = await api |
|
398
|
.delete( |
|
399
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
400
|
{ |
|
401
|
data: { |
|
402
|
code: "does-not-exist", |
|
403
|
}, |
|
404
|
...storeHeaders, |
|
405
|
} |
|
406
|
) |
|
407
|
.catch((err) => err) |
|
408
|
|
|
409
|
expect(response.data.message).toEqual( |
|
410
|
"Gift card (does-not-exist) not found in cart" |
|
411
|
) |
|
412
|
}) |
|
413
|
}) |
|
414
|
|
|
415
|
describe("POST /store/carts/:id/line-items/:id", () => { |
|
416
|
let largeGiftCard |
|
417
|
|
|
418
|
beforeEach(async () => { |
|
419
|
await api.post( |
|
420
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
421
|
{ code: giftCard.code }, |
|
422
|
storeHeaders |
|
423
|
) |
|
424
|
|
|
425
|
await api.post( |
|
426
|
`/store/payment-collections`, |
|
427
|
{ cart_id: cart.id }, |
|
428
|
storeHeaders |
|
429
|
) |
|
430
|
|
|
431
|
largeGiftCard = ( |
|
432
|
await api.post( |
|
433
|
`/admin/gift-cards`, |
|
434
|
{ |
|
435
|
...giftCardPayload, |
|
436
|
code: "LARGE", |
|
437
|
value: 1000, |
|
438
|
}, |
|
439
|
adminHeaders |
|
440
|
) |
|
441
|
).data.gift_card |
|
442
|
}) |
|
443
|
|
|
444
|
it("should refresh gift cards", async () => { |
|
445
|
const { |
|
446
|
data: { cart: cartWithGiftCard }, |
|
447
|
} = await api.post( |
|
448
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
449
|
{ code: largeGiftCard.code }, |
|
450
|
storeHeaders |
|
451
|
) |
|
452
|
|
|
453
|
expect(cartWithGiftCard.credit_lines).toHaveLength(2) |
|
454
|
expect(cartWithGiftCard.gift_cards).toHaveLength(2) |
|
455
|
expect(cartWithGiftCard).toEqual( |
|
456
|
expect.objectContaining({ |
|
457
|
total: 0, |
|
458
|
original_total: 800, |
|
459
|
credit_line_total: 800, |
|
460
|
gift_cards: expect.arrayContaining([ |
|
461
|
expect.objectContaining({ id: giftCard.id }), |
|
462
|
expect.objectContaining({ id: largeGiftCard.id }), |
|
463
|
]), |
|
464
|
payment_collection: expect.objectContaining({ |
|
465
|
amount: 0, |
|
466
|
}), |
|
467
|
credit_lines: expect.arrayContaining([ |
|
468
|
expect.objectContaining({ |
|
469
|
amount: 400, |
|
470
|
reference: "gift-card", |
|
471
|
reference_id: giftCard.id, |
|
472
|
}), |
|
473
|
expect.objectContaining({ |
|
474
|
amount: 400, |
|
475
|
reference: "gift-card", |
|
476
|
reference_id: largeGiftCard.id, |
|
477
|
}), |
|
478
|
]), |
|
479
|
}) |
|
480
|
) |
|
481
|
|
|
482
|
const { |
|
483
|
data: { cart: updatedCart }, |
|
484
|
} = await api.post( |
|
485
|
`/store/carts/${cart.id}/line-items?fields=+credit_line_total,+gift_cards.id,+gift_cards.code,+payment_collection.id`, |
|
486
|
{ quantity: 1, variant_id: cheapVariant.id }, |
|
487
|
storeHeaders |
|
488
|
) |
|
489
|
|
|
490
|
expect(updatedCart.credit_lines).toHaveLength(2) |
|
491
|
expect(updatedCart.gift_cards).toHaveLength(2) |
|
492
|
expect(updatedCart).toEqual( |
|
493
|
expect.objectContaining({ |
|
494
|
total: 0, |
|
495
|
original_total: 1200, |
|
496
|
credit_line_total: 1200, |
|
497
|
gift_cards: expect.arrayContaining([ |
|
498
|
expect.objectContaining({ id: giftCard.id }), |
|
499
|
expect.objectContaining({ id: largeGiftCard.id }), |
|
500
|
]), |
|
501
|
credit_lines: expect.arrayContaining([ |
|
502
|
expect.objectContaining({ |
|
503
|
amount: 400, |
|
504
|
reference: "gift-card", |
|
505
|
reference_id: giftCard.id, |
|
506
|
}), |
|
507
|
expect.objectContaining({ |
|
508
|
amount: 800, |
|
509
|
reference: "gift-card", |
|
510
|
reference_id: largeGiftCard.id, |
|
511
|
}), |
|
512
|
]), |
|
513
|
payment_collection: expect.objectContaining({ |
|
514
|
amount: 0, |
|
515
|
}), |
|
516
|
}) |
|
517
|
) |
|
518
|
}) |
|
519
|
}) |
|
520
|
|
|
521
|
describe("POST /store/carts/:id/complete", () => { |
|
522
|
let largeGiftCard |
|
523
|
|
|
524
|
beforeEach(async () => { |
|
525
|
await api.post( |
|
526
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
527
|
{ code: giftCard.code }, |
|
528
|
storeHeaders |
|
529
|
) |
|
530
|
|
|
531
|
await api.post( |
|
532
|
`/store/payment-collections`, |
|
533
|
{ cart_id: cart.id }, |
|
534
|
storeHeaders |
|
535
|
) |
|
536
|
|
|
537
|
largeGiftCard = ( |
|
538
|
await api.post( |
|
539
|
`/admin/gift-cards`, |
|
540
|
{ |
|
541
|
...giftCardPayload, |
|
542
|
code: "LARGE", |
|
543
|
value: 1000, |
|
544
|
}, |
|
545
|
adminHeaders |
|
546
|
) |
|
547
|
).data.gift_card |
|
548
|
}) |
|
549
|
|
|
550
|
it("should debit (anonymous) store credit accounts upon cart completion", async () => { |
|
551
|
const { |
|
552
|
data: { cart: updatedCart }, |
|
553
|
} = await api.post( |
|
554
|
`/store/carts/${cart.id}/line-items?fields=+credit_line_total,+gift_cards.id,+gift_cards.code,+payment_collection.id`, |
|
555
|
{ quantity: 1, variant_id: cheapVariant.id }, |
|
556
|
storeHeaders |
|
557
|
) |
|
558
|
|
|
559
|
let giftCardWithStoreCreditAccount = ( |
|
560
|
await api.get( |
|
561
|
`/store/gift-cards/${giftCard.code}?fields=*store_credit_account`, |
|
562
|
storeHeaders |
|
563
|
) |
|
564
|
).data.gift_card |
|
565
|
|
|
566
|
expect(giftCardWithStoreCreditAccount).toEqual( |
|
567
|
expect.objectContaining({ |
|
568
|
status: "redeemed", |
|
569
|
code: "TEST1", |
|
570
|
value: 400, |
|
571
|
store_credit_account: expect.objectContaining({ |
|
572
|
balance: 400, |
|
573
|
credits: 400, |
|
574
|
debits: 0, |
|
575
|
}), |
|
576
|
}) |
|
577
|
) |
|
578
|
|
|
579
|
expect(updatedCart.credit_lines).toHaveLength(1) |
|
580
|
expect(updatedCart.gift_cards).toHaveLength(1) |
|
581
|
expect(updatedCart).toEqual( |
|
582
|
expect.objectContaining({ |
|
583
|
total: 800, |
|
584
|
original_total: 1200, |
|
585
|
credit_line_total: 400, |
|
586
|
payment_collection: expect.objectContaining({ |
|
587
|
amount: 800, |
|
588
|
}), |
|
589
|
}) |
|
590
|
) |
|
591
|
|
|
592
|
await api.post( |
|
593
|
`/store/payment-collections/${updatedCart.payment_collection.id}/payment-sessions`, |
|
594
|
{ provider_id: "pp_system_default" }, |
|
595
|
storeHeaders |
|
596
|
) |
|
597
|
|
|
598
|
const { |
|
599
|
data: { order }, |
|
600
|
} = await api.post( |
|
601
|
`/store/carts/${updatedCart.id}/complete`, |
|
602
|
{}, |
|
603
|
storeHeaders |
|
604
|
) |
|
605
|
|
|
606
|
expect(order).toEqual( |
|
607
|
expect.objectContaining({ |
|
608
|
credit_lines: expect.arrayContaining([ |
|
609
|
expect.objectContaining({ |
|
610
|
amount: 400, |
|
611
|
reference: "gift-card", |
|
612
|
reference_id: giftCard.id, |
|
613
|
}), |
|
614
|
]), |
|
615
|
}) |
|
616
|
) |
|
617
|
|
|
618
|
giftCardWithStoreCreditAccount = ( |
|
619
|
await api.get( |
|
620
|
`/store/gift-cards/${giftCard.code}?fields=*store_credit_account`, |
|
621
|
storeHeaders |
|
622
|
) |
|
623
|
).data.gift_card |
|
624
|
|
|
625
|
expect(giftCardWithStoreCreditAccount).toEqual( |
|
626
|
expect.objectContaining({ |
|
627
|
status: "redeemed", |
|
628
|
code: "TEST1", |
|
629
|
value: 400, |
|
630
|
store_credit_account: expect.objectContaining({ |
|
631
|
balance: 0, |
|
632
|
credits: 400, |
|
633
|
debits: 400, |
|
634
|
}), |
|
635
|
}) |
|
636
|
) |
|
637
|
}) |
|
638
|
|
|
639
|
it("should use multiple gift cards on an order", async () => { |
|
640
|
const gc200 = ( |
|
641
|
await api.post( |
|
642
|
`/admin/gift-cards`, |
|
643
|
{ |
|
644
|
...giftCardPayload, |
|
645
|
line_item_id: "lin_2", |
|
646
|
code: "GC200", |
|
647
|
value: 200, |
|
648
|
}, |
|
649
|
adminHeaders |
|
650
|
) |
|
651
|
).data.gift_card |
|
652
|
|
|
653
|
const gc300 = ( |
|
654
|
await api.post( |
|
655
|
`/admin/gift-cards`, |
|
656
|
{ |
|
657
|
...giftCardPayload, |
|
658
|
line_item_id: "lin_1", |
|
659
|
code: "GC300", |
|
660
|
value: 300, |
|
661
|
}, |
|
662
|
adminHeaders |
|
663
|
) |
|
664
|
).data.gift_card |
|
665
|
|
|
666
|
const gc400 = ( |
|
667
|
await api.post( |
|
668
|
`/admin/gift-cards`, |
|
669
|
{ |
|
670
|
...giftCardPayload, |
|
671
|
line_item_id: "lin_3", |
|
672
|
code: "GC400", |
|
673
|
value: 400, |
|
674
|
}, |
|
675
|
adminHeaders |
|
676
|
) |
|
677
|
).data.gift_card |
|
678
|
|
|
679
|
const paymentCollection = ( |
|
680
|
await api.post( |
|
681
|
`/store/payment-collections`, |
|
682
|
{ cart_id: cart.id }, |
|
683
|
storeHeaders |
|
684
|
) |
|
685
|
).data.payment_collection |
|
686
|
|
|
687
|
await api.post( |
|
688
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
689
|
{ provider_id: "pp_system_default" }, |
|
690
|
storeHeaders |
|
691
|
) |
|
692
|
|
|
693
|
// 1. `TEST1` GIFTCARD IS ALREADY APPLIED WITH VALUE 400 |
|
694
|
|
|
695
|
// 2. APPLY `GC200` GIFTCARD WITH VALUE 200 |
|
696
|
await api.post( |
|
697
|
`/store/carts/${cart.id}/gift-cards`, |
|
698
|
{ code: gc200.code }, |
|
699
|
storeHeaders |
|
700
|
) |
|
701
|
|
|
702
|
// 3. APPLY `GC300` GIFTCARD PARTIALLY WITH VALUE 200 since total is 800 |
|
703
|
await api.post( |
|
704
|
`/store/carts/${cart.id}/gift-cards`, |
|
705
|
{ code: gc300.code }, |
|
706
|
storeHeaders |
|
707
|
) |
|
708
|
|
|
709
|
// 4. IGNORE `GC400` GIFTCARD WITH VALUE 400 SINCE THE ENTIRE TOTAL IS CREDITED FROM GIFTCARDS |
|
710
|
await api.post( |
|
711
|
`/store/carts/${cart.id}/gift-cards`, |
|
712
|
{ code: gc400.code }, |
|
713
|
storeHeaders |
|
714
|
) |
|
715
|
|
|
716
|
const { |
|
717
|
data: { order }, |
|
718
|
} = await api.post( |
|
719
|
`/store/carts/${cart.id}/complete?fields=+credit_line_total,*gift_cards,*gift_cards.store_credit_account`, |
|
720
|
{}, |
|
721
|
storeHeaders |
|
722
|
) |
|
723
|
|
|
724
|
expect(order).toEqual( |
|
725
|
expect.objectContaining({ |
|
726
|
total: 0, |
|
727
|
original_total: 800, |
|
728
|
credit_line_total: 800, |
|
729
|
gift_cards: [ |
|
730
|
expect.objectContaining({ |
|
731
|
id: giftCard.id, |
|
732
|
status: "redeemed", |
|
733
|
store_credit_account: expect.objectContaining({ |
|
734
|
balance: 0, |
|
735
|
credits: 400, |
|
736
|
debits: 400, |
|
737
|
}), |
|
738
|
}), |
|
739
|
expect.objectContaining({ |
|
740
|
id: gc200.id, |
|
741
|
status: "redeemed", |
|
742
|
store_credit_account: expect.objectContaining({ |
|
743
|
balance: 0, |
|
744
|
credits: 200, |
|
745
|
debits: 200, |
|
746
|
}), |
|
747
|
}), |
|
748
|
expect.objectContaining({ |
|
749
|
id: gc300.id, |
|
750
|
status: "redeemed", |
|
751
|
store_credit_account: expect.objectContaining({ |
|
752
|
balance: 100, |
|
753
|
credits: 300, |
|
754
|
debits: 200, |
|
755
|
}), |
|
756
|
}), |
|
757
|
], |
|
758
|
credit_lines: expect.arrayContaining([ |
|
759
|
expect.objectContaining({ |
|
760
|
amount: 200, |
|
761
|
reference: "gift-card", |
|
762
|
reference_id: gc200.id, |
|
763
|
}), |
|
764
|
expect.objectContaining({ |
|
765
|
amount: 200, |
|
766
|
reference: "gift-card", |
|
767
|
reference_id: gc300.id, |
|
768
|
}), |
|
769
|
expect.objectContaining({ |
|
770
|
amount: 400, |
|
771
|
reference: "gift-card", |
|
772
|
reference_id: giftCard.id, |
|
773
|
}), |
|
774
|
]), |
|
775
|
}) |
|
776
|
) |
|
777
|
}) |
|
778
|
|
|
779
|
it("should use a gift card across 2 orders correctly", async () => { |
|
780
|
const sharedGiftCard = ( |
|
781
|
await api.post( |
|
782
|
`/admin/gift-cards`, |
|
783
|
{ |
|
784
|
currency_code: "usd", |
|
785
|
value: 400, |
|
786
|
code: "SHARED-CARD", |
|
787
|
line_item_id: "lin_123", |
|
788
|
}, |
|
789
|
adminHeaders |
|
790
|
) |
|
791
|
).data.gift_card |
|
792
|
|
|
793
|
const cart1 = ( |
|
794
|
await api.post( |
|
795
|
`/store/carts`, |
|
796
|
{ |
|
797
|
region_id: region.id, |
|
798
|
sales_channel_id: salesChannel.id, |
|
799
|
items: [{ variant_id: veryCheapVariant.id, quantity: 1 }], |
|
800
|
}, |
|
801
|
storeHeaders |
|
802
|
) |
|
803
|
).data.cart |
|
804
|
|
|
805
|
const paymentCollection1 = ( |
|
806
|
await api.post( |
|
807
|
`/store/payment-collections`, |
|
808
|
{ cart_id: cart1.id }, |
|
809
|
storeHeaders |
|
810
|
) |
|
811
|
).data.payment_collection |
|
812
|
|
|
813
|
await api.post( |
|
814
|
`/store/carts/${cart1.id}/gift-cards?fields=+credit_line_total`, |
|
815
|
{ code: sharedGiftCard.code }, |
|
816
|
storeHeaders |
|
817
|
) |
|
818
|
|
|
819
|
await api.post( |
|
820
|
`/store/payment-collections/${paymentCollection1.id}/payment-sessions`, |
|
821
|
{ provider_id: "pp_system_default" }, |
|
822
|
storeHeaders |
|
823
|
) |
|
824
|
|
|
825
|
const { |
|
826
|
data: { order }, |
|
827
|
} = await api.post( |
|
828
|
`/store/carts/${cart1.id}/complete`, |
|
829
|
{}, |
|
830
|
storeHeaders |
|
831
|
) |
|
832
|
|
|
833
|
expect(order).toEqual( |
|
834
|
expect.objectContaining({ |
|
835
|
total: 0, |
|
836
|
original_total: 200, |
|
837
|
credit_line_total: 200, |
|
838
|
credit_lines: expect.arrayContaining([ |
|
839
|
expect.objectContaining({ |
|
840
|
amount: 200, |
|
841
|
reference: "gift-card", |
|
842
|
reference_id: sharedGiftCard.id, |
|
843
|
}), |
|
844
|
]), |
|
845
|
}) |
|
846
|
) |
|
847
|
|
|
848
|
let giftCardWithStoreCreditAccount = ( |
|
849
|
await api.get( |
|
850
|
`/store/gift-cards/${sharedGiftCard.code}?fields=*store_credit_account`, |
|
851
|
storeHeaders |
|
852
|
) |
|
853
|
).data.gift_card |
|
854
|
|
|
855
|
expect(giftCardWithStoreCreditAccount).toEqual( |
|
856
|
expect.objectContaining({ |
|
857
|
status: "redeemed", |
|
858
|
code: sharedGiftCard.code, |
|
859
|
value: 400, |
|
860
|
store_credit_account: expect.objectContaining({ |
|
861
|
balance: 200, |
|
862
|
credits: 400, |
|
863
|
debits: 200, |
|
864
|
}), |
|
865
|
}) |
|
866
|
) |
|
867
|
|
|
868
|
// create the second cart |
|
869
|
const cart2 = ( |
|
870
|
await api.post( |
|
871
|
`/store/carts`, |
|
872
|
{ |
|
873
|
region_id: region.id, |
|
874
|
sales_channel_id: salesChannel.id, |
|
875
|
items: [{ variant_id: veryCheapVariant.id, quantity: 1 }], |
|
876
|
}, |
|
877
|
storeHeaders |
|
878
|
) |
|
879
|
).data.cart |
|
880
|
|
|
881
|
await api.post( |
|
882
|
`/store/carts/${cart2.id}/gift-cards?fields=+credit_line_total`, |
|
883
|
{ code: sharedGiftCard.code }, |
|
884
|
storeHeaders |
|
885
|
) |
|
886
|
|
|
887
|
const paymentCollection2 = ( |
|
888
|
await api.post( |
|
889
|
`/store/payment-collections`, |
|
890
|
{ cart_id: cart2.id }, |
|
891
|
storeHeaders |
|
892
|
) |
|
893
|
).data.payment_collection |
|
894
|
|
|
895
|
await api.post( |
|
896
|
`/store/payment-collections/${paymentCollection2.id}/payment-sessions`, |
|
897
|
{ provider_id: "pp_system_default" }, |
|
898
|
storeHeaders |
|
899
|
) |
|
900
|
|
|
901
|
/** SUCCESSFULLY COMPLETE THE SECOND CART */ |
|
902
|
|
|
903
|
const { |
|
904
|
data: { order: order2 }, |
|
905
|
} = await api.post( |
|
906
|
`/store/carts/${cart2.id}/complete`, |
|
907
|
{}, |
|
908
|
storeHeaders |
|
909
|
) |
|
910
|
|
|
911
|
expect(order2).toEqual( |
|
912
|
expect.objectContaining({ |
|
913
|
total: 0, |
|
914
|
original_total: 200, |
|
915
|
credit_line_total: 200, |
|
916
|
credit_lines: expect.arrayContaining([ |
|
917
|
expect.objectContaining({ |
|
918
|
amount: 200, |
|
919
|
reference: "gift-card", |
|
920
|
reference_id: sharedGiftCard.id, |
|
921
|
}), |
|
922
|
]), |
|
923
|
}) |
|
924
|
) |
|
925
|
|
|
926
|
giftCardWithStoreCreditAccount = ( |
|
927
|
await api.get( |
|
928
|
`/store/gift-cards/${sharedGiftCard.code}?fields=*store_credit_account`, |
|
929
|
storeHeaders |
|
930
|
) |
|
931
|
).data.gift_card |
|
932
|
|
|
933
|
expect(giftCardWithStoreCreditAccount).toEqual( |
|
934
|
expect.objectContaining({ |
|
935
|
status: "redeemed", |
|
936
|
code: sharedGiftCard.code, |
|
937
|
value: 400, |
|
938
|
store_credit_account: expect.objectContaining({ |
|
939
|
balance: 0, |
|
940
|
credits: 400, |
|
941
|
debits: 400, |
|
942
|
}), |
|
943
|
}) |
|
944
|
) |
|
945
|
|
|
946
|
const giftCardOrders = ( |
|
947
|
await api.get( |
|
948
|
`/admin/gift-cards/${sharedGiftCard.id}/orders`, |
|
949
|
adminHeaders |
|
950
|
) |
|
951
|
).data.orders |
|
952
|
|
|
953
|
expect(giftCardOrders).toHaveLength(2) |
|
954
|
expect(giftCardOrders).toEqual( |
|
955
|
expect.arrayContaining([ |
|
956
|
expect.objectContaining({ id: order.id }), |
|
957
|
expect.objectContaining({ id: order2.id }), |
|
958
|
]) |
|
959
|
) |
|
960
|
|
|
961
|
// create the third cart |
|
962
|
const cart3 = ( |
|
963
|
await api.post( |
|
964
|
`/store/carts`, |
|
965
|
{ |
|
966
|
region_id: region.id, |
|
967
|
sales_channel_id: salesChannel.id, |
|
968
|
items: [{ variant_id: veryCheapVariant.id, quantity: 1 }], |
|
969
|
}, |
|
970
|
storeHeaders |
|
971
|
) |
|
972
|
).data.cart |
|
973
|
|
|
974
|
// when we try to use a gift card which balance is fully used, it should throw an error |
|
975
|
const { response } = await api |
|
976
|
.post( |
|
977
|
`/store/carts/${cart3.id}/gift-cards?fields=+credit_line_total`, |
|
978
|
{ code: sharedGiftCard.code }, |
|
979
|
storeHeaders |
|
980
|
) |
|
981
|
.catch((err) => err) |
|
982
|
|
|
983
|
expect(response.status).toEqual(400) |
|
984
|
expect(response.data.message).toEqual( |
|
985
|
`Gift card (${sharedGiftCard.code}) has no balance` |
|
986
|
) |
|
987
|
}) |
|
988
|
|
|
989
|
it("should throw insufficient funds error when the gift card has insufficient funds", async () => { |
|
990
|
const { |
|
991
|
data: { cart: updatedCart }, |
|
992
|
} = await api.post( |
|
993
|
`/store/carts/${cart.id}/line-items?fields=+credit_line_total,+gift_cards.id,+gift_cards.code,+payment_collection.id`, |
|
994
|
{ quantity: 5, variant_id: cheapVariant.id }, |
|
995
|
storeHeaders |
|
996
|
) |
|
997
|
|
|
998
|
await api.post( |
|
999
|
`/store/payment-collections/${updatedCart.payment_collection.id}/payment-sessions`, |
|
1000
|
{ provider_id: "pp_system_default" }, |
|
1001
|
storeHeaders |
|
1002
|
) |
|
1003
|
|
|
1004
|
const { |
|
1005
|
data: { cart: newCart }, |
|
1006
|
} = await api.post( |
|
1007
|
`/store/carts`, |
|
1008
|
{ |
|
1009
|
region_id: region.id, |
|
1010
|
sales_channel_id: salesChannel.id, |
|
1011
|
items: [{ variant_id: expensiveVariant.id, quantity: 1 }], |
|
1012
|
}, |
|
1013
|
storeHeaders |
|
1014
|
) |
|
1015
|
|
|
1016
|
const { |
|
1017
|
data: { payment_collection: newPaymentCollection }, |
|
1018
|
} = await api.post( |
|
1019
|
`/store/payment-collections`, |
|
1020
|
{ cart_id: newCart.id }, |
|
1021
|
storeHeaders |
|
1022
|
) |
|
1023
|
|
|
1024
|
await api.post( |
|
1025
|
`/store/carts/${newCart.id}/gift-cards`, |
|
1026
|
{ code: giftCard.code }, |
|
1027
|
storeHeaders |
|
1028
|
) |
|
1029
|
|
|
1030
|
await api.post( |
|
1031
|
`/store/payment-collections/${newPaymentCollection.id}/payment-sessions`, |
|
1032
|
{ provider_id: "pp_system_default" }, |
|
1033
|
storeHeaders |
|
1034
|
) |
|
1035
|
|
|
1036
|
await api.post( |
|
1037
|
`/store/carts/${updatedCart.id}/complete`, |
|
1038
|
{}, |
|
1039
|
storeHeaders |
|
1040
|
) |
|
1041
|
|
|
1042
|
const { response } = await api |
|
1043
|
.post(`/store/carts/${newCart.id}/complete`, {}, storeHeaders) |
|
1044
|
.catch((err) => err) |
|
1045
|
|
|
1046
|
expect(response.data).toEqual( |
|
1047
|
expect.objectContaining({ |
|
1048
|
message: "Insufficient balance", |
|
1049
|
}) |
|
1050
|
) |
|
1051
|
|
|
1052
|
const { |
|
1053
|
data: { |
|
1054
|
store_credit_accounts: [storeCreditAccount], |
|
1055
|
}, |
|
1056
|
} = await api.get( |
|
1057
|
`/admin/store-credit-accounts?customer_id=${cart.customer_id}¤cy_code=usd`, |
|
1058
|
adminHeaders |
|
1059
|
) |
|
1060
|
|
|
1061
|
expect(storeCreditAccount).toEqual( |
|
1062
|
expect.objectContaining({ |
|
1063
|
balance: 0, |
|
1064
|
credits: 400, |
|
1065
|
debits: 400, |
|
1066
|
}) |
|
1067
|
) |
|
1068
|
}) |
|
1069
|
}) |
|
1070
|
|
|
1071
|
describe("POST /store/carts/:id/gift-cards", () => { |
|
1072
|
it("should throw error when adding a gift card that does not exist", async () => { |
|
1073
|
const { response } = await api |
|
1074
|
.post( |
|
1075
|
`/store/carts/${cart.id}/gift-cards?fields=+credit_line_total`, |
|
1076
|
{ code: "does-not-exist" }, |
|
1077
|
storeHeaders |
|
1078
|
) |
|
1079
|
.catch((err) => err) |
|
1080
|
|
|
1081
|
expect(response.data.message).toEqual( |
|
1082
|
"Gift card (does-not-exist) not found" |
|
1083
|
) |
|
1084
|
}) |
|
1085
|
}) |
|
1086
|
|
|
1087
|
describe("with authenticated customer", () => { |
|
1088
|
let storeHeadersWithAuth |
|
1089
|
let paymentCollection |
|
1090
|
|
|
1091
|
beforeEach(async () => { |
|
1092
|
const publishableKey = await generatePublishableKey(getContainer()) |
|
1093
|
const newStoreHeaders = generateStoreHeaders({ publishableKey }) |
|
1094
|
|
|
1095
|
const user = await createAuthenticatedCustomer(api, newStoreHeaders, { |
|
1096
|
email: "main@customer.com", |
|
1097
|
}) |
|
1098
|
|
|
1099
|
storeHeadersWithAuth = { |
|
1100
|
...newStoreHeaders, |
|
1101
|
headers: { |
|
1102
|
...newStoreHeaders.headers, |
|
1103
|
Authorization: `Bearer ${user.jwt}`, |
|
1104
|
}, |
|
1105
|
} |
|
1106
|
customer = user.customer |
|
1107
|
|
|
1108
|
const { |
|
1109
|
data: { cart: cartWithAuthCustomer }, |
|
1110
|
} = await api.post( |
|
1111
|
`/store/carts`, |
|
1112
|
{ |
|
1113
|
region_id: region.id, |
|
1114
|
sales_channel_id: salesChannel.id, |
|
1115
|
items: [{ variant_id: expensiveVariant.id, quantity: 1 }], |
|
1116
|
}, |
|
1117
|
storeHeadersWithAuth |
|
1118
|
) |
|
1119
|
|
|
1120
|
paymentCollection = ( |
|
1121
|
await api.post( |
|
1122
|
`/store/payment-collections`, |
|
1123
|
{ cart_id: cartWithAuthCustomer.id }, |
|
1124
|
storeHeadersWithAuth |
|
1125
|
) |
|
1126
|
).data.payment_collection |
|
1127
|
|
|
1128
|
cart = cartWithAuthCustomer |
|
1129
|
}) |
|
1130
|
|
|
1131
|
it.todo( |
|
1132
|
"should not allow unregistered customers to add store credits to a cart" |
|
1133
|
) |
|
1134
|
|
|
1135
|
it.todo( |
|
1136
|
"should throw if adding more credit to the cart than the store account has balance" |
|
1137
|
) |
|
1138
|
|
|
1139
|
it.todo("should complete cart with store credit and gift card added") |
|
1140
|
|
|
1141
|
it("should successfully add a store credits with an amount to a cart", async () => { |
|
1142
|
const redeemedGiftCard = ( |
|
1143
|
await api.get( |
|
1144
|
`/store/gift-cards/${giftCard.code}?fields=*store_credit_account`, |
|
1145
|
storeHeadersWithAuth |
|
1146
|
) |
|
1147
|
).data.gift_card |
|
1148
|
|
|
1149
|
// gift cards anonymous account |
|
1150
|
expect(redeemedGiftCard).toEqual( |
|
1151
|
expect.objectContaining({ |
|
1152
|
status: "redeemed", |
|
1153
|
code: "TEST1", |
|
1154
|
value: 400, |
|
1155
|
store_credit_account: expect.objectContaining({ |
|
1156
|
balance: 400, |
|
1157
|
credits: 400, |
|
1158
|
debits: 0, |
|
1159
|
}), |
|
1160
|
}) |
|
1161
|
) |
|
1162
|
|
|
1163
|
await api.post( |
|
1164
|
`/store/store-credit-accounts/claim`, |
|
1165
|
{ code: redeemedGiftCard.store_credit_account.code }, |
|
1166
|
storeHeadersWithAuth |
|
1167
|
) |
|
1168
|
|
|
1169
|
let customerAccount = ( |
|
1170
|
await api.get( |
|
1171
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=${giftCard.currency_code}`, |
|
1172
|
adminHeaders |
|
1173
|
) |
|
1174
|
).data.store_credit_accounts[0] |
|
1175
|
|
|
1176
|
expect(customerAccount).toEqual( |
|
1177
|
expect.objectContaining({ |
|
1178
|
currency_code: giftCard.currency_code, |
|
1179
|
customer_id: customer.id, |
|
1180
|
balance: 400, |
|
1181
|
credits: 400, |
|
1182
|
debits: 0, |
|
1183
|
}) |
|
1184
|
) |
|
1185
|
|
|
1186
|
let { |
|
1187
|
data: { cart: cartWithStoreCredits }, |
|
1188
|
} = await api.post( |
|
1189
|
`/store/carts/${cart.id}/store-credits?fields=+credit_line_total`, |
|
1190
|
{ amount: 200 }, |
|
1191
|
storeHeadersWithAuth |
|
1192
|
) |
|
1193
|
|
|
1194
|
expect(cartWithStoreCredits).toEqual( |
|
1195
|
expect.objectContaining({ |
|
1196
|
total: 600, |
|
1197
|
original_total: 800, |
|
1198
|
credit_line_total: 200, |
|
1199
|
gift_cards: [], |
|
1200
|
credit_lines: expect.arrayContaining([ |
|
1201
|
expect.objectContaining({ |
|
1202
|
amount: 200, |
|
1203
|
reference: "store-credit", |
|
1204
|
reference_id: expect.any(String), |
|
1205
|
}), |
|
1206
|
]), |
|
1207
|
}) |
|
1208
|
) |
|
1209
|
|
|
1210
|
cartWithStoreCredits = ( |
|
1211
|
await api.post( |
|
1212
|
`/store/carts/${cart.id}/store-credits?fields=+credit_line_total`, |
|
1213
|
{ amount: 300 }, |
|
1214
|
storeHeadersWithAuth |
|
1215
|
) |
|
1216
|
).data.cart |
|
1217
|
|
|
1218
|
// OLD CREDIT LINES ARE REMOVED |
|
1219
|
|
|
1220
|
expect(cartWithStoreCredits).toEqual( |
|
1221
|
expect.objectContaining({ |
|
1222
|
total: 500, |
|
1223
|
original_total: 800, |
|
1224
|
credit_line_total: 300, |
|
1225
|
gift_cards: [], |
|
1226
|
credit_lines: expect.arrayContaining([ |
|
1227
|
expect.objectContaining({ |
|
1228
|
amount: 300, |
|
1229
|
reference: "store-credit", |
|
1230
|
reference_id: expect.any(String), |
|
1231
|
}), |
|
1232
|
]), |
|
1233
|
}) |
|
1234
|
) |
|
1235
|
|
|
1236
|
customerAccount = ( |
|
1237
|
await api.get( |
|
1238
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=${giftCard.currency_code}`, |
|
1239
|
adminHeaders |
|
1240
|
) |
|
1241
|
).data.store_credit_accounts[0] |
|
1242
|
|
|
1243
|
expect(customerAccount).toEqual( |
|
1244
|
expect.objectContaining({ |
|
1245
|
balance: 400, |
|
1246
|
credits: 400, // nothing is debited from the customer account until the cart is completed |
|
1247
|
debits: 0, |
|
1248
|
}) |
|
1249
|
) |
|
1250
|
}) |
|
1251
|
|
|
1252
|
it.todo( |
|
1253
|
"should only use credit first order if store credits are added to two carts active at the same time" |
|
1254
|
) |
|
1255
|
|
|
1256
|
it("should throw if adding more credit than the account has balance", async () => { |
|
1257
|
const customerAccount = ( |
|
1258
|
await api.post( |
|
1259
|
`/admin/store-credit-accounts`, |
|
1260
|
{ currency_code: giftCard.currency_code, customer_id: customer.id }, |
|
1261
|
adminHeaders |
|
1262
|
) |
|
1263
|
).data.store_credit_account |
|
1264
|
|
|
1265
|
await api.post( |
|
1266
|
`/admin/store-credit-accounts/${customerAccount.id}/credit`, |
|
1267
|
{ amount: 150, note: "Crediting customers account" }, |
|
1268
|
adminHeaders |
|
1269
|
) |
|
1270
|
|
|
1271
|
const { response } = await api |
|
1272
|
.post( |
|
1273
|
`/store/carts/${cart.id}/store-credits`, |
|
1274
|
{ amount: 700 }, |
|
1275
|
storeHeadersWithAuth |
|
1276
|
) |
|
1277
|
.catch((err) => err) |
|
1278
|
|
|
1279
|
expect(response.data.message).toEqual( |
|
1280
|
"Amount is greater than the store credit account balance" |
|
1281
|
) |
|
1282
|
}) |
|
1283
|
|
|
1284
|
it("should remove the store credits from the cart when the amount is 0", async () => { |
|
1285
|
const customerAccount = ( |
|
1286
|
await api.post( |
|
1287
|
`/admin/store-credit-accounts`, |
|
1288
|
{ currency_code: giftCard.currency_code, customer_id: customer.id }, |
|
1289
|
adminHeaders |
|
1290
|
) |
|
1291
|
).data.store_credit_account |
|
1292
|
|
|
1293
|
await api.post( |
|
1294
|
`/admin/store-credit-accounts/${customerAccount.id}/credit`, |
|
1295
|
{ amount: 150, note: "Crediting customers account" }, |
|
1296
|
adminHeaders |
|
1297
|
) |
|
1298
|
|
|
1299
|
await api.post( |
|
1300
|
`/store/carts/${cart.id}/store-credits`, |
|
1301
|
{ amount: 100 }, |
|
1302
|
storeHeadersWithAuth |
|
1303
|
) |
|
1304
|
|
|
1305
|
const { |
|
1306
|
data: { cart: cartWithoutStoreCredits }, |
|
1307
|
} = await api.post( |
|
1308
|
`/store/carts/${cart.id}/store-credits?fields=+credit_line_total`, |
|
1309
|
{ amount: 0 }, |
|
1310
|
storeHeadersWithAuth |
|
1311
|
) |
|
1312
|
|
|
1313
|
expect(cartWithoutStoreCredits).toEqual( |
|
1314
|
expect.objectContaining({ |
|
1315
|
total: 800, |
|
1316
|
original_total: 800, |
|
1317
|
credit_line_total: 0, |
|
1318
|
credit_lines: [], |
|
1319
|
}) |
|
1320
|
) |
|
1321
|
}) |
|
1322
|
|
|
1323
|
it("should handle adding more credit than the total is properly", async () => { |
|
1324
|
const customerAccount = ( |
|
1325
|
await api.post( |
|
1326
|
`/admin/store-credit-accounts`, |
|
1327
|
{ |
|
1328
|
currency_code: giftCard.currency_code, |
|
1329
|
customer_id: customer.id, |
|
1330
|
}, |
|
1331
|
adminHeaders |
|
1332
|
) |
|
1333
|
).data.store_credit_account |
|
1334
|
|
|
1335
|
await api.post( |
|
1336
|
`/admin/store-credit-accounts/${customerAccount.id}/credit`, |
|
1337
|
{ amount: 1000, note: "Crediting customers account" }, |
|
1338
|
adminHeaders |
|
1339
|
) |
|
1340
|
|
|
1341
|
await api |
|
1342
|
.post( |
|
1343
|
`/store/carts/${cart.id}/store-credits`, |
|
1344
|
{ amount: 900 }, // --> CREDIT MORE THAN THE CART TOTAL IS |
|
1345
|
storeHeadersWithAuth |
|
1346
|
) |
|
1347
|
.catch((err) => err) |
|
1348
|
|
|
1349
|
await api.post( |
|
1350
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
1351
|
{ provider_id: "pp_system_default" }, |
|
1352
|
storeHeadersWithAuth |
|
1353
|
) |
|
1354
|
|
|
1355
|
const { |
|
1356
|
data: { order }, |
|
1357
|
} = await api.post( |
|
1358
|
`/store/carts/${cart.id}/complete?fields=*credit_lines,+credit_line_total`, |
|
1359
|
{}, |
|
1360
|
storeHeadersWithAuth |
|
1361
|
) |
|
1362
|
|
|
1363
|
expect(order).toEqual( |
|
1364
|
expect.objectContaining({ |
|
1365
|
total: 0, |
|
1366
|
original_total: 800, |
|
1367
|
credit_line_total: 800, |
|
1368
|
credit_lines: expect.arrayContaining([ |
|
1369
|
expect.objectContaining({ |
|
1370
|
amount: 800, |
|
1371
|
reference: "store-credit", |
|
1372
|
reference_id: customerAccount.id, |
|
1373
|
}), |
|
1374
|
]), |
|
1375
|
}) |
|
1376
|
) |
|
1377
|
|
|
1378
|
const accountAfterOrder = ( |
|
1379
|
await api.get( |
|
1380
|
`/store/store-credit-accounts/${customerAccount.id}`, |
|
1381
|
storeHeadersWithAuth |
|
1382
|
) |
|
1383
|
).data.store_credit_account |
|
1384
|
|
|
1385
|
expect(accountAfterOrder).toEqual( |
|
1386
|
expect.objectContaining({ |
|
1387
|
balance: 200, |
|
1388
|
credits: 1000, |
|
1389
|
debits: 800, |
|
1390
|
}) |
|
1391
|
) |
|
1392
|
}) |
|
1393
|
|
|
1394
|
it("should use store credits to purchase 2 orders", async () => { |
|
1395
|
const redeemedGiftCard = ( |
|
1396
|
await api.get( |
|
1397
|
`/store/gift-cards/${giftCard.code}?fields=*store_credit_account`, |
|
1398
|
storeHeadersWithAuth |
|
1399
|
) |
|
1400
|
).data.gift_card |
|
1401
|
|
|
1402
|
await api.post( |
|
1403
|
`/store/store-credit-accounts/claim`, |
|
1404
|
{ code: redeemedGiftCard.store_credit_account.code }, |
|
1405
|
storeHeadersWithAuth |
|
1406
|
) |
|
1407
|
|
|
1408
|
let customerAccount = ( |
|
1409
|
await api.get( |
|
1410
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=${giftCard.currency_code}`, |
|
1411
|
adminHeaders |
|
1412
|
) |
|
1413
|
).data.store_credit_accounts[0] |
|
1414
|
|
|
1415
|
expect(customerAccount).toEqual( |
|
1416
|
expect.objectContaining({ |
|
1417
|
currency_code: giftCard.currency_code, |
|
1418
|
customer_id: customer.id, |
|
1419
|
balance: 400, |
|
1420
|
credits: 400, |
|
1421
|
debits: 0, |
|
1422
|
}) |
|
1423
|
) |
|
1424
|
|
|
1425
|
let { |
|
1426
|
data: { cart: cartWithStoreCredits }, |
|
1427
|
} = await api.post( |
|
1428
|
`/store/carts/${cart.id}/store-credits?fields=+credit_line_total`, |
|
1429
|
{ amount: 200 }, |
|
1430
|
storeHeadersWithAuth |
|
1431
|
) |
|
1432
|
|
|
1433
|
expect(cartWithStoreCredits).toEqual( |
|
1434
|
expect.objectContaining({ |
|
1435
|
total: 600, |
|
1436
|
original_total: 800, |
|
1437
|
credit_line_total: 200, |
|
1438
|
gift_cards: [], |
|
1439
|
credit_lines: expect.arrayContaining([ |
|
1440
|
expect.objectContaining({ |
|
1441
|
amount: 200, |
|
1442
|
reference: "store-credit", |
|
1443
|
reference_id: customerAccount.id, |
|
1444
|
}), |
|
1445
|
]), |
|
1446
|
}) |
|
1447
|
) |
|
1448
|
|
|
1449
|
await api.post( |
|
1450
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
1451
|
{ provider_id: "pp_system_default" }, |
|
1452
|
storeHeadersWithAuth |
|
1453
|
) |
|
1454
|
|
|
1455
|
const { |
|
1456
|
data: { order }, |
|
1457
|
} = await api.post( |
|
1458
|
`/store/carts/${cartWithStoreCredits.id}/complete`, |
|
1459
|
{}, |
|
1460
|
storeHeadersWithAuth |
|
1461
|
) |
|
1462
|
|
|
1463
|
expect(order).toEqual( |
|
1464
|
expect.objectContaining({ |
|
1465
|
total: 600, |
|
1466
|
original_total: 800, |
|
1467
|
credit_line_total: 200, |
|
1468
|
credit_lines: expect.arrayContaining([ |
|
1469
|
expect.objectContaining({ |
|
1470
|
amount: 200, |
|
1471
|
reference: "store-credit", |
|
1472
|
reference_id: customerAccount.id, |
|
1473
|
}), |
|
1474
|
]), |
|
1475
|
}) |
|
1476
|
) |
|
1477
|
|
|
1478
|
customerAccount = ( |
|
1479
|
await api.get( |
|
1480
|
`/store/store-credit-accounts/${customerAccount.id}`, |
|
1481
|
storeHeadersWithAuth |
|
1482
|
) |
|
1483
|
).data.store_credit_account |
|
1484
|
|
|
1485
|
expect(customerAccount).toEqual( |
|
1486
|
expect.objectContaining({ |
|
1487
|
balance: 200, |
|
1488
|
credits: 400, |
|
1489
|
debits: 200, |
|
1490
|
}) |
|
1491
|
) |
|
1492
|
|
|
1493
|
const { |
|
1494
|
data: { cart: secondCartWithAuthCustomer }, |
|
1495
|
} = await api.post( |
|
1496
|
`/store/carts`, |
|
1497
|
{ |
|
1498
|
region_id: region.id, |
|
1499
|
sales_channel_id: salesChannel.id, |
|
1500
|
items: [{ variant_id: expensiveVariant.id, quantity: 1 }], |
|
1501
|
}, |
|
1502
|
storeHeadersWithAuth |
|
1503
|
) |
|
1504
|
|
|
1505
|
// use the rest of store credits |
|
1506
|
await api.post( |
|
1507
|
`/store/carts/${secondCartWithAuthCustomer.id}/store-credits?fields=+credit_line_total`, |
|
1508
|
{ amount: 200 }, |
|
1509
|
storeHeadersWithAuth |
|
1510
|
) |
|
1511
|
|
|
1512
|
paymentCollection = ( |
|
1513
|
await api.post( |
|
1514
|
`/store/payment-collections`, |
|
1515
|
{ cart_id: secondCartWithAuthCustomer.id }, |
|
1516
|
storeHeadersWithAuth |
|
1517
|
) |
|
1518
|
).data.payment_collection |
|
1519
|
|
|
1520
|
await api.post( |
|
1521
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
1522
|
{ provider_id: "pp_system_default" }, |
|
1523
|
storeHeadersWithAuth |
|
1524
|
) |
|
1525
|
|
|
1526
|
const { |
|
1527
|
data: { order: secondOrder }, |
|
1528
|
} = await api.post( |
|
1529
|
`/store/carts/${secondCartWithAuthCustomer.id}/complete`, |
|
1530
|
{}, |
|
1531
|
storeHeadersWithAuth |
|
1532
|
) |
|
1533
|
|
|
1534
|
expect(secondOrder).toEqual( |
|
1535
|
expect.objectContaining({ |
|
1536
|
total: 600, |
|
1537
|
original_total: 800, |
|
1538
|
credit_line_total: 200, |
|
1539
|
credit_lines: expect.arrayContaining([ |
|
1540
|
expect.objectContaining({ |
|
1541
|
amount: 200, |
|
1542
|
reference: "store-credit", |
|
1543
|
reference_id: customerAccount.id, |
|
1544
|
}), |
|
1545
|
]), |
|
1546
|
}) |
|
1547
|
) |
|
1548
|
|
|
1549
|
customerAccount = ( |
|
1550
|
await api.get( |
|
1551
|
`/store/store-credit-accounts/${customerAccount.id}`, |
|
1552
|
storeHeadersWithAuth |
|
1553
|
) |
|
1554
|
).data.store_credit_account |
|
1555
|
|
|
1556
|
expect(customerAccount).toEqual( |
|
1557
|
expect.objectContaining({ |
|
1558
|
balance: 0, // rest of the credit is used and balance is 0 |
|
1559
|
credits: 400, |
|
1560
|
debits: 400, |
|
1561
|
}) |
|
1562
|
) |
|
1563
|
}) |
|
1564
|
}) |
|
1565
|
|
|
1566
|
describe("POST /store/carts/:id/taxes", () => { |
|
1567
|
it("should not generate tax lines for gift card product items (is_giftcard=true)", async () => { |
|
1568
|
const taxService = getContainer().resolve(Modules.TAX) |
|
1569
|
await taxService.createTaxRegions([ |
|
1570
|
{ |
|
1571
|
country_code: "US", |
|
1572
|
provider_id: "tp_system", |
|
1573
|
default_tax_rate: { |
|
1574
|
name: "US Default Rate", |
|
1575
|
rate: 10, |
|
1576
|
code: "US_RATE", |
|
1577
|
}, |
|
1578
|
}, |
|
1579
|
]) |
|
1580
|
|
|
1581
|
const taxRegion = ( |
|
1582
|
await api.post( |
|
1583
|
"/admin/regions", |
|
1584
|
{ |
|
1585
|
payment_providers: ["pp_system_default"], |
|
1586
|
name: "us-tax-region", |
|
1587
|
currency_code: "usd", |
|
1588
|
countries: ["us"], |
|
1589
|
}, |
|
1590
|
adminHeaders |
|
1591
|
) |
|
1592
|
).data.region |
|
1593
|
|
|
1594
|
const giftCardProduct = ( |
|
1595
|
await api.post( |
|
1596
|
`/admin/products`, |
|
1597
|
{ |
|
1598
|
title: "Gift Card Product", |
|
1599
|
status: ProductStatus.PUBLISHED, |
|
1600
|
is_giftcard: true, |
|
1601
|
options: [{ title: "Denomination", values: ["10"] }], |
|
1602
|
variants: [ |
|
1603
|
{ |
|
1604
|
title: "10", |
|
1605
|
manage_inventory: false, |
|
1606
|
options: { Denomination: "10" }, |
|
1607
|
prices: [{ amount: 1000, currency_code: "usd" }], |
|
1608
|
}, |
|
1609
|
], |
|
1610
|
}, |
|
1611
|
adminHeaders |
|
1612
|
) |
|
1613
|
).data.product |
|
1614
|
|
|
1615
|
const taxCart = ( |
|
1616
|
await api.post( |
|
1617
|
`/store/carts`, |
|
1618
|
{ |
|
1619
|
region_id: taxRegion.id, |
|
1620
|
sales_channel_id: salesChannel.id, |
|
1621
|
shipping_address: { country_code: "us" }, |
|
1622
|
items: [ |
|
1623
|
{ variant_id: expensiveVariant.id, quantity: 1 }, |
|
1624
|
{ variant_id: giftCardProduct.variants[0].id, quantity: 1 }, |
|
1625
|
], |
|
1626
|
}, |
|
1627
|
storeHeaders |
|
1628
|
) |
|
1629
|
).data.cart |
|
1630
|
|
|
1631
|
const { data } = await api.post( |
|
1632
|
`/store/carts/${taxCart.id}/taxes`, |
|
1633
|
{}, |
|
1634
|
storeHeaders |
|
1635
|
) |
|
1636
|
|
|
1637
|
expect(data.cart.items).toEqual( |
|
1638
|
expect.arrayContaining([ |
|
1639
|
expect.objectContaining({ |
|
1640
|
is_giftcard: false, |
|
1641
|
tax_lines: expect.arrayContaining([ |
|
1642
|
expect.objectContaining({ rate: expect.any(Number) }), |
|
1643
|
]), |
|
1644
|
}), |
|
1645
|
expect.objectContaining({ |
|
1646
|
is_giftcard: true, |
|
1647
|
tax_lines: [], |
|
1648
|
}), |
|
1649
|
]) |
|
1650
|
) |
|
1651
|
}) |
|
1652
|
}) |
|
1653
|
}, |
|
1654
|
}) |