Stripe事件:如何使用Stripe事件和Webhook捕获成功支付的产品?

3

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

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

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

{
  "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
}

支付是在您的网站上进行还是在Stripe提供的预配置页面上进行? - Alberto Sinigaglia
@Berto99 我正在使用Stripe的结账会话功能。 - undefined
1个回答

3
假设您已经将产品详情放在您创建的结账会话的line_items中(请参阅指南步骤),那么您需要找到如何“履行订单”的方法(请参阅指南步骤),您可以通过监听checkout.session.completed事件来完成。
当您接收到一个事件时,事件data将是一个通过id的结算会话,然后您可以检索该会话并指定expand\[\]=line_items以便能够检查该会话的line_items
更新:我修改了上述内容以反映默认情况下不包括line_items。您必须检索会话并将其包含在扩展中。

我正在通过line_items传递price_id来引用产品和价格。这些信息都不包含在checkout.session.completedpayment_intent.succeeded事件的data中。 - undefined
1
不好意思,默认情况下,line_items没有附加在事件数据中,必须在检索时使用expand参数进行请求。我已经在上面更新了我的回答。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接