目录
- 微信退款
- 申请退款
- 对要发送到微信统一下单接口的数据进行签名
- 排序并格式化
- 数组转字符串方法
- 需要使用证书的请求
- XML数据转换为数组
微信退款
申请退款
public function getWeChatRefund(){$baseConfigModel = new \app\admin\model\Baseconfig();$app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');$mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');$payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');$out_trade_no = 'YS202307089954985798100';$parma = array('appid'=> $app_id, 'mch_id'=> $mch_id, 'nonce_str'=> $this->generate_password(30),'out_refund_no'=> $this->order_number('R'),'out_trade_no'=> $out_trade_no,'total_fee'=> 1, 'refund_fee'=> 1, 'op_user_id' => $mch_id, );$parma['sign'] = $this->getSign1($parma,$payment_key);$xmldata = $this->arrayToXml($parma);$xmlresult = $this->postXmlSSLCurl($xmldata,'https://api.mch.weixin.qq.com/secapi/pay/refund');$result = $this->FromXml($xmlresult);halt($result);return $result;}
对要发送到微信统一下单接口的数据进行签名
protected function getSign1($Obj,$payment_key){foreach ($Obj as $k => $v){$Parameters[$k] = $v;}ksort($Parameters);$String = $this->formatBizQueryParaMap($Parameters, false);$String = $String."&key=".$payment_key;$String = md5($String);$result_ = strtoupper($String);return $result_;}
排序并格式化
protected function formatBizQueryParaMap($paraMap, $urlencode){$buff = "";ksort($paraMap);foreach ($paraMap as $k => $v){if($urlencode){$v = urlencode($v);}$buff .= $k . "=" . $v . "&";}$reqPar;if (strlen($buff) > 0){$reqPar = substr($buff, 0, strlen($buff)-1);}return $reqPar;}
数组转字符串方法
protected function arrayToXml($arr){$xml = "";foreach ($arr as $key=>$val){if (is_numeric($val)){$xml.="<".$key.">".$val."".$key.">";}else{$xml.="<".$key.">.$val."]]>".$key.">";}}$xml.="";return $xml;}
需要使用证书的请求
function postXmlSSLCurl($xml,$url,$second=30){$apiclient_cert = '../public/apiclient_cert';$payment_key = '../public/payment_key';$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, $apiclient_cert);curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');curl_setopt($ch,CURLOPT_SSLKEY, $payment_key);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"."
";curl_close($ch);return false;}}
XML数据转换为数组
public function FromXml($xml){if (!$xml) {echo "xml数据异常!";}libxml_disable_entity_loader(true);$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);return $data;}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!