1
0

stripe-dev-server.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  2. import {
  3. ChannelService,
  4. DefaultLogger,
  5. LanguageCode,
  6. Logger,
  7. LogLevel,
  8. mergeConfig,
  9. OrderService,
  10. RequestContext,
  11. } from '@vendure/core';
  12. import { createTestEnvironment, registerInitializer, SqljsInitializer, testConfig } from '@vendure/testing';
  13. import path from 'path';
  14. import { initialData } from '../../../e2e-common/e2e-initial-data';
  15. import { StripePlugin } from '../src/stripe';
  16. import { stripePaymentMethodHandler } from '../src/stripe/stripe.handler';
  17. /* eslint-disable */
  18. import { StripeCheckoutTestPlugin } from './fixtures/stripe-checkout-test.plugin';
  19. import { StripeServiceExportTestPlugin } from './fixtures/stripe-service-export-test.plugin';
  20. import { CREATE_PAYMENT_METHOD } from './graphql/admin-queries';
  21. import {
  22. CreatePaymentMethodMutation,
  23. CreatePaymentMethodMutationVariables,
  24. } from './graphql/generated-admin-types';
  25. import { AddItemToOrderMutation, AddItemToOrderMutationVariables } from './graphql/generated-shop-types';
  26. import { ADD_ITEM_TO_ORDER } from './graphql/shop-queries';
  27. import {
  28. CREATE_CUSTOM_STRIPE_PAYMENT_INTENT,
  29. CREATE_STRIPE_PAYMENT_INTENT,
  30. setShipping,
  31. } from './payment-helpers';
  32. export let clientSecret: string;
  33. /**
  34. * The actual starting of the dev server
  35. */
  36. (async () => {
  37. require('dotenv').config();
  38. registerInitializer('sqljs', new SqljsInitializer(path.join(__dirname, '__data__')));
  39. const config = mergeConfig(testConfig, {
  40. plugins: [
  41. ...testConfig.plugins,
  42. AdminUiPlugin.init({
  43. route: 'admin',
  44. port: 5001,
  45. }),
  46. StripePlugin.init({}),
  47. StripeCheckoutTestPlugin,
  48. StripeServiceExportTestPlugin,
  49. ],
  50. logger: new DefaultLogger({ level: LogLevel.Debug }),
  51. });
  52. const { server, shopClient, adminClient } = createTestEnvironment(config as any);
  53. await server.init({
  54. initialData,
  55. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  56. customerCount: 1,
  57. });
  58. // Create method
  59. await adminClient.asSuperAdmin();
  60. await adminClient.query<CreatePaymentMethodMutation, CreatePaymentMethodMutationVariables>(
  61. CREATE_PAYMENT_METHOD,
  62. {
  63. input: {
  64. code: 'stripe-payment-method',
  65. enabled: true,
  66. translations: [
  67. {
  68. name: 'Stripe',
  69. description: 'This is a Stripe test payment method',
  70. languageCode: LanguageCode.en,
  71. },
  72. ],
  73. handler: {
  74. code: stripePaymentMethodHandler.code,
  75. arguments: [
  76. { name: 'apiKey', value: process.env.STRIPE_APIKEY! },
  77. { name: 'webhookSecret', value: process.env.STRIPE_WEBHOOK_SECRET! },
  78. ],
  79. },
  80. },
  81. },
  82. );
  83. // Prepare order for payment
  84. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  85. await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(ADD_ITEM_TO_ORDER, {
  86. productVariantId: 'T_1',
  87. quantity: 1,
  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. const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
  101. clientSecret = createStripePaymentIntent;
  102. // Showcasing the custom intent creation
  103. const { createCustomStripePaymentIntent } = await shopClient.query(CREATE_CUSTOM_STRIPE_PAYMENT_INTENT);
  104. Logger.debug('Result of createCustomStripePaymentIntent:', createCustomStripePaymentIntent);
  105. Logger.info('http://localhost:3050/checkout', 'Stripe DevServer');
  106. })();