Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从错误对象获取条带电荷参数_Php_Stripe Payments - Fatal编程技术网

Php 从错误对象获取条带电荷参数

Php 从错误对象获取条带电荷参数,php,stripe-payments,Php,Stripe Payments,我正在建立一个php网站,使用Stripe进行计费。如果充电成功,我会将结果记录在一个表中,目前为止效果良好。如果收费失败(被拒绝),我想做同样的事情,记录消息等,但也记录金额和货币。是否可以从Stripe\Error\Card对象获取这些值 我似乎能够使用 catch(\Stripe\Error\Card $e) { $test = $e->getTrace(); print_r($test[3]['args']); } 但这看起来很狡猾!我想我可以从最初的收费请求中

我正在建立一个php网站,使用Stripe进行计费。如果充电成功,我会将结果记录在一个表中,目前为止效果良好。如果收费失败(被拒绝),我想做同样的事情,记录消息等,但也记录金额和货币。是否可以从Stripe\Error\Card对象获取这些值

我似乎能够使用

catch(\Stripe\Error\Card $e) {

    $test = $e->getTrace();
    print_r($test[3]['args']);
}

但这看起来很狡猾!我想我可以从最初的收费请求中获取它们,只是想知道是否还有其他方法?

我使用try-and-catch来实现这一点,并在错误发生之前将金额和货币存储在变量中,这些变量与我打算在收费中发送给stripe的变量相同。例如:

$customer = \Stripe\Customer::create(array(
  'email' => $customer_email,
  'source'  => $token
));

try {

  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => $amount_in_cents,
    'currency' => 'usd'
  ));

} catch(\Stripe\Error\Card $e) { // Your error handling code }

我希望这是有帮助的,我知道这并不完全是你想要的,但如果你正在进行收费并捕捉到这样的错误,那么你就已经有了可用的金额和货币,而不需要在错误卡中查找它。

你可能正在将这些值传递到[创建费用][API端点,对吗?只需将它们存储在一个变量中,该变量位于try catch可访问的范围内,并在该范围内对它们执行任何您需要的操作。天才Cricket的答案应该可以,但只需将
$amount\u in_cents
存储在try catch块之外。