serveCouponWin($request->code); } /** * Détermine si un BR doit être servi, renvoi la vue de gain BR * * @param mixed $code * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse * @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException. */ private function serveCouponWin($code) { // Ici on a besoin que d'un identifiant pas de tout le coupon. $crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_1'), 1); if (!$crmCoupon) { return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement."); } $crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_2'), 2); if (!$crmCoupon) { return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement."); } $crmCoupon = $this->updateCoupon(config('opbrcharcut.MESBR_COUPON_3'), 3); if (!$crmCoupon) { return back()->withErrors("Une erreur est survenue, merci de réessayez ultérieurement."); } $user = Auth::guard('customer')->user(); Mail::to($user->email)->send(new ReductionWon($user->apiData, $crmCoupon)); // Marquer le code comme utilisé par le user. $code->code_used = 1; $code->customer()->associate($user); $code->save(); return view('front.opbrcharcut.reduction', compact('crmCoupon')); } /** * Update a coupon * * @param mixed $id_coupon * @param mixed $qtty * @return mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException. */ private function updateCoupon($id_coupon, $qtty) { /** @var \App\Lib\MesBROld */ $mesBRCharc = app('mesBR_Charcut'); /** @var \App\Models\Customers $customer */ $customer = Auth::guard('customer')->user(); /** @var \LDC\LDCCRM\Models\Customer */ $crmCustomer = $customer->apiData; $updateCustID = $this->updateCustomer( $crmCustomer->hashcode, $crmCustomer->email, $crmCustomer->country ); if (!$updateCustID) { return false; } $result = $mesBRCharc->updatePanier($crmCustomer, $updateCustID, $id_coupon, $qtty); if ($result["status"] === "OK") { // Ici on a besoin que savoir que c'était OK. $reduction = $result["link"]; } else { if (app()->bound('sentry')) { app('sentry')->captureException(new \Exception(\sprintf( 'MESBR - BR CHARCUTERIE : Une erreur est survenue. Code : %s Message : %s', $result["code"], $result["message"] ))); } return false; } return $reduction; } /** * updateCustomer * * @param mixed $hascode * @param mixed $email * @param mixed $country * @return mixed * @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException. */ private function updateCustomer($hascode, $email, $country) { /** @var \App\Lib\MesBR */ $mesBR = app('mesBR'); $updateCust = $mesBR->updateCustomer( $hascode, $email, $country ); if ($updateCust->status === 'KO') { if (app()->bound('sentry')) { app('sentry')->captureException(new \Exception(\sprintf( 'MESBR - BR CHARCUTERIE : Une erreur est survenue. Code : %s Message : %s', $updateCust->code, $updateCust->message ))); } return false; } if (!isset($updateCust->customer->id)) { return $this->updateCustomer($hascode, $email, $country); } return $updateCust->customer->id; } }