|
@@ -1,8 +1,10 @@
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
-import { Customer, Logger, Order, RequestContext, TransactionalConnection } from '@vendure/core';
|
|
|
|
|
|
|
+import { ModuleRef } from '@nestjs/core';
|
|
|
|
|
+import { Customer, Injector, Logger, Order, RequestContext, TransactionalConnection } from '@vendure/core';
|
|
|
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 { getAmountInStripeMinorUnits } from './stripe-utils';
|
|
import { getAmountInStripeMinorUnits } from './stripe-utils';
|
|
|
import { StripePluginOptions } from './types';
|
|
import { StripePluginOptions } from './types';
|
|
|
|
|
|
|
@@ -13,6 +15,7 @@ export class StripeService {
|
|
|
constructor(
|
|
constructor(
|
|
|
private connection: TransactionalConnection,
|
|
private connection: TransactionalConnection,
|
|
|
@Inject(STRIPE_PLUGIN_OPTIONS) private options: StripePluginOptions,
|
|
@Inject(STRIPE_PLUGIN_OPTIONS) private options: StripePluginOptions,
|
|
|
|
|
+ private moduleRef: ModuleRef,
|
|
|
) {
|
|
) {
|
|
|
this.stripe = new Stripe(this.options.apiKey, {
|
|
this.stripe = new Stripe(this.options.apiKey, {
|
|
|
apiVersion: '2020-08-27',
|
|
apiVersion: '2020-08-27',
|
|
@@ -26,6 +29,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 this.stripe.paymentIntents.create(
|
|
const { client_secret } = await this.stripe.paymentIntents.create(
|
|
|
{
|
|
{
|
|
|
amount: amountInMinorUnits,
|
|
amount: amountInMinorUnits,
|
|
@@ -34,11 +47,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}` },
|
|
|
);
|
|
);
|