<pre>
<?php
/**
- Created by PhpStorm.
- User: Administrator
- Date: 2016/9/8 0008
- Time: 11:09
*/
namespace App\Controller;
use Think\Controller;
class WeChatPaymentEnterprisesController extends BaseController{
protected $payurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
protected $appid = 'wx******************4'; //appid
protected $mchid = '****************';商户号
protected $signkey ='***************';//API密钥需要自己设置
public function pay($data){
$dataXml="<xml>
<mch_appid>".$data['mch_appid']."</mch_appid>
<mchid>".$data['mchid']."</mchid>
<nonce_str>".$data['nonce_str']."</nonce_str>
<partner_trade_no>".$data['partner_trade_no']."</partner_trade_no>
<openid>".$data['openid']."</openid>
<check_name>".$data['check_name']."</check_name>
<re_user_name>".$data['re_user_name']."</re_user_name>
<amount>".$data['amount']."</amount>
<desc>".$data['desc']."</desc>
<spbill_create_ip>".$data['spbill_create_ip']."</spbill_create_ip>
<sign>".$data['sign']."</sign>
</xml>";
return $this->getSslCurl($this->$payurl,$dataXml);
}
public function getSslCurl($url,$data){
$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="E:/apiclient_cert.pem";//地址写绝对路径和相对路径都行 被文档搞死了
$zs2="E:/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 );
}
curl_close ( $ch );
return $info;
}
/**
* 作用:格式化参数,签名过程需要使用
*/
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;
}
/**
* 作用:生成签名
*/
function getSign($Obj)
{
var_dump($Obj);//die;
foreach ($Obj as $k => $v)
{
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'</br>';
//签名步骤二:在string后加入KEY
$String = $String."&key=".$this->signkey;
//echo "【string2】".$String."</br>";
//签名步骤三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}
//测试demo
public function test(){
//封装成数据
$dataArr=array();
$dataArr['amount']=10;//金额
$dataArr['check_name']='NO_CHECK';//校验用户姓名选项,NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
$dataArr['desc']='测试数据';//描述
$dataArr['mch_appid']=$this->$appid;//公众平台的appid
$dataArr['mchid']=$this->mchid;
$dataArr['nonce_str']=date('Ymdhis').rand(100000, 999999);//随机数
$dataArr['openid']="o_HLfsl_TImUtXUE4V1okwMbOO7g";//用户唯一标识
$dataArr['partner_trade_no']='HW'.time().rand(10000, 99999);//商户订单号
$dataArr['re_user_name']="测试";//用户姓名
$dataArr['spbill_create_ip']=$_SERVER["REMOTE_ADDR"];//测试ip
$dataArr['sign'] = $this->getSign($dataArr);
$res = $this->pay($dataArr);
var_dump($res);
}
}
</pre>