Hahhahahahaha.. What’s the point in this post??? But it’s very usefull for me.. and for you?
For client side you just add this code.. simple huuh?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php use Laravel\Cashier\Billable; use Laravel\Cashier\Contracts\Billable as BillableContract; class User extends Model implements BillableContract { use Billable; protected $dates = ['trial_ends_at', 'subscription_ends_at']; } ?> |
Okeeyyyy, let’s me see behind that code! Trace… trace… and trace.. (how i can make a clean code like this!)
Billable Trait see full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?php namespace Laravel\Cashier; use Carbon\Carbon; use Illuminate\Support\Facades\Config; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; trait Billable { /** * The Stripe API key. * * @var string */ protected static $stripeKey; /** * Get the name that should be shown on the entity's invoices. * * @return string */ public function getBillableName() { return $this->email; } /** * Make a "one off" charge on the customer for the given amount. * * @param int $amount * @param array $options * @return bool|mixed */ public function charge($amount, array $options = []) { return (new StripeGateway($this))->charge($amount, $options); } /** * Get a new billing gateway instance for the given plan. * * @param string|null $plan * @return \Laravel\Cashier\StripeGateway */ public function subscription($plan = null) { return new StripeGateway($this, $plan); } /** * Invoice the billable entity outside of regular billing cycle. * * @return bool */ public function invoice() { return $this->subscription()->invoice(); } ...bla..blaa.. |
Billable Contract see full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace Laravel\Cashier\Contracts; interface Billable { /** * Get the name that should be shown on the entity's invoices. * * @return string */ public function getBillableName(); /** * Get a new billing builder instance for the given plan. * * @param string|null $plan * @return \Laravel\Cashier\Builder */ public function subscription($plan = null); ...bla..blaa.. |
You can see Billable Contract and Billable Trait has same method on there? It’s really make me confused… i’m imagine, if i have 500 method on the Contract… On the the Trait i must have 500 method too!? (hahhaahhaha sorry.. maybe i’m really really newbiesss)
Repository (maybe…) see full code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace Laravel\Cashier; interface BillableRepositoryInterface { /** * Find a Billable implementation by Stripe ID. * * @param string $stripeId * @return \Laravel\Cashier\Contracts\Billable */ public function find($stripeId); } ...bla..blaa.. |
Implement a repository (maybe again…) see full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace Laravel\Cashier; use Illuminate\Support\Facades\Config; use Laravel\Cashier\Contracts\Billable as BillableContract; class EloquentBillableRepository implements BillableRepositoryInterface { /** * Find a Billable implementation by Stripe ID. * * @param string $stripeId * @return \Laravel\Cashier\Contracts\Billable */ public function find($stripeId) { $model = $this->createCashierModel(Config::get('services.stripe.model')); return $model->where($model->getStripeIdName(), $stripeId)->first(); } /** * Create a new instance of the Auth model. * * @param string $class * @return \Laravel\Cashier\Contracts\Billable */ protected function createCashierModel($class) { $model = new $class; if (! $model instanceof BillableContract) { throw new \InvalidArgumentException('Model does not implement Billable.'); } return $model; } } ...bla..blaa.. |
Create a Services Provider see full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace Laravel\Cashier; use Illuminate\Support\ServiceProvider; class CashierServiceProvider extends ServiceProvider { /** * Register the service provider. * * @return void */ public function register() { $this->app->bindShared('Laravel\Cashier\BillableRepositoryInterface', function () { return new EloquentBillableRepository; }); } ...bla..blaa.. |
Huufftttttt… for a newbie like me, I feel it’s really hard to make a clean code, i’m very weird about it. In my mind “If i can make code like this.. i believe it’s very hard to maintance that code” (for now), so happy coding!