-
-
Save emmanuelbarturen/73359128a0c36673c085eb1db77948a4 to your computer and use it in GitHub Desktop.
Extending the Laravel package Cashier for creating an customer without a credit card
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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