這次給大家帶來PHP實現jsapi支付與退款步驟詳解,PHP實現jsapi支付與退款的注意事項有哪些,下面就是實戰案例,一起來看一下。
前期準備:
1.當然了,還是要有一個認證服務號,並且開通了支付;
2.在商戶後台配置好支付授權目錄,同時準備好支付的Api認證(支付用不到,退款的時候使用)
3.調用介面支付的話,必須要Crowdsourced Security Testing道該使用者的openid,所以要Crowdsourced Security Testing道怎麼擷取使用者的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( 'wxappid' => '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/> <p 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> </p> </body> </html>
至於支付回調驗證,這裡就不過多講了,不明白的可以看ThinkPHP中實現支付(jsapi支付)流程,這裡詳細講了如何處理回調。
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
PHP實現單向散列加密操作步驟詳解
PECL方式安裝php-mongodb擴充步驟詳解