Viewing file: BankCheckout.php (4.75 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Traits;
use App\{ Models\Order, Models\Setting, Models\TrackOrder, Helpers\EmailHelper, Helpers\PriceHelper, Models\Notification, }; use App\Helpers\SmsHelper; use App\Models\Item; use App\Models\PromoCode; use App\Models\ShippingService; use App\Models\State; use Carbon\Carbon; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use Illuminate\Support\Str;
trait BankCheckout {
public function BankSubmit($data) { try { $user = Auth::user(); $setting = Setting::first(); $cart = Session::get('cart_' . $user->id); $total_tax = 0; $cart_total = 0; $total = 0; $option_price = 0;
if (empty($cart)) { return [ 'status' => false, 'message' => 'Your Cart Is Empty' ]; }
foreach ($cart as $key => $item) { $total += $item['main_price'] * $item['qty']; $option_price += $item['attribute_price']; $cart_total = $total + $option_price; $item = Item::findOrFail($key); if ($item->tax) { $total_tax += $item::taxCalculate($item); } }
if (!PriceHelper::Digital()) { $shipping = null; } else { $shipping = ShippingService::findOrFail($data['shipping_id']); }
$discount = []; if (Session::has('coupon')) { $discount = Session::get('coupon'); }
$grand_total = ($cart_total + ($shipping ? $shipping->price : 0)) + $total_tax; $grand_total = $grand_total - ($discount ? $discount['discount'] : 0); $grand_total += PriceHelper::StatePrce($data['state_id'], $cart_total); $total_amount = PriceHelper::setConvertPrice($grand_total);
$orderData = [ 'state' => $data['state_id'] ? json_encode(State::findOrFail($data['state_id']), true) : null, 'cart' => json_encode($cart, true), 'discount' => json_encode($discount, true), 'shipping' => json_encode($shipping, true), 'tax' => $total_tax, 'state_price' => PriceHelper::StatePrce($data['state_id'], $cart_total), 'shipping_info' => json_encode(Session::get('shipping_address'), true), 'billing_info' => json_encode(Session::get('billing_address'), true), 'payment_method' => 'Bank Transfer', 'user_id' => isset($user) ? $user->id : 0, 'transaction_number' => Str::random(10), 'currency_sign' => PriceHelper::setCurrencySign(), 'currency_value' => PriceHelper::setCurrencyValue(), 'payment_status' => 'Unpaid', 'txnid' => $data['txn_id'], 'order_status' => 'Pending', ];
$order = Order::create($orderData);
$new_txn = 'ORD-' . str_pad(Carbon::now()->format('Ymd'), 4, '0000', STR_PAD_LEFT) . '-' . $order->id; $order->transaction_number = $new_txn; $order->save();
TrackOrder::create([ 'title' => 'Pending', 'order_id' => $order->id, ]);
PriceHelper::Transaction($order->id, $order->transaction_number, EmailHelper::getEmail(), PriceHelper::OrderTotal($order, 'trns')); PriceHelper::LicenseQtyDecrese($cart); PriceHelper::stockDecrese(); Notification::create([ 'order_id' => $order->id ]);
$emailData = [ 'to' => EmailHelper::getEmail(), 'type' => "Order", 'user_name' => isset($user) ? $user->displayName() : Session::get('billing_address')['bill_first_name'], 'order_cost' => $total_amount, 'transaction_number' => $order->transaction_number, 'site_title' => Setting::first()->title, ];
$email = new EmailHelper(); $email->sendTemplateMail($emailData);
if ($discount) { $coupon_id = $discount['code']['id']; $get_coupon = PromoCode::findOrFail($coupon_id); $get_coupon->no_of_times -= 1; $get_coupon->update(); }
if ($setting->is_twilio == 1) { $sms = new SmsHelper(); $user_number = json_decode($order->billing_info, true)['bill_phone']; if ($user_number) { $sms->SendSms($user_number, "'purchase'", $order->transaction_number); } }
Session::put('order_id', $order->id); Session::forget('cart_' . $user->id); Session::forget('discount'); Session::forget('coupon');
return [ 'status' => true ];
} catch (\Exception $e) { return [ 'status' => false, 'message' => 'Error: ' . $e->getMessage() ]; } }
}
|