首页 > 解决方案 > 如何使 Stripe 的 WebhookEndpoint.create() 线程安全

问题描述

Stripe 关于如何创建 webhook 的示例并不安全,它使用静态变量来保存 apiKey。有谁知道如何通过 builder() 方法将密钥传递给 Stripe?他们的 RequestOptionsBuilder 类具有 setApiKey() 方法,我找不到与 webhook 类似的东西。

使用 API 添加端点

// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";

WebhookEndpointCreateParams params =
  WebhookEndpointCreateParams.builder()
    .setUrl("https://example.com/my/webhook/endpoint")
    .addAllEnabledEvent(Arrays.asList(
      WebhookEndpointCreateParams.EnabledEvent.CHARGE__FAILED,
      WebhookEndpointCreateParams.EnabledEvent.CHARGE__SUCCEEDED))
    .build();

WebhookEndpoint endpoint = WebhookEndpoint.create(params);

标签: javastripe-payments

解决方案


按请求配置

有一个重载的方法WebhookEndpoint.create(PaymentIntentCreateParams createParams, RequestOptions requestOptions)。RequestOptions 构建器允许您为每个请求设置各种值,包括私钥。我将其发布为答案,因为在搜索了 Stripe 网站数周后,在他们提出这个建议之前,他们花了几封电子邮件支持 Stripe;所以即使在 Stripe 也不是常识。


推荐阅读