user(); /** @var \LDC\LDCCRM\Models\Customer $crmCustomer */ $crmCustomer = $customer->apiData; $mesBr = $this->fetchActiveBr($crmCustomer); return view('fo.pages.klub.mesbr', compact('crmCustomer', 'mesBr')); } /** * Fetch PDF BR * * @param \App\Http\Requests\Fo\MesBr\PrintCouponsRequest $request * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function fetchBrPdf(PrintCouponsRequest $request): StreamedResponse { /** @var \App\Models\Customer $customer */ $customer = auth('frontend')->user(); $pdf = $this->fetchBrPdfFromMesBr($customer, $request->post('coupon')); if ($pdf === null) { \back()->withErrors([ 'coupon' => trans('Impossible de récupérer le fichier pdf') ])->throwResponse(); } return response()->streamDownload(function () use ($pdf) { echo $pdf; }, trans('mes-bons-de-reduction-:date.pdf', [ 'date' => now()->isoFormat('DD-MM-Y-HH\hmm') ]), [ 'Cache-Control' => 'public, must-revalidate, max-age=0', 'Content-Description' => 'File Transfer', 'Content-Type' => 'application/pdf', 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', // For glory. 'X-Generator' => 'mPDF' ]); } /** * Fetch coupons PDF * * @param \App\Models\Customer $customer * @param array $coupons * @return string|null */ private function fetchBrPdfFromMesBr(\App\Models\Customer $customer, array $coupons): string|null { /** @var \LDCWEB\MesBR */ $mesBr = app('mesbr'); $message = null; try { $res = $mesBr->printCoupons($customer->hashcode, $coupons); if (!$res) { $message = "ERROR MesBR API : printCoupons on api failed {$customer->hashcode}"; } elseif (Arr::get((array)$res, 'status') === 'KO') { $message = Arr::get( (array)$res, 'message', "ERROR MesBR API : printCoupons on api failed (no message) {$customer->hashcode}" ); } if ($message === null) { return \base64_decode(strval(optional($res)->pdf)); } report(new \RuntimeException($message)); return null; } catch (\Exception $e) { report($e); return null; } //end try } /** * Fetch customer BRs * * This should be an object like * * +"status": "OK" * +"name": "Test tregalette" * +"limited_printing": 0 * +"total": 15 * +"available_amount": 10.5 * +"total_amount": 10.5 * +"coupons": array{ * +"code": "2950675055365" * +"name": "Webcoupon 1€ Filet de canard Confit" * +"value": 1 * +"used": false * +"desktop_image_available": "https://dev.mesbr.eu/coupons/96kjV1ZwGb7Z4m8wyzBCuurEFPlSLHFaAGVWuO8F.png" * +"desktop_image_unavailable": "https://dev.mesbr.eu/coupons/K7BSVFOCs2K1XCi78wR2YgEfGyFH6AJthyupTEYz.png" * +"mobile_image_available": "https://dev.mesbr.eu/coupons/KE3YodVeDPSG7pVzPQZhPEpVGlxwjTPWCJ8WGN29.png" * +"mobile_image_unavailable": "https://dev.mesbr.eu/coupons/FOFhdT0d2X1AURQfYm9O7ac2ca9SGUk3IHCTWJiZ.png" * } * * @param \LDC\LDCCRM\Models\Customer $customer * @return object|null */ private function fetchActiveBr(\LDC\LDCCRM\Models\Customer $customer): object|null { if (!$this->createOrUpdateMesBrCustomer($customer)) { return null; } /** @var \LDCWEB\MesBR */ $mesBr = app('mesbr'); $message = null; try { $res = $mesBr->getMurCustomer($customer->hashcode); if (!$res) { $message = "ERROR MesBR API : getMurCustomer on api failed {$customer->hashcode}"; } elseif (Arr::get((array)$res, 'status') === 'KO') { $message = Arr::get( (array)$res, 'message', "ERROR MesBR API : getMurCustomer on api failed (no message) {$customer->hashcode}" ); } if ($message === null) { // * Replace local picture urls for dev Env if (config('app.env') === 'local') { $res = self::replaceCouponsPictureHost($res); } return $res; } report(new \RuntimeException($message)); return null; } catch (\Exception $e) { report($e); return null; } //end try } /** * Report for sure true if the customer is set in mesBr * * @param \LDC\LDCCRM\Models\Customer $customer * @return boolean */ private function createOrUpdateMesBrCustomer(\LDC\LDCCRM\Models\Customer $customer): bool { // * Ajout du customer sur mes BR /** @var \LDCWEB\MesBR */ $mesBr = app('mesbr'); $message = null; /** * Observed behavior, * calling multiple times createCustomer results in * create or update the customer. */ try { // * Create (or update) the customer $res = $mesBr->createCustomer($customer->hashcode, $customer->email, 'France'); if (!$res) { $message = "ERROR MesBR API : createCustomer on api failed {$customer->hashcode}"; } elseif (Arr::get((array)$res, 'status') === 'KO') { $message = Arr::get( (array)$res, 'message', "ERROR MesBR API : createCustomer on api failed (no message) {$customer->hashcode}" ); } if ($message === null) { return true; } report(new \RuntimeException($message)); return false; } catch (\Exception $e) { report($e); return false; } //end try } // * STATIC Fetch BR /** * Static method to fetch br List * * ! see fetchActiveBr for output object definition * * @return object|null */ public static function getStaticBr(): object|null { /** @var \LDCWEB\MesBR */ $mesBr = app('mesbr'); $message = null; try { $res = $mesBr->getMur(); if (!$res) { $message = 'ERROR MesBR API : getMur on api failed'; } elseif (Arr::get((array)$res, 'status') === 'KO') { $message = Arr::get( (array)$res, 'message', 'ERROR MesBR API : getMur on api failed (no message)' ); } if ($message === null) { // * Replace local picture urls for dev Env if (config('app.env') === 'local') { $res = self::replaceCouponsPictureHost($res); } return $res; } report(new \RuntimeException($message)); return null; } catch (\Exception $e) { report($e); return null; } //end try } /** * Replace coupons picture host * * @param object $res * @return object */ private static function replaceCouponsPictureHost(object $res): object { $replaceUrl = fn (?string $url) => $url ? Str::replace( 'https://dev.mesbr.eu', config('mesbr.endpoint'), $url ) : $url; $res->coupons = collect($res->coupons)->map(fn (object $coupon) => (object)array_merge((array)$coupon, [ 'desktop_image_available' => $replaceUrl($coupon->desktop_image_available), 'desktop_image_unavailable' => $replaceUrl($coupon->desktop_image_unavailable), 'mobile_image_available' => $replaceUrl($coupon->mobile_image_available), 'mobile_image_unavailable' => $replaceUrl($coupon->mobile_image_unavailable), ]))->all(); return $res; } }