使用PHP从Stripe Webhook JSON获取数据
我正在使用Stripe Webhook并成功接收数据。我现在正试图从响应中提取单个元素,但在这方面遇到了问题。下面是正在发送/接收的Webhook有效负载的示例:使用PHP从Stripe Webhook JSON获取数据,php,json,stripe-payments,Php,Json,Stripe Payments,我正在使用Stripe Webhook并成功接收数据。我现在正试图从响应中提取单个元素,但在这方面遇到了问题。下面是正在发送/接收的Webhook有效负载的示例: { "created": 1326853478, "livemode": false, "id": "evt_00000000000000", "type": "charge.captured", "object": "event", "request": null, "pending_webhooks":
{
"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>';
$payload=@file\u get\u contents('php://input');
$event=json_decode($payload,TRUE);
$status=$event->data->object->status;
回显“$status:”.$status.“
”;
只返回$status:
不确定我在这里试图从解码的JSON中提取单个元素的错误是什么,比如状态和数量?我只是在Stripe的irc上的一个开发人员的一点帮助下发现了这一点! 您已经发布了webhook数据树,以了解需要提取的信息的位置,但是您的示例无法工作,因为您试图在未知条件下提取数据。根据Webhook条带上的示例代码,条件为: 付款意向成功或付款意向失败 您的代码需要在这些条件内,而不是之前 下面是一个经过测试的工作示例,它为您提供了如何实现这一点的指南。请注意,这是根据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 );
让它工作。试试
$event=json\u decode($payload,FALSE)代码>json\u解码($payload,TRUE)代码>-在这种情况下,TRUE
将导致将数据创建为(关联)数组,而不是对象(请参阅)。因此->
语法无法获取属性。您可以通过执行类似于var\u dump($event)的操作来调试它
查看解码操作中得到的实际变量。您尝试调试该问题的原因是什么?$payload
是否包含预期数据?如果这解决了您的问题,请选择它作为正确答案,如果没有,请说明问题所在。解决方案只是使用$event=json\u decode($payload,FALSE)代码>