国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP開發(fā)實現(xiàn)微信退款功能示例

2024-05-04 21:50:08
字體:
供稿:網(wǎng)友

本文實例講述了PHP開發(fā)實現(xiàn)微信退款功能。分享給大家供大家參考,具體如下:

最近在調(diào)微信退款接口,發(fā)現(xiàn)有許多坑,更大家分享一下

① 要是在測試的時候,網(wǎng)頁提示 curl 58 說明 證書的路徑出現(xiàn)問題(這里要填物理路徑,也就是絕對路徑)

② 網(wǎng)頁提示curl 52 說明你的證書引入少了,在官方的demo上只有兩個證書 apiclient_cert.pem和 apiclient_key.pem  你還需要引入一個證書 rootca.pem,這個證書需要你登錄到

你的商戶平臺上下載

③ 要是網(wǎng)頁提示  "HTTP/1.1 200 OK Server: nginx Date: Tue, 20 Jun 2017 08:08:01 GMT Content-Type: text/plain Content-Length: 852 Connection: keep-alive Keep-Alive: timeout=8

說明你的代碼是沒有問題的 ,應(yīng)該是參數(shù)出錯了 我之前錯的地方是把 out_refund_no和 out_trade_no寫的一樣,微信官網(wǎng)上也是這樣寫的 ,但是就是出現(xiàn)問題,我把out_refund_no 修改了一下 發(fā)現(xiàn)可以了

注意:金額的單位是分

下面就是我寫的接口:

  1. function Home_index() { 
  2.   date_default_timezone_set("Asia/Shanghai"); 
  3.   $date = date("YmdHis"); 
  4.   $appid = ""
  5.   $mch_id = ""
  6.   $out_trade_no = "14487658021497944120"
  7.   $op_user_id = ""
  8.   $out_refund_no = $date
  9.   $total_fee = "500"
  10.   $refund_fee = "500"
  11. //  $transaction_id = "4009542001201706206596667604"; 
  12.   $key = ""
  13.   $nonce_str = nonceStr(); 
  14.   $ref = strtoupper(md5("appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&op_user_id=$op_user_id" 
  15.           . "&out_refund_no=$out_refund_no&out_trade_no=$out_trade_no&refund_fee=$refund_fee&total_fee=$total_fee" 
  16.           . "&key=$key")); //sign加密MD5 
  17.   $refund = array
  18.   'appid' =>$appid//應(yīng)用ID,固定 
  19.   'mch_id' => $mch_id//商戶號,固定 
  20.   'nonce_str' => $nonce_str//隨機字符串 
  21.   'op_user_id' => $op_user_id//操作員 
  22.   'out_refund_no' => $out_refund_no//商戶內(nèi)部唯一退款單號 
  23.   'out_trade_no' => $out_trade_no//商戶訂單號,pay_sn碼 1.1二選一,微信生成的訂單號,在支付通知中有返回 
  24.   // 'transaction_id'=>'1',//微信訂單號 1.2二選一,商戶側(cè)傳給微信的訂單號 
  25.   'refund_fee' => $refund_fee//退款金額 
  26.   'total_fee' => $total_fee//總金額 
  27.   'sign' => $ref//簽名 
  28.   ); 
  29.   $url = "https://api.mch.weixin.qq.com/secapi/pay/refund"
  30.   ; //微信退款地址,post請求 
  31.   $xml = arrayToXml($refund); 
  32.   $ch = curl_init(); 
  33.   curl_setopt($ch, CURLOPT_URL, $url); 
  34.   curl_setopt($ch, CURLOPT_HEADER, 1); 
  35.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  36.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); //證書檢查 
  37.   if ($useCert == true) { 
  38.     // 設(shè)置證書 
  39.     curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'pem'); 
  40.     curl_setopt($ch, CURLOPT_SSLCERT, dirname(__FILE__) . '/WxPay/cert/apiclient_cert.pem'); 
  41.     curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'pem'); 
  42.     curl_setopt($ch, CURLOPT_SSLKEY, dirname(__FILE__) . '/WxPay/cert/apiclient_key.pem'); 
  43.     curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'pem'); 
  44.     curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/WxPay/cert/rootca.pem'); 
  45.   } 
  46.   curl_setopt($ch, CURLOPT_POST, 1); 
  47.   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 
  48.   $xml = curl_exec($ch); 
  49.   // 返回結(jié)果0的時候能只能表明程序是正常返回不一定說明退款成功而已 
  50.   if ($xml) { 
  51.     curl_close($ch); 
  52.     // 把xml轉(zhuǎn)化成數(shù)組 
  53.     libxml_disable_entity_loader(true); 
  54.     $xmlstring = simplexml_load_string($xml'SimpleXMLElement', LIBXML_NOCDATA); 
  55. //    var_dump($xmlstring); 
  56.     $result['errNum'] = 0; 
  57.     $result['info'] = object_to_array($xmlstring); 
  58. //    var_dump($result); 
  59.     return $result
  60.   } else { 
  61.     $error = curl_errno($ch); 
  62.     curl_close($ch); 
  63.     // 錯誤的時候返回錯誤碼。 
  64.     $result['errNum'] = $error
  65.     return $result
  66.   } 
  67. function arrayToXml($arr) { 
  68.   $xml = "<root>"
  69.   foreach ($arr as $key => $val) { 
  70.     if (is_array($val)) { 
  71.       $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">"
  72.     } else { 
  73.       $xml .= "<" . $key . ">" . $val . "</" . $key . ">"
  74.     } 
  75.   } 
  76.   $xml .= "</root>"
  77.   return $xml
  78. function object_to_array($obj) { 
  79.   $obj = (array$obj
  80.   foreach ($obj as $k => $v) { 
  81.     if (gettype($v) == 'resource') { 
  82.       return
  83.     } 
  84.     if (gettype($v) == 'object' || gettype($v) == 'array') { 
  85.       $obj[$k] = (array) object_to_array($v); 
  86.     } 
  87.   } 
  88.   return $obj
  89. function nonceStr() { 
  90.   $chars = "abcdefghijklmnopqrstuvwxyz0123456789"
  91.   $str = ""
  92.   $length = 32; 
  93.   for ($i = 0; $i < $length$i++) { 
  94.     $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 
  95.   } //Vevb.com 
  96.   // 隨機字符串 
  97.   return $str
  98. Home_index(); 

趕快添加到你的項目中去吧!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尤溪县| 嘉善县| 浦北县| 武安市| 新郑市| 庆云县| 绵阳市| 长子县| 四川省| 萨嘎县| 独山县| 茂名市| 葫芦岛市| 雅安市| 成武县| 沁阳市| 房产| 岫岩| 福贡县| 潮安县| 开原市| 都江堰市| 罗源县| 建水县| 额济纳旗| 华容县| 西安市| 綦江县| 五原县| 来安县| 大冶市| 宜兰县| 和硕县| 新竹县| 冕宁县| 中西区| 巴林右旗| 桃园市| 华阴市| 登封市| 大英县|