文档
注:开放平台的微信支付和公众号的微信支付是不一样的,公众平台和开放平台的支付申请下来之后会有各自对应的商户平台账号
function wechat($appid,$mchid,$appkey,$cert_path,$key_path,$order_id,$openid,$amount,$desc){
$arr = [
'mch_appid'=>$appid,//注意区分公众号和app商户号不同
'mchid'=>$mchid,
'nonce_str'=>str_random(32),//随机数
'partner_trade_no'=>$order_id,//自己定义一个不重复订单号
'openid'=>$openid,//微信openid 通过微信授权登录获取
'check_name'=>'NO_CHECK',
'amount'=>$amount*100,//注意这里传给微信的单位是分
'desc'=>$desc,
'spbill_create_ip'=>\Request::getClientIp(),
'sign'=>'',
];
ksort($arr);
$sign="";
foreach ($arr as $key => $value) {
if($value && $key!="sign" && $key!="key"){
$sign.=$key."=".$value."&";
}
}
$sign.="key=".$appkey;//商户后台自定义的
$arr['sign'] = strtoupper(md5($sign));
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_URL,'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//默认格式为PEM
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,$cert_path);//注意区分公众号和app商户号的证书不同,需要到pay.weixin.qq.com后台下载
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,$key_path);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
$data = curl_exec($ch);
$data = json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
curl_close($ch);
return $data;//$data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS' 支付成功
}
}
function https_request($url,$type="get",$res="json",$data = ''){
//1.初始化curl
$curl = curl_init();
//2.设置curl的参数
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($type == "post"){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
//3.采集
$output = curl_exec($curl);
//4.关闭
curl_close($curl);
if ($res == 'json') {
return json_decode($output,true);
}
}
function getWxAccessToken(){
if ( $_SESSION['access_token'] && $_SESSION['expire_time'] > time() ) {
//未过期
return $_SESSION['access_token'];
}else {
$appid = "我的id";
$appsecret = "我的appsecret ";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res =$this->https_request($url,'get','json');
$access_token = $res["access_token"];
//将重新获取到的access_token存到session里
$_SESSION['access_token']=$access_token;
$_SESSION['expire_time']=time()+7200;
return $access_token;
}
}
//上传图片素材(该方法只能获取到图片的URL)
function addMaterialUrl() {
$access_token = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
$filename = '我是图片';
$data=array("media"=>'@'. $filename);
$res=$this->https_request( $url ,'post', 'json', $data);
//dump($res); exit();
return $res['url'];
}
php5.6加上curl_setopt ($curl, CURLOPT_SAFE_UPLOAD, false);或者直接"media" => new CURLFile(realpath($real_path)), https://segmentfault.com/a/1190000000725185
附:
https://youqingkui.me/note/e5...
python版微信支付
http://bblove.me/2015/10/25/w...
微信APP支付服务端php sdk开发教程
https://github.com/fanhefan/w...
微信红包API接口
http://jeffchen.sinaapp.com/
http://tao.logdown.com/posts/...
微信支付 一步一个坑的APP支付
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。