diff --git a/config/shop.php b/config/shop.php index cec81a1..f0d0dc7 100644 --- a/config/shop.php +++ b/config/shop.php @@ -247,6 +247,24 @@ return [ // product is sold in several named prices. Off by default. 'line_item_price_name' => env('SHOP_STRIPE_LINE_ITEM_PRICE_NAME', false), + /* + * Stripe Checkout Sessions built by Cart::checkoutSession(). + * + * `payment_method_types`: the list sent to Stripe. ['card'] is the + * historic behaviour. null (or []) leaves the key out, so the payment + * methods enabled in the Stripe Dashboard apply (PayPal, SEPA, Link, + * wallets, …). A config without this key keeps ['card']. + * + * `adaptive_pricing`: null leaves the key out (the Dashboard setting + * applies); true/false sends adaptive_pricing.enabled. Stripe's + * conversion to the buyer's currency hides PayPal, Klarna and Revolut + * Pay on converted sessions. + */ + 'checkout' => [ + 'payment_method_types' => ['card'], + 'adaptive_pricing' => null, + ], + // Webhook events that the shop package listens for // You can customize this list to add/remove events as needed 'webhook_events' => [ diff --git a/src/Models/Cart.php b/src/Models/Cart.php index 2f10811..0521379 100644 --- a/src/Models/Cart.php +++ b/src/Models/Cart.php @@ -2622,9 +2622,12 @@ class Cart extends Model ? $cancel_url.'&cart_id='.$this->id : $cancel_url.'?cart_id='.$this->id; - // Prepare session parameters + // Prepare session parameters. payment_method_types comes from + // shop.stripe.checkout.payment_method_types (['card'] unless configured; + // null leaves it out so the Dashboard's payment methods apply). + $methodTypes = $this->checkoutPaymentMethodTypes(); $sessionParams = [ - 'payment_method_types' => ['card'], + ...($methodTypes !== null ? ['payment_method_types' => $methodTypes] : []), 'currency' => strtolower($this->currency ?? config('shop.currency', 'usd')), 'line_items' => $lineItems, 'mode' => $mode, @@ -2636,6 +2639,10 @@ class Cart extends Model ], $options['metadata'] ?? []), ]; + if ($adaptivePricing = $this->checkoutAdaptivePricing()) { + $sessionParams['adaptive_pricing'] = $adaptivePricing; + } + // Propagate the cart id onto the subscription itself so invoice/ // subscription webhooks can map back to this cart, not just the // originating checkout session. @@ -2688,6 +2695,61 @@ class Cart extends Model } } + /** + * The `payment_method_types` sent to Stripe Checkout, from + * `shop.stripe.checkout.payment_method_types`. A config without the key + * keeps the historic ['card']; null or [] returns null (leave the key out, + * so the payment methods enabled in the Stripe Dashboard apply). A + * comma-separated string is accepted too. + * + * @return list|null + */ + protected function checkoutPaymentMethodTypes(): ?array + { + if (! config()->has('shop.stripe.checkout.payment_method_types')) { + return ['card']; + } + + $types = config('shop.stripe.checkout.payment_method_types'); + + if (is_string($types)) { + $types = explode(',', $types); + } + + if (! is_array($types)) { + return null; + } + + $types = array_values(array_unique(array_filter( + array_map(static fn ($type) => trim((string) $type), $types), + static fn (string $type) => $type !== '', + ))); + + return $types === [] ? null : $types; + } + + /** + * The `adaptive_pricing` block for Stripe Checkout, from + * `shop.stripe.checkout.adaptive_pricing`: null (the default) leaves it + * out, true/false sends ['enabled' => bool]. + * + * @return array{enabled: bool}|null + */ + protected function checkoutAdaptivePricing(): ?array + { + $enabled = config('shop.stripe.checkout.adaptive_pricing'); + + if ($enabled === null || $enabled === '') { + return null; + } + + $enabled = is_bool($enabled) + ? $enabled + : filter_var($enabled, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + + return $enabled === null ? null : ['enabled' => $enabled]; + } + /** * Get the checkout session link for this cart. * diff --git a/tests/Feature/Checkout/CartCheckoutSessionTest.php b/tests/Feature/Checkout/CartCheckoutSessionTest.php index 54c4cc4..a6169ad 100644 --- a/tests/Feature/Checkout/CartCheckoutSessionTest.php +++ b/tests/Feature/Checkout/CartCheckoutSessionTest.php @@ -645,6 +645,52 @@ class CartCheckoutSessionTest extends TestCase /** * Mock Stripe Checkout Session creation to avoid actual API calls */ + #[Test] + public function payment_method_types_and_adaptive_pricing_follow_the_config() + { + config(['shop.stripe.enabled' => true]); + config(['services.stripe.secret' => 'sk_test_fake']); + + $product = Product::factory()->create(['name' => 'PM', 'manage_stock' => false]); + ProductPrice::factory()->create([ + 'purchasable_id' => $product->id, + 'purchasable_type' => Product::class, + 'unit_amount' => 500, + 'currency' => 'EUR', + 'is_default' => true, + 'type' => 'one_time', + 'interval' => null, + 'interval_count' => null, + ]); + $this->cart->addToCart($product, 1); + + $captured = null; + \Stripe\Checkout\Session::$createCallback = function ($params) use (&$captured) { + $captured = $params; + $s = new \stdClass(); + $s->id = 'mock'; + + return $s; + }; + $urls = ['success_url' => 'https://example.com/s', 'cancel_url' => 'https://example.com/c']; + + // Default: today's card-only page, no adaptive_pricing key. + $this->cart->checkoutSession($urls); + $this->assertSame(['card'], $captured['payment_method_types']); + $this->assertArrayNotHasKey('adaptive_pricing', $captured); + + // null leaves the key out, so the Dashboard's methods (PayPal, ...) apply. + config(['shop.stripe.checkout.payment_method_types' => null, 'shop.stripe.checkout.adaptive_pricing' => false]); + $this->cart->checkoutSession($urls); + $this->assertArrayNotHasKey('payment_method_types', $captured); + $this->assertSame(['enabled' => false], $captured['adaptive_pricing']); + + config(['shop.stripe.checkout.payment_method_types' => ['card', 'paypal'], 'shop.stripe.checkout.adaptive_pricing' => true]); + $this->cart->checkoutSession($urls); + $this->assertSame(['card', 'paypal'], $captured['payment_method_types']); + $this->assertSame(['enabled' => true], $captured['adaptive_pricing']); + } + protected function mockStripeCheckoutSession() { // Create a simple mock that returns a session object