如何从条带对象PHP获取关联数组
我正在尝试使用Stripe的PHP库从端点检索的对象中检索元数据和支付金额 我使用的是他们示例代码的一个稍加修改的版本:如何从条带对象PHP获取关联数组,php,stripe-payments,Php,Stripe Payments,我正在尝试使用Stripe的PHP库从端点检索的对象中检索元数据和支付金额 我使用的是他们示例代码的一个稍加修改的版本: $payload = @file_get_contents('php://input'); $event = null; try { $event = \Stripe\Event::constructFrom( json_decode($payload, true) ); } catch(\UnexpectedValueException $
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
// Read the result.
$paymentIntentValues = $paymentIntent->values(); // Returns standard array??
break;
case 'payment_method.attached':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
// Read the result.
$paymentMethodValues = $paymentMethod->values(); // Returns standard array??
break;
// ... handle other event types
default:
// Unexpected event type
http_response_code(400);
exit();
}
http_response_code(200);
问题是,当我转储任何对象(例如$paymentIntent)时,转储只是电荷对象的转储,而当我尝试使用values()获取值时它给了我一个模棱两可的标准数组
e、 g
我希望能够读取$paymentIntent对象的值,并通过关联数组或对象一致地使用这些值。为了检索列出条带对象(例如paymentIntent对象)值的关联数组,必须使用toArray()函数 e、 g 结果:
array (45) [
'id' => string (12) "pi_secretkey"
'object' => string (12) "paymentIntent"
'amount' => integer 247
'amount_refunded' => integer 0
'application' => null
'application_fee' => null
'application_fee_amount' => null
'balance_transaction' => string (13) "anothersecret"
etc... etc...
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
// Read the result.
$paymentIntentValues = $paymentIntent->toArray(); // Returns associative array of values.
break;
case 'payment_method.attached':
$paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
// Read the result.
$paymentMethodValues = $paymentMethod->toArray(); // Returns associative array of values.
break;
// ... handle other event types
default:
// Unexpected event type
http_response_code(400);
exit();
}
array (45) [
'id' => string (12) "pi_secretkey"
'object' => string (12) "paymentIntent"
'amount' => integer 247
'amount_refunded' => integer 0
'application' => null
'application_fee' => null
'application_fee_amount' => null
'balance_transaction' => string (13) "anothersecret"
etc... etc...