mollie-dev-server.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  2. import {
  3. ChannelService,
  4. DefaultLogger,
  5. DefaultSearchPlugin,
  6. LogLevel,
  7. mergeConfig,
  8. RequestContext,
  9. } from '@vendure/core';
  10. import { createTestEnvironment, registerInitializer, SqljsInitializer, testConfig } from '@vendure/testing';
  11. import { compileUiExtensions } from '@vendure/ui-devkit/compiler';
  12. import gql from 'graphql-tag';
  13. import localtunnel from 'localtunnel';
  14. import path from 'path';
  15. import { initialData } from '../../../e2e-common/e2e-initial-data';
  16. import { molliePaymentHandler } from '../package/mollie/mollie.handler';
  17. import { MolliePlugin } from '../src/mollie';
  18. import { CREATE_PAYMENT_METHOD } from './graphql/admin-queries';
  19. import {
  20. CreatePaymentMethodMutation,
  21. CreatePaymentMethodMutationVariables,
  22. LanguageCode,
  23. } from './graphql/generated-admin-types';
  24. import { AddItemToOrderMutation, AddItemToOrderMutationVariables } from './graphql/generated-shop-types';
  25. import { ADD_ITEM_TO_ORDER, ADJUST_ORDER_LINE } from './graphql/shop-queries';
  26. import { CREATE_MOLLIE_PAYMENT_INTENT, setShipping } from './payment-helpers';
  27. /**
  28. * This should only be used to locally test the Mollie payment plugin
  29. * Make sure you have `MOLLIE_APIKEY=test_xxxx` in your .env file
  30. * Make sure you have `MOLLIE_APIKEY=test_xxxx` in your .env file
  31. */
  32. /* eslint-disable @typescript-eslint/no-floating-promises */
  33. async function runMollieDevServer() {
  34. // eslint-disable-next-line @typescript-eslint/no-var-requires
  35. require('dotenv').config();
  36. registerInitializer('sqljs', new SqljsInitializer(path.join(__dirname, '__data__')));
  37. const tunnel = await localtunnel({ port: 3050 });
  38. const config = mergeConfig(testConfig, {
  39. plugins: [
  40. ...testConfig.plugins,
  41. DefaultSearchPlugin,
  42. AdminUiPlugin.init({
  43. route: 'admin',
  44. port: 5001,
  45. }),
  46. MolliePlugin.init({ vendureHost: tunnel.url }),
  47. ],
  48. logger: new DefaultLogger({ level: LogLevel.Debug }),
  49. apiOptions: {
  50. adminApiPlayground: true,
  51. shopApiPlayground: true,
  52. },
  53. });
  54. const { server, shopClient, adminClient } = createTestEnvironment(config as any);
  55. await server.init({
  56. initialData,
  57. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  58. customerCount: 1,
  59. });
  60. // Set EUR as currency for Mollie
  61. await adminClient.asSuperAdmin();
  62. await adminClient.query(gql`
  63. mutation {
  64. updateChannel(input: { id: "T_1", currencyCode: EUR }) {
  65. __typename
  66. }
  67. }
  68. `);
  69. // Create method
  70. await adminClient.query<CreatePaymentMethodMutation, CreatePaymentMethodMutationVariables>(
  71. CREATE_PAYMENT_METHOD,
  72. {
  73. input: {
  74. code: 'mollie',
  75. translations: [
  76. {
  77. languageCode: LanguageCode.en,
  78. name: 'Mollie payment test',
  79. description: 'This is a Mollie test payment method',
  80. },
  81. ],
  82. enabled: true,
  83. handler: {
  84. code: molliePaymentHandler.code,
  85. arguments: [
  86. {
  87. name: 'redirectUrl',
  88. value: `${tunnel.url}/admin/orders?filter=open&page=1`,
  89. },
  90. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  91. { name: 'apiKey', value: process.env.MOLLIE_APIKEY! },
  92. { name: 'autoCapture', value: 'false' },
  93. ],
  94. },
  95. },
  96. },
  97. );
  98. // Prepare order with 2 items
  99. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  100. // Add another item to the order
  101. await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(ADD_ITEM_TO_ORDER, {
  102. productVariantId: 'T_4',
  103. quantity: 1,
  104. });
  105. await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(ADD_ITEM_TO_ORDER, {
  106. productVariantId: 'T_5',
  107. quantity: 1,
  108. });
  109. await setShipping(shopClient);
  110. // Create payment intent
  111. // Create payment intent
  112. const { createMolliePaymentIntent } = await shopClient.query(CREATE_MOLLIE_PAYMENT_INTENT, {
  113. input: {
  114. redirectUrl: `${tunnel.url}/admin/orders?filter=open&page=1`,
  115. paymentMethodCode: 'mollie',
  116. // molliePaymentMethodCode: 'klarnapaylater'
  117. },
  118. });
  119. if (createMolliePaymentIntent.errorCode) {
  120. throw createMolliePaymentIntent;
  121. }
  122. // eslint-disable-next-line no-console
  123. console.log('\x1b[41m', `Mollie payment link: ${createMolliePaymentIntent.url as string}`, '\x1b[0m');
  124. // Remove first orderLine
  125. await shopClient.query(ADJUST_ORDER_LINE, {
  126. orderLineId: 'T_1',
  127. quantity: 0,
  128. });
  129. await setShipping(shopClient);
  130. // Create another intent after Xs, should update the mollie order
  131. await new Promise(resolve => setTimeout(resolve, 5000));
  132. const { createMolliePaymentIntent: secondIntent } = await shopClient.query(CREATE_MOLLIE_PAYMENT_INTENT, {
  133. input: {
  134. redirectUrl: `${tunnel.url}/admin/orders?filter=open&page=1&dynamicRedirectUrl=true`,
  135. paymentMethodCode: 'mollie',
  136. },
  137. });
  138. // eslint-disable-next-line no-console
  139. console.log('\x1b[41m', `Second payment link: ${secondIntent.url as string}`, '\x1b[0m');
  140. }
  141. (async () => {
  142. await runMollieDevServer();
  143. })();