Skip to content

Instantly share code, notes, and snippets.

@daugaard47
Created July 25, 2021 04:53
Show Gist options
  • Save daugaard47/7de136dc5f41e26d06b83202a9b80692 to your computer and use it in GitHub Desktop.
Save daugaard47/7de136dc5f41e26d06b83202a9b80692 to your computer and use it in GitHub Desktop.
Get customers on same payment schedule in Stripe
<?php
//THIS IS ONLY A SNIPPET
namespace App\Http\Livewire\Subscribe;
class Subscribe extends Component
{
/*
* Creating the UNIX Timestamp.
* This will grab today's date and make a UNIX Timestamp for the following month on the 1st
* This is to get all customers on the same payment schedule
*/
public $now;
public $firstOfNextMonthUnix;
public function mount(){
$this->now = Carbon::now()->setTimezone('America/Chicago');
$this->firstOfNextMonthUnix = Carbon::parse($this->now)->addMonth()->startOfMonth()->setTime(8, 0, 0, 0)->timestamp;
}
/*
* Creating the Subscription In STRIPE.
* This will charge the full price immediately
*/
$subscriptionStripe = $stripe->subscriptions->create([
'customer' => auth()->user()->stripe_id,
'default_payment_method' => $paymentMethod->id,
'items' => $items,
'metadata' => [],
]);
/*
* Add the newly create subscription in your database.
*/
$subscription = Subscription::create([
'stripe_id' => $subscriptionStripe->id,
'stripe_payment_method_id' => $paymentMethod->id,
// etc...
]);
/*
* Update the Subscription
* This will update the subscription to a trial
* The next invoice will end on the 1st day of the following month
*/
\Stripe\Subscription::update($subscription->stripe_id, [
'default_payment_method' => $subscription->stripe_payment_method_id,
'trial_end' => $this->firstOfNextMonthUnix,
'proration_behavior' => 'none',
]);
/*
* Now you can go into your Stripe dashboard and update the subscription trial to end now.
* This will allow you to test for the next months subscription
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment