<?php if(!defined('ONGPHP'))exit('Error ONGSOFT');

plus( array('p','jiami','jianli','mima','shanchu','qcurl','qfopen','x','memcc','txtcc','db','isutf8','setsession','pagec','pinyin','ip','post','funciton','sslget','sslpost','vcode','update','mysqlcc') );



$D = db('');
//if(! isset( $_SERVER['HTTP_REFERER']) ) exit( 'Greetings to your family');
$Memsession =  $Mem = new txtcc();

$LANG = include QTLANG;
$Mem1 = new txtcc(ONGPHP.'/mode/');
$shezhi = $Mem1->g('shezhi');

$mch_appid = $shezhi['appid'];//公众账号appid

$mchid = $shezhi['mchid'];//商户号 1488033902

$nonce_str='whjc'.rand(100000, 999999);//随机数

$partner_trade_no='whjc'.time().rand(10000, 99999);//商户订单号

//$_SESSION['uid'] = 3142;

$fan = $D ->setbiao('user')->where( array("uid"=>$_SESSION['uid']))-> find();
$openid = $fan['openid']; //用户唯一标识,上一步授权中获取  o33R41o3lF8zDMx-giIqE_LOG-CQ

$check_name='NO_CHECK';//校验用户姓名选项，NO_CHECK：不校验真实姓名， FORCE_CHECK：强校验真实姓名（未实名认证的用户会校验失败，无法转账），OPTION_CHECK：针对已实名认证的用户才校验真实姓名（未实名认证用户不校验，可以转账成功）

$re_user_name=$fan['name'];//用户姓名

$amount=100;//企业金额，这里是以分为单位（必须大于100分）

$desc='亲，这是核对凭证(⊙o⊙)哦';//描述

$spbill_create_ip = '47.92.50.252';//请求ip

$dataArr=array();

$dataArr['amount']=$amount;

$dataArr['check_name']=$check_name;

$dataArr['desc']=$desc;

$dataArr['mch_appid']=$mch_appid;

$dataArr['mchid']=$mchid;

$dataArr['nonce_str']=$nonce_str;

$dataArr['openid']=$openid;

$dataArr['partner_trade_no']=$partner_trade_no;

$dataArr['re_user_name']=$re_user_name;

$dataArr['spbill_create_ip']=$spbill_create_ip;

$sign=getSign($dataArr);//getSign($dataArr);见结尾
//拼写正确的xml参数
$data="<xml>

<mch_appid>".$mch_appid."</mch_appid>

<mchid>".$mchid."</mchid>

<nonce_str>".$nonce_str."</nonce_str>

<partner_trade_no>".$partner_trade_no."</partner_trade_no>

<openid>".$openid."</openid>

<check_name>".$check_name."</check_name>

<re_user_name>".$re_user_name."</re_user_name>

<amount>".$amount."</amount>

<desc>".$desc."</desc>

<spbill_create_ip>".$spbill_create_ip."</spbill_create_ip>

<sign>".$sign."</sign>

</xml>";

$ch = curl_init ();

$MENU_URL="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

curl_setopt ( $ch, CURLOPT_URL, $MENU_URL );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );

//两个证书（必填，请求需要双向证书。）

$zs1="./ong/moudl/cert/apiclient_cert.pem";
$zs2="./ong/moudl/cert/apiclient_key.pem";

curl_setopt($ch,CURLOPT_SSLCERT,$zs1);

curl_setopt($ch,CURLOPT_SSLKEY,$zs2);

curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );

curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );

curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );



$info = curl_exec ( $ch );

if (curl_errno ( $ch )) {

    echo 'Errno' . curl_error ( $ch );
	exit;

}
curl_close ( $ch );
dump($info);

//作用：生成签名
function getSign($Obj)
{
    //var_dump($Obj);//die;
    foreach ($Obj as $k => $v)
    {
        $Parameters[$k] = $v;
    }
    //签名步骤一：按字典序排序参数
    ksort($Parameters);
    $String = formatBizQueryParaMap($Parameters, false);//方法如下
    //echo '【string1】'.$String.'</br>';
    //签名步骤二：在string后加入KEY
    $String = $String."&key=dfdsqwrerwxfdgasaedsrewweqeasfdg";//dfdsqwrerwxfdgasaedsrewweqeasfdg
    
    //echo "【string2】".$String."</br>";
    //签名步骤三：MD5加密
    $String = md5($String);
    //echo "【string3】 ".$String."</br>";
    //签名步骤四：所有字符转为大写
    $result_ = strtoupper($String);
    //echo "【result】 ".$result_."</br>";
    return $result_;
}

/**
 * 作用：格式化参数，签名过程需要使用
 */
function formatBizQueryParaMap($paraMap, $urlencode)
{
    //var_dump($paraMap);//die;
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v)
    {
        if($urlencode)
        {
            $v = urlencode($v);
        }
        //$buff .= strtolower($k) . "=" . $v . "&";
        $buff .= $k . "=" . $v . "&";
    }
    $reqPar = '';
    if (strlen($buff) > 0)
    {
        $reqPar = substr($buff, 0, strlen($buff)-1);
    }
    //var_dump($reqPar);//die;
    return $reqPar;
}
