PHP的微信支付接口使用方法講解_第1頁
PHP的微信支付接口使用方法講解_第2頁
PHP的微信支付接口使用方法講解_第3頁
PHP的微信支付接口使用方法講解_第4頁
PHP的微信支付接口使用方法講解_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第PHP的微信支付接口使用方法講解在開發(fā)之中經(jīng)常會使用到支付的功能,現(xiàn)在常用的兩種支付方式是支付寶和微信。相對而言,支付寶的文檔較為健全,并且配置和調(diào)用方式方式比較簡單,這里就不過多的描述。

首先去微信官網(wǎng)網(wǎng)站下去下載服務端的demo:/wiki/doc/api/jsapi.phpchapter=11_1

這里雖然是官網(wǎng)提供的公眾號支付的demo,雖然微信支付的預下單等都可以在前端進行實現(xiàn),不過官方還是建議在服務端進行處理。下載后,將其中的demo引入你的項目就好,注意的是如果是公眾號的支付用到的類文件WxPay.JsApiPay.php在文件中example目錄下。

接下來我們就可以進行引用了并實現(xiàn)。以thinkphp框架下進行調(diào)用為例(以下案例包括移動端以及公眾號支付以及公眾號獲取openid等功能)。以下代碼為了能夠更容易理解,將一些類中的方法提取了出來,寫的有點亂,請見諒。

/*微信APP下支付預下單*/

publicfunctionwxAppOrder(){

//TODO:首先獲取訂單詳情,例如傳遞過來訂單號或訂單id,獲取訂單的詳情信息,例如將取出的數(shù)據(jù)存放入$user_order_info數(shù)組,訂單中包含的商品在$user_order_product_info之中。

/*向微信發(fā)起請求*/

vendor('WxpayAPI.lib.WxPay','','.Api.php');

vendor('WxpayAPI.lib.WxPay','','.Data.php');//生成數(shù)據(jù)

//統(tǒng)一下單輸入對象

$order_info=newWxPayUnifiedOrder();

$order_info-SetOut_trade_no($user_order_info['orderNo']);//商品訂單號

$body=$user_order_product_info['productName'];

//$body=iconv('UTF-8','ISO-8859-1',$user_order_product_info['productName']);

$order_info-SetBody($body);//商品描述

$order_info-SetTrade_type('CNY');//人民幣

$order_info-SetTotal_fee(intval($user_order_info['sumPrice']*100));//總金額,以分為單位

$order_info-SetTrade_type('APP');//交易類型

$order_info-SetAppid(C('wxAPPID'));

$order_info-SetMch_id(C('wxMCHID'));

$order_info-SetNotify_url('你的回調(diào)地址');

$order_info-SetSign();

//進行統(tǒng)一支付

$wxpay=newWxPayApi();

$order_result=$wxpay-unifiedOrder($order_info);//統(tǒng)一下單

if($order_result['return_code']=='FAIL'){

$arr=array(

'resultCode'='99',

'resultDesc'=$order_result['return_msg'],

'resultObj'=array(''=''),

echoJSON($arr);

exit();

if($order_result['result_code']=='SUCCESS'){

//預下單成功后,重新簽名返回給移動端

$wxpay_result=newWxPayResults();

$timestamp=time();

$wxpay_result-SetData('appid',$order_result['appid']);

$wxpay_result-SetData('partnerid',$order_result['mch_id']);

$wxpay_result-SetData('prepayid',$order_result['prepay_id']);

$wxpay_result-SetData('timestamp',$timestamp);

$wxpay_result-SetData('noncestr',$order_result['nonce_str']);

$wxpay_result-SetData('package','Sign=WXPay');

//$wxpay_result-SetData('key',C('wxKEY'));

//上方注釋的代碼是再簽名中必要的一步,只是這個包含在了微信demo的類中,如果像該項目中既有app支付,又有公眾號支付,最好是注釋類中代碼,并自己寫入

$resign_result=$wxpay_result-SetSign();

//處理返回數(shù)據(jù)

$result=array(

'appid'=$order_result['appid'],//appid

'partnerid'=$order_result['mch_id'],//商戶號

'prepayid'=$order_result['prepay_id'],//與支付id

'package'='Sign=WXPay',

'noncestr'=$order_result['nonce_str'],

'timestamp'=$timestamp,

'sign'=$resign_result,

$arr=array(

'resultCode'='00',

'resultDesc'='成功',

'resultObj'=$result,

echoJSON($arr);

exit();

}else{

$arr=array(

'resultCode'='99',

'resultDesc'='失敗',

'resultObj'=$order_result,

echoJSON($arr);

exit();

/*微信支付回調(diào)函數(shù)*/

publicfunctionwxpayNotify(){

vendor('WxpayAPI.lib.Logwx','','.Log.php');//在回調(diào)中最好是引入日志進行記錄,在這里因為Log類與thinkphp中的log類重復,需要進行處理

$handle=newCLogFileHandler('./Public/wxlog.txt');

$log=Logwx::Init($handle);

$xml=$GLOBALS['HTTP_RAW_POST_DATA'];//獲取數(shù)據(jù)

vendor('WxpayAPI.lib.WxPay','','.Api.php');

vendor('WxpayAPI.lib.WxPay','','.Data.php');

$wxpay=newWxPayApi();

$notify=newWxPayNotifyReply();

$result=WxPayResults::Init($xml);//獲取數(shù)據(jù)并轉(zhuǎn)換為數(shù)組

if($result['return_code']=='SUCCESS'$result['result_code']=='SUCCESS'){//此字段是通信標識,非交易標識,交易是否成功需要查看result_code來判斷

//TODO:進行數(shù)據(jù)庫操作的業(yè)務邏輯處理,假設其成功與否的數(shù)據(jù)為$res

if($res){

$log-INFO('訂單:'.$result['out_trade_no'].'支付成功');

$notify-SetReturn_code('SUCCESS');

$notify-SetReturn_msg('OK');

$notify-SetSign();

}else{

$log-ERROR('微信支付失敗');

$notify-SetReturn_code('FAIL');

$notify-SetReturn_msg('客戶服務器錯誤');

}else{

$log-ERROR('微信回調(diào)返回錯誤');

$notify-SetReturn_code('FAIL');

$notify-SetReturn_msg('微信支付失敗');

//返回微信端

$wxpay-replyNotify($notify-ToXml());

/*微信公眾賬號下單

*獲取code等信息

*跳轉(zhuǎn)至獲取信息

**/

publicfunctionwxPubOrder(){

//此流程中

$orderId=$_GET['orderId'];

//注意:此處如果想要回調(diào)成功,需要在微信公眾平臺設置回調(diào)域名

//print_r('Location:/connect/oauth2/authorizeappid='.C('wxAPPID').'redirect_uri='.'http://你的域名/Pay/getOpenid/orderId/'.$orderId.'response_type=codescope=snsapi_basestate=123#wechat_redirect');

//exit();

header('Location:/connect/oauth2/authorizeappid='.'*******'.'redirect_uri='.urlencode('http://*****/Pay/getOpenid/orderId/'.$orderId).'response_type=codescope=snsapi_basestate=123#wechat_redirect');

exit();

/*微信獲取openid,跳轉(zhuǎn)到微信同意下單接口*/

publicfunctiongetOpenid(){

//code

$code=$_GET['code'];

$state=$_GET['state'];

$orderId=$_GET['orderId'];

$appid='******';

$appsecret='******';

//獲取openid

$get_token_url='/sns/oauth2/access_tokenappid='.$appid.'secret='.$appsecret.'code='.$code.'grant_type=authorization_code';

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$get_token_url);

curl_setopt($ch,CURLOPT_HEADER,0);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);

$res=curl_exec($ch);

curl_close($ch);

$json_obj=json_decode($res,true);

$openId=$json_obj['openid'];

//跳轉(zhuǎn)到預下單

//echo$openId;exit();

$url='http://******/html5/#/pay/'.$orderId.'openid='.$openId;

header('Location:'.$url);

/*微信公眾賬號統(tǒng)一下單*/

publicfunctionwxOrder(){

$orderId=$_GET['orderId'];

$openId=$_GET['openId'];

if(empty($orderId)||empty($openId)){

$arr=array(

'resultCode'='66',

'resultDesc'='缺少參數(shù)',

'resultObj'=array(),

echoJSON($arr);

exit();

//TODO:獲取訂單和訂單商品信息,分別存儲在$user_order_info中和$user_order_good_info中

if(empty($user_order_info)){

$arr=array(

'resultCode'='99',

'resultDesc'='不存在該訂單',

'resultObj'=array(),

echoJSON($arr);

exit();

/*向微信發(fā)起請求*/

vendor('WxpayAPI.lib.WxPay','','.Api.php');

vendor('WxpayAPI.lib.WxPay','','.Data.php');//生成數(shù)據(jù)

//vendor('WxpayAPI.lib.WxPay','','.JsApiPay.php');

//統(tǒng)一下單輸入對象

$order_info=newWxPayUnifiedOrder();

$wxpay=newWxPayApi();

$order_info-SetMch_id('***');//商戶號

$order_info-SetAppid('****');//微信號APPID//wx70a40dfa2711c4fe

$order_info-SetOut_trade_no($user_order_info['orderNo']);//商品訂單號

$order_info-SetBody($user_order_good_info['productName']);//商品描述

$order_info-SetTrade_type('CNY');//人民幣

$order_info-SetTotal_fee(intval($user_order_info['sumPrice']*100));//總金額,以分為單位

$order_info-SetTrade_type('JSAPI');//交易類型

$order_info-SetNonce_str($wxpay-getNonceStr(32));

$order_info-SetSpbill_create_ip('');

//$order_info-SetOpenid($user_info['openId']);

$order_info-SetOpenid($openId);

//TODO:

$order_info-SetNotify_url('http://****/Pay/wxpayNotify');

$order_info-SetSign();//設置簽名

//進行統(tǒng)一支付

$order_result=$wxpay-unifiedOrder($order_info);//統(tǒng)一下單

//同意下單后再加

if($order_result['return_code']=='FAIL'){

$arr=array(

'resultCode'='99',

'resultDesc'=$order_result['return_code'].':'.$order_result['return_msg'],

'resultObj'=array(),

echoJSON($arr);

exit();

if($order_result['result_code']=='SUCCESS'){

$jsapi=newWxPayJsApiPay();

$jsapi-SetAppid($order_result["appid"]);

$timeStamp=time();

$jsapi-SetTimeStamp("$timeStamp");

$jsapi-SetNonceStr(WxPayApi::getNonceStr());

$jsapi-SetPackage("prepay_id=".$order_result['prepay_id']);

$jsapi-SetSignType("MD5");

$

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論