private function wxrefundapi($order){ //通過微信api進行退款流程 $parma = array( 'appid'=>"XXXXX", 'mch_id'=>'XXXXX', 'nonce_str'=>$this->createNoncestr(), 'out_refund_no'=>$order['out_trade_no'], 'transaction_id'=>$order['transaction_id'],//微信訂單號 'total_fee'=> $order['price']*100, //單位爲分,實際支付金額 這裏默認與前臺一志寫死0.01 'refund_fee'=> $order['price']*100, //單位爲分,實際退款金額 這裏默認與前臺一志寫死0.01 // 'total_fee'=> 0.01, // 'refund_fee'=> 0.01, ); $parma['sign'] = $this->getSign($parma); $xmldata = $this->arrayToXml($parma); $xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund'); $result = $this->xmlToArray($xmlresult); return $result; } /* * 對要發送到微信統一下單接口的數據進行簽名 */ protected function getSign($Obj){ foreach ($Obj as $k => $v){ $param[$k] = $v; } //簽名步驟一:按字典序排序參數 ksort($param); $String = $this->formatBizQueryParaMap($param, false); //簽名步驟二:在string後加入KEY $wx_key='xxxxxxxxxxxxxxxxxxxxxxxx'; //申請支付後有給予一個商戶賬號和密碼,登陸後自己設置的key $String = $String."&key=".$wx_key; //簽名步驟三:MD5加密 $String = md5($String); //簽名步驟四:所有字符轉爲大寫 $result_ = strtoupper($String); // var_dump($result_); return $result_; } /* *排序並格式化參數方法,簽名時需要使用 */ protected function formatBizQueryParaMap($paraMap, $urlencode){ $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v){ if($urlencode){ $v = urlencode($v); } //$buff .= strtolower($k) . "=" . $v . "&"; $buff .= $k . "=" . $v . "&"; } $reqPar = ""; if (strlen($buff) > 0){ $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } /* * 生成隨機字符串方法 */ protected function createNoncestr($length = 32 ){ $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } //數組轉字符串方法 protected function arrayToXml($arr){ $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } //將xml字符串轉換爲數組 protected static function xmlToArray($xml){ $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data; } //需要使用證書的請求 //發送xml請求方法 protected function postXmlSSLCurl($xml, $url, $second = 30) { $isdir = "/www/aaa/";//證書位置 $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLCERT, $isdir.'apiclient_cert.pem'); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, $isdir.'apiclient_key.pem'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $data = curl_exec($ch); if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出錯,錯誤碼:$error" . "<br>"; curl_close($ch); return false; } }
php 微信退款
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.