簡體   English   中英

Stripe : 列出 PHP 的所有費用

[英]Stripe : list all charges with PHP

我試圖列出我的 Stripe 帳戶的所有費用以獲取 statement_descriptor(我的產品)的列表,但我有一個錯誤,提示無法找到費用...

<?php
$off = 0;
$has_more = True;
$listeProducts = array();
ini_set('max_execution_time', 2000);
while($has_more){
    $req = \Stripe\Charge::all(array("limit" => 100, "starting_after" => $off));
    $res = json_decode($req->__toJSON(), true);

    foreach($res["data"] as &$prodUni){
        array_push($listeProducts, $prodUni["statement_descriptor"]);
    }

    $has_more = $res["has_more"];
    $off += 100;
}
$_SESSION["products"] = $listeProducts;

這是錯誤消息:

Fatal error: Uncaught exception 'Stripe\Error\InvalidRequest' with message 'No such charge: 0' in C:\wamp64\www\...\stripe-php-5.1.3\lib\ApiRequestor.php:124 from API request '...' in C:\wamp64\www\...\stripe-php-5.1.3\lib\ApiRequestor.php on line 124

我不能放置“偏移量”,因為已棄用,它與之前的結尾做同樣的事情。

問題是我放了 0 但需要充值 ID ......我不想在那里放充值 ID ......我需要像我有抵消一樣

starting_after接受一個對象 - 理想情況下是前一個獲取結果中的最后一個對象。

https://stripe.com/docs/api?lang=php#list_charges-starting_after

問題答案的第一部分是在檢查 has_more 之前獲得前 100 個。

但在那之后,我無法檢索第一筆費用,不要忘記,當我們使用 Stripe API 檢索時,列表是逆序的!!! 所以我必須得到最后一個或第一個? 我怎樣才能更正我的代碼 pease ? 這條線正確嗎? $lastId = $res["data"]["99"]["id"];

 <?php $req = \\Stripe\\Charge::all(array("limit" => 1, "starting_after" => $lastId)); $res = json_decode($req->__toJSON(), true); $lastId = $res["data"]["99"]["id"]; $has_more = True; while($has_more){ foreach($res["data"] as &$chargeUni){ //... } $req = \\Stripe\\Charge::all(array("limit" => 10, "starting_after" => $lastId)); $res = json_decode($req->__toJSON(), true); $has_more = False;//$res["has_more"]; $lastId = $res["data"]["99"]["id"]; } ?>

Stripe API 提供了自動分頁功能

此功能可以輕松處理獲取大量資源列表,而無需手動對結果進行分頁並執行后續請求。

改編自文檔,這將很好地完成這個技巧(並且更容易閱讀):

\Stripe\Stripe::setApiKey("sk_…");

/**
 * Automatically go through _all_ charges, fetching the data in chunks/pages of
 * 100 results.
 */
$charges = \Stripe\Charge::all(["limit" => 100]);
foreach ($charges->autoPagingIterator() as $charge) {
  // Do something with $charge
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM