Viewing File: /home/ubuntu/vedadeals-backend-base/app/Services/OrderService.php
<?php
namespace App\Services;
use Illuminate\Http\Request;
use App\Models\{ User, Product, OrderPayment, Order, OrderProduct, Cart, UserWallet };
use DB, Exception, Setting, stdClass;
use App\Jobs\SendEmailJob;
use App\Helpers\Helper;
class OrderService {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(Request $request)
{
}
/**
* @method handle_direct_checkout()
*
* @uses to complete & save a order payment in direct checkout type.
*
* @created Karthick
*
* @updated
*
* @param object $order_product
*
* @return success / failure status
*/
public static function handle_direct_checkout($checkout_data) {
try {
$user = User::find($checkout_data->user_id);
if(!$user) {
throw new Exception(api_error(100), 100);
}
$product = Product::Available()->find($checkout_data->product_id);
ProductService::products_base_validations($product, $checkout_data->quantity);
$product_price = $product->price;
$checkout_amount = $product_price * $checkout_data->quantity;
$user_wallet = UserWallet::firstOrCreate(['user_id' => $user->id]);
if($user_wallet->remaining < $checkout_amount && $checkout_data->payment_mode == PAYMENT_MODE_WALLET) {
throw new Exception(api_error(114), 114);
}
DB::beginTransaction();
$order = Order::Create([
'user_id' => $user->id,
'product_id' => $product->id,
'delivery_address_id' => $checkout_data->delivery_address_id,
'total_products' => $checkout_data->quantity,
'sub_total' => $checkout_amount,
'total' => $checkout_amount,
]);
$order_product = OrderProduct::Create([
'user_id' => $user->id,
'product_id' => $product->id,
'order_id' => $order->id,
'original_price' => $product->original_price,
'discount_price' => $product->discount_price,
'per_quantity_price' => $product->price,
'quantity' => $checkout_data->quantity,
'sub_total' => $checkout_amount,
'total' => $checkout_amount,
]);
$order_payment = OrderPayment::Create([
'user_id' => $user->id,
'order_id' => $order->id,
'payment_id' => "OP-".substr(generate_payment_id(), 0, 8),
'payment_mode' => PAYMENT_MODE_WALLET,
'currency' => Setting::get('currency'),
'sub_total' => $order->sub_total ? : 0.00,
'total' => $order->total ? : 0.00,
'payment_mode' => $checkout_data->payment_mode,
'status' => PAID,
'paid_date' => now(),
]);
if($checkout_data->payment_mode == PAYMENT_MODE_WALLET) {
$payment_data = new stdClass;
$payment_data->user_id = $user->id; $payment_data->amount = $order_payment->total;
$payment_data->amount_type = AMOUNT_TYPE_MINUS; $payment_data->payment_type = WALLET_PAYMENT_TYPE_PAID;
$payment_data->payment_mode = PAYMENT_MODE_WALLET; $payment_data->user_billing_account_id = 0;
$user_wallet_payment = PaymentService::user_wallet_payment_save($payment_data);
if(!$user_wallet_payment['result']) {
throw new Exception($user_wallet_payment['message']);
}
} elseif($checkout_data->payment_mode == PAYMENT_MODE_CARD) {
$payment_data = new stdClass;
$payment_data->user_id = $user->id; $payment_data->amount = $order_payment->total;
$payment_data->user_card_id = $checkout_data->user_card_id;
$user_card_payment = PaymentService::user_card_payment_save($payment_data);
if(!$user_card_payment['result']) {
throw new Exception($user_card_payment['message']);
}
}
$update_product_inventory = ProductService::update_product_inventory($order_product->product_id, $order_product->quantity, PRODUCT_SOLD)->getData();
if(!$update_product_inventory->result) {
throw new Exception($update_product_inventory->error);
}
DB::commit();
$response_array = ['result' => true, 'message' => tr('order_payment_save_success'), 'order_unique_id' => $order->unique_id];
return $response_array;
throw new Exception(tr('order_payment_save_failed'));
} catch(Exception $e) {
DB::rollback();
$response_array = ['result' => false, 'message' => $e->getMessage(), 'error_code' => $e->getCode()];
return $response_array;
}
}
/**
* @method handle_cart_checkout()
*
* @uses to complete & save a order payment in cart checkout type.
*
* @created Karthick
*
* @updated
*
* @param object $order_product
*
* @return success / failure status
*/
public static function handle_cart_checkout($checkout_data) {
try {
$user = User::find($checkout_data->user_id);
if(!$user) {
throw new Exception(api_error(100), 100);
}
$cart_products = Cart::has('product')->where(['user_id' => $user->id])->distinct('product_id')->get(['id', 'product_id', 'quantity']);
if(!$cart_products->count()) {
throw new Exception(api_error(129), 129);
}
$cart_checkout_amount = 0;
foreach($cart_products as $cart_product) {
$product = Product::find($cart_product->product_id);
if($product) {
if($product->stock_status == IN_STOCK) {
if($product->quantity < $cart_product->quantity) {
$message = tr('product_quantity_error_message_first_half', $product->quantity).' '.tr('product_quantity_error_message_second_half', $product->name);
throw new Exception($message, 127);
}
$product_price = $product->price;
$cart_checkout_amount += $product_price * $cart_product->quantity;
$cart_ids[] = $cart_product->id;
}
}
}
$user_wallet = UserWallet::firstOrCreate(['user_id' => $checkout_data->user_id]);
if($user_wallet->remaining < $cart_checkout_amount && $checkout_data->payment_mode == PAYMENT_MODE_WALLET) {
throw new Exception(api_error(114), 114);
}
if(!isset($cart_ids)) {
throw new Exception(api_error(139), 139);
}
$available_cart_products = Cart::has('product')->whereIn('id', $cart_ids)->distinct('product_id')->get(['id', 'product_id', 'quantity']);
DB::beginTransaction();
$order = Order::Create([
'user_id' => $user->id,
'delivery_address_id' => $checkout_data->delivery_address_id,
'total_products' => $available_cart_products->count(),
'total' => $cart_checkout_amount,
'sub_total' => $cart_checkout_amount,
'checkout_type' => CART_CHECKOUT
]);
foreach($available_cart_products as $available_cart_product) {
$product = Product::find($available_cart_product->product_id);
$order_product = OrderProduct::Create([
'user_id' => $user->id,
'product_id' => $product->id,
'order_id' => $order->id,
'original_price' => $product->original_price,
'discount_price' => $product->discount_price,
'per_quantity_price' => $product->price,
'quantity' => $available_cart_product->quantity,
'sub_total' => $available_cart_product->quantity * $product->price,
'total' => $available_cart_product->quantity * $product->price,
]);
}
$order_payment = OrderPayment::Create([
'user_id' => $user->id,
'order_id' => $order->id,
'payment_id' => $checkout_data->payment_mode == PAYMENT_MODE_UPI ? $checkout_data->payment_id : "OP-".substr(generate_payment_id(), 0, 8),
'currency' => Setting::get('currency'),
'sub_total' => $cart_checkout_amount ? : 0.00,
'total' => $cart_checkout_amount ? : 0.00,
'payment_mode' => $checkout_data->payment_mode,
'status' => PAID,
'paid_date' => now(),
]);
if($checkout_data->payment_mode == PAYMENT_MODE_WALLET) {
$payment_data = new stdClass;
$payment_data->user_id = $user->id; $payment_data->amount = $order_payment->total;
$payment_data->amount_type = AMOUNT_TYPE_MINUS; $payment_data->payment_type = WALLET_PAYMENT_TYPE_PAID;
$payment_data->payment_mode = PAYMENT_MODE_WALLET; $payment_data->user_billing_account_id = 0;
$user_wallet_payment = PaymentService::user_wallet_payment_save($payment_data);
if(!$user_wallet_payment['result']) {
throw new Exception($user_wallet_payment['message']);
}
} elseif($checkout_data->payment_mode == PAYMENT_MODE_CARD) {
$payment_data = new stdClass;
$payment_data->user_id = $user->id; $payment_data->amount = $order_payment->total;
$payment_data->user_card_id = $checkout_data->user_card_id;
$user_card_payment = PaymentService::user_card_payment_save($payment_data);
if(!$user_card_payment['result']) {
throw new Exception($user_card_payment['message']);
}
}
$update_product_inventory = ProductService::update_cart_product_inventory($order->id)->getData();
if(!$update_product_inventory->result) {
throw new Exception($update_product_inventory->error);
}
$available_cart_products->each->delete();
Helper::cache_update_for_products($user->id, CART);
DB::commit();
$response_array = ['result' => true, 'message' => tr('order_payment_save_success'), 'order_unique_id' => $order->unique_id];
return $response_array;
throw new Exception(tr('order_payment_save_failed'));
} catch(Exception $e) {
DB::rollback();
$response_array = ['result' => false, 'message' => $e->getMessage(), 'error_code' => $e->getCode()];
return $response_array;
}
}
/**
* @method handle_checkout()
*
* @uses to complete & save a order payment.
*
* @created Karthick
*
* @updated
*
* @param object $order_product
*
* @return success / failure status
*/
public static function handle_checkout($checkout_data) {
try {
$user = User::find($checkout_data->user_id);
if(!$user) {
throw new Exception(api_error(100), 100);
}
$cart_products = Cart::has('product')->where(['user_id' => $user->id])->distinct('product_id')->get(['id', 'product_id', 'quantity']);
if(!$cart_products->count()) {
throw new Exception(api_error(129), 129);
}
$cart_checkout_amount = 0;
foreach($cart_products as $cart_product) {
$product = Product::find($cart_product->product_id);
if($product) {
if($product->stock_status == IN_STOCK) {
if($product->quantity < $cart_product->quantity) {
$message = tr('product_quantity_error_message_first_half', $product->quantity).' '.tr('product_quantity_error_message_second_half', $product->name);
throw new Exception($message, 127);
}
$product_price = $product->price;
$cart_checkout_amount += $product_price * $cart_product->quantity;
$cart_ids[] = $cart_product->id;
}
}
}
if(!isset($cart_ids)) {
throw new Exception(api_error(139), 139);
}
$available_cart_products = Cart::has('product')->whereIn('id', $cart_ids)->distinct('product_id')->get(['id', 'product_id', 'quantity']);
DB::beginTransaction();
$order = Order::Create([
'user_id' => $user->id,
'delivery_address_id' => $checkout_data->delivery_address_id,
'total_products' => $available_cart_products->count(),
'total' => $cart_checkout_amount,
'sub_total' => $cart_checkout_amount,
'checkout_type' => CART_CHECKOUT,
'order_status' => ORDER_STATUS_PENDING
]);
foreach($available_cart_products as $available_cart_product) {
$product = Product::find($available_cart_product->product_id);
$order_product = OrderProduct::Create([
'user_id' => $user->id,
'product_id' => $product->id,
'order_id' => $order->id,
'original_price' => $product->original_price,
'discount_price' => $product->discount_price,
'per_quantity_price' => $product->price,
'quantity' => $available_cart_product->quantity,
'sub_total' => $available_cart_product->quantity * $product->price,
'total' => $available_cart_product->quantity * $product->price,
]);
}
$order_payment = OrderPayment::Create([
'user_id' => $user->id,
'order_id' => $order->id,
'payment_id' => "OP-".substr(generate_payment_id(), 0, 8),
'transaction_hash' => $checkout_data->transaction_hash,
'wallet_address' => $checkout_data->wallet_address,
'currency' => $checkout_data->currency,
'sub_total' => $cart_checkout_amount ? : 0.00,
'total' => $cart_checkout_amount ? : 0.00,
'payment_mode' => $checkout_data->payment_mode,
'status' => PAID,
'paid_date' => now(),
]);
$update_product_inventory = ProductService::update_cart_product_inventory($order->id)->getData();
if(!$update_product_inventory->result) {
throw new Exception($update_product_inventory->error);
}
$available_cart_products->each->delete();
DB::commit();
$response_array = ['result' => true, 'message' => tr('order_payment_save_success'), 'order_unique_id' => $order->unique_id];
return $response_array;
throw new Exception(tr('order_payment_save_failed'));
} catch(Exception $e) {
DB::rollback();
$response_array = ['result' => false, 'message' => $e->getMessage(), 'error_code' => $e->getCode()];
return $response_array;
}
}
}
Back to Directory
File Manager