如何检索条带化的客户收费数据

时间:2019-01-25 19:17:05

标签: php pdo stripe-payments

我对编程非常陌生。这让我很困惑。我正在尝试从Stripe检索客户信息。我可以检索新的客户ID。我正在尝试检索收费明细。我想确保在我的代码继续进行之前成功进行了充电。接下来,我将尝试通过Webhooks触发事件并验证数据条签名。 (我肯定会再问一两个问题!)

我在此站点上找到了一个很好的示例,该示例使我了解了客户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?还是面向对象?我不熟悉该如何处理。任何帮助我快速入门的帮助将不胜感激。这是相当令人生畏的。谢谢。

1 个答案:

答案 0 :(得分:2)

目前,您的代码有两件事:创建一个Customer,然后创建一个Subscription。第二步将把这5个计划与您的客户相关联,以使他每周或每月自动获得费用。这将导致在第一个结算周期(假设没有试用期)立即产生费用。

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

如果没有异常,则创建订阅成功,并且成功为客户的卡充值。这意味着为您所有计划的预期总金额创建了Invoice,并且出于相同的原因,发票导致创建了Charge

现在,在大多数情况下,您可能希望将所有这些信息存储在数据库中:第一步获得的客户ID def mainVerticleName = 'io.vertx.starter.MainVerticle',第二步获得的订阅ID cus_1234然后是发票ID sub_2222和费用ID in_ABCD

对于最后两个,最好的解决方案是使用List Invoices API并在ch_FFFF参数中传递客户ID。由于这是该客户第一次收到发票,因此您将只能得到一个结果。代码如下:

customer

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

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