php - 如何使用 PHP 从 Stripe 获取收费 ID?

标签 php stripe-payments

首先,我知道有人问过这个问题,但他们没有解释如何使用

 charge.succeeded

我正在使用 Stripe 创建授权和捕获费用。我需要收费 ID 来确认/获取收费。

以下代码创建费用但在获得批准之前不会收费。

\Stripe\Charge::create(array(
    "amount" => $price,
    "currency" => "usd",
    "customer" => $custID, // obtained with Stripe.js
    "description" => "Charge for test@example.com",         
    "capture" => false
));

我假设我可以通过以下方式获取收费 ID:

$chargeID = $charge->id;

因为这就是我在创建客户时获取客户 ID 的方式。但是当我回显变量时我什么也没得到,这无济于事,因为我想将它保存在数据库中。我到处都看过,但找不到任何具体的或最近的东西。

然后完成充电。我必须这样捕捉它:

$ch = \Stripe\Charge::retrieve("$chargeID");
$ch->capture();

这全部来自 Stripe API 引用,但没有解释如何获取收费 ID。它甚至没有告诉您如何获取客户 ID,幸运的是 Stack Overflow 解决了这个问题。

这一切都是使用 PHP 完成的。

最佳答案

根据 Stripe's Documentation ,当创建一个新的费用时,你会得到 Stripe\Charge JSON 对象作为返回(或者如果出现问题则异常)。

您可以var_dump您刚刚创建的对象以查看您的对象的外观,但是根据API您可以使用id返回对象的:

$charge = \Stripe\Charge::create(array(
    "amount" => $price,
    "currency" => "usd",
    "customer" => $custID, // obtained with Stripe.js
    "description" => "Charge for test@example.com",         
    "capture" => false
));

// you can use the var_dump function to see what's inside the $charge object
// var_dump($charge);

$chargeID = $charge->id;

(而且你不需要再次调用Charge::retrieve,你可以使用当前的$charge对象)。

关于php - 如何使用 PHP 从 Stripe 获取收费 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38337794/

相关文章:

php - 如何在 zend 框架中访问数据文件夹中的文件?

直接接近时PHP重定向?

ruby-on-rails - 检查 Stripe 优惠券是否无效

javascript - 延迟加载 strip - 导致未定义的错误

javascript - 使用 Stripe.js 和 Skeuocard

php - 当我添加我的新代码然后在 laravel 中收到错误 "This cache store does not support tagging"?

javascript - url 的正则表达式一些问题

php - 如何从 PayPal 付款中获取 eBay 商品编号?

php - 为什么我收到 Stripe 400 错误 - 无法多次使用 Stripe token ?

php - Symfony2/Sylius 未检测到 stripe/stripe-php 库