stripe-payment.e2e-spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { EntityHydrator, mergeConfig } from '@vendure/core';
  3. import {
  4. CreateProductMutation,
  5. CreateProductMutationVariables,
  6. CreateProductVariantsMutation,
  7. CreateProductVariantsMutationVariables,
  8. TestCreateStockLocationDocument,
  9. } from '@vendure/core/e2e/graphql/generated-e2e-admin-types';
  10. import { CREATE_PRODUCT, CREATE_PRODUCT_VARIANTS } from '@vendure/core/e2e/graphql/shared-definitions';
  11. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
  12. import gql from 'graphql-tag';
  13. import nock from 'nock';
  14. import fetch from 'node-fetch';
  15. import path from 'path';
  16. import { Stripe } from 'stripe';
  17. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  18. import { initialData } from '../../../e2e-common/e2e-initial-data';
  19. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  20. import { StripePlugin } from '../src/stripe';
  21. import { stripePaymentMethodHandler } from '../src/stripe/stripe.handler';
  22. import { CREATE_CHANNEL, CREATE_PAYMENT_METHOD, GET_CUSTOMER_LIST } from './graphql/admin-queries';
  23. import {
  24. CreateChannelMutation,
  25. CreateChannelMutationVariables,
  26. CreatePaymentMethodMutation,
  27. CreatePaymentMethodMutationVariables,
  28. CurrencyCode,
  29. GetCustomerListQuery,
  30. GetCustomerListQueryVariables,
  31. LanguageCode,
  32. } from './graphql/generated-admin-types';
  33. import {
  34. AddItemToOrderMutation,
  35. AddItemToOrderMutationVariables,
  36. GetActiveOrderQuery,
  37. TestOrderFragmentFragment,
  38. } from './graphql/generated-shop-types';
  39. import { ADD_ITEM_TO_ORDER, GET_ACTIVE_ORDER } from './graphql/shop-queries';
  40. import { setShipping } from './payment-helpers';
  41. export const CREATE_STRIPE_PAYMENT_INTENT = gql`
  42. mutation createStripePaymentIntent {
  43. createStripePaymentIntent
  44. }
  45. `;
  46. describe('Stripe payments', () => {
  47. const devConfig = mergeConfig(testConfig(), {
  48. plugins: [
  49. StripePlugin.init({
  50. storeCustomersInStripe: true,
  51. }),
  52. ],
  53. });
  54. const { shopClient, adminClient, server } = createTestEnvironment(devConfig);
  55. let started = false;
  56. let customers: GetCustomerListQuery['customers']['items'];
  57. let order: TestOrderFragmentFragment;
  58. let serverPort: number;
  59. beforeAll(async () => {
  60. serverPort = devConfig.apiOptions.port;
  61. await server.init({
  62. initialData,
  63. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  64. customerCount: 2,
  65. });
  66. started = true;
  67. await adminClient.asSuperAdmin();
  68. ({
  69. customers: { items: customers },
  70. } = await adminClient.query<GetCustomerListQuery, GetCustomerListQueryVariables>(GET_CUSTOMER_LIST, {
  71. options: {
  72. take: 2,
  73. },
  74. }));
  75. }, TEST_SETUP_TIMEOUT_MS);
  76. afterAll(async () => {
  77. await server.destroy();
  78. });
  79. it('Should start successfully', () => {
  80. expect(started).toEqual(true);
  81. expect(customers).toHaveLength(2);
  82. });
  83. it('Should prepare an order', async () => {
  84. await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
  85. const { addItemToOrder } = await shopClient.query<
  86. AddItemToOrderMutation,
  87. AddItemToOrderMutationVariables
  88. >(ADD_ITEM_TO_ORDER, {
  89. productVariantId: 'T_1',
  90. quantity: 2,
  91. });
  92. order = addItemToOrder as TestOrderFragmentFragment;
  93. expect(order.code).toBeDefined();
  94. });
  95. it('Should add a Stripe paymentMethod', async () => {
  96. const { createPaymentMethod } = await adminClient.query<
  97. CreatePaymentMethodMutation,
  98. CreatePaymentMethodMutationVariables
  99. >(CREATE_PAYMENT_METHOD, {
  100. input: {
  101. code: `stripe-payment-${E2E_DEFAULT_CHANNEL_TOKEN}`,
  102. translations: [
  103. {
  104. name: 'Stripe payment test',
  105. description: 'This is a Stripe test payment method',
  106. languageCode: LanguageCode.en,
  107. },
  108. ],
  109. enabled: true,
  110. handler: {
  111. code: stripePaymentMethodHandler.code,
  112. arguments: [
  113. { name: 'apiKey', value: 'test-api-key' },
  114. { name: 'webhookSecret', value: 'test-signing-secret' },
  115. ],
  116. },
  117. },
  118. });
  119. expect(createPaymentMethod.code).toBe(`stripe-payment-${E2E_DEFAULT_CHANNEL_TOKEN}`);
  120. await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
  121. await setShipping(shopClient);
  122. });
  123. it('if no customer id exists, makes a call to create', async () => {
  124. let createCustomerPayload: { name: string; email: string } | undefined;
  125. const emptyList = { data: [] };
  126. nock('https://api.stripe.com/')
  127. .get(/\/v1\/customers.*/)
  128. .reply(200, emptyList);
  129. nock('https://api.stripe.com/')
  130. .post('/v1/customers', body => {
  131. createCustomerPayload = body;
  132. return true;
  133. })
  134. .reply(201, {
  135. id: 'new-customer-id',
  136. });
  137. nock('https://api.stripe.com/').post('/v1/payment_intents').reply(200, {
  138. client_secret: 'test-client-secret',
  139. });
  140. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  141. expect(createCustomerPayload).toEqual({
  142. email: 'hayden.zieme12@hotmail.com',
  143. name: 'Hayden Zieme',
  144. });
  145. });
  146. it('should send correct payload to create payment intent', async () => {
  147. let createPaymentIntentPayload: any;
  148. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  149. nock('https://api.stripe.com/')
  150. .post('/v1/payment_intents', body => {
  151. createPaymentIntentPayload = body;
  152. return true;
  153. })
  154. .reply(200, {
  155. client_secret: 'test-client-secret',
  156. });
  157. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  158. expect(createPaymentIntentPayload).toEqual({
  159. amount: activeOrder?.totalWithTax.toString(),
  160. currency: activeOrder?.currencyCode?.toLowerCase(),
  161. customer: 'new-customer-id',
  162. 'automatic_payment_methods[enabled]': 'true',
  163. 'metadata[channelToken]': E2E_DEFAULT_CHANNEL_TOKEN,
  164. 'metadata[orderId]': '1',
  165. 'metadata[orderCode]': activeOrder?.code,
  166. });
  167. expect(createStripePaymentIntent).toEqual('test-client-secret');
  168. });
  169. // https://github.com/vendure-ecommerce/vendure/issues/1935
  170. it('should attach metadata to stripe payment intent', async () => {
  171. StripePlugin.options.metadata = async (injector, ctx, currentOrder) => {
  172. const hydrator = injector.get(EntityHydrator);
  173. await hydrator.hydrate(ctx, currentOrder, { relations: ['customer'] });
  174. return {
  175. customerEmail: currentOrder.customer?.emailAddress ?? 'demo',
  176. };
  177. };
  178. let createPaymentIntentPayload: any;
  179. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  180. nock('https://api.stripe.com/')
  181. .post('/v1/payment_intents', body => {
  182. createPaymentIntentPayload = body;
  183. return true;
  184. })
  185. .reply(200, {
  186. client_secret: 'test-client-secret',
  187. });
  188. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  189. expect(createPaymentIntentPayload).toEqual({
  190. amount: activeOrder?.totalWithTax.toString(),
  191. currency: activeOrder?.currencyCode?.toLowerCase(),
  192. customer: 'new-customer-id',
  193. 'automatic_payment_methods[enabled]': 'true',
  194. 'metadata[channelToken]': E2E_DEFAULT_CHANNEL_TOKEN,
  195. 'metadata[orderId]': '1',
  196. 'metadata[orderCode]': activeOrder?.code,
  197. 'metadata[customerEmail]': customers[0].emailAddress,
  198. });
  199. expect(createStripePaymentIntent).toEqual('test-client-secret');
  200. StripePlugin.options.metadata = undefined;
  201. });
  202. // https://github.com/vendure-ecommerce/vendure/issues/2412
  203. it('should attach additional params to payment intent using paymentIntentCreateParams', async () => {
  204. StripePlugin.options.paymentIntentCreateParams = async (injector, ctx, currentOrder) => {
  205. const hydrator = injector.get(EntityHydrator);
  206. await hydrator.hydrate(ctx, currentOrder, { relations: ['customer'] });
  207. return {
  208. description: `Order #${currentOrder.code} for ${currentOrder.customer!.emailAddress}`,
  209. };
  210. };
  211. let createPaymentIntentPayload: any;
  212. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  213. nock('https://api.stripe.com/')
  214. .post('/v1/payment_intents', body => {
  215. createPaymentIntentPayload = body;
  216. return true;
  217. })
  218. .reply(200, {
  219. client_secret: 'test-client-secret',
  220. });
  221. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  222. expect(createPaymentIntentPayload).toEqual({
  223. amount: activeOrder?.totalWithTax.toString(),
  224. currency: activeOrder?.currencyCode?.toLowerCase(),
  225. customer: 'new-customer-id',
  226. description: `Order #${activeOrder!.code} for ${activeOrder!.customer!.emailAddress}`,
  227. 'automatic_payment_methods[enabled]': 'true',
  228. 'metadata[channelToken]': E2E_DEFAULT_CHANNEL_TOKEN,
  229. 'metadata[orderId]': '1',
  230. 'metadata[orderCode]': activeOrder?.code,
  231. });
  232. expect(createStripePaymentIntent).toEqual('test-client-secret');
  233. StripePlugin.options.paymentIntentCreateParams = undefined;
  234. });
  235. // https://github.com/vendure-ecommerce/vendure/issues/3183
  236. it('should attach additional options to payment intent using requestOptions', async () => {
  237. StripePlugin.options.requestOptions = async (injector, ctx, currentOrder) => {
  238. return {
  239. stripeAccount: 'acct_connected',
  240. };
  241. };
  242. let connectedAccountHeader: any;
  243. let createPaymentIntentPayload: any;
  244. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  245. nock('https://api.stripe.com/', {
  246. reqheaders: {
  247. 'Stripe-Account': headerValue => {
  248. connectedAccountHeader = headerValue;
  249. return true;
  250. },
  251. },
  252. })
  253. .post('/v1/payment_intents', body => {
  254. createPaymentIntentPayload = body;
  255. return true;
  256. })
  257. .reply(200, {
  258. client_secret: 'test-client-secret',
  259. });
  260. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  261. expect(createPaymentIntentPayload).toEqual({
  262. amount: activeOrder?.totalWithTax.toString(),
  263. currency: activeOrder?.currencyCode?.toLowerCase(),
  264. customer: 'new-customer-id',
  265. 'automatic_payment_methods[enabled]': 'true',
  266. 'metadata[channelToken]': E2E_DEFAULT_CHANNEL_TOKEN,
  267. 'metadata[orderId]': '1',
  268. 'metadata[orderCode]': activeOrder?.code,
  269. });
  270. expect(connectedAccountHeader).toEqual('acct_connected');
  271. expect(createStripePaymentIntent).toEqual('test-client-secret');
  272. StripePlugin.options.paymentIntentCreateParams = undefined;
  273. });
  274. // https://github.com/vendure-ecommerce/vendure/issues/2412
  275. it('should attach additional params to customer using customerCreateParams', async () => {
  276. StripePlugin.options.customerCreateParams = async (injector, ctx, currentOrder) => {
  277. const hydrator = injector.get(EntityHydrator);
  278. await hydrator.hydrate(ctx, currentOrder, { relations: ['customer'] });
  279. return {
  280. description: `Description for ${currentOrder.customer!.emailAddress}`,
  281. phone: '12345',
  282. };
  283. };
  284. await shopClient.asUserWithCredentials(customers[1].emailAddress, 'test');
  285. const { addItemToOrder } = await shopClient.query<
  286. AddItemToOrderMutation,
  287. AddItemToOrderMutationVariables
  288. >(ADD_ITEM_TO_ORDER, {
  289. productVariantId: 'T_1',
  290. quantity: 2,
  291. });
  292. order = addItemToOrder as TestOrderFragmentFragment;
  293. let createCustomerPayload: { name: string; email: string } | undefined;
  294. const emptyList = { data: [] };
  295. nock('https://api.stripe.com/')
  296. .get(/\/v1\/customers.*/)
  297. .reply(200, emptyList);
  298. nock('https://api.stripe.com/')
  299. .post('/v1/customers', body => {
  300. createCustomerPayload = body;
  301. return true;
  302. })
  303. .reply(201, {
  304. id: 'new-customer-id',
  305. });
  306. nock('https://api.stripe.com/').post('/v1/payment_intents').reply(200, {
  307. client_secret: 'test-client-secret',
  308. });
  309. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  310. await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  311. expect(createCustomerPayload).toEqual({
  312. email: 'trevor_donnelly96@hotmail.com',
  313. name: 'Trevor Donnelly',
  314. description: `Description for ${activeOrder!.customer!.emailAddress}`,
  315. phone: '12345',
  316. });
  317. });
  318. // https://github.com/vendure-ecommerce/vendure/issues/2450
  319. it('Should not crash on signature validation failure', async () => {
  320. const MOCKED_WEBHOOK_PAYLOAD = {
  321. id: 'evt_0',
  322. object: 'event',
  323. api_version: '2022-11-15',
  324. data: {
  325. object: {
  326. id: 'pi_0',
  327. currency: 'usd',
  328. status: 'succeeded',
  329. },
  330. },
  331. livemode: false,
  332. pending_webhooks: 1,
  333. request: {
  334. id: 'req_0',
  335. idempotency_key: '00000000-0000-0000-0000-000000000000',
  336. },
  337. type: 'payment_intent.succeeded',
  338. };
  339. const payloadString = JSON.stringify(MOCKED_WEBHOOK_PAYLOAD, null, 2);
  340. const result = await fetch(`http://localhost:${serverPort}/payments/stripe`, {
  341. method: 'post',
  342. body: payloadString,
  343. headers: { 'Content-Type': 'application/json' },
  344. });
  345. // We didn't provided any signatures, it should result in a 400 - Bad request
  346. expect(result.status).toEqual(400);
  347. });
  348. // TODO: Contribution welcome: test webhook handling and order settlement
  349. // https://github.com/vendure-ecommerce/vendure/issues/2450
  350. it("Should validate the webhook's signature properly", async () => {
  351. await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
  352. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  353. order = activeOrder!;
  354. const MOCKED_WEBHOOK_PAYLOAD = {
  355. id: 'evt_0',
  356. object: 'event',
  357. api_version: '2022-11-15',
  358. data: {
  359. object: {
  360. id: 'pi_0',
  361. currency: 'usd',
  362. metadata: {
  363. orderCode: order.code,
  364. orderId: parseInt(order.id.replace('T_', ''), 10),
  365. channelToken: E2E_DEFAULT_CHANNEL_TOKEN,
  366. },
  367. amount_received: order.totalWithTax,
  368. status: 'succeeded',
  369. },
  370. },
  371. livemode: false,
  372. pending_webhooks: 1,
  373. request: {
  374. id: 'req_0',
  375. idempotency_key: '00000000-0000-0000-0000-000000000000',
  376. },
  377. type: 'payment_intent.succeeded',
  378. };
  379. const payloadString = JSON.stringify(MOCKED_WEBHOOK_PAYLOAD, null, 2);
  380. const stripeWebhooks = new Stripe('test-api-secret', { apiVersion: '2023-08-16' }).webhooks;
  381. const header = stripeWebhooks.generateTestHeaderString({
  382. payload: payloadString,
  383. secret: 'test-signing-secret',
  384. });
  385. const event = stripeWebhooks.constructEvent(payloadString, header, 'test-signing-secret');
  386. expect(event.id).to.equal(MOCKED_WEBHOOK_PAYLOAD.id);
  387. await setShipping(shopClient);
  388. // Due to the `this.orderService.transitionToState(...)` fails with the internal lookup by id,
  389. // we need to put the order into `ArrangingPayment` state manually before calling the webhook handler.
  390. // const transitionResult = await adminClient.query(TRANSITION_TO_ARRANGING_PAYMENT, { id: order.id });
  391. // expect(transitionResult.transitionOrderToState.__typename).toBe('Order')
  392. const result = await fetch(`http://localhost:${serverPort}/payments/stripe`, {
  393. method: 'post',
  394. body: payloadString,
  395. headers: { 'Content-Type': 'application/json', 'Stripe-Signature': header },
  396. });
  397. // I would expect to the status to be 200, but at the moment either the
  398. // `orderService.transitionToState()` or the `orderService.addPaymentToOrder()`
  399. // throws an error of 'error.entity-with-id-not-found'
  400. expect(result.status).toEqual(200);
  401. });
  402. // https://github.com/vendure-ecommerce/vendure/issues/3249
  403. it('Should skip events without expected metadata, when the plugin option is set', async () => {
  404. StripePlugin.options.skipPaymentIntentsWithoutExpectedMetadata = true;
  405. const MOCKED_WEBHOOK_PAYLOAD = {
  406. id: 'evt_0',
  407. object: 'event',
  408. api_version: '2022-11-15',
  409. data: {
  410. object: {
  411. id: 'pi_0',
  412. currency: 'usd',
  413. metadata: {
  414. dummy: 'not a vendure payload',
  415. },
  416. amount_received: 10000,
  417. status: 'succeeded',
  418. },
  419. },
  420. livemode: false,
  421. pending_webhooks: 1,
  422. request: {
  423. id: 'req_0',
  424. idempotency_key: '00000000-0000-0000-0000-000000000000',
  425. },
  426. type: 'payment_intent.succeeded',
  427. };
  428. const payloadString = JSON.stringify(MOCKED_WEBHOOK_PAYLOAD, null, 2);
  429. const stripeWebhooks = new Stripe('test-api-secret', { apiVersion: '2023-08-16' }).webhooks;
  430. const header = stripeWebhooks.generateTestHeaderString({
  431. payload: payloadString,
  432. secret: 'test-signing-secret',
  433. });
  434. const result = await fetch(`http://localhost:${serverPort}/payments/stripe`, {
  435. method: 'post',
  436. body: payloadString,
  437. headers: { 'Content-Type': 'application/json', 'Stripe-Signature': header },
  438. });
  439. expect(result.status).toEqual(200);
  440. });
  441. // https://github.com/vendure-ecommerce/vendure/issues/1630
  442. describe('currencies with no fractional units', () => {
  443. let japanProductId: string;
  444. beforeAll(async () => {
  445. const JAPAN_CHANNEL_TOKEN = 'japan-channel-token';
  446. const { createChannel } = await adminClient.query<
  447. CreateChannelMutation,
  448. CreateChannelMutationVariables
  449. >(CREATE_CHANNEL, {
  450. input: {
  451. code: 'japan-channel',
  452. currencyCode: CurrencyCode.JPY,
  453. token: JAPAN_CHANNEL_TOKEN,
  454. defaultLanguageCode: LanguageCode.en,
  455. defaultShippingZoneId: 'T_1',
  456. defaultTaxZoneId: 'T_1',
  457. pricesIncludeTax: true,
  458. },
  459. });
  460. adminClient.setChannelToken(JAPAN_CHANNEL_TOKEN);
  461. shopClient.setChannelToken(JAPAN_CHANNEL_TOKEN);
  462. const { createStockLocation } = await adminClient.query(TestCreateStockLocationDocument, {
  463. input: {
  464. name: 'Japan warehouse',
  465. },
  466. });
  467. const { createProduct } = await adminClient.query<
  468. CreateProductMutation,
  469. CreateProductMutationVariables
  470. >(CREATE_PRODUCT, {
  471. input: {
  472. translations: [
  473. {
  474. languageCode: LanguageCode.en,
  475. name: 'Channel Product',
  476. slug: 'channel-product',
  477. description: 'Channel product',
  478. },
  479. ],
  480. },
  481. });
  482. const { createProductVariants } = await adminClient.query<
  483. CreateProductVariantsMutation,
  484. CreateProductVariantsMutationVariables
  485. >(CREATE_PRODUCT_VARIANTS, {
  486. input: [
  487. {
  488. productId: createProduct.id,
  489. sku: 'PV1',
  490. optionIds: [],
  491. price: 5000,
  492. stockLevels: [
  493. {
  494. stockLocationId: createStockLocation.id,
  495. stockOnHand: 100,
  496. },
  497. ],
  498. translations: [{ languageCode: LanguageCode.en, name: 'Variant 1' }],
  499. },
  500. ],
  501. });
  502. japanProductId = createProductVariants[0]!.id;
  503. // Create a payment method for the Japan channel
  504. await adminClient.query<CreatePaymentMethodMutation, CreatePaymentMethodMutationVariables>(
  505. CREATE_PAYMENT_METHOD,
  506. {
  507. input: {
  508. code: `stripe-payment-${E2E_DEFAULT_CHANNEL_TOKEN}`,
  509. translations: [
  510. {
  511. name: 'Stripe payment test',
  512. description: 'This is a Stripe test payment method',
  513. languageCode: LanguageCode.en,
  514. },
  515. ],
  516. enabled: true,
  517. handler: {
  518. code: stripePaymentMethodHandler.code,
  519. arguments: [
  520. { name: 'apiKey', value: 'test-api-key' },
  521. { name: 'webhookSecret', value: 'test-signing-secret' },
  522. ],
  523. },
  524. },
  525. },
  526. );
  527. });
  528. it('prepares order', async () => {
  529. await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
  530. const { addItemToOrder } = await shopClient.query<
  531. AddItemToOrderMutation,
  532. AddItemToOrderMutationVariables
  533. >(ADD_ITEM_TO_ORDER, {
  534. productVariantId: japanProductId,
  535. quantity: 1,
  536. });
  537. expect((addItemToOrder as any).totalWithTax).toBe(5000);
  538. });
  539. it('sends correct amount when creating payment intent', async () => {
  540. let createPaymentIntentPayload: any;
  541. const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
  542. nock('https://api.stripe.com/')
  543. .post('/v1/payment_intents', body => {
  544. createPaymentIntentPayload = body;
  545. return true;
  546. })
  547. .reply(200, {
  548. client_secret: 'test-client-secret',
  549. });
  550. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  551. expect(createPaymentIntentPayload.amount).toBe((activeOrder!.totalWithTax / 100).toString());
  552. expect(createPaymentIntentPayload.currency).toBe('jpy');
  553. });
  554. });
  555. });