<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use alipay\Alipaysubmit;
use alipay\Alipaynotify;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://mifenlei.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Alipay extends Controller
{
    protected function _initialize()
    {
        $this->alipay_config['partner'] = set('partner', 2);
        //收款支付宝账号，以2088开头由16位纯数字组成的字符串，一般情况下收款账号就是签约账号
        $this->alipay_config['seller_id'] = $this->alipay_config['partner'];
        // MD5密钥，安全检验码，由数字和字母组成的32位字符串，查看地址：https://b.alipay.com/order/pidAndKey.htm
        $this->alipay_config['key'] = set('key', 2);
        // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
        $this->alipay_config['return_url'] = 'http://' . $_SERVER['HTTP_HOST'] . getbaseurl() . '/index.php/index/alipay/return_url.html';
        //签名方式
        $this->alipay_config['sign_type'] = strtoupper('MD5');
        //字符编码格式 目前支持 gbk 或 utf-8
        $this->alipay_config['input_charset'] = strtolower('utf-8');
        //ca证书路径地址，用于curl中ssl校验
        //请保证cacert.pem文件在当前文件夹目录中
        $this->alipay_config['cacert'] = getcwd() . '\\cacert.pem';
        //访问模式,根据自己的服务器是否支持ssl访问，若支持请选择https；若不支持请选择http
        $this->alipay_config['transport'] = 'http';
        // 支付类型 ，无需修改
        $this->alipay_config['payment_type'] = "1";
        // 产品类型，无需修改
        $this->alipay_config['service'] = "create_direct_pay_by_user";
        //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
        //↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息，如果没开通防钓鱼功能，为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        // 防钓鱼时间戳  若要使用请调用类文件submit中的query_timestamp函数
        $this->alipay_config['anti_phishing_key'] = "";
        // 客户端的IP地址 非局域网的外网IP地址，如：221.0.0.1
        $this->alipay_config['exter_invoke_ip'] = "";
        //↑↑↑↑↑↑↑↑↑↑请在这里配置防钓鱼信息，如果没开通防钓鱼功能，为空即可 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
        //商户订单号，商户网站订单系统中唯一订单号，必填
    }
    public function return_url()
    {
        $alipayNotify = new AlipayNotify($this->alipay_config);
        $verify_result = $alipayNotify->verifyReturn();
        if ($verify_result) {
            $out_trade_no = $_GET['out_trade_no'];
            $trade_no = $_GET['trade_no'];
            $trade_status = $_GET['trade_status'];
            $map['id'] = $out_trade_no;
            $info = Db::name('dingdan')->where($map)->find();
            if ($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                if ($info['status'] != 1) {
                    Db::name('dingdan')->where($map)->update(['trade_no' => $trade_no, 'status' => 1]);
                    //添加积分记录
                    point($info['score'], session('userid'), set('pointtitle', 2));
                    point(set('jifen5', 2), session('userid'), '充值额外返现');
                    if (set('open11',4)==1) {
                        // 邮件发送
                        send_mail(set('mailUsername',1), '【' . set('title') . '】新的充值订单', '您网站有新的用户进行了积分充值');
                    }
                }
            }
            $this->success('支付成功,正在跳转', url('index/user/chongzhi'));
        } else {
            $this->error('支付失败', '/');
        }
    }
    //创建订单
    public function paysubmit()
    {
        $pay = $this->request->param();
        if ($pay['jiage'] < set('pointzs', 2)) {
            $this->error('充值金额低于最小金额');
        }
        $out_trade_no = generate_password(16) . time();
        $rmb = $pay['jiage'];
        $subject = set('pointtitle', 2);
        $body = set('pointtitle', 2);
        $data['id'] = $out_trade_no;
        $data['trade_no'] = 0;
        $data['status'] = 0;
        $data['tid'] = 0;
        $data['jiage'] = $rmb;
        $data['uid'] = session('userid');
        $data['add_time'] = time();
        $data['errorcode'] = 0;
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['score'] = $rmb * set('bili', 2);
        Db::name('dingdan')->insert($data);
        $parameter = array("service" => $this->alipay_config['service'], "partner" => $this->alipay_config['partner'], "seller_id" => $this->alipay_config['seller_id'], "payment_type" => $this->alipay_config['payment_type'], "return_url" => $this->alipay_config['return_url'], "anti_phishing_key" => $this->alipay_config['anti_phishing_key'], "exter_invoke_ip" => $this->alipay_config['exter_invoke_ip'], "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $rmb, "body" => $body, "_input_charset" => trim(strtolower($this->alipay_config['input_charset'])));
        //建立请求
        $alipaySubmit = new AlipaySubmit($this->alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认");
        $this->success('等待跳转', $html_text);
    }
}