Range::class, 'product' => Product::class, ]); // Cashier shall use customers. Cashier::useCustomerModel(Customer::class); // * HTTPS FIX URL::forceScheme('https'); // * HA PROXY FIX App::singleton('clientIp', function () { if (config('app.env') === 'local') { return request()->getClientIp(); } else { return request()->server('HTTP_X_FORWARDED_FOR') ?? request()->getClientIp(); } }); // * Basket singleton App::singleton('basket', function () { return Cart::instance('basket'); }); $this->setAppLocaleAndPaymentCurrency(); $this->setCorsAllowedPaymentEndpoints(); $this->sharetoView(); } /** * Set app locale, formatters and payment currency * * @return void */ private function setAppLocaleAndPaymentCurrency(): void { // * LOCAL FORCE eg: for Carbon setlocale(LC_ALL, \sprintf( '%s_%s.UTF-8', config('app.locale'), \strtoupper(config('app.locale')) )); Carbon::setLocale(config('app.locale')); // * Php Money currency (This is used everywhere), default is Euro App::singleton('phpMoneyCurrency', function () { return new Currency(Str::upper('eur')); }); // * Php Money formater (using app locale) App::singleton('phpMoneyFormatter', function () { // * Number format using app locale. $currencies = new ISOCurrencies(); $numberFormatter = new \NumberFormatter(\sprintf( '%s_%s', config('app.locale'), \strtoupper(config('app.locale')) ), \NumberFormatter::CURRENCY); return new IntlMoneyFormatter($numberFormatter, $currencies); }); // * Cashier currency locale \config()->set('cashier.key', \sprintf( '%s_%s', config('app.locale'), \strtoupper(config('app.locale')) )); $this->setCashierStripeEnvVars(); } /** * Set cors allowed allowed_origins_patterns * ! This is for payment mode * * @return void */ private function setCorsAllowedPaymentEndpoints(): void { $paymentModeList = \collect(PaymentModeEnum::toArray()) ->map(function (PaymentModeEnum $paymentMode) { return 'https:\/\/[a-z0-9\-\.]*\.' . \str_replace('.', '\.', $paymentMode->endpoint); }); $allowed_origins_patterns = \collect(\config('cors.allowed_origins_patterns', []))->concat($paymentModeList); \config()->set('cors.allowed_origins_patterns', $allowed_origins_patterns->all()); } /** * Inject cashier Stripe env vars * * @return void */ private function setCashierStripeEnvVars(): void { // * Set Cashier env variables from config. /** @var \App\Models\Shop\PaymentMode|null $paymentModeSettings */ $paymentModeSettings = PaymentMode::getCached()->where('mode', PaymentModeEnum::stripe())->first(); if (!$paymentModeSettings) { $message = 'Missing Stripe payment mode config'; Log::warning($message); $previousMessage = session()->get('error', ''); session()->now('error', $message . " {$previousMessage}"); return; } $stripePublicKey = \strval($paymentModeSettings->data->payment_mode_stripe_key->value ?? ''); $stripeSecretKey = \strval($paymentModeSettings->data->payment_mode_stripe_secret->value ?? ''); $stripeSecretKeyWebhook = \strval($paymentModeSettings->data->payment_mode_stripe_webhook_secret->value ?? ''); if ($paymentModeSettings->status and empty($stripePublicKey)) { $message = 'Missing Stripe payment public key config'; Log::warning($message); $previousMessage = session()->get('error', ''); session()->now('error', $message . " {$previousMessage}"); return; } if ($paymentModeSettings->status and empty($stripeSecretKey)) { $message = 'Missing Stripe payment private key config'; Log::warning($message); $previousMessage = session()->get('error', ''); session()->now('error', $message . " {$previousMessage}"); return; } if ($paymentModeSettings->status and empty($stripeSecretKeyWebhook)) { $message = 'Missing Stripe payment private webhook key config'; Log::warning($message); $previousMessage = session()->get('error', ''); session()->now('error', $message . " {$previousMessage}"); return; } if (empty($stripePublicKey) or empty($stripeSecretKey) or empty($stripeSecretKeyWebhook)) { return; } \config()->set('cashier.key', $stripePublicKey); \config()->set('cashier.secret', $stripeSecretKey); \config()->set('cashier.webhook.secret', $stripeSecretKeyWebhook); } /** * Share Vars to view * * @return void */ private function sharetoView(): void { // * View shares // ! Add view file pathes (also needed for console xml job). View::addNamespace('back', resource_path('views')); View::addNamespace('theme', resource_path('theme/views')); if (!app()->runningInConsole()) { $appInfos = Cache::remember( 'composer', 360, function () { return json_decode(File::get(\app_path('../composer.json'))); } ); // Share composer.json infos . View::share('cpsr_name', $appInfos->name); View::share('cpsr_version', $appInfos->version); View::share('cpsr_license', $appInfos->license); View::share('cpsr_time', now()->parse($appInfos->time)->isoFormat('Y')); try { View::share('GeneralConfigGlobal', GeneralConfig::getCached()); } catch (\Exception $e) { report($e); View::share('GeneralConfigGlobal', collect()); } //end try // * FORCE BOOTSTRAP PAGINATOR (or custom if in front) // Early boot, request wont be filled as expected. if (collect(explode('/', \request()->getPathInfo()))->get(1) === 'bo') { Paginator::useBootstrap(); } else { Paginator::defaultview('theme::modules.pagination'); } // * Payment Modes View::share('PaymentModesGlobal', PaymentMode::getCached()); \app()->singleton('PaymentModesGlobal', function () { return PaymentMode::getCached(); }); // * Delivery Mode View::share('DeliveryModesGlobal', DeliveryMode::getCached()); \app()->singleton('DeliveryModesGlobal', function () { return DeliveryMode::getCached(); }); // * TAGS View::share('PublishedTagsGlobal', Tag::getCached()); // * RANGES View::share('PublishedRangesGlobal', Range::getCached()); // * CRUD STATIC PAGES View::share('CrudStaticPagesGlobal', StaticPage::getCached()->filter(function (StaticPage $staticPage) { return \in_array($staticPage->type, [ StaticPageType::crud(), StaticPageType::contact() ]); })->sortBy('type', \SORT_REGULAR, true)); } //end if } }