mollie-dev-server.ts 5.1 KB

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