简体   繁体   English

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

[英]How to retrieve stripe customer charge data

I am very new to programming. 我对编程非常陌生。 This is very confusing to me. 这让我很困惑。 I am trying to retrieve customer information from Stripe. 我正在尝试从Stripe检索客户信息。 I can retrieve the new customer id. 我可以检索新的客户ID。 I am trying to also retrieve the charge details. 我正在尝试检索收费明细。 I want to ensure that it was a successful charge before my code proceeds further. 我想确保在我的代码继续进行之前成功进行了充电。 Next I will be attempting webhooks to trigger events and verify stripe signatures. 接下来,我将尝试通过Webhooks触发事件并验证数据条签名。 (I will be back with another question, or two, I am very sure!) (我很肯定会再回答一两个问题!)

I have found on this site a great example that got me to the customer id. 我在此站点上找到了一个很好的示例,使我了解了客户ID。 I have been playing around with the code to see what spits out so that I can incorporate that into my database update, below: 我一直在研究代码,以查看产生了什么,以便将其合并到下面的数据库更新中:

<?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

This appears to be PDO? 这似乎是PDO? or OOP? 还是面向对象? I am not familiar with how to approach this. 我不熟悉该如何处理。 Any assistance to get me jump started would be appreciated. 任何帮助我快速入门的帮助将不胜感激。 This is quite intimidating. 这是相当令人生畏的。 Thank you. 谢谢。

At the moment, your code does two things: it creates a Customer and then it creates a Subscription . 目前,您的代码有两件事:创建客户 ,然后创建Subscription The second step will associated the 5 Plans with your customer so that he gets charged automatically every week or every month. 第二步将把这5个计划与您的客户相关联,以使他每周或每月自动获得费用。 This causes a charge to be created immediately for the first billing cycle (assuming no trial period). 这将导致在第一个计费周期立即创建费用(假设没有试用期)。

If the charge had failed, the subscription creation would fail. 如果收费失败,则订阅创建将失败。 You need to make sure that your code properly catches any exception raised by the library as explained in Stripe error documentation . 您需要确保您的代码正确地捕获了库引发的任何异常,如Stripe 错误文档中所述

If no exception is raised, then the Subscription creation succeeded and the customer's card was charged successfully. 如果没有异常,则创建订阅成功,并且成功为客户的卡充值。 This means that an Invoice was created for the expected total amount of all your plans and that Invoice caused a Charge to be created for the same reason. 这意味着已为所有计划的预期总金额创建了发票 ,并且由于相同的原因,发票导致要创建费用

Now, in most cases you likely want to store all of that information in your database: the customer id cus_1234 you got at the first step, the Subscription id sub_2222 you got at the second step and then the Invoice id in_ABCD and the Charge id ch_FFFF . 现在,在大多数情况下,您可能希望将所有这些信息存储在数据库中: cus_1234获得的客户ID cus_1234获得的Subscription ID sub_2222 ,然后是发票ID in_ABCD和Charge ID ch_FFFF

For the last two, the best solution is to use the List Invoices API and pass the Customer id in the customer parameter. 对于最后两个,最好的解决方案是使用列表发票API并在customer参数中传递客户ID。 Since this is the first time this Customer has an invoice, you know you'll only get one result. 由于这是该客户第一次收到发票,因此您将只能得到一个结果。 The code would look like this: 代码如下所示:

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

You can also combine this with the Expand feature so that you get the full Charge object back with more information for example on the card's last 4 digits: 您还可以将此功能扩展功能结合使用,以便获得完整的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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM