<?php


namespace App\Http\Controllers\Game;


use App\Facade\TableName;
use App\Http\Controllers\Api\BroQrController;
use App\Services\PayMentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class PaymentEntryController {
    // 新支付总入口---区分签名
    public function pay(Request $request) {
//        return apiReturnFail(['web.payment.paytype_error','PayType ERROR']);
        // 获取数据
        $userId = (int)$request->globalUser->UserID;//(int)$request->input('userID');
        $payAmt = $request->input('payAmt');
        $userName = $request->input('userName');
        $userEmail = $request->input('userEmail');
        $userPhone = $request->input('userPhone');
        $pay_type = (int)$request->input('pay_type',28);
        $pay_method = $request->input('pay_method', 1);
        $GiftsID = (int)$request->input('GiftsID');
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');
        $buyIP = $request->ip();
        if ($GiftsID == 'undefined') $GiftsID = '';

        $where[] = ['type', 'pay_method'];
        $where[] = ['status', 1];
        $where[] = ['id', $pay_type];

//        $payMethods = [
//            'cashapp' => 1,
//            'paypal' => 2,
//            'applepay' => 4,
//            'googlepay' => 8,
//
//        ];


        $query = DB::table('agent.dbo.admin_configs')->where($where)->value('config_key');

        if ($query === null || $query === false) {
            return apiReturnFail(['web.payment.paytype_error','PayType ERROR']);
        }
        $channel = $request->globalUser->Channel;
        $payServiceString = PayMentService::getServiceByPayMethod($query, $payAmt,$pay_method);


        Log::info('channel', [
            'string' => $payServiceString
        ]);
        $service = PayMentService::pay_order($payServiceString);

        if(isset($AdId)&&!empty($AdId))Redis::set('user_ad_'.$userId,$AdId);
        $result = $service->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method);
        return $result;
    }
    // 新支付总入口---区分签名
    public function payQR(Request $request) {
        // 获取数据
        $userId = (int)$request->globalUser->UserID;//(int)$request->input('userID');
        $payAmt = (int)$request->input('payAmt');
        $userName = $request->input('userName');
        $userEmail = $request->input('userEmail');
        $userPhone = $request->input('userPhone');
        $pay_type = (int)$request->input('pay_type');
        $GiftsID = (int)$request->input('GiftsID');
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');
        $buyIP = $request->ip();

        $pay_method = (int)$request->input('pay_method','clabe');

        if ($GiftsID == 'undefined') $GiftsID = '';


        Log::info('channel', [
            'string' => 'BroQrController'
        ]);
        $service =new BroQrController();

        if(isset($AdId)&&!empty($AdId))Redis::set('user_ad_'.$userId,$AdId);
        $result = $service->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType);
        return $result;
    }
    // 新支付总入口---区分签名
    public function iospay(Request $request) {
        // 获取数据
        $userId =(int)$request->globalUser->UserID;// (int)$request->input('userID');
        $payAmt = (int)$request->input('payAmt');
        $userName = $request->input('userName');
        $userEmail = $request->input('userEmail');
        $userPhone = $request->input('userPhone');
        $pay_type = (int)$request->input('pay_type');
        $GiftsID = (int)$request->input('GiftsID');
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');
        $buyIP = $request->ip();
        if ($GiftsID == 'undefined') $GiftsID = '';




        $service = PayMentService::pay_order('apple');

        $result = $service->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType);
        return $result;
    }
    // 新支付总入口---区分签名
    public function googlepay(Request $request) {
        // 获取数据
        $userId = (int)$request->globalUser->UserID;//(int)$request->input('userID');
        $payAmt = (int)$request->input('payAmt');
        $userName = $request->input('userName');
        $userEmail = $request->input('userEmail');
        $userPhone = $request->input('userPhone');
        $pay_type = (int)$request->input('pay_type');
        $GiftsID = (int)$request->input('GiftsID');
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');
        $buyIP = $request->ip();
        if ($GiftsID == 'undefined') $GiftsID = '';




        $service = PayMentService::pay_order('google');

        $result = $service->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType);
        return $result;
    }
}
