微信支付
*/
class wxpay{
private $pay_url = ‘https://api.mch.weixin.qq.com/pay/unifiedorder‘; // 统一支付地址
private $h5_pay_url = ‘https://api.mch.weixin.qq.com/pay/unifiedorder‘; // H5支付地址
private $h5_order = ‘https://api.mch.weixin.qq.com/pay/orderquery‘; // H5支付订单查询地址
private $refund_url = ‘https://api.mch.weixin.qq.com/secapi/pay/refund‘; // 退款地址
private $transfers = ‘https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers‘; // 企业付款到零钱 需支付证书
// 证书文件放在与入口文件同级的cert目录下
private $SSLCERT_PATH = ‘cert/apiclient_cert.pem‘; // api证书路径
private $SSLKEY_PATH = ‘cert/apiclient_key.pem‘; // 密钥证书路径
/**
@return [type] [description]
*/
public function pay($appid,$body,$mch_id,$notify_url,$openid,$order_no,$total_fee,$trade_type,$spbill_create_ip,$wx_key){
$post = [];
$nonce_str = $this->nonce_str();
//这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
$post[‘appid‘] = $appid; // 小程序APPid
$post[‘body‘] = $body; // 商品描述
$post[‘mch_id‘] = $mch_id; // 商户id
$post[‘nonce_str‘] = $nonce_str; // 随机字符串
$post[‘notify_url‘] = $notify_url; // 异步通知地址
$post[‘openid‘] = $openid; // 微信用户唯一标识符
$post[‘out_trade_no‘] = $order_no; // 商户自定义订单号
$post[‘spbill_create_ip‘] = $spbill_create_ip; // 终端的ip
$post[‘total_fee‘] = (int)$total_fee; // 总金额
$post[‘trade_type‘] = $trade_type; // 交易类型
$sign = $this->sign($post,$wx_key); //签名
$post_xml = ‘<xml>
<appid>‘.$appid.‘</appid>
<body>‘.$post[‘body‘].‘</body>
<mch_id>‘.$mch_id.‘</mch_id>
<nonce_str>‘.$nonce_str.‘</nonce_str>
<notify_url>‘.$notify_url.‘</notify_url>
<openid>‘.$openid.‘</openid>
<out_trade_no>‘.$order_no.‘</out_trade_no>
<spbill_create_ip>‘.$spbill_create_ip.‘</spbill_create_ip>
<total_fee>‘.(int)$total_fee.‘</total_fee>
<trade_type>‘.$trade_type.‘</trade_type>
<sign>‘.$sign.‘</sign>
</xml> ‘;
$xml = $this->http_request($this->pay_url,$post_xml);
// 将xml转数组
$arr = simplexml_load_string($xml, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
$xmljson = json_encode($arr);
$array = json_decode($xmljson,true);
if ($array[‘return_code‘] == ‘SUCCESS‘ && $array[‘result_code‘] == ‘SUCCESS‘) {
$time = time();
$tmp = ‘‘;//临时数组用于签名
$tmp[‘appId‘] = $appid;
$tmp[‘nonceStr‘] = $nonce_str;
$tmp[‘package‘] = ‘prepay_id=‘.$array[‘prepay_id‘];
$tmp[‘signType‘] = ‘MD5‘;
$tmp[‘timeStamp‘] = "$time";
$data[‘code‘] = 1;
$data[‘msg‘] = ‘成功!‘;
$data[‘data‘][‘timeStamp‘] = "$time"; // 时间戳
$data[‘data‘][‘nonceStr‘] = $nonce_str; // 随机字符串
$data[‘data‘][‘signType‘] = ‘MD5‘; // 签名算法,暂支持 MD5
$data[‘data‘][‘package‘] = ‘prepay_id=‘.$array[‘prepay_id‘]; // 统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
$data[‘data‘][‘paySign‘] = $this->sign($tmp,$wx_key); // 签名,具体签名方案参见微信公众号支付帮助文档;
$data[‘data‘][‘out_trade_no‘] = $order_no;
}else{
$data[‘code‘] = 0;
$data[‘msg‘] = $array[‘err_code_des‘];
$data[‘data‘][‘return_code‘] = $array[‘return_code‘]; // 微信支付返回码
$data[‘data‘][‘return_msg‘] = $array[‘err_code_des‘]; // 微信支付返回消息
}
return $data;
}
/**
原文:https://blog.51cto.com/13346331/2488859