require_once "include/lib/Api.class.php";
require_once "include/lib/JsApiPay.class.php";
require_once "include/lib/PayNotifyCallBack.class.php";
require_once "include/lib/Config.class.php";
require_once "include/lib/JSSDK.class.php";
class WeiPay
{
//企业向个人付款
public static function payToUser($data)
{
//微信付款到个人的接口 前4个参数,去微信平台获取
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
$params["mch_appid"] = 'wxed179f31e9bd43**'; //公众账号appid
$params["mchid"] = '153660**11'; //商户号 微信支付平台账号
$params['key'] = '8ffbb9de88a5****edd1a14858b0'; //对应的商户号的key 秘钥
$params["nonce_str"] = 'aedwnewe'.mt_rand(100,999); //随机字符串
$params["partner_trade_no"] = mt_rand(10000000,99999999); //商户订单号
$params["amount"] = $data['pay_amount']; //金额
$params["desc"] = $data['order_title']; //企业付款描述
$params["openid"] = $data['openID']; //用户openid
$params["check_name"] = 'NO_CHECK'; //不检验用户姓名 固定值 可以
$params['spbill_create_ip'] = '101.37.163.73'; //获取IP 服务器ip
//生成签名(签名算法后面详细介绍)
$str = 'amount='.$params["amount"].'&check_name='.$params["check_name"].'&desc='.$params["desc"].
'&mch_appid='.$params["mch_appid"].'&mchid='.$params["mchid"].'&nonce_str='.$params["nonce_str"].
'&openid='.$params["openid"].'&partner_trade_no='.$params["partner_trade_no"].'&spbill_create_ip='.
$params['spbill_create_ip'].'&key='.$params['key'];
//md5加密 转换成大写
$sign = strtoupper(md5($str));
$params["sign"] = $sign;//签名
$xml = self::arrayToXml($params);
return self::curl_post_ssl($url, $xml);
}
//遍历数组方法
public static function arraytoxml($data){
$str='<xml>';
foreach($data as $k=>$v) {
$str.='<'.$k.'>'.$v.'</'.$k.'>';
}
$str.='</xml>';
return $str;
}
public static function xmltoarray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
//上个方法中用到的curl_post_ssl()
public static function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
{
$isdir = "/www/wwwroot/qbb/tapi/storage/cert/";//证书位置
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
}
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送
$data = curl_exec($ch);//执行回话
if ($data) {
curl_close($ch);
$res = self::xmltoarray($data);
return $res;
} else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
}
注意:
1,金额必须是整数,最低是30=3毛钱
2,用公众号的APPID,对应的商户号 和key秘钥
3,对应的公众号的openID
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。