Skip to content

Instantly share code, notes, and snippets.

@drsii
Created June 2, 2014 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drsii/71c925f688c587ac812d to your computer and use it in GitHub Desktop.
Save drsii/71c925f688c587ac812d to your computer and use it in GitHub Desktop.
cartalyst stripe package

User

Apply a coupon to the user

$coupon = Input::get('coupon');

$user = User::find(1);

$user->applyCoupon($coupon);

Update the Default Credit Card

$token = Input::get('stripeToken');

$user = User::find(1);

$user->updateDefaultCard($token);

Check if the user has any active subscription

$user = User::find(1);

if ($user->isSubscribed())
{
	//
}

Subscriptions

List all the user subscriptions

$user = User::find(1);

$subscriptions = $user->subscriptions;

Creating subscriptions

Subscribing a user to a plan

$token = Input::get('stripeToken');

$user = User::find(1);

$user
	->subscription()
	->onPlan('monthly')
	->create($token);

Subscribing a user to a plan and apply a coupon to this new subscription

$token = Input::get('stripeToken');

$coupon = Input::get('coupon');

$user = User::find(1);

$user
	->subscription()
	->onPlan('monthly')
	->withCoupon($coupon)
	->create($token);

Create a trial subscription

$token = Input::get('stripeToken');

$user = User::find(1);

$user
	->subscription()
	->onPlan('monthly')
	->trialFor(Carbon::now()->addDays(14))
	->create($token);

Cancelling subscriptions

Cancel a Subscription using its id

$user = User::find(1);

$user
	->subscription(3)
	->cancel();

Cancelling a subscription by passing a Cartalyst\Stripe\Subscription\IlluminateSubscription object.

$user = User::find(1);

$subscription = $user->subscriptions()->where('stripe_id', 'sub_48w0VyQzcNWCe3')->first();

$user
	->subscription($subscription)
	->cancel();

Cancel a subscription at the End of the Period

$user = User::find(1);

$user
	->subscription(3)
	->cancelAtEndOfPeriod();

Updating subscriptions

Apply a trial period on a subscription

$user = User::find(1);

$user
	->subscription(3)
	->setTrialPeriod(Carbon::now()->addDays(14))

Removing the trial period from a subscription

$user = User::find(1);

$user
	->subscription(3)
	->removeTrialPeriod()

Apply a coupon to an existing subscription

$coupon = Input::get('coupon');

$user = User::find(1);

$user
	->subscription(3)
	->applyCoupon($coupon);

Remove a coupon from an existing subscription

$user = User::find(1);

$user
	->subscription(3)
	->removeCoupon();

Resuming subscriptions

Resume a canceled subscription

$user = User::find(1);

$user
	->subscription(3)
	->resume();

Resume a canceled subscription and remove its trial period

$user = User::find(1);

$user
	->subscription(3)
	->skipTrial()
	->resume();

Resume a canceled subscription and change its trial period end date

$user = User::find(1);

$user
	->subscription(3)
	->trialFor(Carbon::now()->addDays(14))
	->resume()

Checking a Subscription Status

First, we need to grab the subscription:

$user = User::find(1);

$subscription = $user->subscriptions->find(3);

To determine if the subscription is on the trial period, you may use the onTrialPeriod() method:

if ($subscription->onTrialPeriod())
{
	//
}

To determine if the subscription is marked as canceled, you may use the canceled method:

if ($subscription->canceled())
{
	//
}

To determine if the subscription has expired, you may use the expired method:

if ($subscription->expired())
{
	//
}

You may also determine if a subscription, is still on their "grace period" until the subscription fully expires. For example, if a user cancels a subscription on March 5th that was scheduled to end on March 10th, the user is on their "grace period" until March 10th.

if ($subscription->onGracePeriod())
{
	//
}

Still a work in progress.

Cards

List all the available Credit Cards

$user = User::find(1);

$cards = $user->cards;

Create a new Credit Card

$token = Input::get('stripeToken');

$user = User::find(1);

$user
	->card()
	->create($token);

If you want to make this new credit card the default credit card, you can use the ->setDefault():

$user = User::find(1);

$user
	->card()
	->setDefault()
	->create($token);

Update a Credit Card

$user = User::find(1);

//$card = $user->cards->find(3);

$arguments = [
	'name' => 'John Doe',
];

$user
	->card(3)
	->update($arguments);
//$user->updateCard($card, $arguments);

Delete a Credit Card

$user = User::find(1);

$user
	->card(3)
	->delete();

Charges

List all the user charges

$user = User::find(1);

$charges = $user->charges;

Refund a payment

$user = User::find(1);

$user
	->charge(3)
	->refund(:amount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment