简体   繁体   中英

Get Data from Stripe Webhook JSON using PHP

I'm working with a Stripe Webhook and receiving the data successfully. I'm now trying to get individual elements out of the response but having trouble with this. Here's an example of the Webhook payload that is being sent/received:

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

I'm doing the following in my PHP file:

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

which just returns $status: <br>

Not sure what I'm doing wrong with trying to extract individual elements from the decoded JSON here such a the status and amount?

I just figured this out with a little help from a dev on Stripe's irc. You already posted your webhook data tree to know where the information is you need to pull but your example won't work because you are trying to pull data on a yet unknown condition, As per the sample code on Stripe for webhooks; the conditions are either;

payment_intent.succeeded OR payment_intent.failed

Your code needs to be inside these conditions, not before!

Here's a working and tested example which gives you a guide on how to approach this. Be aware that is according to my tree from the webhook, not yours but the only difference is the one line of code which is commented at the end.

<?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);
?>

For you own specific purposes just replace the line I commented with your own;

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

Now you're free to do whatever you want with that variable in either condition.

The use of the TRUE parameter was the issue. Using:

$event= json_decode( $payload, FALSE );

made it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM