<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:23
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace app\api\controller\v1;

use app\BaseController;
use app\model\api\PayTrade;
use app\model\api\Recharge;
use app\model\api\Site;
use app\model\api\Warehouse;
use app\Request;
use library\services\UtilService;
use library\utils\alipay;

class Ott extends BaseController
{


    public function alipay(Request $request) {
        $post =  UtilService::getMore([
            ['order_id','0','empty','参数错误']
        ],$request);
        $recharge = new Recharge();
        $data = $recharge->where('order_id',$post['order_id'])->find();
        if(empty($data)) {
            return app('json')->fail('数据错误');
        }

        if(!empty($data['status'])) {
            return app('json')->fail('订单已经支付');
        }
        $alipay = new alipay();
        $data = $alipay->wapPay([
            'name'  => '在线充值'.$data['v'].'元',
            'money' => $data['v'],
            'order_id' => $data['order_id']
        ]);
        echo $data;
    }


    /**
     * 支付宝返回
     */
    public function alipayNotifyUrl(){
        $alipay = new alipay();
        $book = $alipay->verifyNotify($_POST);
        if(empty($book)) {
            return "fail";
        }
        $out_trade_no = $_POST['out_trade_no'];
        $payTrade = new PayTrade();
        $data = $payTrade->where('out_trade_no',$out_trade_no)->find();
        if(empty($data)) {
            return 'success';
        }
        if($data['status'] == 1) {
            return 'success';
        }
        //支付成功
        $queryStr = $alipay->query($out_trade_no)->httpBody;
        $qAr = json_decode($queryStr,true);
        if($qAr['alipay_trade_query_response']['trade_status'] == 'TRADE_CLOSED') {
            return "fail";
        }
        //充值费用
        if($data['type'] == 'recharge') {
            $recharge = new Recharge();
            $recharge->rechargeSuccess($out_trade_no);
        }
        $payTrade->where('id',$data['id'])->save(['status'=>1,'pay_time'=>time()]);
        return 'success';
    }

    /**
     * 支付返回
     * @param Request $request
     */
    public function alipayReturnUrl(Request $request){
        [$out_trade_no] = UtilService::getMore([
            ['out_trade_no','','empty','参数错误']
        ],$request,true);
        $payTrade = new PayTrade();
        $data = $payTrade->where('out_trade_no',$out_trade_no)->find();
        if(empty($data)) {
            $this->assign('error',"找不到订单数据");
            return  $this->display('alipay_error');
        }
        $this->assign('data',$data);
        echo $this->display('alipay_ok');

    }



    // 打印log
    function log_result($file, $word) {
        $fp = fopen($file, "a");
        flock($fp, LOCK_EX);
        fwrite($fp, "执行日期：" . strftime("%Y-%m-%d-%H：%M：%S", time()) . "\n" . $word . "\n\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }





}
