繁体   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