Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payments-plugin): Allow use of Stripe-Account header #3193

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion license/signatures/version1/cla.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@
"created_at": "2024-10-31T08:42:52Z",
"repoId": 136938012,
"pullRequestNo": 3174
},
{
"name": "kkerti",
"id": 47832952,
"comment_id": 2458191015,
"created_at": "2024-11-05T21:33:05Z",
"repoId": 136938012,
"pullRequestNo": 3187
},
{
"name": "shingoaoyama1",
"id": 17615101,
"comment_id": 2459213307,
"created_at": "2024-11-06T10:15:37Z",
"repoId": 136938012,
"pullRequestNo": 3192
}
]
}
}
40 changes: 40 additions & 0 deletions packages/payments-plugin/e2e/stripe-payment.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,46 @@ describe('Stripe payments', () => {
StripePlugin.options.paymentIntentCreateParams = undefined;
});

// https://github.com/vendure-ecommerce/vendure/issues/3183
it('should attach additional options to payment intent using requestOptions', async () => {
StripePlugin.options.requestOptions = async (injector, ctx, currentOrder) => {
return {
stripeAccount: 'acct_connected',
};
};
let connectedAccountHeader: any;
let createPaymentIntentPayload: any;
const { activeOrder } = await shopClient.query<GetActiveOrderQuery>(GET_ACTIVE_ORDER);
nock('https://api.stripe.com/', {
reqheaders: {
'Stripe-Account': headerValue => {
connectedAccountHeader = headerValue;
return true;
},
},
})
.post('/v1/payment_intents', body => {
createPaymentIntentPayload = body;
return true;
})
.reply(200, {
client_secret: 'test-client-secret',
});
const { createStripePaymentIntent } = await shopClient.query(CREATE_STRIPE_PAYMENT_INTENT);
expect(createPaymentIntentPayload).toEqual({
amount: activeOrder?.totalWithTax.toString(),
currency: activeOrder?.currencyCode?.toLowerCase(),
customer: 'new-customer-id',
'automatic_payment_methods[enabled]': 'true',
'metadata[channelToken]': E2E_DEFAULT_CHANNEL_TOKEN,
'metadata[orderId]': '1',
'metadata[orderCode]': activeOrder?.code,
});
expect(connectedAccountHeader).toEqual('acct_connected');
expect(createStripePaymentIntent).toEqual('test-client-secret');
StripePlugin.options.paymentIntentCreateParams = undefined;
});

// https://github.com/vendure-ecommerce/vendure/issues/2412
it('should attach additional params to customer using customerCreateParams', async () => {
StripePlugin.options.customerCreateParams = async (injector, ctx, currentOrder) => {
Expand Down
10 changes: 9 additions & 1 deletion packages/payments-plugin/src/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export class StripeService {
ctx,
order,
);
const additionalOptions = await this.options.requestOptions?.(
new Injector(this.moduleRef),
ctx,
order,
);
const metadata = sanitizeMetadata({
...(typeof this.options.metadata === 'function'
? await this.options.metadata(new Injector(this.moduleRef), ctx, order)
Expand All @@ -69,7 +74,10 @@ export class StripeService {
...(additionalParams ?? {}),
metadata: allMetadata,
},
{ idempotencyKey: `${order.code}_${amountInMinorUnits}` },
{
idempotencyKey: `${order.code}_${amountInMinorUnits}`,
...(additionalOptions ?? {}),
},
);

if (!client_secret) {
Expand Down
37 changes: 37 additions & 0 deletions packages/payments-plugin/src/stripe/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type AdditionalPaymentIntentCreateParams = Partial<
Omit<Stripe.PaymentIntentCreateParams, 'amount' | 'currency' | 'customer'>
>;

type AdditionalRequestOptions = Partial<Omit<Stripe.RequestOptions, 'idempotencyKey'>>;

type AdditionalCustomerCreateParams = Partial<Omit<Stripe.CustomerCreateParams, 'email'>>;

/**
Expand Down Expand Up @@ -107,6 +109,41 @@ export interface StripePluginOptions {
order: Order,
) => AdditionalPaymentIntentCreateParams | Promise<AdditionalPaymentIntentCreateParams>;

/**
* @description
* Provide additional options to the Stripe payment intent creation. By default,
* the plugin will already pass the `idempotencyKey` parameter.
*
* For example, if you want to provide a `stripeAccount` for the payment intent, you can do so like this:
*
* @example
* ```ts
* import { VendureConfig } from '\@vendure/core';
* import { StripePlugin } from '\@vendure/payments-plugin/package/stripe';
*
* export const config: VendureConfig = {
* // ...
* plugins: [
* StripePlugin.init({
* requestOptions: (injector, ctx, order) => {
* return {
* stripeAccount: ctx.channel.seller?.customFields.connectedAccountId
* },
* }
* }),
* ],
* };
* ```
*
* @since 3.0.6
*
*/
requestOptions?: (
injector: Injector,
ctx: RequestContext,
order: Order,
) => AdditionalRequestOptions | Promise<AdditionalRequestOptions>;

/**
* @description
* Provide additional parameters to the Stripe customer creation. By default,
Expand Down
Loading