Skip to content

Commit

Permalink
first.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 4, 2014
0 parents commit 23b2d6f
Show file tree
Hide file tree
Showing 29 changed files with 2,972 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php

php:
- 5.3
- 5.4
- 5.5

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script: phpunit
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "laravel/cashier",
"description": "",
"authors": [
{
"name": "Taylor Otwell",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/filesystem": "~4.1",
"illuminate/support": "~4.1",
"nesbot/carbon": "~1.0",
"stripe/stripe-php": "~1.0",
"symfony/http-kernel": "~2.4"
},
"require-dev": {
"illuminate/view": "~4.1",
"mockery/mockery": "0.8.*"
},
"autoload": {
"psr-0": {
"Laravel\\Cashier\\": "src/"
}
},
"minimum-stability": "dev"
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
154 changes: 154 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Laravel Cashier

- [Configuration](#configuration)
- [Subscribing To A Plan](#subscribing-to-a-plan)
- [No Card Up Front](#no-card-up-front)
- [Swapping Subscriptions](#swapping-subscriptions)
- [Cancelling A Subscription](#cancelling-a-subscription)
- [Resuming A Subscription](#resuming-a-subscription)
- [Checking Subscription Status](#checking-subscription-status)
- [Invoices](#invoices)

<a name="configuration"></a>
## Configuration

First, add the Cashier package to your `composer.json` file:

"laravel/cashier": "~1.0"

To use Cashier, we'll need to add several columns to your database. Don't worry, you can use the `cashier:table` Artisan command to create a migration to add the necessary column. Once the migration has been created, simply run the `migrate` command.

Next, add the BillableTrait and appropriate date mutators to your model definition:

use Laravel\Cashier\BillableTrait;

class User extends Eloquent {

use BillableTrait;

protected $dates = ['trial_ends_at', 'subscription_ends_at'];

}

<a name="subscribing-to-a-plan"></a>
## Subscribing To A Plan

Once you have a model instance, you can easily subscribe that user to a given Stripe plane:

$user = User::find(1);

$user->subscription('monthly')->create($creditCardToken);

If you would like to apply a coupon when creating the subscription, you may use the `withCoupon` method:

$user->subscription('monthly')
->withCoupon('code')
->create($creditCardToken);

The `subscription` method will automatically create the Stripe subscription, as well as update your database with Stripe customer ID and other relevant billing information. If your plan includes a trial, the trial end date will also automatically be set on the user record.

If your plan has a trial period, make sure to set the trial end date on your model after subscribing:

$user->trial_ends_at = Carbon::now()->addDays(14);

$user->save();

<a name="no-card-up-front"></a>
## No Card Up Front

If your application offers a free-trial with no credit-card up front, set the `cardUpFront` property on your model to `false`:

protected $cardUpFront = false;

On account creation, be sure to set the trial end date on the model:

$user->trial_ends_at = Carbon::now()->addDays(14);

$user->save();

<a name="swapping-subscriptions"></a>
## Swapping Subscriptions

To swap a user to a new subscription, use the `swap` method:

$user->subscription('premium')->swap();

If the user is on trial, the trial will be maintained as normal. Also, if a "quantity" exists for the subscription, that quantity will also be maintained.

<a name="cancelling-a-subscription"></a>
## Cancelling A Subscription

Cancelling a subscription is a walk in the park:

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

When a subscription is cancelled, Cashier will automatically set the `subscription_ends_at` column on your database. This column is used to know when the `subscribed` method should begin returning `false`. For example, if a customer cancels a subscription on March 1st, but the subscription was not scheduled to end until March 5th, the `subscribed` method will continue to return `true` until March 5th.

<a name="resuming-a-subscription"></a>
## Resuming A Subscription

If a user has cancelled their subscription and you wish to resume it, use the `resume` method:

$user->subscription('monthly')->resume($creditCardToken);

If the user cancels a subscription and then resumes that subscription before the subscription has fully expired, they will not be billed immediately. Their subscription will simply be re-activated, and they will be billed on the original billing cycle.

<a name="checking-subscription-status"></a>
## Checking Subscription Status

To verify that a user is subscribed to your application, use the `subscribed` command:

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

You may also determine if the user is still within their trial period (if applicable) using the `onTrial` method:

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

To determine if the user was once an active subscriber, but has cancelled their subscription, you may use the `cancelled` method:

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

You may also determine if a user has cancelled their subscription, but are 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. Note that the `subscribed` method still returns `true` during this time.

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

The `everSubscribed` method may be used to determine if the user has ever subscribed to a plan in your application:

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

<a name="invoices"></a>
## Invoices

You can easily retrieve an array of a user's invoices using the `invoices` method:

$invoices = $user->invoices();

When listing the invoices for the customer, you may use these helper methods to display the relevant invoice information:

{{ $invoice->id }}

{{ $invoice->dateString() }}

{{ $invoice->dollars() }}

Use the `downloadInvoice` method to generate a PDF download of the invoice. Yes, it's really this easy:

return $user->downloadInvoice($invoice->id, [
'vendor' => 'Your Company',
'product' => 'Your Product',
]);
168 changes: 168 additions & 0 deletions src/Laravel/Cashier/BillableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php namespace Laravel\Cashier;

use DateTime;

interface BillableInterface {

/**
* Get the name that should be shown on the entity's invoices.
*
* @return string
*/
public function getBillableName();

/**
* Write the entity to persistent storage.
*
* @return void
*/
public function saveBillableInstance();

/**
* Get a new billing builder instance for the given plan.
*
* @param \Laravel\Cashier\PlanInterface|string|null $plan
* @return \Laravel\Cashier\Builder
*/
public function subscription($plan = null);

/**
* Invoice the billable entity outside of regular billing cycle.
*
* @return void
*/
public function invoice();

/**
* Find an invoice by ID.
*
* @param string $id
* @return \Laravel\Cashier\Invoice|null
*/
public function findInvoice($id);

/**
* Get an array of the entity's invoices.
*
* @return array
*/
public function invoices();

/**
* Apply a coupon to the billable entity.
*
* @param string $coupon
* @return void
*/
public function applyCoupon($coupon);

/**
* Determine if the entity is within their trial period.
*
* @return bool
*/
public function onTrial();

/**
* Determine if the entity has an active subscription.
*
* @return bool
*/
public function subscribed();

/**
* Determine if the entity's trial has expired.
*
* @return bool
*/
public function expired();

/**
* Determine if the entity is on the given plan.
*
* @param \Laravel\Cashier\PlanInterface|string $plan
* @return bool
*/
public function onPlan($plan);

/**
* Determine if billing requires a credit card up front.
*
* @return bool
*/
public function requiresCardUpFront();

/**
* Determine if the entity is a Stripe customer.
*
* @return bool
*/
public function readyForBilling();

/**
* Determine if the entity has a current Stripe subscription.
*
* @return bool
*/
public function stripeIsActive();

/**
* Set whether the entity has a current Stripe subscription.
*
* @param bool $active
* @return \Laravel\Cashier\BillableInterface
*/
public function setStripeIsActive($active = true);

/**
* Get the Stripe ID for the entity.
*
* @return string
*/
public function getStripeId();

/**
* Set the Stripe ID for the entity.
*
* @param string $stripe_id
* @return \Laravel\Cashier\BillableInterface
*/
public function setStripeId($stripe_id);

/**
* Get the last four digits of the entity's credit card.
*
* @return string
*/
public function getLastFourCardDigits();

/**
* Set the last four digits of the entity's credit card.
*
* @return \Laravel\Cashier\BillableInterface
*/
public function setLastFourCardDigits($digits);

/**
* Get the date on which the trial ends.
*
* @return \DateTime
*/
public function getTrialEndDate();

/**
* Get the subscription end date for the entity.
*
* @return \DateTime
*/
public function getSubscriptionEndDate();

/**
* Set the subscription end date for the entity.
*
* @param \DateTime|null $date
* @return void
*/
public function setSubscriptionEndDate($date);

}
Loading

0 comments on commit 23b2d6f

Please sign in to comment.