|
@@ -1,8 +1,10 @@
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
+import { ModuleRef } from '@nestjs/core';
|
|
|
import { ConfigArg } from '@vendure/common/lib/generated-types';
|
|
import { ConfigArg } from '@vendure/common/lib/generated-types';
|
|
|
import {
|
|
import {
|
|
|
Ctx,
|
|
Ctx,
|
|
|
Customer,
|
|
Customer,
|
|
|
|
|
+ Injector,
|
|
|
Logger,
|
|
Logger,
|
|
|
Order,
|
|
Order,
|
|
|
Payment,
|
|
Payment,
|
|
@@ -14,6 +16,7 @@ import {
|
|
|
import Stripe from 'stripe';
|
|
import Stripe from 'stripe';
|
|
|
|
|
|
|
|
import { loggerCtx, STRIPE_PLUGIN_OPTIONS } from './constants';
|
|
import { loggerCtx, STRIPE_PLUGIN_OPTIONS } from './constants';
|
|
|
|
|
+import { sanitizeMetadata } from './metadata-sanitize';
|
|
|
import { VendureStripeClient } from './stripe-client';
|
|
import { VendureStripeClient } from './stripe-client';
|
|
|
import { getAmountInStripeMinorUnits } from './stripe-utils';
|
|
import { getAmountInStripeMinorUnits } from './stripe-utils';
|
|
|
import { stripePaymentMethodHandler } from './stripe.handler';
|
|
import { stripePaymentMethodHandler } from './stripe.handler';
|
|
@@ -25,6 +28,7 @@ export class StripeService {
|
|
|
private connection: TransactionalConnection,
|
|
private connection: TransactionalConnection,
|
|
|
private paymentMethodService: PaymentMethodService,
|
|
private paymentMethodService: PaymentMethodService,
|
|
|
@Inject(STRIPE_PLUGIN_OPTIONS) private options: StripePluginOptions,
|
|
@Inject(STRIPE_PLUGIN_OPTIONS) private options: StripePluginOptions,
|
|
|
|
|
+ private moduleRef: ModuleRef,
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
async createPaymentIntent(ctx: RequestContext, order: Order): Promise<string> {
|
|
async createPaymentIntent(ctx: RequestContext, order: Order): Promise<string> {
|
|
@@ -35,6 +39,16 @@ export class StripeService {
|
|
|
customerId = await this.getStripeCustomerId(ctx, order);
|
|
customerId = await this.getStripeCustomerId(ctx, order);
|
|
|
}
|
|
}
|
|
|
const amountInMinorUnits = getAmountInStripeMinorUnits(order);
|
|
const amountInMinorUnits = getAmountInStripeMinorUnits(order);
|
|
|
|
|
+
|
|
|
|
|
+ const metadata = sanitizeMetadata({
|
|
|
|
|
+ ...(typeof this.options.metadata === 'function'
|
|
|
|
|
+ ? await this.options.metadata(new Injector(this.moduleRef), ctx, order)
|
|
|
|
|
+ : {}),
|
|
|
|
|
+ channelToken: ctx.channel.token,
|
|
|
|
|
+ orderId: order.id,
|
|
|
|
|
+ orderCode: order.code,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
const { client_secret } = await stripe.paymentIntents.create(
|
|
const { client_secret } = await stripe.paymentIntents.create(
|
|
|
{
|
|
{
|
|
|
amount: amountInMinorUnits,
|
|
amount: amountInMinorUnits,
|
|
@@ -43,11 +57,7 @@ export class StripeService {
|
|
|
automatic_payment_methods: {
|
|
automatic_payment_methods: {
|
|
|
enabled: true,
|
|
enabled: true,
|
|
|
},
|
|
},
|
|
|
- metadata: {
|
|
|
|
|
- channelToken: ctx.channel.token,
|
|
|
|
|
- orderId: order.id,
|
|
|
|
|
- orderCode: order.code,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ metadata,
|
|
|
},
|
|
},
|
|
|
{ idempotencyKey: `${order.code}_${amountInMinorUnits}` },
|
|
{ idempotencyKey: `${order.code}_${amountInMinorUnits}` },
|
|
|
);
|
|
);
|