【问题标题】:Getting Stripe charge parameters from Error object从 Error 对象获取 Stripe 费用参数
【发布时间】:2017-12-10 23:34:11
【问题描述】:

我正在构建一个 php 站点,使用 Stripe 进行计费。如果收费成功,我会将结果记录在表格中,到目前为止一切顺利。如果收费失败(被拒绝)我想做同样的事情,记录消息等,但还要记录金额和货币。是否可以从 Stripe\Error\Card 对象中获取这些值?

我似乎能够使用

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

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

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

【问题讨论】:

  • 您大概将这些值传递到 [Create Charge][stripe.com/docs/api#create_charge] API Endpoint,对吧?只需将它们存储在一个变量中,该变量位于 try-catch 可访问的范围内,并在那里对它们进行任何您需要的操作。 Talented Cricket 的答案应该可以,但只需将 $amount_in_cents 存储在 try-catch 块之外。

标签: php stripe-payments


【解决方案1】:

为此,我使用 try 和 catch 并将金额和货币存储在错误之前的变量中,这些变量与我打算发送到条带中的变量相同。例如:

$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 }

我希望这对您有所帮助,我知道这不是您想要的,但如果您正在收费并发现这样的错误,那么您已经有了可用的金额和货币,而无需寻找它在错误卡中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2020-09-26
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多