diff --git a/src/Models/Product.php b/src/Models/Product.php index 58e4bc8..e2c3df1 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -528,4 +528,9 @@ class Product extends Model implements Purchasable, Cartable }) ->where('product_id', $this->id); } + + public function hasPrice() : bool + { + return $this->prices()->exists(); + } } diff --git a/src/Models/ProductPrice.php b/src/Models/ProductPrice.php index c0cd683..a895797 100644 --- a/src/Models/ProductPrice.php +++ b/src/Models/ProductPrice.php @@ -54,7 +54,7 @@ class ProductPrice extends Model implements Cartable public function getCurrentPrice(bool|null $sale_price = null): float { if ($sale_price) { - return $this->sale_unit_amount; + return $this->sale_unit_amount ?? $this->unit_amount; } return $this->unit_amount; diff --git a/tests/Feature/StripeChargeFlowTest.php b/tests/Feature/StripeChargeFlowTest.php index 091123f..ae4fb2d 100644 --- a/tests/Feature/StripeChargeFlowTest.php +++ b/tests/Feature/StripeChargeFlowTest.php @@ -24,10 +24,18 @@ class StripeChargeFlowTest extends TestCase { parent::setUp(); + $key = env('STRIPE_KEY', 'your_stripe_test_key_here'); + $secret = env('STRIPE_SECRET', 'your_stripe_test_secret_here'); + + if (strpos($key, 'your_stripe_test_key_here') >= 0 || + strpos($secret, 'your_stripe_test_secret_here') >= 0) { + $this->markTestSkipped('Stripe test keys are not set in environment variables.'); + } + // Set Stripe test keys config([ - 'cashier.key' => env('STRIPE_KEY', 'sk_test_fake'), - 'cashier.secret' => env('STRIPE_SECRET', 'sk_test_fake'), + 'cashier.key' => $key, + 'cashier.secret' => $secret, ]); Stripe::setApiKey(config('cashier.secret')); diff --git a/tests/Unit/CartTest.php b/tests/Unit/CartTest.php index 9f7387d..97e12cc 100644 --- a/tests/Unit/CartTest.php +++ b/tests/Unit/CartTest.php @@ -52,18 +52,31 @@ class CartTest extends TestCase public function cart_respects_sale_prices() { $cart = Cart::create(); - $product = Product::factory()->create(); + $product = Product::factory()->withPrices(1,50)->create([ + 'sale_start' => now()->subDay(), + 'sale_end' => now()->addDay(), + ]); + + $product->prices()->first()->update([ + 'is_default' => false, + ]); + $price = ProductPrice::create([ 'purchasable_id' => $product->id, 'purchasable_type' => get_class($product), 'unit_amount' => 100.00, 'sale_unit_amount' => 80.00, 'currency' => 'USD', + 'is_default' => true, ]); - $cartItem = $cart->addToCart($price, quantity: 1); + // Assert product has price + $this->assertTrue($product->hasPrice()); + $this->assertEquals(2, $product->prices()->count()); - $this->assertEquals(80.00, $cartItem->price); + $cartItem = $cart->addToCart($product, quantity: 1); + + $this->assertEquals(80.00, $cartItem->getSubtotal()); $this->assertEquals(100.00, $cartItem->regular_price); }