Viewing File: /home/ubuntu/vedadeals-backend-base/app/Http/Controllers/ApplicationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Helpers\Helper;

use DB, Exception, Setting, Hash, Storage, Log;

use App\Models\User, App\Models\Domain, App\Models\Watchlist, App\Models\Bid, App\Models\UserWallet;

use App\Jobs\{ AuctionCompleteJob };
use App\Models\OrderPayment;
use App\Models\OrderProduct;
use App\Repositories\HandshakeRepository as HandshakeRepo;
use App\Services\ProductService;
use Illuminate\Support\Facades\Http;

class ApplicationController extends Controller
{

    public function __construct(Request $request) {

        Log::info(url()->current());
    }
    
    /**
     * @method cron_bid_approve()
     *
     * @uses
     *
     * @created Arun
     *
     * @edited
     *
     * @param - 
     *
     * @return JSON Response
     */

    public function cron_bid_approve(Request $request) {

        try {

            Log::info('Approve Bid started');

            DB::begintransaction();

            $current_timestamp = date("Y-m-d H:i:s");

            $base_query = $total_query = Domain::whereNotNull('auction_end_at')
                                            ->where('auction_status', AUCTION_LIVE)
                                            ->where('auction_end_at','>', $current_timestamp);

            $domains = $base_query->get();

            $data['total'] = $total_query->count() ?? 0;

            $approved = 0;

            foreach($domains as $domain) {

                $bids = Bid::where('domain_id', $domain->id)->where('bid_status', BID_STATUS_PENDING)->get();

                foreach ($bids as $bid) {

                    $request->request->add([
                        'id' => $bid->user_id ?? 0,
                        'amount' => $bid->amount ?? 0.00,
                    ]);
                    
                    //Approve the bid which is created in DB and not approved on blockchain.
                    $create_bid = HandshakeRepo::create_bid($domain, $request)->getData();

                    $auction_end_at = $domain->auction_end_at ?? date('Y-m-d H:i:s',strtotime('+3 weeks',strtotime(date("Y-m-d H:i:s"))));

                    if ($create_bid->success) {

                        $auction_end_at = date('Y-m-d H:i:s', $create_bid->mtime ?? strtotime('+3 weeks',strtotime(date("Y-m-d H:i:s")))) ?? $auction_end_at;
                        
                        $bid->bid_transaction_hash = $create_bid->hash ?? "";

                        $bid->bid_status = BID_STATUS_APPROVED;

                        $bid->save();

                        $approved++;
                    }
                }
            }

            DB::commit();

            $data['approved'] = $approved ?? 0;

            info("Total Bid ".print_r($data['total'], true));

            info("Approved Bid ".print_r($approved, true));

            return $this->sendResponse($message = "", $success_code = "", $data);

        } catch(Exception $e) {

            DB::rollback();

            return $this->sendError($e->getMessage(), $e->getCode());

        }

    }

    /**
     * @method cron_auction_complete()
     *
     * @uses to complete the bid, refund bid amount to users lost the bid and assign domain to user won the user - using cron jon Scheduling.
     *
     * @created Karthick
     *
     * @updated  
     *
     * @param
     *
     * @return
     */

    public function cron_auction_complete(Request $request) {

        try {

            DB::beginTransaction();

            $domains = Domain::where([ ['auction_status', AUCTION_LIVE], ['is_auction_opened', YES] ,['auction_end_at', '<', now()] ])->get();

            info('Bid Completed Domains : '.$domains->count());

            foreach($domains as $domain) {

                $result = $domain->update(['auction_status' => AUCTION_COMPLETED, 'transfer_status' => TRANSFER_INITIATED]);

                if(!$result) {

                    throw new Exception(tr('auction_completed_failed'));
                }

                AuctionCompleteJob::dispatch($domain->id);
            }

            DB::commit();

            info('CRON Auction Complete Success');

            return true;

        } catch (Exception $e) {

            DB::rollback();

            info("CRON Auction Complete Error : ".print_r($e->getMessage(), true));

            return false;
        }
    }

     /**
     * @method payin_callback()
     *
     * @uses 
     *
     * @created Arun
     *
     * @updated  
     *
     * @param
     *
     * @return
     */

     public function payin_callback(Request $request) {

        info("Mobileware Response ".print_r($request->all(), true));

        try {

            $endpoint = config('app.mobileware.api_url');

            $response = Http::timeout(30)->post($endpoint, $request->all());

            return $response;

        } catch (Exception $e) {

            info("Payin Callback response Error : ".print_r($e->getMessage(), true));

            $response = ['success' => false, 'error' => $e->getMessage(), 'error_code' => 400];
        }
    }
}
Back to Directory File Manager