<?php
namespace app\mobile\controller;
use think\Request;
class Fubei extends MobileBase {

    /**
     * 方法描述：付呗 调用网页授权接口
     * @author Zhaojiepeng<zhaojiepeng@suxuantech.cn> 2018年6月4日 14:17:38
     */
    public function fubei(){
        header("Content-type:text/html;charset=utf-8");
        $order_id = I('order_id/d'); //订单id
        $money = I('account');

        //随机字符串
        $str="1234567890qwertyuiopasdfghjklzxcvbnm";
        $str_rand=substr(str_shuffle($str),26,10);
    
        //网页授权接口
        $url = "http://".$_SERVER['HTTP_HOST']."/index.php/Mobile/Fubei/fubei_getpay?order_id=".$order_id."&account=".$money;
        $biz_content = json_encode(['store_id'=>377352,'url'=>$url]);
        $sign = 'app_id=20180528164650586571&biz_content='.$biz_content.'&format=json&method=openapi.payment.auth.auth&nonce='.$str_rand.'&sign_method=md5&version=1.07929063db1577e76d0817c4f6cb8cbef';
        $sign = strtoupper(md5($sign)); //签名
        $data = array(
            "biz_content"=>$biz_content,
            "method"=>"openapi.payment.auth.auth",
            "format"=>"json",
            "sign_method"=>"md5",
            "sign"=>$sign,
            "app_id"=>"20180528164650586571",
            "nonce"=>$str_rand,
            "version"=>"1.0"
        );
        $data = json_encode($data);
        $res = "https://shq-api.51fubei.com/gateway";
        $arr = http_request($res,'POST',$data,array('Content-Type: application/json'));
        $a = json_decode($arr['response'],true);
        $b = $a['data']['authUrl'];     //返回的authUrl
        header('Location: '.$b);die;
    }
    /**
     * 方法描述：付呗 微信公众号支付接口
     * @author Zhaojiepeng<zhaojiepeng@suxuantech.cn> 2018年6月4日 19:17:38
     */
    public function fubei_getpay(){
        $open_id = input("open_id");        //公众号授权id
        $sub_open_id = input("sub_open_id");//支付帐号授权id
        $account = input("account");
        $order_id = input("order_id");
        //随机字符串
        $str="1234567890qwertyuiopasdfghjklzxcvbnm";
        $str_rand=substr(str_shuffle($str),26,10);
        $url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Mobile/Fubei/fubei_log';        
        //生成签名        
        $biz_content = json_encode(['call_back_url'=>$url,'merchant_order_sn'=>$order_id,'open_id'=>$open_id,'store_id'=>377352,'sub_open_id'=>$sub_open_id,'total_fee'=>$account,]);        $sign = 'app_id=20180528164650586571&biz_content='.$biz_content.'&format=json&method=openapi.payment.order.h5pay&nonce='.$str_rand.'&sign_method=md57929063db1577e76d0817c4f6cb8cbef';
        $sign = strtoupper(md5($sign)); //签名
        //支付接口
        $data = array(
            "app_id"=>"20180528164650586571",
            "method"=>"openapi.payment.order.h5pay",
            "format"=>"json",
            "sign_method"=>"md5",
            "nonce"=>$str_rand,
            "biz_content"=>$biz_content,
            "sign"=>$sign
        );
        $data = json_encode($data);
        $res = "https://shq-api.51fubei.com/gateway";
        $arr = http_request($res,'POST',$data,array('Content-Type: application/json'));
        $pay_data = json_decode($arr['response'],true);
        
        //拉起支付
        $prepay_id = $pay_data['data']["prepay_id"];    //预支付订单号
        $order_sn = $pay_data['data']["order_sn"];      //付呗订单号
        $merchant_order_sn = $pay_data['data']["merchant_order_sn"];//第三方订单号（自己生成的订单号）
        $callback_url = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Mobile/Order/order_detail?id='.$merchant_order_sn; //支付成功跳转地址
        $cancel_url   = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/Mobile/Cart/cart4?order_id='.$merchant_order_sn; //支付失败跳转地址
        $res2 = 'https://shq-api.51fubei.com/paypage?prepay_id='.$prepay_id.'&callback_url='.$callback_url.'&cancel_url='.$cancel_url;
        header('Location: '.$res2);die;
    }
    
    /**
     * 方法描述：支付成功校验
     * @author Zhaojiepeng<zhaojiepeng@suxuantech.cn> 2018年6月11日 9:51:38
     */
    public function fubei_log(){
        $result_code = input("result_code");        //返回的状态码
        $result_message = input("result_message");  //返回的状态
        $result_sign = input("sign");               //返回的签名
        $data        = htmlspecialchars_decode(input("data"));//返回的data
        $sign        = "data=".$data."&result_code=".$result_code."&result_message=".$result_message."7929063db1577e76d0817c4f6cb8cbef";
        $data        = json_decode($data,true);
        if($result_code == 200){                    //1、判断状态
            $sign = strtoupper(md5($sign));                       //2、判断签名
            if($sign == $result_sign){
                $order = M('order')->where(array('order_id'=>$data['merchant_order_sn']))->find();
                if($data['total_fee'] == $order['order_amount']){ //3、判断金额
                    update_pay_status($order['order_sn']);
                    $order_id = input('order_id');     //订单号
                    $create_time = time();             //时间
                    $user_id = session('id');          //用户id
                    $account = input("account");       //金额
                    $c = [
                	       'order_id'=>$order_id,
                	       'create_time'=>$create_time,
                	       'user_id'=>$user_id,
                	       'account'=>$account
                    ];
                    Db::name("payment_log")->add($c);
                }    
           }
        }
    }
}