<?php
include_once __DIR__ . '/../common/function.php';
$uid   = isset($_GET[ 'uid' ]) ? purge($_GET[ 'uid' ]) : '';
$order = isset($_GET[ 'order' ]) ? purge($_GET[ 'order' ]) : '';
if (empty($uid) || empty($order)) {
    exit(ReturnError('数据不完整,请重新发起支付'));
}
// 对比数据库信息
$old         = Db::table('app_order')
                 ->where(['pay_order' => $order])
                 ->find();
$old_user_id = json_decode($old[ 'pay_content' ] , true)[ 'user_id' ];
$old_data    = [
    'uid'         => $old_user_id ,
    'group_order' => $old[ 'pay_order' ]
];
$new_data    = [
    'uid'         => $uid ,
    'group_order' => $order
];
if ( !array_compare($old_data , $new_data)) {
    exit(ReturnError('订单信息不匹配，请重新创建订单:' . json_encode($old , 320)));
}
$pay_type = $old[ 'pay_type' ];
if (__BRANCH__) {
    // 易支付(包含微信、支付宝)
    $epay = new EpayCore([
        'apiurl' => __EPAY_URL__ ,
        'pid'    => __EPAY_APPID__ ,
        'key'    => __EPAY_KEY__ ,
    ]);
    exit($epay->pagePay([
        "pid"          => __EPAY_APPID__ ,
        "type"         => $pay_type ,
        "notify_url"   => URL . 'user/notify.php' ,
        "return_url"   => URL . 'user/return.php' ,
        "out_trade_no" => $old[ 'pay_order' ] ,
        "name"         => $old[ 'pay_name' ] ,
        "money"        => $old[ 'pay_money' ]
    ]));
} else {
    // 微信Native、支付宝当面付
    if ($pay_type === 'alipay') {
        $alipay = new Alipay();
        $alipay->setAppid(__ALI_APPID__);
        $alipay->setPrivateKey(__ALI_PRIVATE_KEY__);
        $alipay->setPublicKey(__ALI_PUBLIC_KEY__);
        $result = $alipay->qrcodePay($old[ 'pay_order' ] , $old[ 'pay_name' ] , $old[ 'pay_money' ] , URL . 'user/notify.php');
        if ($result[ 'alipay_trade_precreate_response' ][ 'code' ] === '40006') {
            exit($result[ 'alipay_trade_precreate_response' ][ 'sub_msg' ]);
        }
        // 创建session
        session_start();
        $data[ 'out_trade_no' ]         = $result[ 'alipay_trade_precreate_response' ][ 'out_trade_no' ];
        $data[ 'qr_code' ]              = $result[ 'alipay_trade_precreate_response' ][ 'qr_code' ];
        $data[ 'total_amount' ]         = $old[ 'pay_money' ];
        $data[ 'subject' ]              = $old[ 'pay_name' ];
        $data[ 'order_time' ]           = $old[ 'pay_time' ];
        $_SESSION[ 'alipayQrcodeData' ] = json_encode($data);
        header('Location:./getshop.php?trade_no=' . $old[ 'pay_order' ]);
        exit;

    } else if ($pay_type === 'wxpay') {
        $wxPay      = new WxpayService(__WX_MCHID__ , __WX_APPID__ , __WX_KEY__);
        $outTradeNo = $old[ 'pay_order' ];
        $payAmount  = $old[ 'pay_money' ];
        $orderName  = $old[ 'pay_name' ];
        $notifyUrl  = URL . 'user/getshop2.php';
        $payTime    = time();
        $arr        = $wxPay->createJsBizPackage($payAmount , $outTradeNo , $orderName , $notifyUrl , $payTime);
        session_start();
        $_SESSION[ 'wxPayQrcodeData' ] = [
            'order'   => $old[ 'pay_order' ] ,
            'money'   => $old[ 'pay_money' ] ,
            'name'    => $old[ 'pay_name' ] ,
            'qr_code' => $arr[ 'code_url' ] ,
            'time'    => date('Y-m-d H:i:s' , $payTime)
        ];
        header('Location:./getshop2.php?trade_no=' . $old[ 'pay_order' ]);
        exit;
    }
}