簡體   English   中英

使用 PHP 從 Stripe Webhook JSON 獲取數據

[英]Get Data from Stripe Webhook JSON using PHP

我正在使用 Stripe Webhook 並成功接收數據。 我現在正試圖從響應中提取單個元素,但遇到了麻煩。 以下是正在發送/接收的 Webhook 有效負載示例:

{
  "created": 1326853478,
  "livemode": false,
  "id": "evt_00000000000000",
  "type": "charge.captured",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "api_version": "2019-09-09",
  "data": {
    "object": {
      "id": "ch_00000000000000",
      "object": "charge",
      "amount": 100,
      "amount_refunded": 0,
      "application": null,
      "application_fee": null,
      "application_fee_amount": null,
      "balance_transaction": "txn_00000000000000",
      "billing_details": {
        "address": {
          "city": null,
          "country": null,
          "line1": null,
          "line2": null,
          "postal_code": null,
          "state": null
        },
        "email": null,
        "name": "Jenny Rosen",
        "phone": null
      },
      "captured": true,
      "created": 1572137813,
      "currency": "aud",
      "customer": null,
      "description": "My First Test Charge (created for API docs)",
      "dispute": null,
      "failure_code": null,
      "failure_message": null,
      "fraud_details": {
      },
      "invoice": null,
      "livemode": false,
      "metadata": {
      },
      "on_behalf_of": null,
      "order": null,
      "outcome": null,
      "paid": true,
      "payment_intent": null,
      "payment_method": "card_00000000000000",
      "payment_method_details": {
        "card": {
          "brand": "visa",
          "checks": {
            "address_line1_check": null,
            "address_postal_code_check": null,
            "cvc_check": null
          },
          "country": "US",
          "exp_month": 8,
          "exp_year": 2020,
          "fingerprint": "OZhbqnP4UGjfz2sg",
          "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_19tTXNGNdpzDd4Jh/ch_1FY03tGNdpzDd4Jhg9poqFWr/rcpt_G48KYZOYRKrGjAmC9bqOx6TkDAkQ19W",
      "refunded": false,
      "refunds": {
        "object": "list",
        "data": [
        ],
        "has_more": false,
        "url": "/v1/charges/ch_1FY03tGNdpzDd4Jhg9poqFWr/refunds"
      },
      "review": null,
      "shipping": null,
      "source_transfer": null,
      "statement_descriptor": null,
      "statement_descriptor_suffix": null,
      "status": "succeeded",
      "transfer_data": null,
      "transfer_group": null
    }
  }
}

我在我的 PHP 文件中執行以下操作:

$payload = @file_get_contents('php://input');
$event= json_decode( $payload, TRUE );
$status = $event->data->object->status;
echo '$status: '.$status.'<br>';

它只返回$status: <br>

不確定我在嘗試從解碼的 JSON 中提取單個元素時做錯了什么,例如狀態和數量?

我只是在 Stripe 的 irc 開發人員的幫助下才弄清楚這一點。 您已經發布了您的 webhook 數據樹,以了解您需要提取信息的位置,但您的示例將無法正常工作,因為您正試圖在未知條件下提取數據,根據 Stripe for webhooks 上的示例代碼; 條件是;

payment_intent.succeeded 或 payment_intent.failed

您的代碼需要在這些條件之內,而不是之前!

這是一個工作且經過測試的示例,它為您提供有關如何處理此問題的指南。 請注意,這是根據 webhook 中的我的樹,而不是您的樹,但唯一的區別是最后注釋的一行代碼。

<?php
// Payment success
if ($event->type == "payment_intent.succeeded") {
$intent = $event->data->object;
$order = $event->data->object->charges->data[0]->description; // this is my line of code
/////////////////////////////////////////////////////////
// Do your server stuff here for payment_intent.succeeded
/////////////////////////////////////////////////////////
printf("Succeeded: %s", $intent->id);
http_response_code(200);
?>

對於您自己的特定目的,只需將我評論的行替換為您自己的;

<?php
$status = $event->data->object->status; // this is your line of code
?>

現在,您可以在任何一種情況下隨意使用該變量做任何您想做的事情。

使用TRUE參數是個問題。 使用:

$event= json_decode( $payload, FALSE );

讓它工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM