我正在使用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提取单个元素(例如状态和数量)时我做错了什么吗?
答案 0 :(得分:0)
我只是在Stripe的irc的一名开发人员的帮助下解决了这个问题! 您已经发布了Webhook数据树,以了解需要从何处提取信息,但是您的示例无法正常工作,因为您尝试在未知的条件下提取数据。根据Stripe上用于Webhooks的示例代码,条件为:
payment_intent.succeed或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
?>
现在无论哪种情况,您都可以随意使用该变量进行操作。
答案 1 :(得分:0)
问题是使用TRUE
参数。使用:
$event= json_decode( $payload, FALSE );
使其有效。