mollie-dev-server.ts 4.6 KB

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