|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Helpers; |
| 4 | + |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use Illuminate\Support\Facades\App; |
| 8 | +use Illuminate\Support\Facades\Cache; |
| 9 | +use Laravel\Paddle\Cashier; |
| 10 | +use Laravel\Paddle\ProductPrice; |
| 11 | + |
| 12 | +class Products |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Get the prices for a set of products for a given user. |
| 16 | + * |
| 17 | + * @param \Illuminate\Support\Collection $products |
| 18 | + * @param \App\Models\User|null $user |
| 19 | + * @param int $quantity |
| 20 | + * @return \Illuminate\Support\Collection |
| 21 | + */ |
| 22 | + public function getProductPrices(Collection $products, ?User $user = null, int $quantity = 1): Collection |
| 23 | + { |
| 24 | + $country = $user !== null ? $user->paddleCountry() : config('customers.fallback_country'); |
| 25 | + |
| 26 | + return $this->getPrices($products, $country) |
| 27 | + ->map(function (array $price) use ($country, $quantity) { |
| 28 | + $price['price'] = collect($price['price']) |
| 29 | + ->mapWithKeys(fn ($item, $key) => [$key => $item * $quantity]) |
| 30 | + ->toArray(); |
| 31 | + $pprice = new ProductPrice($country, $price); |
| 32 | + |
| 33 | + return [ |
| 34 | + 'product_id' => $price['product_id'], |
| 35 | + 'price' => $pprice->price()->gross(), |
| 36 | + 'price_value' => $pprice->price()->rawGross(), |
| 37 | + 'currency' => $price['currency'], |
| 38 | + 'frequency' => $pprice->planInterval(), |
| 39 | + 'frequency_name' => $this->getFrequency($pprice), |
| 40 | + ]; |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private function getPrices(Collection $products, string $country): Collection |
| 45 | + { |
| 46 | + $key = $this->getKey($country, $products); |
| 47 | + |
| 48 | + return Cache::remember($key, 60 * 60, function () use ($products, $country) { |
| 49 | + $prices = Cashier::productPrices($products->toArray(), [ |
| 50 | + 'customer_country' => $country, |
| 51 | + ]); |
| 52 | + |
| 53 | + return $prices->map(fn (ProductPrice $price) => $price->toArray()); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private function getKey(string $country, Collection $products): string |
| 58 | + { |
| 59 | + return App::getLocale().'|'.$country.'|'.$products->implode(','); |
| 60 | + } |
| 61 | + |
| 62 | + private function getFrequency(ProductPrice $price): ?string |
| 63 | + { |
| 64 | + $interval = $price->planInterval(); |
| 65 | + $frequency = $price->planFrequency(); |
| 66 | + |
| 67 | + switch ($interval) { |
| 68 | + case 'day': |
| 69 | + return trans_choice('day|:period days', $frequency, ['period' => $frequency]); |
| 70 | + case 'week': |
| 71 | + return trans_choice('week|:period weeks', $frequency, ['period' => $frequency]); |
| 72 | + case 'month': |
| 73 | + return trans_choice('month|:period months', $frequency, ['period' => $frequency]); |
| 74 | + case 'year': |
| 75 | + return trans_choice('year|:period years', $frequency, ['period' => $frequency]); |
| 76 | + default: |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments