Skip to content

Commit

Permalink
Merge pull request #1900 from stripe/latest-codegen-beta
Browse files Browse the repository at this point in the history
Update generated code for beta
  • Loading branch information
stripe-openapi[bot] authored Sep 15, 2023
2 parents 8b225e8 + c235c44 commit 0ccb783
Show file tree
Hide file tree
Showing 48 changed files with 4,028 additions and 88 deletions.
1 change: 0 additions & 1 deletion API_VERSION

This file was deleted.

2 changes: 1 addition & 1 deletion OPENAPI_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v515
v532
72 changes: 72 additions & 0 deletions examples/webhook-signing/nestjs/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// create payment controller

import {
Controller,
Post,
Headers,
Get,
RawBodyRequest,
Req,
Res,
Injectable,
Inject,
} from '@nestjs/common';
import {Request, Response} from 'express';
import Stripe from 'stripe';
import {ConfigService} from '@nestjs/config';

@Controller()
export class AppController {
private readonly client: Stripe;
constructor(@Inject(ConfigService) private readonly config: ConfigService) {
this.client = new Stripe(this.config.get('Stripe.secret_key'), {
apiVersion: '2022-11-15',
typescript: true,
});
}

@Get('/')
async index(): Promise<string> {
return 'ok';
}

@Post('/webhooks')
async webhooks(
@Headers('stripe-signature') sig: string,
@Req() req: RawBodyRequest<Request>,
@Res() res: Response
) {
let event: Stripe.Event;

try {
event = this.client.webhooks.constructEvent(
req.rawBody,
sig,
this.config.get('Stripe.webhook_secret')
);
} catch (err) {
// On error, log and return the error message
console.log(`❌ Error message: ${err.message}`);
res.status(400).send(`Webhook Error: ${err.message}`);
return;
}

// Successfully constructed event
console.log('✅ Success:', event.id);

// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
const stripeObject: Stripe.PaymentIntent = event.data
.object as Stripe.PaymentIntent;
console.log(`💰 PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
const charge = event.data.object as Stripe.Charge;
console.log(`💵 Charge id: ${charge.id}`);
} else {
console.warn(`🤷‍♀️ Unhandled event type: ${event.type}`);
}

// Return a response to acknowledge receipt of the event
res.status(200).json({received: true});
}
}
16 changes: 16 additions & 0 deletions examples/webhook-signing/nestjs/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Module} from '@nestjs/common';
import {ConfigModule} from '@nestjs/config';
import {config} from './config';
import {AppController} from './app.controller';

@Module({
controllers: [AppController],
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [config],
envFilePath: `.env`,
}),
],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions examples/webhook-signing/nestjs/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Config = {
Stripe: {
secret_key: string;
webhook_secret: string;
};
};

export const config = (): Config => ({
Stripe: {
secret_key: process.env.STRIPE_SECRET_KEY || '',
webhook_secret: process.env.STRIPE_WEBHOOK_SECRET || '',
},
});
18 changes: 18 additions & 0 deletions examples/webhook-signing/nestjs/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env -S npm run-script run

import {NestFactory} from '@nestjs/core';
import {INestApplication} from '@nestjs/common';
import {AppModule} from './app.module';

async function bootstrap() {
const app = await NestFactory.create<INestApplication>(AppModule, {
rawBody: true,
});
app.enableCors({
origin: '*',
});

await app.listen(0);
console.log(`Webhook endpoint available at ${await app.getUrl()}/webhooks`);
}
bootstrap();
30 changes: 30 additions & 0 deletions examples/webhook-signing/nestjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "webhook-signing-example-nestjs",
"version": "1.0.0",
"description": "Nestjs webhook parsing sample",
"repository": {},
"main": "./main.ts",
"scripts": {
"run": "ts-node-transpile-only ./main.ts",
"prepare": "../prepare.sh"
},
"author": "Ali karimi",
"license": "ISC",
"dependencies": {
"@nestjs/common": "^10.2.1",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.2.1",
"dotenv": "^16.3.1",
"@nestjs/platform-express": "^10.2.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"stripe": "^12.18.0"
},
"devDependencies": {
"eslint": "^8.33.0",
"@types/node": "^20.5.4",
"@types/express": "^4.17.17",
"typescript": "^5.2.2",
"ts-node": "^10.9.1"
}
}
4 changes: 3 additions & 1 deletion examples/webhook-signing/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/* Advanced Options */
/* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,

"experimentalDecorators": true,
}
}
10 changes: 9 additions & 1 deletion scripts/updateAPIVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ const edit = (file, cb) => write(file, cb(read(file)));
const API_VERSION = '2[0-9][2-9][0-9]-[0-9]{2}-[0-9]{2}';

const main = () => {
const apiVersion = read('API_VERSION').trim();
const matches = [
...read('src/apiVersion.ts').matchAll(/ApiVersion . '([^']*)'/g),
];
if (matches.length !== 1) {
throw new Error(
`Expected src/apiVersion.ts to include 1 match for ApiVersion = '...' but found ${matches.length}`
);
}
const apiVersion = matches[0][1];

const replaceAPIVersion = (file, pattern) =>
edit(file, (text) => {
Expand Down
5 changes: 5 additions & 0 deletions src/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {resourceNamespace} from './ResourceNamespace.js';
import {Accounts as FinancialConnectionsAccounts} from './resources/FinancialConnections/Accounts.js';
import {Authorizations as TestHelpersIssuingAuthorizations} from './resources/TestHelpers/Issuing/Authorizations.js';
import {Authorizations as IssuingAuthorizations} from './resources/Issuing/Authorizations.js';
import {Calculations as TaxCalculations} from './resources/Tax/Calculations.js';
import {CardBundles as IssuingCardBundles} from './resources/Issuing/CardBundles.js';
Expand Down Expand Up @@ -49,6 +50,7 @@ import {Sessions as FinancialConnectionsSessions} from './resources/FinancialCon
import {Settings as TaxSettings} from './resources/Tax/Settings.js';
import {TestClocks as TestHelpersTestClocks} from './resources/TestHelpers/TestClocks.js';
import {TransactionEntries as TreasuryTransactionEntries} from './resources/Treasury/TransactionEntries.js';
import {Transactions as TestHelpersIssuingTransactions} from './resources/TestHelpers/Issuing/Transactions.js';
import {Transactions as FinancialConnectionsTransactions} from './resources/FinancialConnections/Transactions.js';
import {Transactions as GiftCardsTransactions} from './resources/GiftCards/Transactions.js';
import {Transactions as IssuingTransactions} from './resources/Issuing/Transactions.js';
Expand All @@ -67,6 +69,7 @@ export {ApplicationFees} from './resources/ApplicationFees.js';
export {Balance} from './resources/Balance.js';
export {BalanceTransactions} from './resources/BalanceTransactions.js';
export {Charges} from './resources/Charges.js';
export {ConfirmationTokens} from './resources/ConfirmationTokens.js';
export {CountrySpecs} from './resources/CountrySpecs.js';
export {Coupons} from './resources/Coupons.js';
export {CreditNotes} from './resources/CreditNotes.js';
Expand Down Expand Up @@ -175,8 +178,10 @@ export const TestHelpers = resourceNamespace('testHelpers', {
Refunds: TestHelpersRefunds,
TestClocks: TestHelpersTestClocks,
Issuing: resourceNamespace('issuing', {
Authorizations: TestHelpersIssuingAuthorizations,
CardDesigns: TestHelpersIssuingCardDesigns,
Cards: TestHelpersIssuingCards,
Transactions: TestHelpersIssuingTransactions,
}),
Terminal: resourceNamespace('terminal', {
Readers: TestHelpersTerminalReaders,
Expand Down
10 changes: 10 additions & 0 deletions src/resources/ConfirmationTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// File generated from our OpenAPI spec

import {StripeResource} from '../StripeResource.js';
const stripeMethod = StripeResource.method;
export const ConfirmationTokens = StripeResource.extend({
retrieve: stripeMethod({
method: 'GET',
fullPath: '/v1/confirmation_tokens/{confirmation_token}',
}),
});
1 change: 1 addition & 0 deletions src/resources/Issuing/CardDesigns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {StripeResource} from '../../StripeResource.js';
const stripeMethod = StripeResource.method;
export const CardDesigns = StripeResource.extend({
create: stripeMethod({method: 'POST', fullPath: '/v1/issuing/card_designs'}),
retrieve: stripeMethod({
method: 'GET',
fullPath: '/v1/issuing/card_designs/{card_design}',
Expand Down
27 changes: 27 additions & 0 deletions src/resources/TestHelpers/Issuing/Authorizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// File generated from our OpenAPI spec

import {StripeResource} from '../../../StripeResource.js';
const stripeMethod = StripeResource.method;
export const Authorizations = StripeResource.extend({
create: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/authorizations',
}),
capture: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/authorizations/{authorization}/capture',
}),
expire: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/authorizations/{authorization}/expire',
}),
increment: stripeMethod({
method: 'POST',
fullPath:
'/v1/test_helpers/issuing/authorizations/{authorization}/increment',
}),
reverse: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/authorizations/{authorization}/reverse',
}),
});
5 changes: 5 additions & 0 deletions src/resources/TestHelpers/Issuing/CardDesigns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export const CardDesigns = StripeResource.extend({
fullPath:
'/v1/test_helpers/issuing/card_designs/{card_design}/status/deactivate',
}),
rejectTestmode: stripeMethod({
method: 'POST',
fullPath:
'/v1/test_helpers/issuing/card_designs/{card_design}/status/reject',
}),
});
18 changes: 18 additions & 0 deletions src/resources/TestHelpers/Issuing/Transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// File generated from our OpenAPI spec

import {StripeResource} from '../../../StripeResource.js';
const stripeMethod = StripeResource.method;
export const Transactions = StripeResource.extend({
createForceCapture: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/transactions/create_force_capture',
}),
createUnlinkedRefund: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/transactions/create_unlinked_refund',
}),
refund: stripeMethod({
method: 'POST',
fullPath: '/v1/test_helpers/issuing/transactions/{transaction}/refund',
}),
});
9 changes: 9 additions & 0 deletions test/Integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,13 @@ describe('Integration test', function() {
});

it('Webhook sample deno', () => runWebhookTest('deno'));

it('Webhook sample nestjs', function() {
// Next.js supports Node.js >=16
if (nodeVersion < 16) {
this.skip();
}

runWebhookTest('nestjs');
});
});
Loading

0 comments on commit 0ccb783

Please sign in to comment.