【问题标题】:How to retrieve stripe customer charge data如何检索条带客户费用数据
【发布时间】:2019-01-25 19:17:05
【问题描述】:

我对编程很陌生。这让我很困惑。我正在尝试从 Stripe 检索客户信息。我可以检索新的客户 ID。我也在尝试检索收费详情。我想确保在我的代码进一步执行之前成功收费。接下来我将尝试使用 webhook 来触发事件并验证条带签名。 (我会再回答一个或两个问题,我很确定!)

我在这个网站上找到了一个很好的例子,可以让我找到客户 ID。我一直在研究代码以查看吐出的内容,以便将其合并到我的数据库更新中,如下所示:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

// Retrieve the request's body and parse it as JSON:

require_once('stripe-php-6.28.0/init.php');

$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// Do something with $event_json

http_response_code(200); // PHP 5.4 or greater

/*Do something with object, structure will be like:
 * $object->accountId
 * $object->details->items[0]['contactName']
 */

// dump to file so you can see
file_put_contents('callback.test.txt', print_r($object, true));

}
//----------------------------

//For stripe data 

$stripe = [
"secret_key"      => "sk_test_itsasecret",
"publishable_key" => "pk_test_notpublished",
];

\Stripe\Stripe::setApiKey($stripe['secret_key']);


$token  = $_POST['stripeToken'];
$email  = $_POST['stripeEmail'];
$stripecustid = $customer->id;

$customer = \Stripe\Customer::create([
'email' => $email,
  'source'  => $token,
  'id' => $stripecustid,
  'status' => $charge,   //want to get some type of charge status success here
 ]);

\Stripe\Subscription::create([
"customer" => $customer->id,
"items" => [
 [

  "plan" => $1plan,
],
[
  "plan" => $2plan,
],
[
  "plan" => $3plan,
],
[
  "plan" => $4plan,
],
[
  "plan" => $5plan,
 ],
],
]);


 $stripecustid = $customer->id;

 echo "This is the stripe customer id: " . $customer->id . "<br />";
 echo "customer:  " . $customer->id . "<br />";
 echo "stripe:  " . $stripecustid . "<br />";
 echo "charge:  " . $charge->id . "<br />"; //returns nothing
 echo "charge result:  " . $charge->paid . "<br />"; //returns nothing
 echo "object charge amount test result:  " . $object->charge->amount;  //returns nothing
 echo "object charge paid test result:  " . $object->charge->paid; // returns nothing

这似乎是 PDO?还是面向对象?我不熟悉如何处理这个问题。任何让我开始的帮助将不胜感激。这是相当吓人的。谢谢。

【问题讨论】:

  • 您能否发布您从 Stripe 返回的数组的输出?可能是var_dump($customer)
  • 您向我们展示的代码不完整 - $customer 设置在哪里? $charge 呢?你一定是在某个地方给 Stripe 打电话,对吧?给我们看一看。 The Stripe API docs自己很好,包括例子
  • var_dump($customer); 返回一大堆东西。但我看不到任何关于充电状态的信息。
  • @Don'tPanic - Stripe API 文档非常好。但是,我不明白这一点。
  • 要获取与客户关联的费用列表,您可以使用 List Charges (stripe.com/docs/api#list_charges) 端点并传入映射到客户 ID 的 customer 参数。

标签: php pdo stripe-payments


【解决方案1】:

目前,您的代码做了两件事:创建Customer,然后创建Subscription。第二步将 5 个计划与您的客户相关联,以便他每周或每月自动收费。这会导致立即为第一个计费周期创建费用(假设没有试用期)。

如果收费失败,订阅创建将失败。您需要确保您的代码正确捕获库引发的任何异常,如 Stripe error documentation 中所述。

如果没有引发异常,则订阅创建成功并且客户的卡已成功扣款。这意味着为您的所有计划的预期总金额创建了一个 Invoice,并且由于同样的原因,发票导致创建了一个 Charge

现在,在大多数情况下,您可能希望将所有这些信息存储在数据库中:第一步获得的客户 ID cus_1234,第二步获得的订阅 ID sub_2222,然后是发票id in_ABCD 和 Charge id ch_FFFF

对于最后两个,最好的解决方案是使用List Invoices API 并在customer 参数中传递客户ID。由于这是该客户第一次有发票,您知道您只会得到一个结果。代码如下所示:

$invoices = \Stripe\Invoice::all(["limit" => 3]);
$invoiceId = $invoices->data[0]->id;
$chargeId = $invoices->data[0]->charge;

您还可以将其与Expand feature 结合使用,以便您获取完整的 Charge 对象以及更多信息,例如卡的最后 4 位数字:

$invoices = \Stripe\Invoice::all([
  "limit" => 3,
  "expand" => ["data.charge"],
]);
$invoiceId = $invoices->data[0]->id;
$chargeId = $invoices->data[0]->charge->id;
$cardLast4 = $invoices->data[0]->charge->source->last4; 

【讨论】:

  • 工作就像一个魅力!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2019-04-17
相关资源
最近更新 更多