先說一下有企業公衆號的情況
1.登錄微信公衆號https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN:開發>接口權限>功能服務>微信支付>微信支付支付接口 (如果沒有建議申請爲企業公用號)
微信支付 | 微信支付接口 | - |
已獲得 |
微信支付 | 發起一個微信支付請求 | 無上限 | 已獲得 |
這兩處開通狀態
2.點擊微信支付接口https://pay.weixin.qq.com/wiki/doc/api/index.html
選着你的項目類型
點開你要的項目類型:比如 H5支付 app支付(不要關閉這個頁面)
然後點擊:https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2Findex.php
註冊 好後開始 設置 api
配置路徑:
● API密鑰(KEY):用於接口API的簽名計算
配置路徑:【登錄商戶平臺—>賬戶中心—>API安全—>API密鑰】
APIv3密鑰:在微信支付接口V3版本,爲了保證安全性,微信支付在回調通知和平臺證書下載接口中,對關鍵信息進行了AES-256-GCM加密。API v3密鑰是解密時使用的對稱密鑰。
配置路徑:【登錄商戶平臺—>賬戶中心—>API安全—>APIv3密鑰】
支付目錄:JSAPI支付(公衆號支付)、H5支付都要求發起支付請求的頁面域名必須在商戶平臺配置後纔可正常調用微信支付
配置路徑:【登錄商戶平臺—>產品中心—>開發配置—>支付配置】
● 授權域名:獲取用戶身份標識openid時,要求請求來源域名必須在公衆平臺配置過,才被允許獲取用戶身份標識openid
配置路徑:【登錄公衆平臺—>公衆號設置—>功能設置—>網頁授權域名】
以上設置好,那麼就下一步>>>> 下載官方包https://pan.baidu.com/s/18hrzA8Dkf5ZVKL79L4lHNg
密碼:tht8
項目的詳細步驟說明:項目裏面有幾乎含蓋所有>
首先對authorize.php配置:
<?php
error_reporting(1);
header('Content-type:text/html; Charset=utf-8');
/* 配置開始 */
$appid = ''; //微信公衆平臺->開發->基本配置->AppID
$appKey = ''; //微信公衆平臺->開發->基本配置->AppSecret
/* 配置結束 */
//①、獲取用戶openid
$wxPay = new WxService($appid,$appKey);
$data = $wxPay->GetOpenid(); //獲取openid
if(!$data['openid']) exit('獲取openid失敗');
//②、獲取用戶信息
$user = $wxPay->getUserInfo($data['openid'],$data['access_token']);
?>
然後對 barcode.php配置測試(配置好後別忙測試) 這個文件模擬 掃碼配置,所以手動輸入付款碼,看看是否成功
<?php
/**
* 微信支付測試》
* 提醒:提交支付請求後微信會同步返回支付結果(沒有異步回調通知)。當返回結果爲“系統錯誤”時,商戶系統等待5秒後調用【查詢訂單API】,查詢支付實際交易結果;當返回結果爲“USERPAYING”時,商戶系統可設置間隔時間(建議10秒)重新查詢支付結果,直到支付成功或超時(建議30秒);
*/
header('Content-type:text/html; Charset=utf-8');
$mchid = 'xxxxx'; //微信支付商戶號 PartnerID 通過微信支付商戶資料審覈後郵件發送
$appid = 'xxxxx'; //公衆號APPID 通過微信支付商戶資料審覈後郵件發送
$apiKey = 'xxxxx'; //https://pay.weixin.qq.com 帳戶設置-安全設置-API安全-API密鑰-設置API密鑰
$outTradeNo = uniqid(); //你自己的商品訂單號,不能重複
$payAmount = 0.01; //付款金額,單位:元
$orderName = '支付測試'; //訂單標題
$authCode = 'xxxxx'; //用戶付款碼(商戶使用設備掃碼用戶的付款條碼讀取到的條碼數字,或 打開微信-》我-》錢包-》收付款 點擊可查看付款碼數字)
$wxPay = new WxpayService($mchid,$appid,$apiKey);
$wxPay->setTotalFee($payAmount);
$wxPay->setOutTradeNo($outTradeNo);
$wxPay->setOrderName($orderName);
$wxPay->setAuthCode($authCode);
$arr = $wxPay->createJsBizPackage();
配置h5這個 然後訪問 h5 基本功能收款 搞定
$mchid = 'xxxxx'; //微信支付商戶號 PartnerID 通過微信支付商戶資料審覈後郵件發送
$appid = 'xxxxx'; //微信支付申請對應的公衆號的APPID
$appKey = 'xxxxx'; //微信支付申請對應的公衆號的APP Key
$apiKey = 'xxxxx'; //https://pay.weixin.qq.com 帳戶設置-安全設置-API安全-API密鑰-設置API密鑰
$outTradeNo = uniqid(); //你自己的商品訂單號
$payAmount = 0.01; //付款金額,單位:元
$orderName = '支付測試'; //訂單標題
$notifyUrl = 'http://www.xxx.com/wx/notify.php'; //付款成功後的回調地址(不要有問號)
$returnUrl = 'http://www.baidu.com'; //付款成功後,頁面跳轉的地址
$wapUrl = 'www.xxx.com'; //WAP網站URL地址
$wapName = 'H5支付'; //WAP 網站名
/** 配置結束 */
那麼 我們怎麼才能自主獲得訂單狀態呢? 還記得設置的回調地址碼? 在哪裏獲取呢? 配置
orderquery.php,顯示訂單狀態,如果成功,就像郵箱發送郵件通知!
<?php
header('Content-type:text/html; Charset=utf-8');
/** 請填寫以下配置信息 */
$mchid = 'xxxxx'; //微信支付商戶號 PartnerID 通過微信支付商戶資料審覈後郵件發送
$appid = 'xxxxx'; //公衆號APPID 通過微信支付商戶資料審覈後郵件發送
$apiKey = 'xxxxx'; //https://pay.weixin.qq.com 帳戶設置-安全設置-API安全-API密鑰-設置API密鑰
$outTradeNo = ''; //要查詢的訂單號
/** 配置結束 */
完整項目下載》》》》
不要那麼全面?只要簡單的付款成功就跳轉》》 直接複製一下代碼
相關包下載:APi裏面Config裏面配置就能用。不建議型》https://pan.baidu.com/s/17cH2slZqgEXaCU0lEUSW6Q 密碼 8znn
public function _initialize()
{
Vendor('Wxpay.example.lib.WxPay.Api');
Vendor('Wxpay.example.Wxpay.Config');
Vendor('Wxpay.example.Wxpay.JsApiPay');
}
public function wachat_pay(){
$config = config('Wechat');
$order_id = intval(input("param.order_id/d",0));
if(empty($order_id)){
$this->error("訂單有誤");
}
$order = Db::name('order')->where(array('id'=>$order_id))->find();
if(empty($order)){
$this->error("訂單有誤");
}
$theurl="https://api.mch.weixin.qq.com/pay/unifiedorder";
$input['appid']='wx***********';
$input['mch_id']='15*********';
$input['nonce_str']=$this->getNonceStr();
$input['device_info']='WEB';
$input['body']='購物';
$input['out_trade_no']=$order['order_sn'];;
$input['total_fee']=($order['total_price']+$order['express_fee'])*100;
// $input['total_fee']=1;
$input['spbill_create_ip']=$this->get_ip();
$input['notify_url']='http://'.$_SERVER['HTTP_HOST'].'/index/Wxpay/notify_url';
$input['trade_type']='MWEB';
$input['scene_info']='{"h5_info":{"type":"Wap","wap_url":"http://www.*******.com","wap_name":"商城"}}';
$thesign['appid']=$input['appid'];
$thesign['mch_id']=$input['mch_id'];
$thesign['device_info']=$input['device_info'];
$thesign['nonce_str']=$input['nonce_str'];
$thesign['body']=$input['body'];
$thesign['notify_url']=$input['notify_url'];
$thesign['trade_type']=$input['trade_type'];
$thesign['scene_info']=$input['scene_info'];
$thesign['total_fee']=$input['total_fee'];
$thesign['spbill_create_ip']=$input['spbill_create_ip'];
$thesign['out_trade_no']=$input['out_trade_no'];
$input['sign']=$this->MakeSign($thesign);
$post_data = "<xml>
<appid>{$thesign['appid']}</appid>
<body>{$thesign['body']}</body>
<mch_id>{$thesign['mch_id']}</mch_id>
<nonce_str>{$thesign['nonce_str']}</nonce_str>
<notify_url>{$thesign['notify_url']}</notify_url>
<out_trade_no>{$thesign['out_trade_no']}</out_trade_no>
<scene_info>{$thesign['scene_info']}</scene_info>
<spbill_create_ip>{$thesign['spbill_create_ip']}</spbill_create_ip>
<total_fee>{$thesign['total_fee']}</total_fee>
<trade_type>{$thesign['trade_type']}</trade_type>
<sign>{$input['sign']}</sign>
</xml>";//拼接成XML 格式
$content=$this->http_post($theurl,$post_data);
$objectxml = (array)simplexml_load_string($content,'SimpleXMLElement',LIBXML_NOCDATA);
if($objectxml['return_code'] == 'SUCCESS') {
if($objectxml['result_code'] == 'SUCCESS'){//如果這兩個都爲此狀態則返回mweb_url,詳情看‘統一下單’接口文檔
$url_encode_redirect_url = urlencode($config['RETURN_URL']);//支付成功回調路徑,在Home/config.php文件中可配置
$url = $objectxml['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付鏈接
echo "<script>window.location.href='" . $url ."';</script>";
exit();
}
if($objectxml['result_code'] == 'FAIL') {
return $err_code_des = $objectxml['err_code_des'];
}
}
}
//獲取用戶ip
public function get_ip(){
$ip='unknown';
if($_SERVER['REMOTE_ADDR']){
$ip=$_SERVER['REMOTE_ADDR'];
}elseif(getenv('REMOTE_ADDR')){
$ip=getenv('REMOTE_ADDR');
}
return $ip;
}
/*隨機字符串*/
public function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
//簽名生成
public function MakeSign($config)
{
$stringA = "appid=".$config['appid']."&body=".$config['body']."&mch_id=".$config['mch_id']."&nonce_str=".$config['nonce_str']."¬ify_url=".$config['notify_url']."&out_trade_no=".$config['out_trade_no']."&scene_info=".$config['scene_info']."&spbill_create_ip=".$config['spbill_create_ip']."&total_fee=".$config['total_fee']."&trade_type=".$config['trade_type'];
$string=md5($stringA."&key=*************************************");
$result = strtoupper($string);
return $result;
}
public function http_post($url='',$post_data=array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
public function notify_url(){
$testxml = file_get_contents("php://input");
libxml_disable_entity_loader(true);
$jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
$result = json_decode($jsonxml, true);//轉成數組,
if($result){
//如果成功返回了
$out_trade_no = $result['out_trade_no'];
$order = Db::name('order')->where('order_sn',$out_trade_no)->where('status',5)->find();
if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
//執行業務邏輯 改變訂單狀態等
}
}
return '<xml><return_code><![CDATA[SUCCESS]></return_code><return_msg><![CDATA]></return_msg></xml>';
}