CheckoutController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Utility\PayfastUtility;
  4. use Illuminate\Http\Request;
  5. use App\Models\Category;
  6. use App\Models\Cart;
  7. use App\Models\Order;
  8. use App\Models\Coupon;
  9. use App\Models\CouponUsage;
  10. use App\Models\Address;
  11. use App\Models\CombinedOrder;
  12. use App\Models\Product;
  13. use App\Utility\PayhereUtility;
  14. use App\Utility\NotificationUtility;
  15. use Illuminate\Support\Facades\DB;
  16. use Session;
  17. use Auth;
  18. class CheckoutController extends Controller
  19. {
  20. public function __construct()
  21. {
  22. //
  23. }
  24. //check the selected payment gateway and redirect to that controller accordingly
  25. public function checkout(Request $request)
  26. {
  27. // Minumum order amount check
  28. if (get_setting('minimum_order_amount_check') == 1) {
  29. $subtotal = 0;
  30. foreach (Cart::where('user_id', Auth::user()->id)->get() as $key => $cartItem) {
  31. $product = Product::find($cartItem['product_id']);
  32. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  33. }
  34. if ($subtotal < get_setting('minimum_order_amount')) {
  35. flash(translate('You order amount is less then the minimum order amount'))->warning();
  36. return redirect()->route('home');
  37. }
  38. }
  39. // Minumum order amount check end
  40. if ($request->payment_option != null) {
  41. (new OrderController)->store($request);
  42. $request->session()->put('payment_type', 'cart_payment');
  43. $data['combined_order_id'] = $request->session()->get('combined_order_id');
  44. $request->session()->put('payment_data', $data);
  45. if ($request->session()->get('combined_order_id') != null) {
  46. // If block for Online payment, wallet and cash on delivery. Else block for Offline payment
  47. $decorator = __NAMESPACE__ . '\\Payment\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $request->payment_option))) . "Controller";
  48. if (class_exists($decorator)) {
  49. return (new $decorator)->pay($request);
  50. } else {
  51. $combined_order = CombinedOrder::findOrFail($request->session()->get('combined_order_id'));
  52. foreach ($combined_order->orders as $order) {
  53. $order->manual_payment = 1;
  54. $order->save();
  55. }
  56. flash(translate('Your order has been placed successfully. Please submit payment information from purchase history'))->success();
  57. return redirect()->route('order_confirmed');
  58. }
  59. }
  60. } else {
  61. flash(translate('Select Payment Option.'))->warning();
  62. return back();
  63. }
  64. }
  65. //redirects to this method after a successfull checkout
  66. public function checkout_done($combined_order_id, $payment)
  67. {
  68. $combined_order = CombinedOrder::findOrFail($combined_order_id);
  69. foreach ($combined_order->orders as $key => $order) {
  70. $order = Order::findOrFail($order->id);
  71. $order->payment_status = 'paid';
  72. $order->payment_details = $payment;
  73. $order->save();
  74. calculateCommissionAffilationClubPoint($order);
  75. }
  76. Session::put('combined_order_id', $combined_order_id);
  77. return redirect()->route('order_confirmed');
  78. }
  79. public function get_shipping_info(Request $request)
  80. {
  81. $carts = Cart::where('user_id', Auth::user()->id)->get();
  82. // if (Session::has('cart') && count(Session::get('cart')) > 0) {
  83. if ($carts && count($carts) > 0) {
  84. $categories = Category::all();
  85. return view('frontend.shipping_info', compact('categories', 'carts'));
  86. }
  87. flash(translate('Your cart is empty'))->success();
  88. return back();
  89. }
  90. public function store_shipping_info(Request $request)
  91. {
  92. if ($request->address_id == null) {
  93. flash(translate("Please add shipping address"))->warning();
  94. return back();
  95. }
  96. $carts = Cart::where('user_id', Auth::user()->id)->get();
  97. foreach ($carts as $key => $cartItem) {
  98. $cartItem->address_id = $request->address_id;
  99. $cartItem->save();
  100. }
  101. return view('frontend.delivery_info', compact('carts'));
  102. // return view('frontend.payment_select', compact('total'));
  103. }
  104. public function store_delivery_info(Request $request)
  105. {
  106. $carts = Cart::where('user_id', Auth::user()->id)
  107. ->get();
  108. if ($carts->isEmpty()) {
  109. flash(translate('Your cart is empty'))->warning();
  110. return redirect()->route('home');
  111. }
  112. $shipping_info = Address::where('id', $carts[0]['address_id'])->first();
  113. $total = 0;
  114. $tax = 0;
  115. $shipping = 0;
  116. $subtotal = 0;
  117. if ($carts && count($carts) > 0) {
  118. foreach ($carts as $key => $cartItem) {
  119. $product = Product::find($cartItem['product_id']);
  120. $tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  121. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  122. if ($request['shipping_type_' . $product->user_id] == 'pickup_point') {
  123. $cartItem['shipping_type'] = 'pickup_point';
  124. $cartItem['pickup_point'] = $request['pickup_point_id_' . $product->user_id];
  125. } else {
  126. $cartItem['shipping_type'] = 'home_delivery';
  127. }
  128. $cartItem['shipping_cost'] = 0;
  129. if ($cartItem['shipping_type'] == 'home_delivery') {
  130. $cartItem['shipping_cost'] = getShippingCost($carts, $key);
  131. }
  132. if (isset($cartItem['shipping_cost']) && is_array(json_decode($cartItem['shipping_cost'], true))) {
  133. foreach (json_decode($cartItem['shipping_cost'], true) as $shipping_region => $val) {
  134. if ($shipping_info['city'] == $shipping_region) {
  135. $cartItem['shipping_cost'] = (double)($val);
  136. break;
  137. } else {
  138. $cartItem['shipping_cost'] = 0;
  139. }
  140. }
  141. } else {
  142. if (!$cartItem['shipping_cost'] ||
  143. $cartItem['shipping_cost'] == null ||
  144. $cartItem['shipping_cost'] == 'null') {
  145. $cartItem['shipping_cost'] = 0;
  146. }
  147. }
  148. $shipping += $cartItem['shipping_cost'];
  149. $cartItem->save();
  150. }
  151. $total = $subtotal + $tax + $shipping;
  152. $tpwd = Auth::user()->tpwd;
  153. return view('frontend.payment_select', compact('carts', 'shipping_info', 'total', 'tpwd'));
  154. } else {
  155. flash(translate('Your Cart was empty'))->warning();
  156. return redirect()->route('home');
  157. }
  158. }
  159. public function apply_coupon_code(Request $request)
  160. {
  161. $coupon = Coupon::where('code', $request->code)->first();
  162. $response_message = array();
  163. if ($coupon != null) {
  164. if (strtotime(date('d-m-Y')) >= $coupon->start_date && strtotime(date('d-m-Y')) <= $coupon->end_date) {
  165. if (CouponUsage::where('user_id', Auth::user()->id)->where('coupon_id', $coupon->id)->first() == null) {
  166. $coupon_details = json_decode($coupon->details);
  167. $carts = Cart::where('user_id', Auth::user()->id)
  168. ->where('owner_id', $coupon->user_id)
  169. ->get();
  170. $coupon_discount = 0;
  171. if ($coupon->type == 'cart_base') {
  172. $subtotal = 0;
  173. $tax = 0;
  174. $shipping = 0;
  175. foreach ($carts as $key => $cartItem) {
  176. $product = Product::find($cartItem['product_id']);
  177. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  178. $tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  179. $shipping += $cartItem['shipping_cost'];
  180. }
  181. $sum = $subtotal + $tax + $shipping;
  182. if ($sum >= $coupon_details->min_buy) {
  183. if ($coupon->discount_type == 'percent') {
  184. $coupon_discount = ($sum * $coupon->discount) / 100;
  185. if ($coupon_discount > $coupon_details->max_discount) {
  186. $coupon_discount = $coupon_details->max_discount;
  187. }
  188. } elseif ($coupon->discount_type == 'amount') {
  189. $coupon_discount = $coupon->discount;
  190. }
  191. }
  192. } elseif ($coupon->type == 'product_base') {
  193. foreach ($carts as $key => $cartItem) {
  194. $product = Product::find($cartItem['product_id']);
  195. foreach ($coupon_details as $key => $coupon_detail) {
  196. if ($coupon_detail->product_id == $cartItem['product_id']) {
  197. if ($coupon->discount_type == 'percent') {
  198. $coupon_discount += (cart_product_price($cartItem, $product, false, false) * $coupon->discount / 100) * $cartItem['quantity'];
  199. } elseif ($coupon->discount_type == 'amount') {
  200. $coupon_discount += $coupon->discount * $cartItem['quantity'];
  201. }
  202. }
  203. }
  204. }
  205. }
  206. if ($coupon_discount > 0) {
  207. Cart::where('user_id', Auth::user()->id)
  208. ->where('owner_id', $coupon->user_id)
  209. ->update(
  210. [
  211. 'discount' => $coupon_discount / count($carts),
  212. 'coupon_code' => $request->code,
  213. 'coupon_applied' => 1
  214. ]
  215. );
  216. $response_message['response'] = 'success';
  217. $response_message['message'] = translate('Coupon has been applied');
  218. } else {
  219. $response_message['response'] = 'warning';
  220. $response_message['message'] = translate('This coupon is not applicable to your cart products!');
  221. }
  222. } else {
  223. $response_message['response'] = 'warning';
  224. $response_message['message'] = translate('You already used this coupon!');
  225. }
  226. } else {
  227. $response_message['response'] = 'warning';
  228. $response_message['message'] = translate('Coupon expired!');
  229. }
  230. } else {
  231. $response_message['response'] = 'danger';
  232. $response_message['message'] = translate('Invalid coupon!');
  233. }
  234. $carts = Cart::where('user_id', Auth::user()->id)
  235. ->get();
  236. $shipping_info = Address::where('id', $carts[0]['address_id'])->first();
  237. $returnHTML = view('frontend.partials.cart_summary', compact('coupon', 'carts', 'shipping_info'))->render();
  238. return response()->json(array('response_message' => $response_message, 'html' => $returnHTML));
  239. }
  240. public function remove_coupon_code(Request $request)
  241. {
  242. Cart::where('user_id', Auth::user()->id)
  243. ->update(
  244. [
  245. 'discount' => 0.00,
  246. 'coupon_code' => '',
  247. 'coupon_applied' => 0
  248. ]
  249. );
  250. $coupon = Coupon::where('code', $request->code)->first();
  251. $carts = Cart::where('user_id', Auth::user()->id)
  252. ->get();
  253. $shipping_info = Address::where('id', $carts[0]['address_id'])->first();
  254. return view('frontend.partials.cart_summary', compact('coupon', 'carts', 'shipping_info'));
  255. }
  256. public function apply_club_point(Request $request)
  257. {
  258. if (addon_is_activated('club_point')) {
  259. $point = $request->point;
  260. if (Auth::user()->point_balance >= $point) {
  261. $request->session()->put('club_point', $point);
  262. flash(translate('Point has been redeemed'))->success();
  263. } else {
  264. flash(translate('Invalid point!'))->warning();
  265. }
  266. }
  267. return back();
  268. }
  269. public function remove_club_point(Request $request)
  270. {
  271. $request->session()->forget('club_point');
  272. return back();
  273. }
  274. public function order_confirmed()
  275. {
  276. $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
  277. Cart::where('user_id', $combined_order->user_id)
  278. ->delete();
  279. //Session::forget('club_point');
  280. //Session::forget('combined_order_id');
  281. foreach ($combined_order->orders as $order) {
  282. NotificationUtility::sendOrderPlacedNotification($order);
  283. }
  284. return view('frontend.order_confirmed', compact('combined_order'));
  285. }
  286. }