Stripe 事件:如何通过 Stripe 事件和 webhook 捕获成功支付的产品? [英] Stripe events: How to capture the product that was successfully paid with Stripe events and a webhook?

查看:84
本文介绍了Stripe 事件:如何通过 Stripe 事件和 webhook 捕获成功支付的产品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我网站的用户将使用 Stripe 的单次付款工作流程和结账会话购买积分.他们可以以折扣价购买单个积分或积分套餐,例如 10 个积分套餐.成功付款后,我想使用 webhook 捕获事件并根据用户支付的产品和数量更新用户余额.

My website's users will buy credits using Stripe's single-payment workflow with a checkout session. They can buy single credits or credit packages, like 10 credits package at discount price. After a successfully payment I want to capture the event with a webhook and update the user balance according to the product and quantity paid by the user.

我目前正在跟踪 payment_intent.succeeded 事件,我可以看到付款和费用信息,但是我找不到与所订购产品相关的任何信息.我错过了什么?谢谢.

I'm currently tracking payment_intent.succeeded events and I can see payment and charge information however I can't find any information related to the product ordered. What am I missing? Thank you.

这是我正在捕获的付款意图事件的示例:

This is an example of the payment intent event I'm capturing:

{
  "amount": 5250,
  "amount_capturable": 0,
  "amount_received": 5250,
  "application": null,
  "application_fee_amount": null,
  "canceled_at": null,
  "cancellation_reason": null,
  "capture_method": "automatic",
  "charges": {
    "data": [
      {
        "amount": 5250,
        "amount_captured": 5250,
        "amount_refunded": 0,
        "application": null,
        "application_fee": null,
        "application_fee_amount": null,
        "balance_transaction": "txn_1HUSonCfZ37XLQD8mLERR2YR",
        "billing_details": {
          "address": {
            "city": null,
            "country": "JP",
            "line1": null,
            "line2": null,
            "postal_code": null,
            "state": null
          },
          "email": "xxxx.xxxx.xxxx@gmail.com",
          "name": "asd",
          "phone": null
        },
        "calculated_statement_descriptor": "XXX XXXX",
        "captured": true,
        "created": 1600847712,
        "currency": "jpy",
        "customer": "cus_I4c27DIUQuvAHQ",
        "description": null,
        "destination": null,
        "dispute": null,
        "disputed": false,
        "failure_code": null,
        "failure_message": null,
        "fraud_details": {},
        "id": "ch_1HUSomCfZ37XLQD8d133buOK",
        "invoice": null,
        "livemode": false,
        "metadata": {},
        "object": "charge",
        "on_behalf_of": null,
        "order": null,
        "outcome": {
          "network_status": "approved_by_network",
          "reason": null,
          "risk_level": "normal",
          "risk_score": 39,
          "seller_message": "Payment complete.",
          "type": "authorized"
        },
        "paid": true,
        "payment_intent": "pi_1HUSoWCfZ37XLQD82vnE1yQT",
        "payment_method": "pm_1HUSolCfZ37XLQD8gSn0oy4x",
        "payment_method_details": {
          "card": {
            "brand": "visa",
            "checks": {
              "address_line1_check": null,
              "address_postal_code_check": null,
              "cvc_check": "pass"
            },
            "country": "US",
            "exp_month": 11,
            "exp_year": 2050,
            "fingerprint": "uaJa23vzDgA7fnSC",
            "funding": "credit",
            "installments": null,
            "last4": "4242",
            "network": "visa",
            "three_d_secure": null,
            "wallet": null
          },
          "type": "card"
        },
        "receipt_email": null,
        "receipt_number": null,
        "receipt_url": "https://pay.stripe.com/receipts/acct_1HU77uCfZ37XLQD8/ch_1HUSomCfZ37XLQD8d133buOK/rcpt_I4c233eGeSYOaN9cPvncC4AcU2Sm4s7",
        "refunded": false,
        "refunds": {},
        "review": null,
        "shipping": null,
        "source": null,
        "source_transfer": null,
        "statement_descriptor": null,
        "statement_descriptor_suffix": null,
        "status": "succeeded",
        "transfer_data": null,
        "transfer_group": null
      }
    ],
    "has_more": false,
    "object": "list",
    "total_count": 1,
    "url": "/v1/charges?payment_intent=pi_1HUSoWCfZ37XLQD82vnE1yQT"
  },
  "client_secret": "pi_1HUSoWCfZ37XLQD82vnE1yQT_secret_XXX",
  "confirmation_method": "automatic",
  "created": 1600847696,
  "currency": "jpy",
  "customer": "cus_I4c27DIUQuvAHQ",
  "description": null,
  "id": "pi_1HUSoWCfZ37XLQD82vnE1yQT",
  "invoice": null,
  "last_payment_error": null,
  "livemode": false,
  "metadata": {},
  "next_action": null,
  "object": "payment_intent",
  "on_behalf_of": null,
  "payment_method": "pm_1HUSolCfZ37XLQD8gSn0oy4x",
  "payment_method_options": {
    "card": {
      "installments": null,
      "network": null,
      "request_three_d_secure": "automatic"
    }
  },
  "payment_method_types": [
    "card"
  ],
  "receipt_email": null,
  "review": null,
  "setup_future_usage": null,
  "shipping": null,
  "source": null,
  "statement_descriptor": null,
  "statement_descriptor_suffix": null,
  "status": "succeeded",
  "transfer_data": null,
  "transfer_group": null
}

推荐答案

假设您已将产品详细信息放入您创建的 Checkout 会话的 line_items (参见指南步骤),那么您要寻找的是如何"履行订单"(参见指南步骤),您可以通过听checkout.session.completed 事件.

Assuming you've put the product details in the line_items of the Checkout session you created (see guide step), then what you're looking for is how to "fulfill the order" (see guide step), which you can do by listening to checkout.session.completed events.

当您收到一个事件时,该事件data 将是一个通过 id 的 Checkout 会话,然后您可以检索该会话 并指定 expand\[\]=line_items 为能够检查 line_items会议.

When you receive an event, the event data will be a Checkout session by id, and you can then retrieve that session and specify expand\[\]=line_items to be able to inspect the line_items for the session.

更新:我修改了上面的内容以反映默认情况下不包含 line_items.您必须检索会话并将其包含在展开中.

Update: I modified the above to reflect that line_items is not included by default. You must retrieve the session and include it in an expand.

这篇关于Stripe 事件:如何通过 Stripe 事件和 webhook 捕获成功支付的产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆