之前有寫過幾篇文章将微信支付和退款:
1.PHP實作微信支付(jsapi支付)流程
2.ThinkPHP中實作微信支付(jsapi支付)流程
3.PHP實作微信申請退款
這幾篇都是使用了微信官方給的PHP版本的SDK,進行支付的時候寫代碼可以省不少事,步驟也挺簡化,但是內建SDK有很多坑,很多人說引入的SDK老報錯,或者說官方SDK本身有不少錯誤,改起來很麻煩,也确實挺麻煩的,對于新手搞支付很容易被繞進去,那麼今天就來講講不內建支付SDK直接調用支付接口實作支付和退款。
前期準備:
1.當然了,還是要有一個微信認證服務号,并且開通了微信支付;
2.在微信商戶背景配置好支付授權目錄,同時準備好支付的Api證書(支付用不到,退款的時候使用)
3.調用接口支付的話,必須要先知道該使用者的openid,是以要先知道怎麼擷取使用者的openid,這個也很簡單,我之前也有文章講怎麼擷取使用者的openid,詳見文章微信公衆号擷取使用者的openid。
好了,話不多說,直接貼上主要代碼:
/**
* 微信支付請求接口(POST)
* @param string $openid openid
* @param string $body 商品簡單描述
* @param string $order_sn 訂單編号
* @param string $total_fee 金額
* @return json的資料
*/
public function wxpay($openid,$total_fee,$body,$order_sn){
$config = $this->config;
//統一下單參數構造
$unifiedorder = array(
'appid' => $config['appid'],
'mch_id' => $config['mch_id'],
'nonce_str' => self::getNonceStr(),
'body' => $body,
'out_trade_no' => $order_sn,
'total_fee' => $total_fee * 100,
'spbill_create_ip' => self::getip(),
'notify_url' => 'http://'.$_SERVER['HTTP_HOST'].'/notify.php',
'trade_type' => 'JSAPI',
'openid' => $openid
);
$unifiedorder['sign'] = self::makeSign($unifiedorder);
//return $unifiedorder;
//請求資料,統一下單
$xmldata = self::array2xml($unifiedorder);
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$res = self::curl_post_ssl($url, $xmldata);
if(!$res){
return array('status'=>0, 'msg'=>"Can't connect the server" );
}
// 這句file_put_contents是用來檢視伺服器傳回的結果 測試完可以删除了
//file_put_contents('./log.txt',$res,FILE_APPEND);
$content = self::xml2array($res);
if(strval($content['result_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
}
if(strval($content['return_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['return_msg']));
}
$time = time();
settype($time, "string"); //jsapi支付界面,時間戳必須為字元串格式
$resdata = array(
'appId' => strval($content['appid']),
'nonceStr' => strval($content['nonce_str']),
'package' => 'prepay_id='.strval($content['prepay_id']),
'signType' => 'MD5',
'timeStamp' => $time
);
$resdata['paySign'] = self::makeSign($resdata);
return json_encode($resdata);
}
/**
* 微信退款(POST)
* @param string(28) $transaction_id 在微信支付的時候,微信伺服器生成的訂單流水号,在支付通知中有傳回
* @param string $out_refund_no 商品簡單描述
* @param string $total_fee 微信支付的時候支付的總金額(機關:分)
* @param string $refund_fee 此次要退款金額(機關:分)
* @return string xml格式的資料
*/
public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){
$config = $this->config;
//退款參數
$refundorder = array(
'appid' => $config['appid'],
'mch_id' => $config['mch_id'],
'nonce_str' => self::getNonceStr(),
'transaction_id'=> $transaction_id,
'out_refund_no' => $out_refund_no,
'total_fee' => $total_fee * 100,
'refund_fee' => $refund_fee * 100
);
$refundorder['sign'] = self::makeSign($refundorder);
//請求資料,進行退款
$xmldata = self::array2xml($refundorder);
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$res = self::curl_post_ssl($url, $xmldata);
if(!$res){
return array('status'=>0, 'msg'=>"Can't connect the server" );
}
// 這句file_put_contents是用來檢視伺服器傳回的結果 測試完可以删除了
//file_put_contents('./log3.txt',$res,FILE_APPEND);
$content = self::xml2array($res);
if(strval($content['result_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
}
if(strval($content['return_code']) == 'FAIL'){
return array('status'=>0, 'msg'=>strval($content['return_msg']));
}
return $content;
}
這是封裝好的類,使用起來也超級簡單:
<?php
require_once "wxpay.class.php";
$config = array(
'appid' => 'wx123456789876',
'mch_id' => '123456789',
'pay_apikey' => '123456789876123456789876123456789876'
);
$wxpay = new WxPay($config);
$result = $wxpay->paytest();
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>江南極客支付</title>
<script type="text/javascript">
//調用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',<?php echo $result; ?>,
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res);
if(res.err_msg == "get_brand_wcpay_request:ok"){
alert("支付成功!");
}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
alert("使用者取消支付!");
}else{
alert("支付失敗!");
}
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
</script>
</head>
<body>
<br/>
<font color="#9ACD32"><b>該筆訂單支付金額為<span style="color:#f00;font-size:50px">1分</span>錢</b></font><br/><br/>
<font color="#9ACD32"><b><span style="color:#f00;font-size:50px;margin-left:40%;">1分</span>錢也是愛</b></font><br/><br/>
<div align="center">
<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >果斷買買買^_^</button>
</div>
</body>
</html>
至于支付回調驗證,這裡就不過多講了,不明白的可以看ThinkPHP中實作微信支付(jsapi支付)流程,這裡詳細講了如何處理回調。
類檔案以及使用執行個體代碼源碼下載下傳:http://download.csdn.net/download/sinat_35861727/9957810
如果真的感覺好用,請點個贊留個好評,謝謝!有問題也可以評論區告訴我!