#xfechx
1 messages · Page 1 of 1 (latest)
what's the problem exactly?
Hi @past gust
I had to write a program to 'print' tax invoices for accountant in Germany. Each tax invoice needs to follow a ordered number.
What it does it grabs, payments, transfers and appfees, then prints the invoice onto a PDF and saves to folder (year/month/file)
With transfers and appfees, it sums them per customer and month, then creates a PDF with the sum of appfees, or transfers.
However the problem is that balance transaction only can pull 100 items at a time....
And I have waaaay too many transfers and appfees to account, so I cannot in the api call enter the whole month. I then have to do the generation by week.
But now my problem is that obviously the index gets repeated if I do it weekly, as I wrote the program for monthly
is there a way to pull more than 100 balance transactions in one consult? How can I resolve this, I am stuck
are you aware of https://stripe.com/docs/api/pagination/auto ?
I think when I was working last on this, someone mentioned it. But I never undrestood and they were not able to help me :/
I use php
what is your specific question about it?
But I never undrestood and they were not able to help me :/
have you tried using it? when you ran the code from the API reference example does it do what you expect? what do you have so far in terms of making that paginated call and what I can help clarify?
Would you help me to translate my code so that it uses pagination?
i just do not understand how to use it
can you try?
should I add it here?
// foreach ($balance_transactions->data as $payment) {
// Iterate over the payments in reverse order (from oldest to newest)
for ($i = count($balance_transactions->data) - 1; $i >= 0; $i--) {```
'limit' => 3,
]);
foreach ($bts->autoPagingIterator() as $balance_transaction) {
// Do something with $balance_transaction
}
seems sensible! you can change your code and run it and test code and see what it does.
Can you just help me keep that $i in the for loop?
the iterator in the code is sensible yes....
$bts = $stripe->balanceTransactions->all([
'limit' => 3,
]);
$i = 0;
foreach ($bts->autoPagingIterator() as $balance_transaction) {
// Do something with $balance_transaction
$i++;
}
because it stops summing when it encounters app fee, so that for example invoice number 4 has many app fees
just normal PHP, this is not anything special to Stripe
you deleted your code, but from what I remember seeing, your $i is the total of the list returned, (up to 100) and then you count down; so to be clear you can not get a count of exactly how many BalanceTransactions there are in total, that's not something the API supports, to be clear. But you use auto-pagination and it returns all of them and you can execute code for each
my code is there
I deleted to remove something i added by mistake
for ($i = count($balance_transactions->data) - 1; $i >= 0; $i--) {
yes, after many hours of working on it, I cannot remember why I had to count down instead of up
I modified like this now:
$balance_transactions = $stripe->balanceTransactions->all([
'created' => [
'gte' => strtotime($startDate),
'lt' => strtotime($endDate) + 86400 // Add 1 day to include payments on the end date
],
'limit' => 100,
expand => [
'data.source.customer',
'data.source.payment_intent',
'data.source.invoice',
]
], $connected_account
);
// Initialize an empty array to store summed fees
$summedFees = [];
// Maintain a counter for each month
$currentMonth = null;
// Get the total number of payments
$totalPayments = count($balance_transactions->data);
//$orderNumber = 1;
$saved_order_number_transfers = 0;
$saved_order_number_appfees = 0;
// Generate tax invoices for each payment
// foreach ($balance_transactions->data as $payment) {
// Iterate over the payments in reverse order (from oldest to newest)
//THIS WAS WORKING (SORT OF): for ($i = count($balance_transactions->data) - 1; $i >= 0; $i--) {
$i = 0;
foreach ($balance_transactions->autoPagingIterator() as $balance_transaction) {
// Do something with $balance_transaction
$i++;
// Get the payment object
$payment = $balance_transaction->data[$i];
$month = date('m', $payment->created);
//TRANSFERS:
if($payment->type == 'payment' && !empty($payment->source->source->object == 'account')){```
cool
you don't need $balance_transaction->data[$i], because you're nor working with a list;. It's just $balance_transaction (like the comment says)
ok, changed that. It works, but it doesn't print any of app_fees or transfer related invoices, i think is because of this:
if ($month !== $currentMonth) {
$currentMonth = $month;
$orderNumber = 1;
$saved_order_number_appfees = 0;
} else {
$orderNumber++;
}
if($saved_order_number_appfees === 0){
$saved_order_number_appfees = $orderNumber;
} else {
$orderNumber--;
}```
I don't know sorry, that is details of your own code
ok, that's ok.
but the problem keeps being the same.... not printing enough balance transactions
I have the limit set at 100, but how can we print more?
in one go
if you use autoPagingIterator it will execute the code in the loop for every BalanceTransaction that exists on your account that matches the query, not just 100.
ok i think it is working, at least better.
I have another question
from collected fees, I am getting 'Application fee from application Asevatu Digital Europe for xfechx@gmail.com (txn_1NFwFZHlZBj3A1TOZCzGU8Cx) - EUR 37.46'
can I use the description of what originally was the charge for?
you could get that description yes, it will require digging back through the chain of API objects.
for example if that is the BT of an a fee, then the $balance_transaction->source is the ID of an ApplicationFee , and then that object has a ->charge that is the ID of the original Charge https://stripe.com/docs/api/application_fees/object?lang=php#application_fee_object-charge ; and then that object has things like its own description
so you can write code to retrieve the objects in the chain and get the information needed from them
I am getting repeated order numbers with appfees and transfers. Do you know where I can get my code revised?
I need help
Hey! Taking over for my colleague. Sorry, we don't provide code review but if you have specific use case we are happy to help.
I am getting repeated order numbers with appfees and transfers.
Where are you getting this ?
From Stripe API call or in your frontend ?
frontend
Sorry, not sure if we can help you as this isn't a Stripe related issue to its APIs.
Unless you are getting duplicates in the Stripe API response.