使用Stripe PHP Api

时间:2017-04-03 03:56:24

标签: php stripe-payments

这是我想要完成的前提:

Stripe为每月订阅生成发票,并将webhook发送到我服务器上的页面。我收到webhook并进行快速测试,看看用户是否有负余额(财务应用程序),在这种情况下,我想“原谅”发票。但是,我无法保存发票。代码如下,但感谢您解决此问题的任何帮助。

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json
$Stripe_Cust_ID = $event_json->data->object->customer;
$Plan = $event_json->data->object->lines->data[0]->plan->id;
$Invoice_ID = $event_json->data->object->id;

//...
//Small amount of logic and a DB query to figure out user's balance.
//....

if ($Balance < 0) {
    $invoice = \Stripe\Invoice::retrieve($event_json->data->object->id);
    echo "Invoice Forgiven?: ".$invoice->forgiven;
    $invoice->forgiven = true;
    echo "Invoice Forgiven Round 2?: ".$invoice->forgiven;
    $invoice->save();
}

http_response_code(200); // PHP 5.4 or greater

我的代码回显了完整的发票回复,但似乎不会原谅发票或保存。我做错了吗?

1 个答案:

答案 0 :(得分:1)

在调用Stripe的API之前,您是否使用以下内容设置了Stripe API密钥?

\Stripe\Stripe::setApiKey("sk_test_XXYYZZ");

除此之外,您的请求应该在编码时工作。我建议您查看Stripe帐户的API日志[0],以确保您的请求到达Stripe并查看对您的请求的响应。我还建议您查看自己的服务器日志,查找可能存在的任何错误。

[0] https://dashboard.stripe.com/logs/overview

相关问题