mollie-dev-server.ts 4.4 KB

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