diff --git a/resources/views/checkout.blade.php b/resources/views/checkout.blade.php new file mode 100644 index 000000000..2c01383c6 --- /dev/null +++ b/resources/views/checkout.blade.php @@ -0,0 +1,25 @@ + + + +
+ + diff --git a/src/Checkout.php b/src/Checkout.php new file mode 100644 index 000000000..4a495eb1e --- /dev/null +++ b/src/Checkout.php @@ -0,0 +1,56 @@ +owner = $owner; + $this->session = $session; + } + + /** + * Get the View instance for the button. + * + * @param string $label + * @param array $options + * @return \Illuminate\Contracts\View\View + */ + public function button($label = 'Checkout', array $options = []) + { + return View::make('cashier::checkout', array_merge($options, [ + 'label' => $label, + 'stripeKey' => Cashier::stripeKey(), + 'sessionId' => $this->session->id, + ])); + } + + /** + * @return \Stripe\Checkout\Session + */ + public function asStripeCheckoutSession() + { + return $this->session; + } +} diff --git a/src/SubscriptionBuilder.php b/src/SubscriptionBuilder.php index 796d4aa6b..8a6ef69cb 100644 --- a/src/SubscriptionBuilder.php +++ b/src/SubscriptionBuilder.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use DateTimeInterface; +use Stripe\Checkout\Session; use Laravel\Cashier\Exceptions\SubscriptionCreationFailed; class SubscriptionBuilder @@ -226,6 +227,36 @@ public function create($token = null, array $options = []) ]); } + /** + * Begin a new Checkout Session. + * + * @param array $options + * @return \Laravel\Cashier\Checkout + */ + public function checkout(array $options = []) + { + $customer = $this->getStripeCustomer(null, $options); + + $session = Session::create([ + 'customer' => $customer->id, + 'success_url' => route('cashier.checkout.success'), + 'cancel_url' => route('cashier.checkout.cancelled'), + 'payment_method_types' => ['card'], + 'subscription_data' => [ + 'items' => [ + [ + 'plan' => $this->plan, + 'quantity' => $this->quantity, + ], + ], + 'metadata' => $this->metadata, + 'trial_end' => $this->getTrialEndForPayload(), + ], + ], Cashier::stripeOptions()); + + return new Checkout($customer, $session); + } + /** * Get the Stripe customer instance for the current user and token. *