stripe-payment.e2e-spec.ts 20 KB

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