我尝试在PHP中读取Stripe Webhook的值。 发送的Webhook具有以下格式:
{
"created": 1312403445,
"livemode": false,
"id": "evt_00000000000000",
"type": "checkout.session.completed",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2019-03-14",
"data": {
"object": {
"id": "cs_00000000000000",
"object": "checkout.session",
"billing_address_collection": null,
"cancel_url": "https://example.com/cancel",
"client_reference_id": null,
"customer": null,
"customer_email": null,
"display_items": [
{
"amount": 1500,
"currency": "usd",
"custom": {
"description": "Comfortable cotton t-shirt",
"images": null,
"name": "T-shirt"
},
"quantity": 2,
"type": "custom"
}
],
"livemode": false,
"locale": null,
"payment_intent": "pi_00000000000000",
"payment_method_types": [
"card"
],
"subscription": null,
"success_url": "https://example.com/success"
}
}
}
我将发送的值写入数据库中的表。我服务器上的PHP是:
require 'connectFile.php'; // connect my database
require_once('vendor/autoload.php');
require_once('vendor/stripe/stripe-php/init.php');
$input = @file_get_contents('php://input');
$event_json = json_decode($input);
$value_id=$event_json->data->object->id;
//value_created= $event_json->created;
$sql = "INSERT INTO myTable VALUES ("test","."'".$value_id."'".")";
$prepare = $pdo->prepare($sql);
$prepare ->execute();
http_response_code(200); // PHP 5.4 or greater
问题是当我尝试读取数据->对象-> id时,它在数据库中写入0。但是,如果我读取value_created = $ event_json-> created,它将正确读取该值。
您知道什么地方出错了,为什么它显示为0?
答案 0 :(得分:1)
我认为您准备了错误的查询。试试:
$sql = "INSERT INTO myTable VALUES ('test',':prepared_id')";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':prepared_id', $value_id, PDO::PARAM_STR);
$stmt->execute();
它也是更安全的方法,因为在查询中构建字符串不安全:D