Skip to content

Commit

Permalink
fix(checkout): Unset 'return_url' for embedded UI without redirection
Browse files Browse the repository at this point in the history
Adjusts the checkout logic to unset 'return_url' when the UI is in 'embedded' mode and the 'redirect_on_completion' option is set to 'never'. This change addresses API constraints and prevents errors associated with empty 'return_url' values in no-redirect scenarios.
  • Loading branch information
lowbits committed Jan 8, 2024
1 parent 735ca3c commit fbcce40
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public static function create($owner, array $sessionOptions = [], array $custome
// 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');

// Remove return URL for embedded UI mode when no redirection is desired on completion...
if(isset($data['redirect_on_completion']) && $data['redirect_on_completion'] === 'never') {
unset($data['return_url']);
}
} else {
$data['success_url'] = $sessionOptions['success_url'] ?? route('home').'?checkout=success';
$data['cancel_url'] = $sessionOptions['cancel_url'] ?? route('home').'?checkout=cancelled';
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,26 @@ public function test_customers_can_start_an_embedded_product_checkout_session()

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

public function test_customers_can_start_an_embedded_product_checkout_session_without_a_redirect()
{
$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',
'redirect_on_completion' => 'never',
]);

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

0 comments on commit fbcce40

Please sign in to comment.