mollie-dev-server.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 { CreatePaymentMethod } from './graphql/generated-admin-types';
  22. import { AddItemToOrder } from './graphql/generated-shop-types';
  23. import { ADD_ITEM_TO_ORDER } from './graphql/shop-queries';
  24. import { CREATE_MOLLIE_PAYMENT_INTENT, setShipping } from './payment-helpers';
  25. /**
  26. * This should only be used to locally test the Mollie payment plugin
  27. */
  28. /* tslint:disable:no-floating-promises */
  29. (async () => {
  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<CreatePaymentMethod.Mutation,
  66. CreatePaymentMethod.Variables>(CREATE_PAYMENT_METHOD, {
  67. input: {
  68. code: 'mollie',
  69. name: 'Mollie payment test',
  70. description: 'This is a Mollie test payment method',
  71. enabled: true,
  72. handler: {
  73. code: molliePaymentHandler.code,
  74. arguments: [
  75. { name: 'redirectUrl', value: `${tunnel.url}/admin/orders?filter=open&page=1` },
  76. // tslint:disable-next-line:no-non-null-assertion
  77. { name: 'apiKey', value: process.env.MOLLIE_APIKEY! },
  78. { name: 'autoCapture', value: 'false' },
  79. ],
  80. },
  81. },
  82. });
  83. // Prepare order for payment
  84. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  85. await shopClient.query<AddItemToOrder.Order, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
  86. productVariantId: 'T_5',
  87. quantity: 10,
  88. });
  89. const ctx = new RequestContext({
  90. apiType: 'admin',
  91. isAuthorized: true,
  92. authorizedAsOwnerOnly: false,
  93. channel: await server.app.get(ChannelService).getDefaultChannel()
  94. });
  95. await server.app.get(OrderService).addSurchargeToOrder(ctx, 1, {
  96. description: 'Negative test surcharge',
  97. listPrice: -20000,
  98. });
  99. await setShipping(shopClient);
  100. // Add pre payment to order
  101. const order = await server.app.get(OrderService).findOne(ctx, 1);
  102. // tslint:disable-next-line:no-non-null-assertion
  103. await server.app.get(PaymentService).createManualPayment(ctx, order!, 10000 ,{
  104. method: 'Manual',
  105. // tslint:disable-next-line:no-non-null-assertion
  106. orderId: order!.id,
  107. metadata: {
  108. bogus: 'test'
  109. }
  110. });
  111. const { createMolliePaymentIntent } = await shopClient.query(CREATE_MOLLIE_PAYMENT_INTENT, {
  112. input: {
  113. paymentMethodCode: 'mollie',
  114. // molliePaymentMethodCode: 'klarnapaylater'
  115. },
  116. });
  117. if (createMolliePaymentIntent.errorCode) {
  118. throw createMolliePaymentIntent;
  119. }
  120. Logger.info(`Mollie payment link: ${createMolliePaymentIntent.url}`, 'Mollie DevServer');
  121. })();