first(); return view('front.basket',compact('items','total','banner')); } public function addCart(Request $request) { $product = Product::where('slug', $request->slug)->first(); if (!$product) { return response()->json(['message' => 'Product not found'], 404); } if ($request->price == 0) { return response()->json(['message' => 'Price should not be 0'], 400); } $uniqueId = $product->id . '_' . $request->size; $cartItem = \Cart::get($uniqueId); if ($cartItem) { \Cart::update($uniqueId, [ 'quantity' => +1, ]); } else { \Cart::add([ 'id' => $uniqueId, 'name' => $product->name, 'slug' => $product->slug, 'price' => $request->price, 'quantity' => 1, 'attributes' => [ 'weight' => $request->size, 'dealer' => $request->dealer, 'image' => $product->imageCover->image, ], 'associatedModel' => $product, ]); } return response()->json(['message' => 'Item has been added to the cart']); } public function getCartQuantity() { $totalQuantity = \Cart::getTotalQuantity(); return response()->json(['totalQuantity' => $totalQuantity]); } public function getCartPrice() { $totalPrice = \Cart::getTotal(); return response()->json(['totalPrice' => $totalPrice]); } public function addQuantity($productId) { \Cart::update($productId, [ 'quantity' => +1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been increased', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function decreaseQuantity($productId) { \Cart::update($productId, [ 'quantity' => -1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been decrease', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function removeItem($productId) { \Cart::remove($productId); return response()->json([ 'message' => 'Item has been removed from the cart', ]); } public function applyCoupon(Request $request) { $couponCode = $request->input('coupon_code'); $code = CouponCode::where('name',$couponCode)->first(); if ($code && $code->isUse == '0') { $code->update([ 'isUse' => '1' ]); $value = '-'.$code->discount.'%'; $condition = new \Darryldecode\Cart\CartCondition(array( 'name' => $code->name, 'type' => 'coupon', 'target' => 'subtotal', 'value' => $value )); \Cart::condition($condition); return response()->json([ 'message' => 'Coupon code used successfully', ]); } else { // \Cart::removeConditionsByType('coupon'); return response()->json([ 'message' => 'You have entered an invalid coupon or used coupon code.', ],500); } } public function Checkout() { $banner = Banner::where('page', 'Checkout')->first(); $total = \Cart::getTotal(); $items = \Cart::getContent(); //dd($items); $totalWeight = 0; foreach ($items as $item) { if (!$item->attributes->dealer) { $weight = Size::whereId($item->attributes->weight)->first()->weight ?? 0; $totalWeight += $weight * $item->quantity; } } $shippingPrice = $this->calculateShipping($totalWeight); $grandTotal = $total + $shippingPrice; $countryList = [ "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua and Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "BY" => "Belarus", "BE" => "Belgium", "BZ" => "Belize", "BJ" => "Benin", "BM" => "Bermuda", "BT" => "Bhutan", "BO" => "Bolivia", "BA" => "Bosnia and Herzegovina", "BW" => "Botswana", "BV" => "Bouvet Island", "BR" => "Brazil", "BQ" => "British Antarctic Territory", "IO" => "British Indian Ocean Territory", "VG" => "British Virgin Islands", "BN" => "Brunei", "BG" => "Bulgaria", "BF" => "Burkina Faso", "BI" => "Burundi", "KH" => "Cambodia", "CM" => "Cameroon", "CA" => "Canada", "CT" => "Canton and Enderbury Islands", "CV" => "Cape Verde", "KY" => "Cayman Islands", "CF" => "Central African Republic", "TD" => "Chad", "CL" => "Chile", "CN" => "China", "CX" => "Christmas Island", "CC" => "Cocos [Keeling] Islands", "CO" => "Colombia", "KM" => "Comoros", "CG" => "Congo - Brazzaville", "CD" => "Congo - Kinshasa", "CK" => "Cook Islands", "CR" => "Costa Rica", "HR" => "Croatia", "CU" => "Cuba", "CY" => "Cyprus", "CZ" => "Czech Republic", "CI" => "Côte d’Ivoire", "DK" => "Denmark", "DJ" => "Djibouti", "DM" => "Dominica", "DO" => "Dominican Republic", "NQ" => "Dronning Maud Land", "DD" => "East Germany", "EC" => "Ecuador", "EG" => "Egypt", "SV" => "El Salvador", "GQ" => "Equatorial Guinea", "ER" => "Eritrea", "EE" => "Estonia", "ET" => "Ethiopia", "FK" => "Falkland Islands", "FO" => "Faroe Islands", "FJ" => "Fiji", "FI" => "Finland", "FR" => "France", "GF" => "French Guiana", "PF" => "French Polynesia", "TF" => "French Southern Territories", "FQ" => "French Southern and Antarctic Territories", "GA" => "Gabon", "GM" => "Gambia", "GE" => "Georgia", "DE" => "Germany", "GH" => "Ghana", "GI" => "Gibraltar", "GR" => "Greece", "GL" => "Greenland", "GD" => "Grenada", "GP" => "Guadeloupe", "GU" => "Guam", "GT" => "Guatemala", "GG" => "Guernsey", "GN" => "Guinea", "GW" => "Guinea-Bissau", "GY" => "Guyana", "HT" => "Haiti", "HM" => "Heard Island and McDonald Islands", "HN" => "Honduras", "HK" => "Hong Kong SAR China", "HU" => "Hungary", "IS" => "Iceland", "IN" => "India", "ID" => "Indonesia", "IR" => "Iran", "IQ" => "Iraq", "IE" => "Ireland", "IM" => "Isle of Man", "IL" => "Israel", "IT" => "Italy", "JM" => "Jamaica", "JP" => "Japan", "JE" => "Jersey", "JT" => "Johnston Island", "JO" => "Jordan", "KZ" => "Kazakhstan", "KE" => "Kenya", "KI" => "Kiribati", "KW" => "Kuwait", "KG" => "Kyrgyzstan", "LA" => "Laos", "LV" => "Latvia", "LB" => "Lebanon", "LS" => "Lesotho", "LR" => "Liberia", "LY" => "Libya", "LI" => "Liechtenstein", "LT" => "Lithuania", "LU" => "Luxembourg", "MO" => "Macau SAR China", "MK" => "Macedonia", "MG" => "Madagascar", "MW" => "Malawi", "MY" => "Malaysia", "MV" => "Maldives", "ML" => "Mali", "MT" => "Malta", "MH" => "Marshall Islands", "MQ" => "Martinique", "MR" => "Mauritania", "MU" => "Mauritius", "YT" => "Mayotte", "FX" => "Metropolitan France", "MX" => "Mexico", "FM" => "Micronesia", "MI" => "Midway Islands", "MD" => "Moldova", "MC" => "Monaco", "MN" => "Mongolia", "ME" => "Montenegro", "MS" => "Montserrat", "MA" => "Morocco", "MZ" => "Mozambique", "MM" => "Myanmar [Burma]", "NA" => "Namibia", "NR" => "Nauru", "NP" => "Nepal", "NL" => "Netherlands", "AN" => "Netherlands Antilles", "NT" => "Neutral Zone", "NC" => "New Caledonia", "NZ" => "New Zealand", "NI" => "Nicaragua", "NE" => "Niger", "NG" => "Nigeria", "NU" => "Niue", "NF" => "Norfolk Island", "KP" => "North Korea", "VD" => "North Vietnam", "MP" => "Northern Mariana Islands", "NO" => "Norway", "OM" => "Oman", "PC" => "Pacific Islands Trust Territory", "PK" => "Pakistan", "PW" => "Palau", "PS" => "Palestinian Territories", "PA" => "Panama", "PZ" => "Panama Canal Zone", "PG" => "Papua New Guinea", "PY" => "Paraguay", "YD" => "People's Democratic Republic of Yemen", "PE" => "Peru", "PH" => "Philippines", "PN" => "Pitcairn Islands", "PL" => "Poland", "PT" => "Portugal", "PR" => "Puerto Rico", "QA" => "Qatar", "RO" => "Romania", "RU" => "Russia", "RW" => "Rwanda", "RE" => "Réunion", "BL" => "Saint Barthélemy", "SH" => "Saint Helena", "KN" => "Saint Kitts and Nevis", "LC" => "Saint Lucia", "MF" => "Saint Martin", "PM" => "Saint Pierre and Miquelon", "VC" => "Saint Vincent and the Grenadines", "WS" => "Samoa", "SM" => "San Marino", "SA" => "Saudi Arabia", "SN" => "Senegal", "RS" => "Serbia", "CS" => "Serbia and Montenegro", "SC" => "Seychelles", "SL" => "Sierra Leone", "SG" => "Singapore", "SK" => "Slovakia", "SI" => "Slovenia", "SB" => "Solomon Islands", "SO" => "Somalia", "ZA" => "South Africa", "GS" => "South Georgia and the South Sandwich Islands", "KR" => "South Korea", "ES" => "Spain", "LK" => "Sri Lanka", "SD" => "Sudan", "SR" => "Suriname", "SJ" => "Svalbard and Jan Mayen", "SZ" => "Swaziland", "SE" => "Sweden", "CH" => "Switzerland", "SY" => "Syria", "ST" => "São Tomé and Príncipe", "TW" => "Taiwan", "TJ" => "Tajikistan", "TZ" => "Tanzania", "TH" => "Thailand", "TL" => "Timor-Leste", "TG" => "Togo", "TK" => "Tokelau", "TO" => "Tonga", "TT" => "Trinidad and Tobago", "TN" => "Tunisia", "TR" => "Turkey", "TM" => "Turkmenistan", "TC" => "Turks and Caicos Islands", "TV" => "Tuvalu", "UM" => "U.S. Minor Outlying Islands", "PU" => "U.S. Miscellaneous Pacific Islands", "VI" => "U.S. Virgin Islands", "UG" => "Uganda", "UA" => "Ukraine", "SU" => "Union of Soviet Socialist Republics", "AE" => "United Arab Emirates", "GB" => "United Kingdom", "US" => "United States", "ZZ" => "Unknown or Invalid Region", "UY" => "Uruguay", "UZ" => "Uzbekistan", "VU" => "Vanuatu", "VA" => "Vatican City", "VE" => "Venezuela", "VN" => "Vietnam", "WK" => "Wake Island", "WF" => "Wallis and Futuna", "EH" => "Western Sahara", "YE" => "Yemen", "ZM" => "Zambia", "ZW" => "Zimbabwe", "AX" => "Åland Islands", ]; return view('front.checkout', compact('banner', 'total', 'items', 'totalWeight', 'shippingPrice', 'grandTotal', 'countryList')); } public function processPayment(Request $request) { $nonce = $request->input('nonce'); $amount = $request->amount; $contact = $request->input('contact'); $shipping = $request->input('shipping'); $validated = $request->validate([ 'nonce' => 'required|string', 'amount' => 'required|numeric', 'contact' => 'required|array', 'contact.phone' => 'required|string', 'contact.email' => 'required|email', 'shipping' => 'required|array', 'shipping.first_name' => 'required|string', 'shipping.last_name' => 'required|string', 'shipping.address_line1' => 'required|string', 'shipping.city' => 'required|string', 'shipping.state' => 'required|string', 'shipping.zip_code' => 'required|string', ]); $client = new SquareClient([ 'accessToken' => env('SQUARE_ACCESS_TOKEN'), 'environment' => env('SQUARE_ENVIRONMENT') === 'production' ? Environment::PRODUCTION : Environment::SANDBOX, ]); // Set amount money (in cents) $amount_money = new Money(); $amount_money->setAmount($amount * 100); // amount in cents $amount_money->setCurrency('GBP'); // Adjust currency if necessary // Create payment request body $body = new CreatePaymentRequest($nonce, uniqid()); $body->setAmountMoney($amount_money); $body->setAutocomplete(true); // Complete payment automatically $body->setLocationId(env('SQUARE_LOCATION_ID')); try { // Send Payment request to Square API $api_response = $client->getPaymentsApi()->createPayment($body); // Handle API response if ($api_response->isSuccess()) { $result = $api_response->getResult(); // Store the contact and shipping information in the database (Optional) $this->storeOrderDetails($contact, $shipping, $result); \Cart::clear(); session()->flash('success'); session()->flash('message', 'Payment successful!'); return response()->json([ 'success' => true, 'msg' => 'Payment successful!', 'payment' => $result, ]); } else { $errors = $api_response->getErrors(); session()->flash('failed'); session()->flash('message', 'Payment failed: ' . json_encode($errors)); return response()->json([ 'success' => false, 'msg' => 'Payment failed', 'errors' => $errors, ], 400); } } catch (ApiException|\Exception $e) { session()->flash('failed'); session()->flash('message', $e->getMessage()); return response()->json([ 'success' => false, 'msg' => $e->getMessage(), ], 500); } } private function storeOrderDetails($contact, $shipping, $paymentResult) { // Extract the payment object from the CreatePaymentResponse $payment = $paymentResult->getPayment(); // Check if payment object exists if (!$payment) { throw new \Exception('Payment object is missing in the response.'); } // Get cart items $cartItems = \Cart::getContent(); // Prepare an array to store the details of each cart item $items = []; foreach ($cartItems as $item) { // Extract the details you want to store $itemDetails = [ 'id' => $item->id, // Cart Item ID 'name' => $item->name, // Product Name 'price' => $item->price, // Product Price 'quantity' => $item->quantity, // Quantity of the Product 'dealer' => $item->attributes->dealer, // Dealer (from item attributes) 'weight' => $item->attributes->weight, // Weight (from item attributes) ]; // Add the item details to the items array $items[] = $itemDetails; } // Store the order details with the item details in JSON format $order = new Order(); // Assuming you have an Order model $order->items = json_encode($items); // Store item details as a JSON array $order->contact_country = $contact['country']; $order->contact_phone = $contact['phone']; $order->contact_email = $contact['email']; $order->shipping_first_name = $shipping['first_name']; $order->shipping_last_name = $shipping['last_name']; $order->shipping_address_line1 = $shipping['address_line1']; $order->shipping_address_line2 = $shipping['address_line2'] ?? null; // Optional $order->shipping_city = $shipping['city']; $order->shipping_state = $shipping['state']; $order->shipping_zip_code = $shipping['zip_code']; $order->payment_id = $payment->getId(); // Access the ID of the payment $order->amount = $payment->getAmountMoney()->getAmount() / 100; // Convert back to dollars $order->status = $payment->getStatus(); // Use the status from the payment $order->save(); } private function calculateShipping($totalWeight) { if ($totalWeight == 0) { return 0; } elseif ($totalWeight <= 250) { return 4.00; } elseif ($totalWeight <= 1000) { return 6.00; } elseif ($totalWeight <= 5000) { return 12.00; } elseif ($totalWeight <= 10000) { return 16.00; } elseif ($totalWeight <= 30000) { return 22.00; } elseif ($totalWeight > 30001) { return 30.00; } else { return 7.00; } } }first(); return view('front.basket',compact('items','total','banner')); } public function addCart(Request $request) { $product = Product::where('slug', $request->slug)->first(); if (!$product) { return response()->json(['message' => 'Product not found'], 404); } if ($request->price == 0) { return response()->json(['message' => 'Price should not be 0'], 400); } $uniqueId = $product->id . '_' . $request->size; $cartItem = \Cart::get($uniqueId); if ($cartItem) { \Cart::update($uniqueId, [ 'quantity' => +1, ]); } else { \Cart::add([ 'id' => $uniqueId, 'name' => $product->name, 'slug' => $product->slug, 'price' => $request->price, 'quantity' => 1, 'attributes' => [ 'weight' => $request->size, 'dealer' => $request->dealer, 'image' => $product->imageCover->image, ], 'associatedModel' => $product, ]); } return response()->json(['message' => 'Item has been added to the cart']); } public function getCartQuantity() { $totalQuantity = \Cart::getTotalQuantity(); return response()->json(['totalQuantity' => $totalQuantity]); } public function getCartPrice() { $totalPrice = \Cart::getTotal(); return response()->json(['totalPrice' => $totalPrice]); } public function addQuantity($productId) { \Cart::update($productId, [ 'quantity' => +1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been increased', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function decreaseQuantity($productId) { \Cart::update($productId, [ 'quantity' => -1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been decrease', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function removeItem($productId) { \Cart::remove($productId); return response()->json([ 'message' => 'Item has been removed from the cart', ]); } public function applyCoupon(Request $request) { $couponCode = $request->input('coupon_code'); $code = CouponCode::where('name',$couponCode)->first(); if ($code && $code->isUse == '0') { $code->update([ 'isUse' => '1' ]); $value = '-'.$code->discount.'%'; $condition = new \Darryldecode\Cart\CartCondition(array( 'name' => $code->name, 'type' => 'coupon', 'target' => 'subtotal', 'value' => $value )); \Cart::condition($condition); return response()->json([ 'message' => 'Coupon code used successfully', ]); } else { // \Cart::removeConditionsByType('coupon'); return response()->json([ 'message' => 'You have entered an invalid coupon or used coupon code.', ],500); } } public function Checkout() { $banner = Banner::where('page', 'Checkout')->first(); $total = \Cart::getTotal(); $items = \Cart::getContent(); //dd($items); $totalWeight = 0; foreach ($items as $item) { if (!$item->attributes->dealer) { $weight = Size::whereId($item->attributes->weight)->first()->weight ?? 0; $totalWeight += $weight * $item->quantity; } } $shippingPrice = $this->calculateShipping($totalWeight); $grandTotal = $total + $shippingPrice; $countryList = [ "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua and Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "BY" => "Belarus", "BE" => "Belgium", "BZ" => "Belize", "BJ" => "Benin", "BM" => "Bermuda", "BT" => "Bhutan", "BO" => "Bolivia", "BA" => "Bosnia and Herzegovina", "BW" => "Botswana", "BV" => "Bouvet Island", "BR" => "Brazil", "BQ" => "British Antarctic Territory", "IO" => "British Indian Ocean Territory", "VG" => "British Virgin Islands", "BN" => "Brunei", "BG" => "Bulgaria", "BF" => "Burkina Faso", "BI" => "Burundi", "KH" => "Cambodia", "CM" => "Cameroon", "CA" => "Canada", "CT" => "Canton and Enderbury Islands", "CV" => "Cape Verde", "KY" => "Cayman Islands", "CF" => "Central African Republic", "TD" => "Chad", "CL" => "Chile", "CN" => "China", "CX" => "Christmas Island", "CC" => "Cocos [Keeling] Islands", "CO" => "Colombia", "KM" => "Comoros", "CG" => "Congo - Brazzaville", "CD" => "Congo - Kinshasa", "CK" => "Cook Islands", "CR" => "Costa Rica", "HR" => "Croatia", "CU" => "Cuba", "CY" => "Cyprus", "CZ" => "Czech Republic", "CI" => "Côte d’Ivoire", "DK" => "Denmark", "DJ" => "Djibouti", "DM" => "Dominica", "DO" => "Dominican Republic", "NQ" => "Dronning Maud Land", "DD" => "East Germany", "EC" => "Ecuador", "EG" => "Egypt", "SV" => "El Salvador", "GQ" => "Equatorial Guinea", "ER" => "Eritrea", "EE" => "Estonia", "ET" => "Ethiopia", "FK" => "Falkland Islands", "FO" => "Faroe Islands", "FJ" => "Fiji", "FI" => "Finland", "FR" => "France", "GF" => "French Guiana", "PF" => "French Polynesia", "TF" => "French Southern Territories", "FQ" => "French Southern and Antarctic Territories", "GA" => "Gabon", "GM" => "Gambia", "GE" => "Georgia", "DE" => "Germany", "GH" => "Ghana", "GI" => "Gibraltar", "GR" => "Greece", "GL" => "Greenland", "GD" => "Grenada", "GP" => "Guadeloupe", "GU" => "Guam", "GT" => "Guatemala", "GG" => "Guernsey", "GN" => "Guinea", "GW" => "Guinea-Bissau", "GY" => "Guyana", "HT" => "Haiti", "HM" => "Heard Island and McDonald Islands", "HN" => "Honduras", "HK" => "Hong Kong SAR China", "HU" => "Hungary", "IS" => "Iceland", "IN" => "India", "ID" => "Indonesia", "IR" => "Iran", "IQ" => "Iraq", "IE" => "Ireland", "IM" => "Isle of Man", "IL" => "Israel", "IT" => "Italy", "JM" => "Jamaica", "JP" => "Japan", "JE" => "Jersey", "JT" => "Johnston Island", "JO" => "Jordan", "KZ" => "Kazakhstan", "KE" => "Kenya", "KI" => "Kiribati", "KW" => "Kuwait", "KG" => "Kyrgyzstan", "LA" => "Laos", "LV" => "Latvia", "LB" => "Lebanon", "LS" => "Lesotho", "LR" => "Liberia", "LY" => "Libya", "LI" => "Liechtenstein", "LT" => "Lithuania", "LU" => "Luxembourg", "MO" => "Macau SAR China", "MK" => "Macedonia", "MG" => "Madagascar", "MW" => "Malawi", "MY" => "Malaysia", "MV" => "Maldives", "ML" => "Mali", "MT" => "Malta", "MH" => "Marshall Islands", "MQ" => "Martinique", "MR" => "Mauritania", "MU" => "Mauritius", "YT" => "Mayotte", "FX" => "Metropolitan France", "MX" => "Mexico", "FM" => "Micronesia", "MI" => "Midway Islands", "MD" => "Moldova", "MC" => "Monaco", "MN" => "Mongolia", "ME" => "Montenegro", "MS" => "Montserrat", "MA" => "Morocco", "MZ" => "Mozambique", "MM" => "Myanmar [Burma]", "NA" => "Namibia", "NR" => "Nauru", "NP" => "Nepal", "NL" => "Netherlands", "AN" => "Netherlands Antilles", "NT" => "Neutral Zone", "NC" => "New Caledonia", "NZ" => "New Zealand", "NI" => "Nicaragua", "NE" => "Niger", "NG" => "Nigeria", "NU" => "Niue", "NF" => "Norfolk Island", "KP" => "North Korea", "VD" => "North Vietnam", "MP" => "Northern Mariana Islands", "NO" => "Norway", "OM" => "Oman", "PC" => "Pacific Islands Trust Territory", "PK" => "Pakistan", "PW" => "Palau", "PS" => "Palestinian Territories", "PA" => "Panama", "PZ" => "Panama Canal Zone", "PG" => "Papua New Guinea", "PY" => "Paraguay", "YD" => "People's Democratic Republic of Yemen", "PE" => "Peru", "PH" => "Philippines", "PN" => "Pitcairn Islands", "PL" => "Poland", "PT" => "Portugal", "PR" => "Puerto Rico", "QA" => "Qatar", "RO" => "Romania", "RU" => "Russia", "RW" => "Rwanda", "RE" => "Réunion", "BL" => "Saint Barthélemy", "SH" => "Saint Helena", "KN" => "Saint Kitts and Nevis", "LC" => "Saint Lucia", "MF" => "Saint Martin", "PM" => "Saint Pierre and Miquelon", "VC" => "Saint Vincent and the Grenadines", "WS" => "Samoa", "SM" => "San Marino", "SA" => "Saudi Arabia", "SN" => "Senegal", "RS" => "Serbia", "CS" => "Serbia and Montenegro", "SC" => "Seychelles", "SL" => "Sierra Leone", "SG" => "Singapore", "SK" => "Slovakia", "SI" => "Slovenia", "SB" => "Solomon Islands", "SO" => "Somalia", "ZA" => "South Africa", "GS" => "South Georgia and the South Sandwich Islands", "KR" => "South Korea", "ES" => "Spain", "LK" => "Sri Lanka", "SD" => "Sudan", "SR" => "Suriname", "SJ" => "Svalbard and Jan Mayen", "SZ" => "Swaziland", "SE" => "Sweden", "CH" => "Switzerland", "SY" => "Syria", "ST" => "São Tomé and Príncipe", "TW" => "Taiwan", "TJ" => "Tajikistan", "TZ" => "Tanzania", "TH" => "Thailand", "TL" => "Timor-Leste", "TG" => "Togo", "TK" => "Tokelau", "TO" => "Tonga", "TT" => "Trinidad and Tobago", "TN" => "Tunisia", "TR" => "Turkey", "TM" => "Turkmenistan", "TC" => "Turks and Caicos Islands", "TV" => "Tuvalu", "UM" => "U.S. Minor Outlying Islands", "PU" => "U.S. Miscellaneous Pacific Islands", "VI" => "U.S. Virgin Islands", "UG" => "Uganda", "UA" => "Ukraine", "SU" => "Union of Soviet Socialist Republics", "AE" => "United Arab Emirates", "GB" => "United Kingdom", "US" => "United States", "ZZ" => "Unknown or Invalid Region", "UY" => "Uruguay", "UZ" => "Uzbekistan", "VU" => "Vanuatu", "VA" => "Vatican City", "VE" => "Venezuela", "VN" => "Vietnam", "WK" => "Wake Island", "WF" => "Wallis and Futuna", "EH" => "Western Sahara", "YE" => "Yemen", "ZM" => "Zambia", "ZW" => "Zimbabwe", "AX" => "Åland Islands", ]; return view('front.checkout', compact('banner', 'total', 'items', 'totalWeight', 'shippingPrice', 'grandTotal', 'countryList')); } public function processPayment(Request $request) { $nonce = $request->input('nonce'); $amount = $request->amount; $contact = $request->input('contact'); $shipping = $request->input('shipping'); $validated = $request->validate([ 'nonce' => 'required|string', 'amount' => 'required|numeric', 'contact' => 'required|array', 'contact.phone' => 'required|string', 'contact.email' => 'required|email', 'shipping' => 'required|array', 'shipping.first_name' => 'required|string', 'shipping.last_name' => 'required|string', 'shipping.address_line1' => 'required|string', 'shipping.city' => 'required|string', 'shipping.state' => 'required|string', 'shipping.zip_code' => 'required|string', ]); $client = new SquareClient([ 'accessToken' => env('SQUARE_ACCESS_TOKEN'), 'environment' => env('SQUARE_ENVIRONMENT') === 'production' ? Environment::PRODUCTION : Environment::SANDBOX, ]); // Set amount money (in cents) $amount_money = new Money(); $amount_money->setAmount($amount * 100); // amount in cents $amount_money->setCurrency('GBP'); // Adjust currency if necessary // Create payment request body $body = new CreatePaymentRequest($nonce, uniqid()); $body->setAmountMoney($amount_money); $body->setAutocomplete(true); // Complete payment automatically $body->setLocationId(env('SQUARE_LOCATION_ID')); try { // Send Payment request to Square API $api_response = $client->getPaymentsApi()->createPayment($body); // Handle API response if ($api_response->isSuccess()) { $result = $api_response->getResult(); // Store the contact and shipping information in the database (Optional) $this->storeOrderDetails($contact, $shipping, $result); \Cart::clear(); session()->flash('success'); session()->flash('message', 'Payment successful!'); return response()->json([ 'success' => true, 'msg' => 'Payment successful!', 'payment' => $result, ]); } else { $errors = $api_response->getErrors(); session()->flash('failed'); session()->flash('message', 'Payment failed: ' . json_encode($errors)); return response()->json([ 'success' => false, 'msg' => 'Payment failed', 'errors' => $errors, ], 400); } } catch (ApiException|\Exception $e) { session()->flash('failed'); session()->flash('message', $e->getMessage()); return response()->json([ 'success' => false, 'msg' => $e->getMessage(), ], 500); } } private function storeOrderDetails($contact, $shipping, $paymentResult) { // Extract the payment object from the CreatePaymentResponse $payment = $paymentResult->getPayment(); // Check if payment object exists if (!$payment) { throw new \Exception('Payment object is missing in the response.'); } // Get cart items $cartItems = \Cart::getContent(); // Prepare an array to store the details of each cart item $items = []; foreach ($cartItems as $item) { // Extract the details you want to store $itemDetails = [ 'id' => $item->id, // Cart Item ID 'name' => $item->name, // Product Name 'price' => $item->price, // Product Price 'quantity' => $item->quantity, // Quantity of the Product 'dealer' => $item->attributes->dealer, // Dealer (from item attributes) 'weight' => $item->attributes->weight, // Weight (from item attributes) ]; // Add the item details to the items array $items[] = $itemDetails; } // Store the order details with the item details in JSON format $order = new Order(); // Assuming you have an Order model $order->items = json_encode($items); // Store item details as a JSON array $order->contact_country = $contact['country']; $order->contact_phone = $contact['phone']; $order->contact_email = $contact['email']; $order->shipping_first_name = $shipping['first_name']; $order->shipping_last_name = $shipping['last_name']; $order->shipping_address_line1 = $shipping['address_line1']; $order->shipping_address_line2 = $shipping['address_line2'] ?? null; // Optional $order->shipping_city = $shipping['city']; $order->shipping_state = $shipping['state']; $order->shipping_zip_code = $shipping['zip_code']; $order->payment_id = $payment->getId(); // Access the ID of the payment $order->amount = $payment->getAmountMoney()->getAmount() / 100; // Convert back to dollars $order->status = $payment->getStatus(); // Use the status from the payment $order->save(); } private function calculateShipping($totalWeight) { if ($totalWeight == 0) { return 0; } elseif ($totalWeight <= 250) { return 4.00; } elseif ($totalWeight <= 1000) { return 6.00; } elseif ($totalWeight <= 5000) { return 12.00; } elseif ($totalWeight <= 10000) { return 16.00; } elseif ($totalWeight <= 30000) { return 22.00; } elseif ($totalWeight > 30001) { return 30.00; } else { return 7.00; } } }first(); return view('front.basket',compact('items','total','banner')); } public function addCart(Request $request) { $product = Product::where('slug', $request->slug)->first(); if (!$product) { return response()->json(['message' => 'Product not found'], 404); } if ($request->price == 0) { return response()->json(['message' => 'Price should not be 0'], 400); } $uniqueId = $product->id . '_' . $request->size; $cartItem = \Cart::get($uniqueId); if ($cartItem) { \Cart::update($uniqueId, [ 'quantity' => +1, ]); } else { \Cart::add([ 'id' => $uniqueId, 'name' => $product->name, 'slug' => $product->slug, 'price' => $request->price, 'quantity' => 1, 'attributes' => [ 'weight' => $request->size, 'dealer' => $request->dealer, 'image' => $product->imageCover->image, ], 'associatedModel' => $product, ]); } return response()->json(['message' => 'Item has been added to the cart']); } public function getCartQuantity() { $totalQuantity = \Cart::getTotalQuantity(); return response()->json(['totalQuantity' => $totalQuantity]); } public function getCartPrice() { $totalPrice = \Cart::getTotal(); return response()->json(['totalPrice' => $totalPrice]); } public function addQuantity($productId) { \Cart::update($productId, [ 'quantity' => +1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been increased', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function decreaseQuantity($productId) { \Cart::update($productId, [ 'quantity' => -1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been decrease', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function removeItem($productId) { \Cart::remove($productId); return response()->json([ 'message' => 'Item has been removed from the cart', ]); } public function applyCoupon(Request $request) { $couponCode = $request->input('coupon_code'); $code = CouponCode::where('name',$couponCode)->first(); if ($code && $code->isUse == '0') { $code->update([ 'isUse' => '1' ]); $value = '-'.$code->discount.'%'; $condition = new \Darryldecode\Cart\CartCondition(array( 'name' => $code->name, 'type' => 'coupon', 'target' => 'subtotal', 'value' => $value )); \Cart::condition($condition); return response()->json([ 'message' => 'Coupon code used successfully', ]); } else { // \Cart::removeConditionsByType('coupon'); return response()->json([ 'message' => 'You have entered an invalid coupon or used coupon code.', ],500); } } public function Checkout() { $banner = Banner::where('page', 'Checkout')->first(); $total = \Cart::getTotal(); $items = \Cart::getContent(); //dd($items); $totalWeight = 0; foreach ($items as $item) { if (!$item->attributes->dealer) { $weight = Size::whereId($item->attributes->weight)->first()->weight ?? 0; $totalWeight += $weight * $item->quantity; } } $shippingPrice = $this->calculateShipping($totalWeight); $grandTotal = $total + $shippingPrice; $countryList = [ "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua and Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "BY" => "Belarus", "BE" => "Belgium", "BZ" => "Belize", "BJ" => "Benin", "BM" => "Bermuda", "BT" => "Bhutan", "BO" => "Bolivia", "BA" => "Bosnia and Herzegovina", "BW" => "Botswana", "BV" => "Bouvet Island", "BR" => "Brazil", "BQ" => "British Antarctic Territory", "IO" => "British Indian Ocean Territory", "VG" => "British Virgin Islands", "BN" => "Brunei", "BG" => "Bulgaria", "BF" => "Burkina Faso", "BI" => "Burundi", "KH" => "Cambodia", "CM" => "Cameroon", "CA" => "Canada", "CT" => "Canton and Enderbury Islands", "CV" => "Cape Verde", "KY" => "Cayman Islands", "CF" => "Central African Republic", "TD" => "Chad", "CL" => "Chile", "CN" => "China", "CX" => "Christmas Island", "CC" => "Cocos [Keeling] Islands", "CO" => "Colombia", "KM" => "Comoros", "CG" => "Congo - Brazzaville", "CD" => "Congo - Kinshasa", "CK" => "Cook Islands", "CR" => "Costa Rica", "HR" => "Croatia", "CU" => "Cuba", "CY" => "Cyprus", "CZ" => "Czech Republic", "CI" => "Côte d’Ivoire", "DK" => "Denmark", "DJ" => "Djibouti", "DM" => "Dominica", "DO" => "Dominican Republic", "NQ" => "Dronning Maud Land", "DD" => "East Germany", "EC" => "Ecuador", "EG" => "Egypt", "SV" => "El Salvador", "GQ" => "Equatorial Guinea", "ER" => "Eritrea", "EE" => "Estonia", "ET" => "Ethiopia", "FK" => "Falkland Islands", "FO" => "Faroe Islands", "FJ" => "Fiji", "FI" => "Finland", "FR" => "France", "GF" => "French Guiana", "PF" => "French Polynesia", "TF" => "French Southern Territories", "FQ" => "French Southern and Antarctic Territories", "GA" => "Gabon", "GM" => "Gambia", "GE" => "Georgia", "DE" => "Germany", "GH" => "Ghana", "GI" => "Gibraltar", "GR" => "Greece", "GL" => "Greenland", "GD" => "Grenada", "GP" => "Guadeloupe", "GU" => "Guam", "GT" => "Guatemala", "GG" => "Guernsey", "GN" => "Guinea", "GW" => "Guinea-Bissau", "GY" => "Guyana", "HT" => "Haiti", "HM" => "Heard Island and McDonald Islands", "HN" => "Honduras", "HK" => "Hong Kong SAR China", "HU" => "Hungary", "IS" => "Iceland", "IN" => "India", "ID" => "Indonesia", "IR" => "Iran", "IQ" => "Iraq", "IE" => "Ireland", "IM" => "Isle of Man", "IL" => "Israel", "IT" => "Italy", "JM" => "Jamaica", "JP" => "Japan", "JE" => "Jersey", "JT" => "Johnston Island", "JO" => "Jordan", "KZ" => "Kazakhstan", "KE" => "Kenya", "KI" => "Kiribati", "KW" => "Kuwait", "KG" => "Kyrgyzstan", "LA" => "Laos", "LV" => "Latvia", "LB" => "Lebanon", "LS" => "Lesotho", "LR" => "Liberia", "LY" => "Libya", "LI" => "Liechtenstein", "LT" => "Lithuania", "LU" => "Luxembourg", "MO" => "Macau SAR China", "MK" => "Macedonia", "MG" => "Madagascar", "MW" => "Malawi", "MY" => "Malaysia", "MV" => "Maldives", "ML" => "Mali", "MT" => "Malta", "MH" => "Marshall Islands", "MQ" => "Martinique", "MR" => "Mauritania", "MU" => "Mauritius", "YT" => "Mayotte", "FX" => "Metropolitan France", "MX" => "Mexico", "FM" => "Micronesia", "MI" => "Midway Islands", "MD" => "Moldova", "MC" => "Monaco", "MN" => "Mongolia", "ME" => "Montenegro", "MS" => "Montserrat", "MA" => "Morocco", "MZ" => "Mozambique", "MM" => "Myanmar [Burma]", "NA" => "Namibia", "NR" => "Nauru", "NP" => "Nepal", "NL" => "Netherlands", "AN" => "Netherlands Antilles", "NT" => "Neutral Zone", "NC" => "New Caledonia", "NZ" => "New Zealand", "NI" => "Nicaragua", "NE" => "Niger", "NG" => "Nigeria", "NU" => "Niue", "NF" => "Norfolk Island", "KP" => "North Korea", "VD" => "North Vietnam", "MP" => "Northern Mariana Islands", "NO" => "Norway", "OM" => "Oman", "PC" => "Pacific Islands Trust Territory", "PK" => "Pakistan", "PW" => "Palau", "PS" => "Palestinian Territories", "PA" => "Panama", "PZ" => "Panama Canal Zone", "PG" => "Papua New Guinea", "PY" => "Paraguay", "YD" => "People's Democratic Republic of Yemen", "PE" => "Peru", "PH" => "Philippines", "PN" => "Pitcairn Islands", "PL" => "Poland", "PT" => "Portugal", "PR" => "Puerto Rico", "QA" => "Qatar", "RO" => "Romania", "RU" => "Russia", "RW" => "Rwanda", "RE" => "Réunion", "BL" => "Saint Barthélemy", "SH" => "Saint Helena", "KN" => "Saint Kitts and Nevis", "LC" => "Saint Lucia", "MF" => "Saint Martin", "PM" => "Saint Pierre and Miquelon", "VC" => "Saint Vincent and the Grenadines", "WS" => "Samoa", "SM" => "San Marino", "SA" => "Saudi Arabia", "SN" => "Senegal", "RS" => "Serbia", "CS" => "Serbia and Montenegro", "SC" => "Seychelles", "SL" => "Sierra Leone", "SG" => "Singapore", "SK" => "Slovakia", "SI" => "Slovenia", "SB" => "Solomon Islands", "SO" => "Somalia", "ZA" => "South Africa", "GS" => "South Georgia and the South Sandwich Islands", "KR" => "South Korea", "ES" => "Spain", "LK" => "Sri Lanka", "SD" => "Sudan", "SR" => "Suriname", "SJ" => "Svalbard and Jan Mayen", "SZ" => "Swaziland", "SE" => "Sweden", "CH" => "Switzerland", "SY" => "Syria", "ST" => "São Tomé and Príncipe", "TW" => "Taiwan", "TJ" => "Tajikistan", "TZ" => "Tanzania", "TH" => "Thailand", "TL" => "Timor-Leste", "TG" => "Togo", "TK" => "Tokelau", "TO" => "Tonga", "TT" => "Trinidad and Tobago", "TN" => "Tunisia", "TR" => "Turkey", "TM" => "Turkmenistan", "TC" => "Turks and Caicos Islands", "TV" => "Tuvalu", "UM" => "U.S. Minor Outlying Islands", "PU" => "U.S. Miscellaneous Pacific Islands", "VI" => "U.S. Virgin Islands", "UG" => "Uganda", "UA" => "Ukraine", "SU" => "Union of Soviet Socialist Republics", "AE" => "United Arab Emirates", "GB" => "United Kingdom", "US" => "United States", "ZZ" => "Unknown or Invalid Region", "UY" => "Uruguay", "UZ" => "Uzbekistan", "VU" => "Vanuatu", "VA" => "Vatican City", "VE" => "Venezuela", "VN" => "Vietnam", "WK" => "Wake Island", "WF" => "Wallis and Futuna", "EH" => "Western Sahara", "YE" => "Yemen", "ZM" => "Zambia", "ZW" => "Zimbabwe", "AX" => "Åland Islands", ]; return view('front.checkout', compact('banner', 'total', 'items', 'totalWeight', 'shippingPrice', 'grandTotal', 'countryList')); } public function processPayment(Request $request) { $nonce = $request->input('nonce'); $amount = $request->amount; $contact = $request->input('contact'); $shipping = $request->input('shipping'); $validated = $request->validate([ 'nonce' => 'required|string', 'amount' => 'required|numeric', 'contact' => 'required|array', 'contact.phone' => 'required|string', 'contact.email' => 'required|email', 'shipping' => 'required|array', 'shipping.first_name' => 'required|string', 'shipping.last_name' => 'required|string', 'shipping.address_line1' => 'required|string', 'shipping.city' => 'required|string', 'shipping.state' => 'required|string', 'shipping.zip_code' => 'required|string', ]); $client = new SquareClient([ 'accessToken' => env('SQUARE_ACCESS_TOKEN'), 'environment' => env('SQUARE_ENVIRONMENT') === 'production' ? Environment::PRODUCTION : Environment::SANDBOX, ]); // Set amount money (in cents) $amount_money = new Money(); $amount_money->setAmount($amount * 100); // amount in cents $amount_money->setCurrency('GBP'); // Adjust currency if necessary // Create payment request body $body = new CreatePaymentRequest($nonce, uniqid()); $body->setAmountMoney($amount_money); $body->setAutocomplete(true); // Complete payment automatically $body->setLocationId(env('SQUARE_LOCATION_ID')); try { // Send Payment request to Square API $api_response = $client->getPaymentsApi()->createPayment($body); // Handle API response if ($api_response->isSuccess()) { $result = $api_response->getResult(); // Store the contact and shipping information in the database (Optional) $this->storeOrderDetails($contact, $shipping, $result); \Cart::clear(); session()->flash('success'); session()->flash('message', 'Payment successful!'); return response()->json([ 'success' => true, 'msg' => 'Payment successful!', 'payment' => $result, ]); } else { $errors = $api_response->getErrors(); session()->flash('failed'); session()->flash('message', 'Payment failed: ' . json_encode($errors)); return response()->json([ 'success' => false, 'msg' => 'Payment failed', 'errors' => $errors, ], 400); } } catch (ApiException|\Exception $e) { session()->flash('failed'); session()->flash('message', $e->getMessage()); return response()->json([ 'success' => false, 'msg' => $e->getMessage(), ], 500); } } private function storeOrderDetails($contact, $shipping, $paymentResult) { // Extract the payment object from the CreatePaymentResponse $payment = $paymentResult->getPayment(); // Check if payment object exists if (!$payment) { throw new \Exception('Payment object is missing in the response.'); } // Get cart items $cartItems = \Cart::getContent(); // Prepare an array to store the details of each cart item $items = []; foreach ($cartItems as $item) { // Extract the details you want to store $itemDetails = [ 'id' => $item->id, // Cart Item ID 'name' => $item->name, // Product Name 'price' => $item->price, // Product Price 'quantity' => $item->quantity, // Quantity of the Product 'dealer' => $item->attributes->dealer, // Dealer (from item attributes) 'weight' => $item->attributes->weight, // Weight (from item attributes) ]; // Add the item details to the items array $items[] = $itemDetails; } // Store the order details with the item details in JSON format $order = new Order(); // Assuming you have an Order model $order->items = json_encode($items); // Store item details as a JSON array $order->contact_country = $contact['country']; $order->contact_phone = $contact['phone']; $order->contact_email = $contact['email']; $order->shipping_first_name = $shipping['first_name']; $order->shipping_last_name = $shipping['last_name']; $order->shipping_address_line1 = $shipping['address_line1']; $order->shipping_address_line2 = $shipping['address_line2'] ?? null; // Optional $order->shipping_city = $shipping['city']; $order->shipping_state = $shipping['state']; $order->shipping_zip_code = $shipping['zip_code']; $order->payment_id = $payment->getId(); // Access the ID of the payment $order->amount = $payment->getAmountMoney()->getAmount() / 100; // Convert back to dollars $order->status = $payment->getStatus(); // Use the status from the payment $order->save(); } private function calculateShipping($totalWeight) { if ($totalWeight == 0) { return 0; } elseif ($totalWeight <= 250) { return 4.00; } elseif ($totalWeight <= 1000) { return 6.00; } elseif ($totalWeight <= 5000) { return 12.00; } elseif ($totalWeight <= 10000) { return 16.00; } elseif ($totalWeight <= 30000) { return 22.00; } elseif ($totalWeight > 30001) { return 30.00; } else { return 7.00; } } }first(); return view('front.basket',compact('items','total','banner')); } public function addCart(Request $request) { $product = Product::where('slug', $request->slug)->first(); if (!$product) { return response()->json(['message' => 'Product not found'], 404); } if ($request->price == 0) { return response()->json(['message' => 'Price should not be 0'], 400); } $uniqueId = $product->id . '_' . $request->size; $cartItem = \Cart::get($uniqueId); if ($cartItem) { \Cart::update($uniqueId, [ 'quantity' => +1, ]); } else { \Cart::add([ 'id' => $uniqueId, 'name' => $product->name, 'slug' => $product->slug, 'price' => $request->price, 'quantity' => 1, 'attributes' => [ 'weight' => $request->size, 'dealer' => $request->dealer, 'image' => $product->imageCover->image, ], 'associatedModel' => $product, ]); } return response()->json(['message' => 'Item has been added to the cart']); } public function getCartQuantity() { $totalQuantity = \Cart::getTotalQuantity(); return response()->json(['totalQuantity' => $totalQuantity]); } public function getCartPrice() { $totalPrice = \Cart::getTotal(); return response()->json(['totalPrice' => $totalPrice]); } public function addQuantity($productId) { \Cart::update($productId, [ 'quantity' => +1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been increased', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function decreaseQuantity($productId) { \Cart::update($productId, [ 'quantity' => -1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been decrease', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function removeItem($productId) { \Cart::remove($productId); return response()->json([ 'message' => 'Item has been removed from the cart', ]); } public function applyCoupon(Request $request) { $couponCode = $request->input('coupon_code'); $code = CouponCode::where('name',$couponCode)->first(); if ($code && $code->isUse == '0') { $code->update([ 'isUse' => '1' ]); $value = '-'.$code->discount.'%'; $condition = new \Darryldecode\Cart\CartCondition(array( 'name' => $code->name, 'type' => 'coupon', 'target' => 'subtotal', 'value' => $value )); \Cart::condition($condition); return response()->json([ 'message' => 'Coupon code used successfully', ]); } else { // \Cart::removeConditionsByType('coupon'); return response()->json([ 'message' => 'You have entered an invalid coupon or used coupon code.', ],500); } } public function Checkout() { $banner = Banner::where('page', 'Checkout')->first(); $total = \Cart::getTotal(); $items = \Cart::getContent(); //dd($items); $totalWeight = 0; foreach ($items as $item) { if (!$item->attributes->dealer) { $weight = Size::whereId($item->attributes->weight)->first()->weight ?? 0; $totalWeight += $weight * $item->quantity; } } $shippingPrice = $this->calculateShipping($totalWeight); $grandTotal = $total + $shippingPrice; $countryList = [ "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua and Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "BY" => "Belarus", "BE" => "Belgium", "BZ" => "Belize", "BJ" => "Benin", "BM" => "Bermuda", "BT" => "Bhutan", "BO" => "Bolivia", "BA" => "Bosnia and Herzegovina", "BW" => "Botswana", "BV" => "Bouvet Island", "BR" => "Brazil", "BQ" => "British Antarctic Territory", "IO" => "British Indian Ocean Territory", "VG" => "British Virgin Islands", "BN" => "Brunei", "BG" => "Bulgaria", "BF" => "Burkina Faso", "BI" => "Burundi", "KH" => "Cambodia", "CM" => "Cameroon", "CA" => "Canada", "CT" => "Canton and Enderbury Islands", "CV" => "Cape Verde", "KY" => "Cayman Islands", "CF" => "Central African Republic", "TD" => "Chad", "CL" => "Chile", "CN" => "China", "CX" => "Christmas Island", "CC" => "Cocos [Keeling] Islands", "CO" => "Colombia", "KM" => "Comoros", "CG" => "Congo - Brazzaville", "CD" => "Congo - Kinshasa", "CK" => "Cook Islands", "CR" => "Costa Rica", "HR" => "Croatia", "CU" => "Cuba", "CY" => "Cyprus", "CZ" => "Czech Republic", "CI" => "Côte d’Ivoire", "DK" => "Denmark", "DJ" => "Djibouti", "DM" => "Dominica", "DO" => "Dominican Republic", "NQ" => "Dronning Maud Land", "DD" => "East Germany", "EC" => "Ecuador", "EG" => "Egypt", "SV" => "El Salvador", "GQ" => "Equatorial Guinea", "ER" => "Eritrea", "EE" => "Estonia", "ET" => "Ethiopia", "FK" => "Falkland Islands", "FO" => "Faroe Islands", "FJ" => "Fiji", "FI" => "Finland", "FR" => "France", "GF" => "French Guiana", "PF" => "French Polynesia", "TF" => "French Southern Territories", "FQ" => "French Southern and Antarctic Territories", "GA" => "Gabon", "GM" => "Gambia", "GE" => "Georgia", "DE" => "Germany", "GH" => "Ghana", "GI" => "Gibraltar", "GR" => "Greece", "GL" => "Greenland", "GD" => "Grenada", "GP" => "Guadeloupe", "GU" => "Guam", "GT" => "Guatemala", "GG" => "Guernsey", "GN" => "Guinea", "GW" => "Guinea-Bissau", "GY" => "Guyana", "HT" => "Haiti", "HM" => "Heard Island and McDonald Islands", "HN" => "Honduras", "HK" => "Hong Kong SAR China", "HU" => "Hungary", "IS" => "Iceland", "IN" => "India", "ID" => "Indonesia", "IR" => "Iran", "IQ" => "Iraq", "IE" => "Ireland", "IM" => "Isle of Man", "IL" => "Israel", "IT" => "Italy", "JM" => "Jamaica", "JP" => "Japan", "JE" => "Jersey", "JT" => "Johnston Island", "JO" => "Jordan", "KZ" => "Kazakhstan", "KE" => "Kenya", "KI" => "Kiribati", "KW" => "Kuwait", "KG" => "Kyrgyzstan", "LA" => "Laos", "LV" => "Latvia", "LB" => "Lebanon", "LS" => "Lesotho", "LR" => "Liberia", "LY" => "Libya", "LI" => "Liechtenstein", "LT" => "Lithuania", "LU" => "Luxembourg", "MO" => "Macau SAR China", "MK" => "Macedonia", "MG" => "Madagascar", "MW" => "Malawi", "MY" => "Malaysia", "MV" => "Maldives", "ML" => "Mali", "MT" => "Malta", "MH" => "Marshall Islands", "MQ" => "Martinique", "MR" => "Mauritania", "MU" => "Mauritius", "YT" => "Mayotte", "FX" => "Metropolitan France", "MX" => "Mexico", "FM" => "Micronesia", "MI" => "Midway Islands", "MD" => "Moldova", "MC" => "Monaco", "MN" => "Mongolia", "ME" => "Montenegro", "MS" => "Montserrat", "MA" => "Morocco", "MZ" => "Mozambique", "MM" => "Myanmar [Burma]", "NA" => "Namibia", "NR" => "Nauru", "NP" => "Nepal", "NL" => "Netherlands", "AN" => "Netherlands Antilles", "NT" => "Neutral Zone", "NC" => "New Caledonia", "NZ" => "New Zealand", "NI" => "Nicaragua", "NE" => "Niger", "NG" => "Nigeria", "NU" => "Niue", "NF" => "Norfolk Island", "KP" => "North Korea", "VD" => "North Vietnam", "MP" => "Northern Mariana Islands", "NO" => "Norway", "OM" => "Oman", "PC" => "Pacific Islands Trust Territory", "PK" => "Pakistan", "PW" => "Palau", "PS" => "Palestinian Territories", "PA" => "Panama", "PZ" => "Panama Canal Zone", "PG" => "Papua New Guinea", "PY" => "Paraguay", "YD" => "People's Democratic Republic of Yemen", "PE" => "Peru", "PH" => "Philippines", "PN" => "Pitcairn Islands", "PL" => "Poland", "PT" => "Portugal", "PR" => "Puerto Rico", "QA" => "Qatar", "RO" => "Romania", "RU" => "Russia", "RW" => "Rwanda", "RE" => "Réunion", "BL" => "Saint Barthélemy", "SH" => "Saint Helena", "KN" => "Saint Kitts and Nevis", "LC" => "Saint Lucia", "MF" => "Saint Martin", "PM" => "Saint Pierre and Miquelon", "VC" => "Saint Vincent and the Grenadines", "WS" => "Samoa", "SM" => "San Marino", "SA" => "Saudi Arabia", "SN" => "Senegal", "RS" => "Serbia", "CS" => "Serbia and Montenegro", "SC" => "Seychelles", "SL" => "Sierra Leone", "SG" => "Singapore", "SK" => "Slovakia", "SI" => "Slovenia", "SB" => "Solomon Islands", "SO" => "Somalia", "ZA" => "South Africa", "GS" => "South Georgia and the South Sandwich Islands", "KR" => "South Korea", "ES" => "Spain", "LK" => "Sri Lanka", "SD" => "Sudan", "SR" => "Suriname", "SJ" => "Svalbard and Jan Mayen", "SZ" => "Swaziland", "SE" => "Sweden", "CH" => "Switzerland", "SY" => "Syria", "ST" => "São Tomé and Príncipe", "TW" => "Taiwan", "TJ" => "Tajikistan", "TZ" => "Tanzania", "TH" => "Thailand", "TL" => "Timor-Leste", "TG" => "Togo", "TK" => "Tokelau", "TO" => "Tonga", "TT" => "Trinidad and Tobago", "TN" => "Tunisia", "TR" => "Turkey", "TM" => "Turkmenistan", "TC" => "Turks and Caicos Islands", "TV" => "Tuvalu", "UM" => "U.S. Minor Outlying Islands", "PU" => "U.S. Miscellaneous Pacific Islands", "VI" => "U.S. Virgin Islands", "UG" => "Uganda", "UA" => "Ukraine", "SU" => "Union of Soviet Socialist Republics", "AE" => "United Arab Emirates", "GB" => "United Kingdom", "US" => "United States", "ZZ" => "Unknown or Invalid Region", "UY" => "Uruguay", "UZ" => "Uzbekistan", "VU" => "Vanuatu", "VA" => "Vatican City", "VE" => "Venezuela", "VN" => "Vietnam", "WK" => "Wake Island", "WF" => "Wallis and Futuna", "EH" => "Western Sahara", "YE" => "Yemen", "ZM" => "Zambia", "ZW" => "Zimbabwe", "AX" => "Åland Islands", ]; return view('front.checkout', compact('banner', 'total', 'items', 'totalWeight', 'shippingPrice', 'grandTotal', 'countryList')); } public function processPayment(Request $request) { $nonce = $request->input('nonce'); $amount = $request->amount; $contact = $request->input('contact'); $shipping = $request->input('shipping'); $validated = $request->validate([ 'nonce' => 'required|string', 'amount' => 'required|numeric', 'contact' => 'required|array', 'contact.phone' => 'required|string', 'contact.email' => 'required|email', 'shipping' => 'required|array', 'shipping.first_name' => 'required|string', 'shipping.last_name' => 'required|string', 'shipping.address_line1' => 'required|string', 'shipping.city' => 'required|string', 'shipping.state' => 'required|string', 'shipping.zip_code' => 'required|string', ]); $client = new SquareClient([ 'accessToken' => env('SQUARE_ACCESS_TOKEN'), 'environment' => env('SQUARE_ENVIRONMENT') === 'production' ? Environment::PRODUCTION : Environment::SANDBOX, ]); // Set amount money (in cents) $amount_money = new Money(); $amount_money->setAmount($amount * 100); // amount in cents $amount_money->setCurrency('GBP'); // Adjust currency if necessary // Create payment request body $body = new CreatePaymentRequest($nonce, uniqid()); $body->setAmountMoney($amount_money); $body->setAutocomplete(true); // Complete payment automatically $body->setLocationId(env('SQUARE_LOCATION_ID')); try { // Send Payment request to Square API $api_response = $client->getPaymentsApi()->createPayment($body); // Handle API response if ($api_response->isSuccess()) { $result = $api_response->getResult(); // Store the contact and shipping information in the database (Optional) $this->storeOrderDetails($contact, $shipping, $result); \Cart::clear(); session()->flash('success'); session()->flash('message', 'Payment successful!'); return response()->json([ 'success' => true, 'msg' => 'Payment successful!', 'payment' => $result, ]); } else { $errors = $api_response->getErrors(); session()->flash('failed'); session()->flash('message', 'Payment failed: ' . json_encode($errors)); return response()->json([ 'success' => false, 'msg' => 'Payment failed', 'errors' => $errors, ], 400); } } catch (ApiException|\Exception $e) { session()->flash('failed'); session()->flash('message', $e->getMessage()); return response()->json([ 'success' => false, 'msg' => $e->getMessage(), ], 500); } } private function storeOrderDetails($contact, $shipping, $paymentResult) { // Extract the payment object from the CreatePaymentResponse $payment = $paymentResult->getPayment(); // Check if payment object exists if (!$payment) { throw new \Exception('Payment object is missing in the response.'); } // Get cart items $cartItems = \Cart::getContent(); // Prepare an array to store the details of each cart item $items = []; foreach ($cartItems as $item) { // Extract the details you want to store $itemDetails = [ 'id' => $item->id, // Cart Item ID 'name' => $item->name, // Product Name 'price' => $item->price, // Product Price 'quantity' => $item->quantity, // Quantity of the Product 'dealer' => $item->attributes->dealer, // Dealer (from item attributes) 'weight' => $item->attributes->weight, // Weight (from item attributes) ]; // Add the item details to the items array $items[] = $itemDetails; } // Store the order details with the item details in JSON format $order = new Order(); // Assuming you have an Order model $order->items = json_encode($items); // Store item details as a JSON array $order->contact_country = $contact['country']; $order->contact_phone = $contact['phone']; $order->contact_email = $contact['email']; $order->shipping_first_name = $shipping['first_name']; $order->shipping_last_name = $shipping['last_name']; $order->shipping_address_line1 = $shipping['address_line1']; $order->shipping_address_line2 = $shipping['address_line2'] ?? null; // Optional $order->shipping_city = $shipping['city']; $order->shipping_state = $shipping['state']; $order->shipping_zip_code = $shipping['zip_code']; $order->payment_id = $payment->getId(); // Access the ID of the payment $order->amount = $payment->getAmountMoney()->getAmount() / 100; // Convert back to dollars $order->status = $payment->getStatus(); // Use the status from the payment $order->save(); } private function calculateShipping($totalWeight) { if ($totalWeight == 0) { return 0; } elseif ($totalWeight <= 250) { return 4.00; } elseif ($totalWeight <= 1000) { return 6.00; } elseif ($totalWeight <= 5000) { return 12.00; } elseif ($totalWeight <= 10000) { return 16.00; } elseif ($totalWeight <= 30000) { return 22.00; } elseif ($totalWeight > 30001) { return 30.00; } else { return 7.00; } } }first(); return view('front.basket',compact('items','total','banner')); } public function addCart(Request $request) { $product = Product::where('slug', $request->slug)->first(); if (!$product) { return response()->json(['message' => 'Product not found'], 404); } if ($request->price == 0) { return response()->json(['message' => 'Price should not be 0'], 400); } $uniqueId = $product->id . '_' . $request->size; $cartItem = \Cart::get($uniqueId); if ($cartItem) { \Cart::update($uniqueId, [ 'quantity' => +1, ]); } else { \Cart::add([ 'id' => $uniqueId, 'name' => $product->name, 'slug' => $product->slug, 'price' => $request->price, 'quantity' => 1, 'attributes' => [ 'weight' => $request->size, 'dealer' => $request->dealer, 'image' => $product->imageCover->image, ], 'associatedModel' => $product, ]); } return response()->json(['message' => 'Item has been added to the cart']); } public function getCartQuantity() { $totalQuantity = \Cart::getTotalQuantity(); return response()->json(['totalQuantity' => $totalQuantity]); } public function getCartPrice() { $totalPrice = \Cart::getTotal(); return response()->json(['totalPrice' => $totalPrice]); } public function addQuantity($productId) { \Cart::update($productId, [ 'quantity' => +1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been increased', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function decreaseQuantity($productId) { \Cart::update($productId, [ 'quantity' => -1 ]); $item = \Cart::get($productId); return response()->json([ 'message' => 'Quantity has been decrease', 'productPrice' => $item->price * $item->quantity, 'newQuantity' => $item->quantity, ]); } public function removeItem($productId) { \Cart::remove($productId); return response()->json([ 'message' => 'Item has been removed from the cart', ]); } public function applyCoupon(Request $request) { $couponCode = $request->input('coupon_code'); $code = CouponCode::where('name',$couponCode)->first(); if ($code && $code->isUse == '0') { $code->update([ 'isUse' => '1' ]); $value = '-'.$code->discount.'%'; $condition = new \Darryldecode\Cart\CartCondition(array( 'name' => $code->name, 'type' => 'coupon', 'target' => 'subtotal', 'value' => $value )); \Cart::condition($condition); return response()->json([ 'message' => 'Coupon code used successfully', ]); } else { // \Cart::removeConditionsByType('coupon'); return response()->json([ 'message' => 'You have entered an invalid coupon or used coupon code.', ],500); } } public function Checkout() { $banner = Banner::where('page', 'Checkout')->first(); $total = \Cart::getTotal(); $items = \Cart::getContent(); //dd($items); $totalWeight = 0; foreach ($items as $item) { if (!$item->attributes->dealer) { $weight = Size::whereId($item->attributes->weight)->first()->weight ?? 0; $totalWeight += $weight * $item->quantity; } } $shippingPrice = $this->calculateShipping($totalWeight); $grandTotal = $total + $shippingPrice; $countryList = [ "AF" => "Afghanistan", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua and Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "BY" => "Belarus", "BE" => "Belgium", "BZ" => "Belize", "BJ" => "Benin", "BM" => "Bermuda", "BT" => "Bhutan", "BO" => "Bolivia", "BA" => "Bosnia and Herzegovina", "BW" => "Botswana", "BV" => "Bouvet Island", "BR" => "Brazil", "BQ" => "British Antarctic Territory", "IO" => "British Indian Ocean Territory", "VG" => "British Virgin Islands", "BN" => "Brunei", "BG" => "Bulgaria", "BF" => "Burkina Faso", "BI" => "Burundi", "KH" => "Cambodia", "CM" => "Cameroon", "CA" => "Canada", "CT" => "Canton and Enderbury Islands", "CV" => "Cape Verde", "KY" => "Cayman Islands", "CF" => "Central African Republic", "TD" => "Chad", "CL" => "Chile", "CN" => "China", "CX" => "Christmas Island", "CC" => "Cocos [Keeling] Islands", "CO" => "Colombia", "KM" => "Comoros", "CG" => "Congo - Brazzaville", "CD" => "Congo - Kinshasa", "CK" => "Cook Islands", "CR" => "Costa Rica", "HR" => "Croatia", "CU" => "Cuba", "CY" => "Cyprus", "CZ" => "Czech Republic", "CI" => "Côte d’Ivoire", "DK" => "Denmark", "DJ" => "Djibouti", "DM" => "Dominica", "DO" => "Dominican Republic", "NQ" => "Dronning Maud Land", "DD" => "East Germany", "EC" => "Ecuador", "EG" => "Egypt", "SV" => "El Salvador", "GQ" => "Equatorial Guinea", "ER" => "Eritrea", "EE" => "Estonia", "ET" => "Ethiopia", "FK" => "Falkland Islands", "FO" => "Faroe Islands", "FJ" => "Fiji", "FI" => "Finland", "FR" => "France", "GF" => "French Guiana", "PF" => "French Polynesia", "TF" => "French Southern Territories", "FQ" => "French Southern and Antarctic Territories", "GA" => "Gabon", "GM" => "Gambia", "GE" => "Georgia", "DE" => "Germany", "GH" => "Ghana", "GI" => "Gibraltar", "GR" => "Greece", "GL" => "Greenland", "GD" => "Grenada", "GP" => "Guadeloupe", "GU" => "Guam", "GT" => "Guatemala", "GG" => "Guernsey", "GN" => "Guinea", "GW" => "Guinea-Bissau", "GY" => "Guyana", "HT" => "Haiti", "HM" => "Heard Island and McDonald Islands", "HN" => "Honduras", "HK" => "Hong Kong SAR China", "HU" => "Hungary", "IS" => "Iceland", "IN" => "India", "ID" => "Indonesia", "IR" => "Iran", "IQ" => "Iraq", "IE" => "Ireland", "IM" => "Isle of Man", "IL" => "Israel", "IT" => "Italy", "JM" => "Jamaica", "JP" => "Japan", "JE" => "Jersey", "JT" => "Johnston Island", "JO" => "Jordan", "KZ" => "Kazakhstan", "KE" => "Kenya", "KI" => "Kiribati", "KW" => "Kuwait", "KG" => "Kyrgyzstan", "LA" => "Laos", "LV" => "Latvia", "LB" => "Lebanon", "LS" => "Lesotho", "LR" => "Liberia", "LY" => "Libya", "LI" => "Liechtenstein", "LT" => "Lithuania", "LU" => "Luxembourg", "MO" => "Macau SAR China", "MK" => "Macedonia", "MG" => "Madagascar", "MW" => "Malawi", "MY" => "Malaysia", "MV" => "Maldives", "ML" => "Mali", "MT" => "Malta", "MH" => "Marshall Islands", "MQ" => "Martinique", "MR" => "Mauritania", "MU" => "Mauritius", "YT" => "Mayotte", "FX" => "Metropolitan France", "MX" => "Mexico", "FM" => "Micronesia", "MI" => "Midway Islands", "MD" => "Moldova", "MC" => "Monaco", "MN" => "Mongolia", "ME" => "Montenegro", "MS" => "Montserrat", "MA" => "Morocco", "MZ" => "Mozambique", "MM" => "Myanmar [Burma]", "NA" => "Namibia", "NR" => "Nauru", "NP" => "Nepal", "NL" => "Netherlands", "AN" => "Netherlands Antilles", "NT" => "Neutral Zone", "NC" => "New Caledonia", "NZ" => "New Zealand", "NI" => "Nicaragua", "NE" => "Niger", "NG" => "Nigeria", "NU" => "Niue", "NF" => "Norfolk Island", "KP" => "North Korea", "VD" => "North Vietnam", "MP" => "Northern Mariana Islands", "NO" => "Norway", "OM" => "Oman", "PC" => "Pacific Islands Trust Territory", "PK" => "Pakistan", "PW" => "Palau", "PS" => "Palestinian Territories", "PA" => "Panama", "PZ" => "Panama Canal Zone", "PG" => "Papua New Guinea", "PY" => "Paraguay", "YD" => "People's Democratic Republic of Yemen", "PE" => "Peru", "PH" => "Philippines", "PN" => "Pitcairn Islands", "PL" => "Poland", "PT" => "Portugal", "PR" => "Puerto Rico", "QA" => "Qatar", "RO" => "Romania", "RU" => "Russia", "RW" => "Rwanda", "RE" => "Réunion", "BL" => "Saint Barthélemy", "SH" => "Saint Helena", "KN" => "Saint Kitts and Nevis", "LC" => "Saint Lucia", "MF" => "Saint Martin", "PM" => "Saint Pierre and Miquelon", "VC" => "Saint Vincent and the Grenadines", "WS" => "Samoa", "SM" => "San Marino", "SA" => "Saudi Arabia", "SN" => "Senegal", "RS" => "Serbia", "CS" => "Serbia and Montenegro", "SC" => "Seychelles", "SL" => "Sierra Leone", "SG" => "Singapore", "SK" => "Slovakia", "SI" => "Slovenia", "SB" => "Solomon Islands", "SO" => "Somalia", "ZA" => "South Africa", "GS" => "South Georgia and the South Sandwich Islands", "KR" => "South Korea", "ES" => "Spain", "LK" => "Sri Lanka", "SD" => "Sudan", "SR" => "Suriname", "SJ" => "Svalbard and Jan Mayen", "SZ" => "Swaziland", "SE" => "Sweden", "CH" => "Switzerland", "SY" => "Syria", "ST" => "São Tomé and Príncipe", "TW" => "Taiwan", "TJ" => "Tajikistan", "TZ" => "Tanzania", "TH" => "Thailand", "TL" => "Timor-Leste", "TG" => "Togo", "TK" => "Tokelau", "TO" => "Tonga", "TT" => "Trinidad and Tobago", "TN" => "Tunisia", "TR" => "Turkey", "TM" => "Turkmenistan", "TC" => "Turks and Caicos Islands", "TV" => "Tuvalu", "UM" => "U.S. Minor Outlying Islands", "PU" => "U.S. Miscellaneous Pacific Islands", "VI" => "U.S. Virgin Islands", "UG" => "Uganda", "UA" => "Ukraine", "SU" => "Union of Soviet Socialist Republics", "AE" => "United Arab Emirates", "GB" => "United Kingdom", "US" => "United States", "ZZ" => "Unknown or Invalid Region", "UY" => "Uruguay", "UZ" => "Uzbekistan", "VU" => "Vanuatu", "VA" => "Vatican City", "VE" => "Venezuela", "VN" => "Vietnam", "WK" => "Wake Island", "WF" => "Wallis and Futuna", "EH" => "Western Sahara", "YE" => "Yemen", "ZM" => "Zambia", "ZW" => "Zimbabwe", "AX" => "Åland Islands", ]; return view('front.checkout', compact('banner', 'total', 'items', 'totalWeight', 'shippingPrice', 'grandTotal', 'countryList')); } public function processPayment(Request $request) { $nonce = $request->input('nonce'); $amount = $request->amount; $contact = $request->input('contact'); $shipping = $request->input('shipping'); $validated = $request->validate([ 'nonce' => 'required|string', 'amount' => 'required|numeric', 'contact' => 'required|array', 'contact.phone' => 'required|string', 'contact.email' => 'required|email', 'shipping' => 'required|array', 'shipping.first_name' => 'required|string', 'shipping.last_name' => 'required|string', 'shipping.address_line1' => 'required|string', 'shipping.city' => 'required|string', 'shipping.state' => 'required|string', 'shipping.zip_code' => 'required|string', ]); $client = new SquareClient([ 'accessToken' => env('SQUARE_ACCESS_TOKEN'), 'environment' => env('SQUARE_ENVIRONMENT') === 'production' ? Environment::PRODUCTION : Environment::SANDBOX, ]); // Set amount money (in cents) $amount_money = new Money(); $amount_money->setAmount($amount * 100); // amount in cents $amount_money->setCurrency('GBP'); // Adjust currency if necessary // Create payment request body $body = new CreatePaymentRequest($nonce, uniqid()); $body->setAmountMoney($amount_money); $body->setAutocomplete(true); // Complete payment automatically $body->setLocationId(env('SQUARE_LOCATION_ID')); try { // Send Payment request to Square API $api_response = $client->getPaymentsApi()->createPayment($body); // Handle API response if ($api_response->isSuccess()) { $result = $api_response->getResult(); // Store the contact and shipping information in the database (Optional) $this->storeOrderDetails($contact, $shipping, $result); \Cart::clear(); session()->flash('success'); session()->flash('message', 'Payment successful!'); return response()->json([ 'success' => true, 'msg' => 'Payment successful!', 'payment' => $result, ]); } else { $errors = $api_response->getErrors(); session()->flash('failed'); session()->flash('message', 'Payment failed: ' . json_encode($errors)); return response()->json([ 'success' => false, 'msg' => 'Payment failed', 'errors' => $errors, ], 400); } } catch (ApiException|\Exception $e) { session()->flash('failed'); session()->flash('message', $e->getMessage()); return response()->json([ 'success' => false, 'msg' => $e->getMessage(), ], 500); } } private function storeOrderDetails($contact, $shipping, $paymentResult) { // Extract the payment object from the CreatePaymentResponse $payment = $paymentResult->getPayment(); // Check if payment object exists if (!$payment) { throw new \Exception('Payment object is missing in the response.'); } // Get cart items $cartItems = \Cart::getContent(); // Prepare an array to store the details of each cart item $items = []; foreach ($cartItems as $item) { // Extract the details you want to store $itemDetails = [ 'id' => $item->id, // Cart Item ID 'name' => $item->name, // Product Name 'price' => $item->price, // Product Price 'quantity' => $item->quantity, // Quantity of the Product 'dealer' => $item->attributes->dealer, // Dealer (from item attributes) 'weight' => $item->attributes->weight, // Weight (from item attributes) ]; // Add the item details to the items array $items[] = $itemDetails; } // Store the order details with the item details in JSON format $order = new Order(); // Assuming you have an Order model $order->items = json_encode($items); // Store item details as a JSON array $order->contact_country = $contact['country']; $order->contact_phone = $contact['phone']; $order->contact_email = $contact['email']; $order->shipping_first_name = $shipping['first_name']; $order->shipping_last_name = $shipping['last_name']; $order->shipping_address_line1 = $shipping['address_line1']; $order->shipping_address_line2 = $shipping['address_line2'] ?? null; // Optional $order->shipping_city = $shipping['city']; $order->shipping_state = $shipping['state']; $order->shipping_zip_code = $shipping['zip_code']; $order->payment_id = $payment->getId(); // Access the ID of the payment $order->amount = $payment->getAmountMoney()->getAmount() / 100; // Convert back to dollars $order->status = $payment->getStatus(); // Use the status from the payment $order->save(); } private function calculateShipping($totalWeight) { if ($totalWeight == 0) { return 0; } elseif ($totalWeight <= 250) { return 4.00; } elseif ($totalWeight <= 1000) { return 6.00; } elseif ($totalWeight <= 5000) { return 12.00; } elseif ($totalWeight <= 10000) { return 16.00; } elseif ($totalWeight <= 30000) { return 22.00; } elseif ($totalWeight > 30001) { return 30.00; } else { return 7.00; } } } Target class [App\Http\Controllers\CartController] does not exist.