Hello,
I was looking to have my subscriptions renew on the same date each month (the 20th) regardless of when the customer signs up. I would still like my customers to be charged for the first month when they sign up as well. I see a method:
anchorBillingCycleOn($date = 'now')
And I found this merge request
But when I attempted it, I received the following error:
Stripe\Error\InvalidRequest with message 'Received unknown parameter: billingCycleAnchor'
It seems as though billingCycleAnchor is outdated. Stripe has an article explaining how to change a customers billing cycle anchor: https://stripe.com/docs/subscriptions/billing-cycle
I was able to figure this out on my own by creating a custom function within the Subscription model:
public function changeBillingAnchorDate($date = 'now')
{
$subscription = $this->asStripeSubscription();
$subscription->plan = $this->stripe_plan;
$subscription->prorate = false;
if ($date instanceof DateTimeInterface) {
$date = $date->getTimestamp();
}
$subscription->trial_end = $date;
$subscription->quantity = $this->quantity;
$subscription->save();
return $this;
}
I've found an error in my code above. The code is successful in changing the anchor date, however it will cause issues if a user cancels their subscription (before the next anchor date) and then try and resume it. When resuming it, Cashier won't communicate the added trial days and the subscription will be automatically charged because its end_trial will be set to now.
To fix this, you would add something like:
$this->update(['trial_ends_at' => $date]);
before the return.
This causes the issue of classifying the difference between an actual trial subscription, and a subscription that happens to have a trial_ends_at field set because of the anchor date. To solve this I added a anchor_billing_date field to the subscriptions table. When checking a users trial status I use if($subscription->onTrial() && ! $subscription->anchor_billing_date !== null)
Most helpful comment
It seems as though billingCycleAnchor is outdated. Stripe has an article explaining how to change a customers billing cycle anchor: https://stripe.com/docs/subscriptions/billing-cycle
I was able to figure this out on my own by creating a custom function within the Subscription model: