Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created December 3, 2014 23:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Braunson/a292e30a8ccf00c8b919 to your computer and use it in GitHub Desktop.
Save Braunson/a292e30a8ccf00c8b919 to your computer and use it in GitHub Desktop.
Extending the Laravel package Cashier for creating an customer without a credit card
namespace Acme\V1\Billing;
// BillableInterface.php
use Laravel\Cashier\BillableInterface as CashierInterface;
interface BillableInterface extends CashierInterface {
}
// BillableTrait.php
namespace Acme\V1\Billing;
use Laravel\Cashier\BillableTrait as CashierTrait;
trait BillableTrait {
use CashierTrait;
/**
* Get a new billing gateway instance for the given plan.
*
* @param \Laravel\Cashier\PlanInterface|string|null $plan
* @return \Acme\V1\Billing\StripeGateway
*/
public function subscription($plan = null)
{
if ($plan instanceof PlanInterface) $plan = $plan->getStripeId();
return new StripeGateway($this, $plan);
}
}
// PlanInterface.php
namespace Acme\V1\Billing;
use Laravel\Cashier\PlanInterface as CashierPlanInterface;
interface PlanInterface extends CashierPlanInterface {
}
// StripeGateway.php
namespace Acme\V1\Billing;
use Laravel\Cashier\StripeGateway as CashierStripeGateway;
class StripeGateway extends CashierStripeGateway {
protected $noCard = false;
/**
* Subscribe to the plan for the first time.
*
* @param string $token
* @param array $properties
* @param object|null $customer
* @return void
*/
public function createWithoutCard( $token = null, array $properties = array(), $customer = null )
{
$freshCustomer = false;
if ( ! $customer)
{
$customer = $this->createStripeCustomer($token, $properties);
$freshCustomer = true;
}
elseif ( ! is_null($token))
{
$this->updateCard($token);
}
$this->billable->setStripeSubscription(
$customer->updateSubscription($this->buildPayload())->id
);
$customer = $this->getStripeCustomer($customer->id);
if ($freshCustomer && $trialEnd = $this->getTrialEndForCustomer($customer))
{
$this->billable->setTrialEndDate($trialEnd);
}
$this->updateLocalStripeData($customer);
}
/**
* @param null $token
* @param array $properties
* @param null $customer
*/
public function updateCardWithoutPlan( $token = null, array $properties = array(), $customer = null )
{
$freshCustomer = false;
if ( ! $customer)
{
$customer = $this->createStripeCustomer($token, $properties);
$freshCustomer = true;
}
elseif ( ! is_null($token))
{
$this->updateCard($token);
}
$customer = $this->getStripeCustomer($customer->id);
if ($freshCustomer && $trialEnd = $this->getTrialEndForCustomer($customer))
{
$this->billable->setTrialEndDate($trialEnd);
}
$this->updateLocalStripeData($customer);
}
/**
* Update the local Stripe data in storage.
*
* @param \Stripe_Customer $customer
* @param string|null $plan
* @return void
*/
public function updateLocalStripeData($customer, $plan = null)
{
try
{
$lastFour = $this->getLastFourCardDigits($customer);
}
catch ( \Stripe_InvalidRequestError $exception )
{
$lastFour = null;
}
$this->billable
->setStripeId($customer->id)
->setStripePlan($plan ?: $this->plan)
->setLastFourCardDigits($lastFour)
->setStripeIsActive(true)
->setSubscriptionEndDate(null)
->saveBillableInstance();
}
/**
* @param int $amount
* @param string $description
* @return bool
*/
public function charge( $amount, $description = '' )
{
$customer = $this->getStripeCustomer();
\Stripe_Charge::create([
'amount' => $amount,
'currency' => 'usd',
'customer' => $customer->id,
'description' => $description,
]);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment