-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acc0485
commit 8e44f86
Showing
6 changed files
with
245 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<button | ||
id="checkout-{{ $sessionId }}" | ||
role="link" | ||
style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em" | ||
> | ||
{{ $label }} | ||
</button> | ||
|
||
<div id="error-message"></div> | ||
|
||
<script> | ||
const checkoutButton = document.getElementById('checkout-{{ $sessionId }}'); | ||
checkoutButton.addEventListener('click', function () { | ||
// When the customer clicks on the button, redirect them to Checkout. | ||
Stripe('{{ config('cashier.key') }}').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`. | ||
if (result.error) { | ||
document.getElementById('error-message').innerText = result.error.message; | ||
} | ||
}); | ||
}); | ||
</script> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Laravel\Cashier; | ||
|
||
use Illuminate\Support\Facades\View; | ||
use Stripe\Checkout\Session; | ||
|
||
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 Checkout Session ID. | ||
* | ||
* @return string | ||
*/ | ||
public function sessionId() | ||
{ | ||
return $this->session->id; | ||
} | ||
|
||
/** | ||
* Begin a new Checkout Session. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $owner | ||
* @param array $sessionOptions | ||
* @param array $customerOptions | ||
* @return \Laravel\Cashier\Checkout | ||
*/ | ||
public static function create($owner, array $sessionOptions = [], array $customerOptions = []) | ||
{ | ||
$customer = $owner->createOrGetStripeCustomer($customerOptions); | ||
|
||
$session = Session::create(array_merge([ | ||
'customer' => $customer->id, | ||
'mode' => 'payment', | ||
'success_url' => route('home').'?checkout=success', | ||
'cancel_url' => route('home').'?checkout=cancelled', | ||
'payment_method_types' => ['card'], | ||
], $sessionOptions), Cashier::stripeOptions()); | ||
|
||
return new static($customer, $session); | ||
} | ||
|
||
/** | ||
* Get the View instance for the button. | ||
* | ||
* @param string $label | ||
* @param array $options | ||
* @return \Illuminate\Contracts\View\View | ||
*/ | ||
public function button($label = 'Check out', array $options = []) | ||
{ | ||
return View::make('cashier::checkout', array_merge([ | ||
'label' => $label, | ||
'sessionId' => $this->session->id, | ||
], $options)); | ||
} | ||
|
||
/** | ||
* @return \Stripe\Checkout\Session | ||
*/ | ||
public function asStripeCheckoutSession() | ||
{ | ||
return $this->session; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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