stripe = new StripeService(); $this->paypal = new PaypalService(); $this->subscriptionCouponsRepository = new SubscriptionCouponsRepository(); } public function index() { return view('wtialacarte::index'); } public function orders() { $orders = AlacarteOrder::with(['user','items.product'])->where('user_id',auth()->id())->latest()->get(); return response()->json([ 'orders' => $orders ]); } public function products() { $products = Alacarte::where('status',1)->latest()->get(); return response()->json([ 'products' => $products ]); } public function checkout(Request $request) { try { if($request->payment_method == 'stripe'){ $stripeResponse = $this->stripe->createSession($request->items); return response()->json([ 'session_id' => $stripeResponse['id'] ],200); } else { $coupon_data = null; if($request->promo_code){ $coupon_data = $this->subscriptionCouponsRepository->validate($request->promo_code, auth()->user()->company->storefront ?? null); } $paypalResponse = $this->paypal->order($request->items,$coupon_data); return response()->json([ 'redirect_url' => $paypalResponse->links[1]->href ],200); } } catch (\Exception $e) { return response()->json([ 'message' => $e->getMessage() ], 500); } } public function applyCoupon(Request $request) { $request->validate([ 'code' => 'required' ]); try { $storefront = auth()->user()->company->storefront ?? null; $response = $this->subscriptionCouponsRepository->validate($request->code, $storefront); if($response == 'already_redeemed_once'){ return response()->json([ 'error' => 'Promo Code can be used once only with same strore front!' ], 500); } else if($response == 'not_found'){ return response()->json([ 'error' => 'Promo Code Not Found!' ], 500); } else if($response){ return response()->json([ 'message' => 'Coupon Successfully Applied', 'alacarte_ids' => $response->alacarte_id, 'type' => $response->type, 'duration' => $response->duration, 'discount' => $response->discount, 'payment_mode' => $response->payment_mode, ], 200); } else { return response()->json([ 'error' => 'Promo Code Not Valid!' ], 500); } } catch (\Exception $e) { return response()->json([ 'message' => $e->getMessage() ], 500); } } }