Skip to content

Commit

Permalink
Implement Checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Apr 26, 2019
1 parent ac5ce34 commit 03b39c2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
25 changes: 25 additions & 0 deletions resources/views/checkout.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Create a button that your customers click to complete their purchase. Customize the styling to suit your branding. -->
<button
style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em"
id="checkout-{{ $sessionId }}"
role="link"
>
{{ $label }}
</button>

<div id="error-message"></div>

<script>
var checkoutButton = document.getElementById('checkout-{{ $sessionId }}');
checkoutButton.addEventListener('click', function () {
// When the customer clicks on the button, redirect them to Checkout.
Stripe('{{ $stripeKey }}').redirectToCheckout({
sessionId: '{{ $sessionId }}'
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
});
</script>
56 changes: 56 additions & 0 deletions src/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Laravel\Cashier;

use Stripe\Checkout\Session;
use Illuminate\Support\Facades\View;

class Checkout
{
/**
* @var \Illuminate\Database\Eloquent\Model
*/
protected $owner;

/**
* @var \Stripe\Checkout\Session
*/
protected $session;

/**
* Create a new Checkout instance.
*
* @param \Illuminate\Database\Eloquent\Model $owner
* @param \Stripe\Checkout\Session $session
* @return void
*/
public function __construct($owner, Session $session)
{
$this->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;
}
}
31 changes: 31 additions & 0 deletions src/SubscriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use DateTimeInterface;
use Stripe\Checkout\Session;
use Laravel\Cashier\Exceptions\SubscriptionCreationFailed;

class SubscriptionBuilder
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 03b39c2

Please sign in to comment.