stripe-service-export-test.plugin.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Args, Mutation, Resolver } from '@nestjs/graphql';
  2. import {
  3. Allow,
  4. Ctx,
  5. OrderService,
  6. Permission,
  7. PluginCommonModule,
  8. RequestContext,
  9. RequestContextService,
  10. VendurePlugin,
  11. } from '@vendure/core';
  12. import gql from 'graphql-tag';
  13. import { StripePlugin } from '../../src/stripe';
  14. import { StripeService } from '../../src/stripe/stripe.service';
  15. @Resolver()
  16. export class CustomStripeResolver {
  17. constructor(
  18. private stripeService: StripeService,
  19. private orderService: OrderService,
  20. private requestContextService: RequestContextService,
  21. ) {}
  22. @Mutation()
  23. @Allow(Permission.Owner)
  24. async createCustomStripePaymentIntent(
  25. @Ctx() ctx: RequestContext,
  26. @Args() args: { orderCode: string; channelToken: string },
  27. ): Promise<string> {
  28. // By the orderCode we find the order where we assume additional payments are required.
  29. // If the order is not in the request's channel context, we can use other means to lookup the order.
  30. const order = await this.orderService.findOneByCode(ctx, args.orderCode);
  31. // The stripe webhook handler expects: channelToken, orderId, orderCode and languageCode
  32. // We can hijack those details, to support cross-channel payments and additional "non-active" order payments
  33. const customCtx = await this.requestContextService.create({
  34. apiType: 'shop',
  35. channelOrToken: args.channelToken,
  36. req: ctx.req,
  37. });
  38. if (!order) {
  39. throw new Error('No order');
  40. }
  41. return this.stripeService.createPaymentIntent(customCtx, order);
  42. }
  43. }
  44. @VendurePlugin({
  45. imports: [PluginCommonModule, StripePlugin],
  46. shopApiExtensions: {
  47. schema: gql`
  48. extend type Mutation {
  49. createCustomStripePaymentIntent(orderCode: String, channelToken: String): String!
  50. }
  51. `,
  52. resolvers: [CustomStripeResolver],
  53. },
  54. })
  55. export class StripeServiceExportTestPlugin {}