<?php

namespace app\api\controller;

use app\admin\model\PayMember;
use app\admin\model\ToBalance;
use app\common\controller\Api;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\inspect;
use think\Db;
use app\admin\model\User;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
use function EasyWeChat\Kernel\Support\generate_sign;
/**
 * 支付接口
 */
class Pay extends Api
{
    protected  $noNeedLogin = ['payMemberNotify','paySecurityNotify','payOrderNotify'];
    protected $noNeedRight = ['*'];
    protected $uid = '';//token存贮uid
    protected $order_status = [];//订单状态
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (充值/支付订单)
     * @ApiSummary  (充值支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="openid", type="string", required=true, description="小程序openid")
     * @ApiParams   (name="num", type="inter", required=false, description="支付订单号")
     *
     */
    public function pay(){
        if($this->request->isPost()){
            $openid = $this->request->post('openid');
            $order_sn = $this->request->post('num');

            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            if($order_sn){
                //支付订单
                $res = Db::name('order')->where(['num'=>$order_sn,'uid'=>$this->uid])->find();
                if($res){
                    if($res['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //创建支付对象
                        $config = config('verify.wx_pay');
                        $app = Factory::payment($config);
                        $result = $app->order->unify([
                            'body' => '山东航拍',
                            'out_trade_no' => $order_sn,//支付订单号
                            'total_fee' => $money*100,//单位分
                            'notify_url' => $notify_url, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
                            'openid' => $openid,
                        ]);
                        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                            $params = [
                                'appId'     => $config['app_id'],
                                'timeStamp' => time(),
                                'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                                'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                                'signType'  => 'MD5', // 签名方法
                            ];
                            // 注意这里用的是商户平台的Key进行二次签名
                            $params['paySign'] = generate_sign($params, $config['key']);
                            $this->success('成功',$params);
                        }
                        $this->error($result['err_code_des']);
                    }
                }else{
                    $this->error('无效的订单');
                }
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    //购买会员回调通知(无需调用)
    public function payMemberNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        Log::info('会员');
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            Log::info($message);
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功
                    //更新成为会员
                    $userModel = new User();
                    $userModel->where('openid',$message['openid'])->update(['member'=>1]);
                    //增加记录
                    $payMemberModel = new PayMember();
                    $payMemberModel->create(['openid'=>$message['openid'],'pay_order'=>$message['out_trade_no']]);
                }elseif($message['result_code'] === 'FAIL') {
                    //支付失败
                }
            } else {
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }

    //支付订单(无需调用)
    public function payOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $orderModel = new \app\admin\model\Order();
                    $orderModel->where(['order_sn'=>$message['out_trade_no']])->update(['status'=>1]);

                    //创建订房记录
                    $this->auth->createHouseRecord($message['out_trade_no']);

                }elseif($message['result_code'] === 'FAIL') {
                    //支付失败
                }
            } else {
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }


    /**
     * @ApiTitle    (更新订单号(支付订单失败后调用))
     * @ApiSummary  (更新订单号(支付订单失败后调用))
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/updateOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_sn", type="inter", required=false, description="支付订单号")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1571812329",
        "data": null
    })
     */
    public function updateOrder(){
        if($this->request->isPost()){
            $order_sn = $this->request->post('order_sn');

            $rule = config('verify.order_sn');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['order_sn'=>$order_sn])) {
                $this->error($validate->getError());
            }

            $orderModel = new \app\admin\model\Order();
            $pay_order = $this->auth->genPayOrderSn('pay');//支付订单号
            $res = $orderModel->where(['order_sn'=>$order_sn,'uid'=>$this->uid])->update(['order_sn'=>$pay_order]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (提现到微信零钱)
     * @ApiSummary  (提现到微信零钱)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/payBalance)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="openid", type="string", required=true, description="小程序openid")
     * @ApiParams   (name="price", type="inter", required=true, description="提现金额")
     */
    public function payBalance(){
        if($this->request->isPost()){
            $price = $this->request->post('price');
            $openid = $this->request->post('openid');

            $rule = config('verify.to_balance');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['price'=>$price,'openid'=>$openid])) {
                $this->error($validate->getError());
            }

            if($price < 0.3){
                $this->error('提现金额不能低于0.3元');
            }

            //每天提现次数不超过三次
            $count = Db::name('to_balance')->where('uid',$this->uid)->whereTime('createtime','today')->count();
            if($count >= 3){
                $this->error('每天提现次数不能超过3次');
            }
            //查询该用户钱包金额
            $userModel = new User();
            $user = $userModel->where(['id'=>$this->uid])->field('id,money,nickname')->find();
            if($price > $user['money']){
                $this->error('钱包金额不足');
            }

            //创建支付对象
            $config = config('verify.wx_pay');
            $app = Factory::payment($config);
            $balance_order = $this->auth->genPayOrderSn('balance');//提现单号
            $result = $app->transfer->toBalance([
                'partner_trade_no' => $balance_order, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
                'openid' => $openid,
                'check_name' => 'NO_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
                're_user_name' => '', // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
                'amount' => $price*100, // 企业付款金额,单位为分
                'desc' => '提现到零钱', // 企业付款操作说明信息。必填
            ]);
            if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                //创建提现记录
                $balanceRecordModel = new ToBalance();
                $data['uid'] = $this->uid;
                $data['openid'] = $openid;
                $data['price'] = $price;
                $data['to_order'] = $result['partner_trade_no'];
                $balanceRecordModel->create($data);
                //更新余额表
                $userModel = new User();
                $userModel->where(['id'=>$this->uid])->setDec('money',$price);
                $this->success('成功');
            }
            $this->error($result['err_code_des']);
        }else{
            $this->error('请求方式错误');
        }
    }
}