style: pint the files touched by the OrderPaid / shop.models.order fix
Formatting only (laravel preset via pint.json): import ordering, FQCN -> use, `new X()` -> `new X`, `!` spacing, phpdoc trims, braces on one-line continue. No behaviour change; suite re-run green (1485 tests). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
0d9823df04
commit
08e459959e
|
|
@ -6,8 +6,12 @@ namespace Blax\Shop\Models;
|
|||
|
||||
use Blax\Shop\Contracts\Cartable;
|
||||
use Blax\Shop\Enums\CartStatus;
|
||||
use Blax\Shop\Enums\PriceType;
|
||||
use Blax\Shop\Enums\PricingStrategy;
|
||||
use Blax\Shop\Enums\ProductType;
|
||||
use Blax\Shop\Enums\PurchaseStatus;
|
||||
use Blax\Shop\Enums\RecurringInterval;
|
||||
use Blax\Shop\Events\CartCreated;
|
||||
use Blax\Shop\Exceptions\CartableInterfaceException;
|
||||
use Blax\Shop\Exceptions\CartAlreadyConvertedException;
|
||||
use Blax\Shop\Exceptions\CartDatesRequiredException;
|
||||
|
|
@ -15,7 +19,9 @@ use Blax\Shop\Exceptions\CartEmptyException;
|
|||
use Blax\Shop\Exceptions\CartItemMissingInformationException;
|
||||
use Blax\Shop\Exceptions\ExceedsMaxPerCartException;
|
||||
use Blax\Shop\Exceptions\ExceedsMaxPerUserException;
|
||||
use Blax\Shop\Exceptions\HasNoPriceException;
|
||||
use Blax\Shop\Exceptions\InvalidDateRangeException;
|
||||
use Blax\Shop\Exceptions\MixedCheckoutModeException;
|
||||
use Blax\Shop\Exceptions\NotEnoughAvailableInTimespanException;
|
||||
use Blax\Shop\Exceptions\NotEnoughStockException;
|
||||
use Blax\Shop\Exceptions\PriceCalculationException;
|
||||
|
|
@ -30,11 +36,16 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Stripe\Checkout\Session;
|
||||
use Stripe\Stripe;
|
||||
use Workbench\App\Models\User;
|
||||
|
||||
class Cart extends Model
|
||||
{
|
||||
use HasUuids, HasExpiration, HasFactory, HasBookingPriceCalculation, ChecksIfBooking;
|
||||
use ChecksIfBooking, HasBookingPriceCalculation, HasExpiration, HasFactory, HasUuids;
|
||||
|
||||
protected $fillable = [
|
||||
'session_id',
|
||||
|
|
@ -66,7 +77,7 @@ class Cart extends Model
|
|||
];
|
||||
|
||||
protected $dispatchesEvents = [
|
||||
'created' => \Blax\Shop\Events\CartCreated::class,
|
||||
'created' => CartCreated::class,
|
||||
];
|
||||
|
||||
public function __construct(array $attributes = [])
|
||||
|
|
@ -97,6 +108,7 @@ class Cart extends Model
|
|||
{
|
||||
$this->last_activity_at = now();
|
||||
$this->saveQuietly(); // Don't trigger events
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
@ -158,6 +170,7 @@ class Cart extends Model
|
|||
{
|
||||
$this->status = CartStatus::EXPIRED;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
@ -168,6 +181,7 @@ class Cart extends Model
|
|||
{
|
||||
$this->status = CartStatus::ABANDONED;
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
@ -232,7 +246,7 @@ class Cart extends Model
|
|||
|
||||
public function purchases(): HasMany
|
||||
{
|
||||
return $this->hasMany(config('shop.models.product_purchase', \Blax\Shop\Models\ProductPurchase::class), 'cart_id');
|
||||
return $this->hasMany(config('shop.models.product_purchase', ProductPurchase::class), 'cart_id');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -240,7 +254,7 @@ class Cart extends Model
|
|||
*/
|
||||
public function order()
|
||||
{
|
||||
return $this->hasOne(config('shop.models.order', \Blax\Shop\Models\Order::class), 'cart_id');
|
||||
return $this->hasOne(config('shop.models.order', Order::class), 'cart_id');
|
||||
}
|
||||
|
||||
public function getTotal(): float
|
||||
|
|
@ -317,8 +331,6 @@ class Cart extends Model
|
|||
* Check if cart is ready for checkout.
|
||||
*
|
||||
* Returns true if all cart items are ready for checkout.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsReadyToCheckoutAttribute(): bool
|
||||
{
|
||||
|
|
@ -347,7 +359,7 @@ class Cart extends Model
|
|||
* }
|
||||
* ```
|
||||
*
|
||||
* @return \Illuminate\Support\Collection Collection of CartItem models requiring adjustments
|
||||
* @return Collection Collection of CartItem models requiring adjustments
|
||||
*/
|
||||
public function getItemsRequiringAdjustments()
|
||||
{
|
||||
|
|
@ -377,6 +389,7 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface|string $until End date (DateTimeInterface or parsable string)
|
||||
* @param bool $validateAvailability Whether to validate product availability for the timespan
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidDateRangeException
|
||||
* @throws NotEnoughAvailableInTimespanException
|
||||
*/
|
||||
|
|
@ -445,6 +458,7 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface|string $from Start date (DateTimeInterface or parsable string)
|
||||
* @param bool $validateAvailability Whether to validate product availability for the timespan
|
||||
* @return $this
|
||||
*
|
||||
* @throws NotEnoughAvailableInTimespanException
|
||||
*/
|
||||
public function setFromDate(
|
||||
|
|
@ -492,6 +506,7 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface|string $until End date (DateTimeInterface or parsable string)
|
||||
* @param bool $validateAvailability Whether to validate product availability for the timespan
|
||||
* @return $this
|
||||
*
|
||||
* @throws NotEnoughAvailableInTimespanException
|
||||
*/
|
||||
public function setUntilDate(\DateTimeInterface|string|int|float $until, bool $validateAvailability = true): self
|
||||
|
|
@ -539,6 +554,7 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface|null $from Optional from date (uses cart's from if not provided)
|
||||
* @param \DateTimeInterface|null $until Optional until date (uses cart's until if not provided)
|
||||
* @return $this
|
||||
*
|
||||
* @throws NotEnoughAvailableInTimespanException
|
||||
*/
|
||||
public function applyDatesToItems(
|
||||
|
|
@ -593,6 +609,7 @@ class Cart extends Model
|
|||
'from' => $itemFrom,
|
||||
'until' => $itemUntil,
|
||||
]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -620,6 +637,7 @@ class Cart extends Model
|
|||
'subtotal' => null,
|
||||
'unit_amount' => null,
|
||||
]);
|
||||
|
||||
// Skip updateDates() since we already set the dates with null price
|
||||
continue;
|
||||
}
|
||||
|
|
@ -646,7 +664,6 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface $from New start date
|
||||
* @param \DateTimeInterface $until New end date
|
||||
* @param bool $overwrite Whether to apply to all items or only those without dates
|
||||
* @return void
|
||||
*/
|
||||
protected function reallocatePoolItems(\DateTimeInterface $from, \DateTimeInterface $until, bool $overwrite = true): void
|
||||
{
|
||||
|
|
@ -654,6 +671,7 @@ class Cart extends Model
|
|||
$poolItems = $this->items()->get()
|
||||
->filter(function ($item) {
|
||||
$product = $item->purchasable;
|
||||
|
||||
return $product instanceof Product && $product->isPool();
|
||||
})
|
||||
->groupBy('purchasable_id');
|
||||
|
|
@ -719,15 +737,16 @@ class Cart extends Model
|
|||
'unit_amount' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sort by pricing strategy
|
||||
usort($singlesWithPrices, function ($a, $b) use ($strategy) {
|
||||
return match ($strategy) {
|
||||
\Blax\Shop\Enums\PricingStrategy::LOWEST => $a['price'] <=> $b['price'],
|
||||
\Blax\Shop\Enums\PricingStrategy::HIGHEST => $b['price'] <=> $a['price'],
|
||||
\Blax\Shop\Enums\PricingStrategy::AVERAGE => 0,
|
||||
PricingStrategy::LOWEST => $a['price'] <=> $b['price'],
|
||||
PricingStrategy::HIGHEST => $b['price'] <=> $a['price'],
|
||||
PricingStrategy::AVERAGE => 0,
|
||||
};
|
||||
});
|
||||
|
||||
|
|
@ -781,13 +800,17 @@ class Cart extends Model
|
|||
$firstAllocation = true;
|
||||
|
||||
foreach ($orderedSingles as $singleInfo) {
|
||||
if ($remainingQty <= 0) break;
|
||||
if ($remainingQty <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$single = $singleInfo['single'];
|
||||
$usedFromSingle = $singleUsage[$single->id] ?? 0;
|
||||
$availableFromSingle = $singleInfo['available'] - $usedFromSingle;
|
||||
|
||||
if ($availableFromSingle <= 0) continue;
|
||||
if ($availableFromSingle <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$qtyToAllocate = min($remainingQty, $availableFromSingle);
|
||||
|
||||
|
|
@ -867,7 +890,7 @@ class Cart extends Model
|
|||
*
|
||||
* @param \DateTimeInterface $from Start date
|
||||
* @param \DateTimeInterface $until End date
|
||||
* @return void
|
||||
*
|
||||
* @throws NotEnoughAvailableInTimespanException
|
||||
*/
|
||||
/**
|
||||
|
|
@ -877,7 +900,6 @@ class Cart extends Model
|
|||
* @param \DateTimeInterface $from Start date
|
||||
* @param \DateTimeInterface $until End date
|
||||
* @param bool $useProvidedDates Whether to use provided dates or item's own dates
|
||||
* @return void
|
||||
*/
|
||||
protected function validateDateAvailability(\DateTimeInterface $from, \DateTimeInterface $until, bool $useProvidedDates = false): void
|
||||
{
|
||||
|
|
@ -951,7 +973,8 @@ class Cart extends Model
|
|||
}
|
||||
|
||||
// If just an ID is passed, try to determine the user model class
|
||||
$userModel = config('auth.providers.users.model', \Workbench\App\Models\User::class);
|
||||
$userModel = config('auth.providers.users.model', User::class);
|
||||
|
||||
return $query->where('customer_id', $userOrId)
|
||||
->where('customer_type', $userModel);
|
||||
}
|
||||
|
|
@ -965,9 +988,6 @@ class Cart extends Model
|
|||
|
||||
/**
|
||||
* Store the cart ID in the session for retrieval across requests
|
||||
*
|
||||
* @param Cart $cart
|
||||
* @return void
|
||||
*/
|
||||
public static function setSession(Cart $cart): void
|
||||
{
|
||||
|
|
@ -982,19 +1002,19 @@ class Cart extends Model
|
|||
* @param array<string, mixed> $parameters Additional parameters for the cart item
|
||||
* @param \DateTimeInterface|null $from Optional start date for bookings
|
||||
* @param \DateTimeInterface|null $until Optional end date for bookings
|
||||
* @return CartItem
|
||||
*
|
||||
* @throws \Exception If the item doesn't implement Cartable interface
|
||||
*/
|
||||
public function addToCart(
|
||||
Model $cartable,
|
||||
int $quantity = 1,
|
||||
array $parameters = [],
|
||||
null|\DateTimeInterface $from = null,
|
||||
null|\DateTimeInterface $until = null
|
||||
?\DateTimeInterface $from = null,
|
||||
?\DateTimeInterface $until = null
|
||||
): CartItem {
|
||||
// $cartable must implement Cartable
|
||||
if (! $cartable instanceof Cartable) {
|
||||
throw new CartableInterfaceException();
|
||||
throw new CartableInterfaceException;
|
||||
}
|
||||
|
||||
// Defaults for cartables that aren't Product / ProductPrice (e.g. an app
|
||||
|
|
@ -1055,6 +1075,7 @@ class Cart extends Model
|
|||
if (! $item->from || ! $item->until) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for overlap: item overlaps if it doesn't end before period starts or start after period ends
|
||||
return ! ($item->until < $from || $item->from > $until);
|
||||
})
|
||||
|
|
@ -1094,6 +1115,7 @@ class Cart extends Model
|
|||
for ($i = 0; $i < $quantity; $i++) {
|
||||
$lastCartItem = $this->addToCart($cartable, 1, $parameters, $from, $until);
|
||||
}
|
||||
|
||||
return $lastCartItem;
|
||||
}
|
||||
|
||||
|
|
@ -1154,7 +1176,7 @@ class Cart extends Model
|
|||
}
|
||||
} elseif ($from || $until) {
|
||||
// If only one date is provided, it's an error
|
||||
throw new CartDatesRequiredException();
|
||||
throw new CartDatesRequiredException;
|
||||
} else {
|
||||
// When adding pool items without dates, validate against total pool capacity
|
||||
// This allows adding items even if currently claimed - date-based validation happens later
|
||||
|
|
@ -1310,7 +1332,7 @@ class Cart extends Model
|
|||
if ($pricePerDay === null) {
|
||||
if ($is_pool) {
|
||||
// For pool products, throw specific error when neither pool nor single items have prices
|
||||
throw \Blax\Shop\Exceptions\HasNoPriceException::poolProductNoPriceAndNoSingleItemPrices($cartable->name);
|
||||
throw HasNoPriceException::poolProductNoPriceAndNoSingleItemPrices($cartable->name);
|
||||
}
|
||||
throw new ProductHasNoPriceException($cartable->name);
|
||||
}
|
||||
|
|
@ -1370,7 +1392,7 @@ class Cart extends Model
|
|||
$priceId = $defaultPrice?->id;
|
||||
$currency = $defaultPrice?->currency;
|
||||
}
|
||||
} elseif ($cartable instanceof \Blax\Shop\Models\ProductPrice) {
|
||||
} elseif ($cartable instanceof ProductPrice) {
|
||||
// If adding a ProductPrice directly, use its ID and currency
|
||||
$priceId = $cartable->id;
|
||||
$currency = $cartable->currency;
|
||||
|
|
@ -1524,6 +1546,7 @@ class Cart extends Model
|
|||
: (array) $item->parameters;
|
||||
ksort($existingParams);
|
||||
ksort($parameters);
|
||||
|
||||
return $existingParams === $parameters;
|
||||
});
|
||||
|
||||
|
|
@ -1831,7 +1854,9 @@ class Cart extends Model
|
|||
|
||||
$dates = [];
|
||||
foreach (($availability['dates'] ?? []) as $iso => $row) {
|
||||
if ($iso < $visibleStartIso || $iso > $visibleEndIso) continue;
|
||||
if ($iso < $visibleStartIso || $iso > $visibleEndIso) {
|
||||
continue;
|
||||
}
|
||||
$dates[$iso] = ($row['max'] ?? 0) >= 1;
|
||||
}
|
||||
|
||||
|
|
@ -1861,8 +1886,7 @@ class Cart extends Model
|
|||
->pluck('id')
|
||||
->all();
|
||||
|
||||
$isAvailableOn = fn (string $iso): bool =>
|
||||
isset($dayRows[$iso]) && ($dayRows[$iso]['max'] ?? 0) >= $required;
|
||||
$isAvailableOn = fn (string $iso): bool => isset($dayRows[$iso]) && ($dayRows[$iso]['max'] ?? 0) >= $required;
|
||||
|
||||
$availableForSelected = $selectedFromIso && $selectedUntilIso;
|
||||
if ($availableForSelected) {
|
||||
|
|
@ -1880,10 +1904,15 @@ class Cart extends Model
|
|||
$windowFitsAt = function (string $startIso) use (&$isAvailableOn, $durationDays, $searchEnd): bool {
|
||||
$cursor = Carbon::parse($startIso);
|
||||
for ($i = 0; $i < $durationDays; $i++) {
|
||||
if ($cursor->gt($searchEnd)) return false;
|
||||
if (! $isAvailableOn($cursor->toDateString())) return false;
|
||||
if ($cursor->gt($searchEnd)) {
|
||||
return false;
|
||||
}
|
||||
if (! $isAvailableOn($cursor->toDateString())) {
|
||||
return false;
|
||||
}
|
||||
$cursor->addDay();
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
|
|
@ -1930,7 +1959,9 @@ class Cart extends Model
|
|||
$datesPartial = [];
|
||||
$partialWindows = [];
|
||||
foreach ($dayRows as $iso => $row) {
|
||||
if ($iso < $visibleStartIso || $iso > $visibleEndIso) continue;
|
||||
if ($iso < $visibleStartIso || $iso > $visibleEndIso) {
|
||||
continue;
|
||||
}
|
||||
$max = $row['max'] ?? 0;
|
||||
$min = $row['min'] ?? 0;
|
||||
if ($max < $required) {
|
||||
|
|
@ -2012,7 +2043,7 @@ class Cart extends Model
|
|||
// Check if cart is already converted
|
||||
if ($this->isConverted()) {
|
||||
if ($throws) {
|
||||
throw new CartAlreadyConvertedException();
|
||||
throw new CartAlreadyConvertedException;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -2024,7 +2055,7 @@ class Cart extends Model
|
|||
|
||||
if ($items->isEmpty()) {
|
||||
if ($throws) {
|
||||
throw new CartEmptyException();
|
||||
throw new CartEmptyException;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -2067,8 +2098,7 @@ class Cart extends Model
|
|||
// Calculate how much of this cart's items are already counted
|
||||
// We need to check if there's still enough stock for what's in this cart
|
||||
$cartItemsForPool = $items->filter(
|
||||
fn($i) =>
|
||||
$i->purchasable_id === $product->id &&
|
||||
fn ($i) => $i->purchasable_id === $product->id &&
|
||||
$i->purchasable_type === get_class($product)
|
||||
);
|
||||
$totalInCart = $cartItemsForPool->sum('quantity');
|
||||
|
|
@ -2087,8 +2117,7 @@ class Cart extends Model
|
|||
// Without dates, check general pool availability
|
||||
$available = $product->getPoolMaxQuantity();
|
||||
$totalInCart = $items->filter(
|
||||
fn($i) =>
|
||||
$i->purchasable_id === $product->id &&
|
||||
fn ($i) => $i->purchasable_id === $product->id &&
|
||||
$i->purchasable_type === get_class($product)
|
||||
)->sum('quantity');
|
||||
|
||||
|
|
@ -2177,10 +2206,10 @@ class Cart extends Model
|
|||
*
|
||||
* @return static The converted cart (fresh state within the transaction scope).
|
||||
*
|
||||
* @throws \Blax\Shop\Exceptions\CartAlreadyConvertedException
|
||||
* @throws \Blax\Shop\Exceptions\CartEmptyException
|
||||
* @throws \Blax\Shop\Exceptions\CartItemMissingInformationException
|
||||
* @throws \Blax\Shop\Exceptions\NotEnoughStockException
|
||||
* @throws CartAlreadyConvertedException
|
||||
* @throws CartEmptyException
|
||||
* @throws CartItemMissingInformationException
|
||||
* @throws NotEnoughStockException
|
||||
* @throws \Throwable For any other unexpected failures during checkout/stock claiming.
|
||||
*/
|
||||
public function checkout(): static
|
||||
|
|
@ -2325,15 +2354,15 @@ class Cart extends Model
|
|||
return null;
|
||||
}
|
||||
|
||||
$type = $price->type instanceof \Blax\Shop\Enums\PriceType
|
||||
$type = $price->type instanceof PriceType
|
||||
? $price->type->value
|
||||
: (string) $price->type;
|
||||
|
||||
if ($type !== \Blax\Shop\Enums\PriceType::RECURRING->value) {
|
||||
if ($type !== PriceType::RECURRING->value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$interval = $price->interval instanceof \Blax\Shop\Enums\RecurringInterval
|
||||
$interval = $price->interval instanceof RecurringInterval
|
||||
? $price->interval->value
|
||||
: (is_string($price->interval) ? $price->interval : null);
|
||||
|
||||
|
|
@ -2346,7 +2375,7 @@ class Cart extends Model
|
|||
$count = 1;
|
||||
}
|
||||
|
||||
if ($interval === \Blax\Shop\Enums\RecurringInterval::QUARTER->value) {
|
||||
if ($interval === RecurringInterval::QUARTER->value) {
|
||||
return ['interval' => 'month', 'interval_count' => 3 * $count];
|
||||
}
|
||||
|
||||
|
|
@ -2368,8 +2397,8 @@ class Cart extends Model
|
|||
*
|
||||
* @param array $options Optional session parameters (success_url, cancel_url, etc.)
|
||||
* @param string|null $url Optional fullPath URL for success and cancel URLs
|
||||
*
|
||||
* @return mixed Stripe\Checkout\Session instance
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function checkoutSession(array $options = [], ?string $url = null)
|
||||
|
|
@ -2379,7 +2408,7 @@ class Cart extends Model
|
|||
}
|
||||
|
||||
// Ensure Stripe is initialized
|
||||
\Stripe\Stripe::setApiKey(config('services.stripe.secret'));
|
||||
Stripe::setApiKey(config('services.stripe.secret'));
|
||||
|
||||
// Validate cart before proceeding (doesn't convert it)
|
||||
$this->validateForCheckout();
|
||||
|
|
@ -2469,6 +2498,7 @@ class Cart extends Model
|
|||
'price' => $priceModel->stripe_price_id,
|
||||
'quantity' => $item->quantity,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -2484,6 +2514,7 @@ class Cart extends Model
|
|||
],
|
||||
'quantity' => $item->quantity,
|
||||
];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -2509,7 +2540,7 @@ class Cart extends Model
|
|||
// session — surface that explicitly rather than letting Stripe reject
|
||||
// it with an opaque error.
|
||||
if ($hasRecurring && $hasOneTime) {
|
||||
throw new \Blax\Shop\Exceptions\MixedCheckoutModeException();
|
||||
throw new MixedCheckoutModeException;
|
||||
}
|
||||
|
||||
$mode = $hasRecurring ? 'subscription' : 'payment';
|
||||
|
|
@ -2565,7 +2596,7 @@ class Cart extends Model
|
|||
}
|
||||
|
||||
try {
|
||||
$session = \Stripe\Checkout\Session::create($sessionParams);
|
||||
$session = Session::create($sessionParams);
|
||||
|
||||
// Store session ID in cart meta
|
||||
$meta = $this->meta ?? (object) [];
|
||||
|
|
@ -2575,14 +2606,14 @@ class Cart extends Model
|
|||
$meta->stripe_session_id = $session->id;
|
||||
$this->update(['meta' => $meta]);
|
||||
|
||||
\Illuminate\Support\Facades\Log::info('Stripe checkout session created', [
|
||||
Log::info('Stripe checkout session created', [
|
||||
'cart_id' => $this->id,
|
||||
'session_id' => $session->id,
|
||||
]);
|
||||
|
||||
return $session;
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error('Stripe checkout session creation failed', [
|
||||
Log::error('Stripe checkout session creation failed', [
|
||||
'cart_id' => $this->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
|
@ -2598,8 +2629,6 @@ class Cart extends Model
|
|||
* - string: The checkout session URL if a session exists and is valid
|
||||
* - null: If no session exists or Stripe is not enabled
|
||||
* - false: If an error occurred while retrieving the session
|
||||
*
|
||||
* @return string|null|false
|
||||
*/
|
||||
public function checkoutSessionLink(array $option = [], ?string $url = null): string|null|false
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ namespace Blax\Shop\Models;
|
|||
|
||||
use Blax\Shop\Enums\OrderStatus;
|
||||
use Blax\Shop\Enums\PurchaseStatus;
|
||||
use Blax\Shop\Events\OrderCreated;
|
||||
use Blax\Shop\Events\OrderPaid;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -14,6 +16,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
|
|
@ -24,7 +27,7 @@ use Illuminate\Support\Facades\DB;
|
|||
*/
|
||||
class Order extends Model
|
||||
{
|
||||
use HasUuids, HasFactory, SoftDeletes;
|
||||
use HasFactory, HasUuids, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'order_number',
|
||||
|
|
@ -89,7 +92,7 @@ class Order extends Model
|
|||
];
|
||||
|
||||
protected $dispatchesEvents = [
|
||||
'created' => \Blax\Shop\Events\OrderCreated::class,
|
||||
'created' => OrderCreated::class,
|
||||
];
|
||||
|
||||
public function __construct(array $attributes = [])
|
||||
|
|
@ -155,7 +158,7 @@ class Order extends Model
|
|||
if ($difference > 0) {
|
||||
$currency = $order->currency ?? config('shop.currency', 'USD');
|
||||
$order->addNote(
|
||||
"Payment received: " . static::formatMoney($difference, $currency),
|
||||
'Payment received: '.static::formatMoney($difference, $currency),
|
||||
'payment',
|
||||
true
|
||||
);
|
||||
|
|
@ -175,7 +178,7 @@ class Order extends Model
|
|||
// and a host subclass configured via shop.models.order gets it too.
|
||||
static::updated(function (Order $order) {
|
||||
if ($order->wasChanged('paid_at') && $order->paid_at && ! $order->getOriginal('paid_at')) {
|
||||
\Blax\Shop\Events\OrderPaid::dispatch($order);
|
||||
OrderPaid::dispatch($order);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -211,6 +214,7 @@ class Order extends Model
|
|||
public static function formatMoney(int $amount, string $currency = 'USD'): string
|
||||
{
|
||||
$formatted = number_format($amount / 100, 2);
|
||||
|
||||
return strtoupper($currency).' '.$formatted;
|
||||
}
|
||||
|
||||
|
|
@ -462,7 +466,7 @@ class Order extends Model
|
|||
|
||||
$currency = $this->currency ?? config('shop.currency', 'USD');
|
||||
$this->addNote(
|
||||
"Refund processed: " . static::formatMoney($amount, $currency) .
|
||||
'Refund processed: '.static::formatMoney($amount, $currency).
|
||||
($reason ? " - Reason: {$reason}" : ''),
|
||||
'refund',
|
||||
true
|
||||
|
|
@ -487,7 +491,7 @@ class Order extends Model
|
|||
* @param string $content The note content
|
||||
* @param string $type The note type (note, status_change, payment, etc.)
|
||||
* @param bool $isCustomerNote Whether the note is visible to the customer
|
||||
* @param \Illuminate\Database\Eloquent\Model|null $author The author model (User, Admin, etc.)
|
||||
* @param Model|null $author The author model (User, Admin, etc.)
|
||||
* @param array|object|null $meta Additional metadata
|
||||
*/
|
||||
public function addNote(
|
||||
|
|
@ -533,7 +537,7 @@ class Order extends Model
|
|||
public function getMeta(?string $key = null, $default = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
return $this->meta ?? new \stdClass();
|
||||
return $this->meta ?? new \stdClass;
|
||||
}
|
||||
|
||||
return $this->meta?->{$key} ?? $default;
|
||||
|
|
@ -544,7 +548,7 @@ class Order extends Model
|
|||
*/
|
||||
public function updateMetaKey(string $key, $value): self
|
||||
{
|
||||
$meta = (array) ($this->meta ?? new \stdClass());
|
||||
$meta = (array) ($this->meta ?? new \stdClass);
|
||||
$meta[$key] = $value;
|
||||
$this->meta = (object) $meta;
|
||||
$this->save();
|
||||
|
|
@ -789,6 +793,7 @@ class Order extends Model
|
|||
public static function getNetRevenue(): int
|
||||
{
|
||||
$result = static::selectRaw('COALESCE(SUM(amount_paid), 0) - COALESCE(SUM(amount_refunded), 0) as net')->first();
|
||||
|
||||
return (int) $result->net;
|
||||
}
|
||||
|
||||
|
|
@ -827,7 +832,7 @@ class Order extends Model
|
|||
public static function getRevenueSummary(\DateTimeInterface $from, \DateTimeInterface $until): array
|
||||
{
|
||||
$stats = static::createdBetween($from, $until)
|
||||
->selectRaw("
|
||||
->selectRaw('
|
||||
COUNT(*) as total_orders,
|
||||
SUM(CASE WHEN status = ? THEN 1 ELSE 0 END) as completed,
|
||||
SUM(CASE WHEN amount_paid > 0 THEN 1 ELSE 0 END) as paid,
|
||||
|
|
@ -837,7 +842,7 @@ class Order extends Model
|
|||
COALESCE(SUM(amount_refunded), 0) as refunded,
|
||||
COALESCE(AVG(amount_total), 0) as avg_order_value,
|
||||
COALESCE(AVG(amount_paid), 0) as avg_paid_amount
|
||||
", [OrderStatus::COMPLETED->value])
|
||||
', [OrderStatus::COMPLETED->value])
|
||||
->first();
|
||||
|
||||
return [
|
||||
|
|
@ -867,7 +872,7 @@ class Order extends Model
|
|||
/**
|
||||
* Get daily revenue breakdown for a date range.
|
||||
*/
|
||||
public static function getDailyRevenue(\DateTimeInterface $from, \DateTimeInterface $until): \Illuminate\Support\Collection
|
||||
public static function getDailyRevenue(\DateTimeInterface $from, \DateTimeInterface $until): Collection
|
||||
{
|
||||
return static::createdBetween($from, $until)
|
||||
->selectRaw('DATE(created_at) as date')
|
||||
|
|
@ -883,7 +888,7 @@ class Order extends Model
|
|||
/**
|
||||
* Get monthly revenue breakdown for a date range.
|
||||
*/
|
||||
public static function getMonthlyRevenue(\DateTimeInterface $from, \DateTimeInterface $until): \Illuminate\Support\Collection
|
||||
public static function getMonthlyRevenue(\DateTimeInterface $from, \DateTimeInterface $until): Collection
|
||||
{
|
||||
return static::createdBetween($from, $until)
|
||||
->selectRaw('YEAR(created_at) as year')
|
||||
|
|
|
|||
|
|
@ -12,12 +12,15 @@ use Blax\Shop\Models\Order;
|
|||
use Blax\Shop\Models\Product;
|
||||
use Blax\Shop\Models\ProductCategory;
|
||||
use Blax\Shop\Models\ProductPrice;
|
||||
use Blax\Shop\Models\ProductPurchase;
|
||||
use Blax\Shop\Models\StripeTransaction;
|
||||
use Blax\Shop\Models\Subscription;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Stripe\Charge;
|
||||
use Stripe\Stripe;
|
||||
|
||||
class ShopService
|
||||
{
|
||||
|
|
@ -27,8 +30,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get all products query builder
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function products(): Builder
|
||||
{
|
||||
|
|
@ -39,7 +40,6 @@ class ShopService
|
|||
* Get a product by ID
|
||||
*
|
||||
* @param mixed $id
|
||||
* @return Product|null
|
||||
*/
|
||||
public function product($id): ?Product
|
||||
{
|
||||
|
|
@ -48,8 +48,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get all categories query builder
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function categories(): Builder
|
||||
{
|
||||
|
|
@ -58,8 +56,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get in-stock products
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function inStock(): Builder
|
||||
{
|
||||
|
|
@ -68,8 +64,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get featured products
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function featured(): Builder
|
||||
{
|
||||
|
|
@ -78,8 +72,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get published and visible products
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function published(): Builder
|
||||
{
|
||||
|
|
@ -88,9 +80,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Search products by query
|
||||
*
|
||||
* @param string $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function search(string $query): Builder
|
||||
{
|
||||
|
|
@ -103,10 +92,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Check if product has available stock for quantity
|
||||
*
|
||||
* @param Product $product
|
||||
* @param int $quantity
|
||||
* @return bool
|
||||
*/
|
||||
public function checkStock(Product $product, int $quantity): bool
|
||||
{
|
||||
|
|
@ -119,9 +104,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get available stock for a product
|
||||
*
|
||||
* @param Product $product
|
||||
* @return int
|
||||
*/
|
||||
public function getAvailableStock(Product $product): int
|
||||
{
|
||||
|
|
@ -134,9 +116,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Check if product is on sale
|
||||
*
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function isOnSale(Product $product): bool
|
||||
{
|
||||
|
|
@ -405,7 +384,7 @@ class ShopService
|
|||
$startOfYear = Carbon::now()->startOfYear();
|
||||
$endOfYear = Carbon::now()->endOfYear();
|
||||
|
||||
$orderStats = $this->orderModel()::selectRaw("
|
||||
$orderStats = $this->orderModel()::selectRaw('
|
||||
COUNT(*) as total,
|
||||
SUM(CASE WHEN status = ? THEN 1 ELSE 0 END) as pending,
|
||||
SUM(CASE WHEN status = ? THEN 1 ELSE 0 END) as processing,
|
||||
|
|
@ -421,7 +400,7 @@ class ShopService
|
|||
COALESCE(SUM(CASE WHEN created_at BETWEEN ? AND ? THEN amount_paid ELSE 0 END), 0) as revenue_this_year,
|
||||
COALESCE(SUM(amount_refunded), 0) as total_refunded,
|
||||
COALESCE(AVG(amount_total), 0) as average_order
|
||||
", [
|
||||
', [
|
||||
OrderStatus::PENDING->value,
|
||||
OrderStatus::PROCESSING->value,
|
||||
OrderStatus::COMPLETED->value,
|
||||
|
|
@ -925,7 +904,6 @@ class ShopService
|
|||
/**
|
||||
* Get shop configuration value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
|
|
@ -936,8 +914,6 @@ class ShopService
|
|||
|
||||
/**
|
||||
* Get default shop currency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function currency(): string
|
||||
{
|
||||
|
|
@ -962,7 +938,7 @@ class ShopService
|
|||
/**
|
||||
* The configured {@see StripeTransaction} model class.
|
||||
*
|
||||
* @return class-string<\Illuminate\Database\Eloquent\Model>
|
||||
* @return class-string<Model>
|
||||
*/
|
||||
protected function ledgerModel(): string
|
||||
{
|
||||
|
|
@ -1233,8 +1209,8 @@ class ShopService
|
|||
}
|
||||
|
||||
try {
|
||||
\Stripe\Stripe::setApiKey($secret);
|
||||
$charge = \Stripe\Charge::retrieve([
|
||||
Stripe::setApiKey($secret);
|
||||
$charge = Charge::retrieve([
|
||||
'id' => $chargeId,
|
||||
'expand' => ['balance_transaction', 'refunds.data.balance_transaction'],
|
||||
]);
|
||||
|
|
@ -1254,7 +1230,7 @@ class ShopService
|
|||
|
||||
return $count;
|
||||
} catch (\Throwable $e) {
|
||||
\Illuminate\Support\Facades\Log::warning('[shop:ledger] syncLedgerForCharge failed', [
|
||||
Log::warning('[shop:ledger] syncLedgerForCharge failed', [
|
||||
'charge' => $chargeId,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue