#Vishu
1 messages · Page 1 of 1 (latest)
Hi there, to avoid bank fees, Stripe doesn’t retry invoice payments that customers made with bank debit methods including BECS direct debit,
Hey Jack, thanks for your response. Can’t we do it in any way? Fox example even if we use the code instead of dashboard such as below
Do you really want to do it? as an unsuccessful debit will incur bank fee to merchant.
Yeah, we would like to do it
Also, we were thinking of implementing a method where the fee will be passed onto the customer as an extra penalty for retrying payment
Can you please suggest a way to do this? Thanks Jack
I'm not sure if you can manage to collect the fee from customer because the debit is unsuccessful in the first place.
Can something like below work?
"<?php
// Replace YOUR_STRIPE_SECRET_KEY with your actual Stripe secret key
\Stripe\Stripe::setApiKey(""YOUR_STRIPE_SECRET_KEY"");
// Set the retry intervals in seconds
$retryInterval1 = 21600; // 6 hours
$retryInterval2 = 43200; // 12 hours
$retryInterval3 = 86400; // 24 hours
// Set the email subjects and messages for the retries
$emailSubjects = [
""Subscription Payment Retry Attempt 1"",
""Subscription Payment Retry Attempt 2"",
""Subscription Payment Retry Attempt 3"",
];
$emailMessages = [
""This is a reminder that we will be retrying your subscription payment in 1 hour. Please ensure that you have sufficient funds in your account to complete the payment."",
""This is a reminder that we will be retrying your subscription payment in 1 hour. Please ensure that you have sufficient funds in your account to complete the payment."",
""This is a reminder that we will be retrying your subscription payment in 1 hour. Please ensure that you have sufficient funds in your account to complete the payment."",
];
// Function to send an email
function sendEmail($to, $subject, $body) {
// Set the email headers
$headers = 'From: noreply@example.com' . ""\r\n"" .
'Reply-To: noreply@example.com' . ""\r\n"" .
'X-Mailer: PHP/' . phpversion();
// Send the email
mail($to, $subject, $body, $headers);
}
// Function to handle errors
function handle_error($e) {
// Log the error message and stack trace
error_log($e->getMessage());
error_log($e->getTraceAsString());
// Optionally, send an email to the developer with the error details
sendEmail(""developer@example.com"", ""Error Occurred"", $e->getMessage() . ""\n"" . $e->getTraceAsString());
}
// Get a list of failed or past due subscriptions
$subscriptions = \Stripe\Subscription::all([
""status"" => ""past_due,failed"",
]);
// Loop through the subscriptions
foreach ($subscriptions->data as $subscription) {
// Check the subscription status
if ($subscription->status == ""failed"" || $subscription->status == ""past_due"") {
// Loop through the retries
for ($i = 0; $i < 3; $i++) {
// Send an email to the customer 1 hour before the retry
sendEmail($subscription->customer_email, $emailSubjects[$i], $emailMessages[$i]);
// Wait for the retry interval before attempting the retry
sleep($retryInterval1 + $i * $retryInterval2);
// Attempt the retry
try {
$subscription->save();
break;
} catch (\Stripe\Error\Card $e) {
// Do nothing, the loop will continue and try again
}
}
// Check the subscription status again
$subscription = \Stripe\Subscription::retrieve($subscription->id);
if ($subscription->status == 'past_due') {
// Add a one-time penalty to the subscription
$invoice = \Stripe\Invoice::create([
""customer"" => $subscription->customer,
""subscription"" => $subscription->id,
""subscription_items"" => [
[
""id"" => $subscription->items->data[0]->id,
""quantity"" => 1,
""price"" => [
""amount"" => 10000, // The penalty amount in cents
""currency"" => ""AUD"",
],
],
],
]);
// Attempt to pay the invoice
try {
$invoice->pay();
} catch (\Stripe\Error\Base $e) {
// An error occurred, so handle it as needed
handle_error($e);
}
}
// Check the subscription status again
$subscription = \Stripe\Subscription::retrieve('SUBSCRIPTION_ID');
if ($subscription->status == 'past_due') {
// Payment failed after three retries, so send a final email to the customer
$to = $subscription->customer_email;
$subject = 'Failed Payment';
$body = ""Your payment for your subscription has failed after multiple retries. As a penalty, an extra AUD$10 has been added to your next payment. To avoid a disruption in service, please follow the instructions below to make a payment:
- Click the following link to make a payment: https://stripe.com/make-payment?amount="".(($subscription->plan->amount + 1000)/100).""¤cy=AUD&customer="".$subscription->customer.""&subscription="".$subscription->id.""
- Select the payment method of your choice.
- The amount of $"" . ($subscription->plan->amount + 1000) / 100 . "" will be automatically populated.
- Confirm the payment.
Thank you for your prompt attention to this matter."";
"
If the customer has insufficient balance in the bank account and they never top up it, you'll always get payment failure responses and therefore be charged for the bank fees.
Yes, that’s right but we only want to try it three times that’s it.
OK, if you really want to do that you can use the invoice item API (https://stripe.com/docs/api/invoiceitems/create?lang=node#create_invoiceitem) to add the penalty to the next invoice.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Can’t we add the penalty to same invoice using the code above?
Your code is to create a separate invoice, and there's a possibility that invoice payment will fail as well.
So my advise is to add the penalty to the upcoming invoice so you can reduce the number of payment failures, but it's your call.
Ok if not retry then can we do something so that the subscription is not cancelled and an invoice is generated along with penalty fees?
Then you just create a new invoice just for the penalty fee, which is independent from the subscription.
Again, I'm strongly against this idea because you'll need to pay lots of bank fee for failed debit payments.
If we do so then there will be two invoices due for the customer
I just want to combine it into one
Then you just use the invoice items API that introduced earlier to add the penalty to the invoice generated by a subscription.
Note that once the subscription is canceled, Stripe would stop auto advancing the related invoice, so you need to manually call the pay API (https://stripe.com/docs/api/invoices/pay?lang=node#pay_invoice) to pay the invoice.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Can we prevent Stripe from canceling the subscriptions automatically?
Yes, you can configure it through settings to leave the subscription as-is https://stripe.com/docs/billing/subscriptions/overview#settings
So, if we leave it as it is then would it try the next payment which could be due next week/month?
yes