<?php


namespace App\Http\Controllers\Api;


use App\Models\Order;
use App\Services\FoxPay;
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) {
        // 获取数据
        $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');
        $pay_method = $request->input('pay_method', '');
        $GiftsID = (int)$request->input('GiftsID');
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');//俄罗斯支付传递特别参数
        $buyIP = $request->ip();
        if ($GiftsID == 'undefined') $GiftsID = '';

        if(in_array(intval($GiftsID),[201,211,212])){
            $query = DB::connection('write')->table('QPAccountsDB.dbo.YN_VIPAccount')
                ->where('UserID', $userId)
                ->value('Recharge');
            if($query)return apiReturnFail(__('messages.api.payment.paytype_error_simple'));
        }

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

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

        if ($query === null || $query === false) {
            return apiReturnFail(__('messages.api.payment.paytype_error_simple'));
        }
        //用户需要适配对应的国家充值渠道
        if(env('PAYMENT_BIND_TO_COUNTRY',0)){
            $user = DB::table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)->select('BindCountry','Channel')->first();

            $payServiceString = PayMentService::getServiceByPayCountry($query, $user->Channel, $user->BindCountry);
        }else{
            $channel = DB::table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)
                ->value('Channel');

            $payServiceString = PayMentService::getServiceByPayMethod($query, $channel);
        }


        if(isset($AdId)&&!empty($AdId))Redis::set('user_ad_'.$userId,$AdId);

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

         if($userId == 4755057){
             $payServiceString = 'SmartFastPay';
         }

        $service = PayMentService::pay_order($payServiceString);

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

        //出现错误，再试一次
        if($result['code']!=200&&$result['msg']!='needphone'){

            //用户需要适配对应的国家充值渠道
            if(env('PAYMENT_BIND_TO_COUNTRY',0)){
                $user = DB::table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)->select('BindCountry','Channel')->first();

                $payServiceString = PayMentService::getServiceByPayCountry($query, $user->Channel, $user->BindCountry,$payServiceString);
            }else{
                $channel = DB::table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)
                    ->value('Channel');

                $payServiceString = PayMentService::getServiceByPayMethod($query, $channel,$payServiceString);
            }
            Log::info('channel retry', [
                'string' => $payServiceString
            ]);

            $service = PayMentService::pay_order($payServiceString);

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

        return $result;
    }
    // 新支付总入口---区分签名
    public function iospay(Request $request) {
        // 获取数据
        $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->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;
    }
    // 新支付总入口---区分签名
    /*public function pay2(Request $request) {
        // 获取数据
        $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 = '';


        // 验证是不是10卢币首充礼包
        if ($GiftsID == 200) {
            // 查询状态
            $first = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                ->where('StatusName', 'FirstChargeGiftBagStatus')
                ->first();

            if ($first->StatusValue != 1) return apiReturnFail(['web.payment.activity_closed',__('messages.api.payment.activity_closed')]);

            // 首充
            $firstCharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                ->where('StatusName', 'FirstChargeGiftBag')
                ->first();
            $pay_type = $firstCharge->StatusValue;
        }

        if (true) {
            $where[] = ['type', 'pay_method'];
            $where[] = ['status', 1];
            $where[] = ['id', $pay_type];

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

            if ($query === null || $query === false) {
                return apiReturnFail(__('messages.api.payment.paytype_error_simple'));
            }
            $channel = DB::table('QPAccountsDB.dbo.AccountsInfo')->where('UserID', $userId)
                ->value('Channel');
            $payServiceString = PayMentService::getServiceByPayMethod3($query, $channel);
            Log::info('channel', [
                'string' => $payServiceString
            ]);
        }
        $service = PayMentService::pay_order($payServiceString);

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