mollie-dev-server.ts 4.6 KB

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