条纹
在您的 NestJS 应用程序中与 Streape API 或消费 Streape webhooks 交互, 现在容易成为馅饼 QQ
开始
* :密码组
npm install ---save @golevelup/nestjs-stripe stripe
yarn add @golevelup/nestjs-stripe stripe
pnpm add @golevelup/nestjs-stripe stripe
* :
特征
-
在控制器和提供者中与 Streape API 交互的可注入条纹客户端
-
QQ 可选地曝光您 NestJS 应用程序中的 API 端点, 用于 Strede 的 Webhook 事件处理 。 默认为
/stripe/webhook但可以轻松配置 -
QQ 自动验证事件有效载荷是使用配置的 Webhook 签名秘密从 Strebe 发送的
-
QQ 从您的应用程序中发现提供者
StripeWebhookHandler财务报告和审定财务报表StripeThinWebhookHandler以及前往它们途中的路线 -
QQ 路由事件到逻辑服务, 只需提供 Strepe webhook 事件类型即可
-
* * 支持传统快照事件和Frede的新 v2 瘦事件
导入
导入并添加 StripeModule 页:1 imports 消费单元的一节(最有可能) AppModule) (中文(简体) ). 您的 Frede API 密钥是需要的, 如果您计划在您的应用程序中消费 Frede 网络hook 事件, 您可以选择包含一个 webhook 配置 .
从你的Dashboard上可以得到的 隐藏秘密 Webhoks 设置。选择一个想要获取该秘密的终点,然后单击“签名秘密”下方的启示录链接.
快照事件( 传统 Webhooks ) :
account - 你帐号上的事件 在Frede Dashboard上注册的 Webhook秘密
accountTest - 在Frede Dashboard上注册的用于测试模式中您账户上的事件的 Webhook 秘密
connect - 在Frede Dashboard上注册的关于Connected账户事件的网站库秘密
connectTest - 在Frede Dashboard为测试模式下连接账户上的事件注册的 Webhook 秘密
细事件( V2 Webhooks - 可选) :
stripeThinSecrets - Frede v2 细小事件(仅在使用时需要)的单独秘密 @StripeThinWebhookHandler(中文(简体) )
import { StripeModule } from '@golevelup/nestjs-stripe';
@Module({
imports: [
StripeModule.forRoot({
apiKey: 'sk_***',
webhookConfig: {
// Snapshot event secrets
stripeSecrets: {
account: 'whsec_***',
accountTest: 'whsec_***',
connect: 'whsec_***',
connectTest: 'whsec_***',
},
// Thin event secrets (optional)
stripeThinSecrets: {
account: 'whsec_***',
accountTest: 'whsec_***',
connect: 'whsec_***',
connectTest: 'whsec_***',
},
},
}),
],
})
export class AppModule {
// ...
}
配置
条纹模块支持 forRoot 财务报告和审定财务报表 forRootAsync 配置模式,这样您就可以轻松地从一个 ConfigService 或其他提供者.
可注射供应商
该模块曝光出两个可注射的供应商,并配有配套的装饰来方便您. 这些可以提供给控制器和其他提供者的构造者:
// injects the instantiated Stripe client which can be used to make API calls
@InjectStripeClient() stripeClient: Stripe
// injects the module configuration
@InjectStripeModuleConfig() config: StripeModuleConfig
正在关闭 Webhooks
包含 API 终点
此模块将自动为您的 NestJS 应用程序添加一个新的 API 端点, 用于处理 Webhooks 。 默认情况下,此端点的路由将是 stripe/webhook 但您可以修改此选项, 使用不同的前缀 controllerPrefix 财产的财产 webhookConfig 当导入模块时.
配置原始请求正文处理
如果您希望您的 NestJS 应用程序能够处理收到的webhooks, 就必须让 Stale 能够访问原始请求的有效载荷 .
默认情况下, NestJS 被配置为使用 JSON body 剖析中间软件,在它被 Strede 库验证之前会转换请求.
你可以选择 通过原始请求正文 根据每项请求, 这不会给您现有的建筑设计和其他API带来副作用.
// main.ts
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
rawBody: true,
});
然后手动设置 bodyProperty 用于使用生的Body :
StripeModule.forRoot({
apiKey: 'sk_***',
webhookConfig: {
stripeSecrets: { ... },
requestBodyProperty: 'rawBody', // <-- Set to 'rawBody'
},
});
装饰处理 Webhook 事件的方法
曝光处理花旗事件所使用的提供者/服务方法很简单!. 简单地使用所提供的装饰并显示处理器应接收的事件类型 .
审查花旗文件 需要更多关于现有事件类型的信息.
快照事件( 传统)
使用 @StripeWebhookHandler 用于传统花旗网游活动。 其中包括有效载荷中的全部事件物体.
@Injectable()
class PaymentCreatedService {
@StripeWebhookHandler('payment_intent.created')
handlePaymentIntentCreated(evt: Stripe.PaymentIntentPaymentCreatedEvent) {
// execute your custom business logic
}
}
Webhook 網址 : https://your-domain.com/stripe/webhook
细事件( V2)
使用 @StripeThinWebhookHandler 因为史崔克的五一事件 这些轻量级事件只包括元数据 - 你需要单独获取完整的对象.
@Injectable()
class BillingService {
constructor(@InjectStripeClient() private stripe: Stripe) {}
@StripeThinWebhookHandler('v1.billing.meter.error_report_triggered')
async handleBillingMeterError(
evt: Stripe.Events.V1BillingMeterErrorReportTriggeredEventNotification,
) {
// Thin events require fetching the full object
const meter = await evt.fetchRelatedObject();
// execute your custom business logic
}
}
Webhook 網址 : https://your-domain.com/stripe/webhook?mode=thin
通卡处理器
你用得着 '*' 用于处理特定类型的所有事件:
@Injectable()
class WebhookLogger {
@StripeWebhookHandler('*')
logAllSnapshotEvents(evt: Stripe.Event) {
console.log('Received snapshot event:', evt.type);
}
@StripeThinWebhookHandler('*')
logAllThinEvents(evt: Stripe.V2.Core.Event) {
console.log('Received thin event:', evt.type);
}
}
Webhook 控制器
您也可以通过任何类的装饰器 decorators 财产的财产 webhookConfig 对象作为模块配置的一部分。 这可以用于诸如使用 @nestjs/throttler 软件包和需要应用 @SkipThrottle() 装饰器,或者当您有全局守护,但需要通过某些元数据跳过路径时.
StripeModule.forRoot({
apiKey: 'sk_***',
webhookConfig: {
stripeSecrets: { ... },
decorators: [SkipThrottle()],
},
}),
使用拦截器、警卫和过滤器
这个库是使用一个叫做NestJS的基本概念建造的 External Contexts 允许将方法纳入NestJS生命周期。 这意味着"护卫","截取"和"滤取"(统称为"增强")可以与"花纹"(Strepe webhook)处理器结合使用. 但是,如果使用下列方法,则会产生意外/意外后果: 全球 应用程序中的增强器,因为这些器件也将适用于所有 Strepe webhook 处理器。 如果您先前期望所有上下文都是正则的 HTTP 上下文, 您可能需要添加条件逻辑来阻止您的增强器应用到 Strepe webhook 处理器 .
您可以按其上下文类型识别 Stale webhook 上下文, 'stripe_webhook'数字 :
import { STRIPE_WEBHOOK_CONTEXT_TYPE } from '@golevelup/nestjs-stripe';
@Injectable()
class ExampleInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>) {
const contextType = context.getType<
'http' | typeof STRIPE_WEBHOOK_CONTEXT_TYPE
>();
// Do nothing if this is a Stripe webhook event
if (contextType === STRIPE_WEBHOOK_CONTEXT_TYPE) {
return next.handle();
}
// Execute custom interceptor logic for HTTP request/response
return next.handle();
}
}
在条形板上配置 Webhooks
遵循来自 条形文档 用于在您启动前测试您与 CLI 的整合等剩余整合步骤, 并正确配置 Frede 仪表板的端点, 以便将正确的事件发送到您的 NestJS 应用程序 .