Skip to content

Commit

Permalink
[14.x] Allow creation of checkouts with ui_mode=embedded (#1605)
Browse files Browse the repository at this point in the history
* Allow creation of embedded checkouts

* wip

* Update Checkout.php

---------

Co-authored-by: Dries Vints <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent 503d6a0 commit 3e0ea73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public static function create($owner, array $sessionOptions = [], array $custome
{
$data = array_merge([
'mode' => Session::MODE_PAYMENT,
'success_url' => $sessionOptions['success_url'] ?? route('home').'?checkout=success',
'cancel_url' => $sessionOptions['cancel_url'] ?? route('home').'?checkout=cancelled',
], $sessionOptions);

if ($owner) {
Expand All @@ -96,6 +94,14 @@ public static function create($owner, array $sessionOptions = [], array $custome
$data['subscription_data']['metadata']['is_on_session_checkout'] = true;
}

// Remove success and cancel URLs if "ui_mode" is "embedded"...
if (isset($data['ui_mode']) && $data['ui_mode'] === 'embedded') {
$data['return_url'] = $sessionOptions['return_url'] ?? route('home');
} else {
$data['success_url'] = $sessionOptions['success_url'] ?? route('home').'?checkout=success';
$data['cancel_url'] = $sessionOptions['cancel_url'] ?? route('home').'?checkout=cancelled';
}

$session = $stripe->checkout->sessions->create($data);

return new static($owner, $session);
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,26 @@ public function test_guest_customers_can_start_a_checkout_session()

$this->assertInstanceOf(Checkout::class, $checkout);
}

public function test_customers_can_start_an_embedded_product_checkout_session()
{
$user = $this->createCustomer('customers_can_start_an_embedded_product_checkout_session');

$shirtPrice = self::stripe()->prices->create([
'currency' => 'USD',
'product_data' => [
'name' => 'T-shirt',
],
'unit_amount' => 1500,
]);

$items = [$shirtPrice->id => 5];

$checkout = $user->checkout($items, [
'ui_mode' => 'embedded',
'return_url' => 'http://example.com',
]);

$this->assertInstanceOf(Checkout::class, $checkout);
}
}

0 comments on commit 3e0ea73

Please sign in to comment.