stripe java实现,如何在Java中从Stripe接收Webhook

博客讲述了在Java中处理Stripe Webhook时遇到的问题,即通过`@RequestBody`注解接收Stripe Event参数导致400错误。解决方案是将接收到的JSON字符串转换为Stripe Event对象,使用Stripe提供的Gson适配器完成转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am trying to receive a webhook via a post request from Stripe Payments. The java method to process it looks like this:

@ResponseBody

@RequestMapping( consumes="application/json",

produces="application/json",

method=RequestMethod.POST,

value="stripeWebhookEndpoint")

public String stripeWebhookEndpoint(Event event){

logger.info("\n\n" + event.toString());

logger.info("\n\n" + event.getId());

return null;

}

But the Stripe Event always comes back with all null values:

JSON: {

"id": null,

"type": null,

"user_id": null,

"livemode": null,

"created": null,

"data": null,

"pending_webhooks": null

}

If the method receives a String instead,and using @RequestBody:

@ResponseBody

@RequestMapping( consumes="application/json",

produces="application/json",

method=RequestMethod.POST,

value="stripeWebhookEndpoint")

public String stripeWebhookEndpoint(@RequestBody String json){

logger.info(json);

return null;

}

Here, it prints the json without null values. Here's part of the request being printed:

{

"created": 1326853478,

"livemode": false,

"id": "evt_00000000000000",

"type": "charge.succeeded",

"object": "event",

"request": null,

"data": {

"object": {

"id": "ch_00000000000000",

"object": "charge",

"created": 1389985862,

"livemode": false,

"paid": true,

"amount": 2995,

"currency": "usd",

...

}

But using @RequestBody with a Stripe Event parameter gives a 400: bad syntax.

So why can't I take in the correct type, a Stripe Event, as the parameter?

解决方案

Here's what I did:

The Java method still takes in the Event as a json String. Then I used Stripe's custom gson adapter and got the Event with:

Event event = Event.gson.fromJson(stripeJsonEvent, Event.class);

Where stripeJsonEvent is the string of json taken in by the webhook endpoint.

### 如何在 Android 应用中集成 Stripe 支付 SDK #### 添加依赖项 为了使用 Stripe 的支付功能,在项目的 `build.gradle` 文件中的 dependencies 部分添加 Stripe SDK 作为依赖项[^1]。 ```gradle dependencies { implementation 'com.stripe:stripe-android:<latest_version>' } ``` 请注意替换 `<latest_version>` 为最新版本号,可以访问 [Stripe Android SDK](https://gitcode.com/gh_mirrors/st/stripe-android) 获取最新的发布信息。 #### 更新 AndroidManifest.xml 确保应用程序的 `AndroidManifest.xml` 中包含了必要的元数据条目以支持 Google Pay 功能[^3]: ```xml <application> ... <meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true"/> </application> ``` #### 初始化并配置 Stripe 客户端 初始化 Stripe 对象实例,并设置 publishable key。通常情况下,publishable key 是从服务器获取的安全方式传递给移动应用[^2]。 ```java import com.stripe.Stripe; // 在适当的位置调用此方法,例如 Application 类或 Activity 的 onCreate 方法内 public void initializeStripe(String publishableKey) { Stripe stripe = new Stripe( getApplicationContext(), PublishableKey.create(publishableKey) ); } ``` #### 创建 PaymentIntent 或 SetupIntent 对于一次性付款操作,应该先创建一个 `PaymentIntent`;而对于保存信用卡以便将来扣款,则应创建 `SetupIntent`。这些 Intent 可以通过后端服务生成,并返回前端所需的 client_secret 参数用于后续处理[^4]。 #### 处理卡片输入与验证 利用 Stripe 提供的 UI 组件简化收集用户卡信息的过程。下面是一个简单的例子展示如何启动 CardInputWidget 来让用户填写他们的银行卡详情。 ```java CardInputWidget cardInputWidget; Button payButton; payButton.setOnClickListener(v -> { PaymentMethodCreateParams params = cardInputWidget.getPaymentMethodCreateParams(); if (params != null) { // 将参数发送到您的服务器或者直接确认 PaymentIntent } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值