<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 1、先建造一個公共方法、也可以用sdk中的代碼 ```php namespace app\api\controller; class common { private $key = ''; /**********下方代碼可以放到公共函數中**********/ /* * * 產生隨機字符串,不長于32位 * @param int $length * @return 產生的隨機字符串 */ public function getNonceStr($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } /* * 生成簽名 * @return 簽名 */ public function makeSign($data) { //獲取微信支付秘鑰 $key = $this->key ; // 去空 $data = array_filter($data); //簽名步驟一:按字典序排序參數 ksort($data); $string_a = http_build_query($data); $string_a = urldecode($string_a); //簽名步驟二:在string后加入KEY //$config=$this->config; $string_sign_temp = $string_a . "&key=" . $key; //簽名步驟三:MD5加密 $sign = md5($string_sign_temp); // 簽名步驟四:所有字符轉為大寫 $result = strtoupper($sign); return $result; } //將一個數組轉換為 XML 結構的字符串 public function array2xml($arr, $level = 1) { $s = $level == 1 ? "<xml>" : ""; foreach ($arr as $tagname => $value) { if (is_numeric($tagname)) { $tagname = $value['TagName']; unset($value["TagName"]); } if (!is_array($value)) { $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : "") . $value . (!is_numeric($value) ? ']]>' : "") . "</{$tagname}>"; } else { $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1) . "</{$tagname}>"; } } $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s); return $level == 1 ? $s . "</xml>" : $s; } /* * 將xml轉為array * @param string $xml xml字符串 * @return array 轉換得到的數組 */ public function xml2array($xml) { libxml_disable_entity_loader(true); $result = json_decode(json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA)), true); return $result; } } ``` ## 2、建立控制器進行退款 退款的curl需要證書、所以這里我們單獨寫在了控制器中 ```php namespace app\api\controller; class Wxrefund { private $appid = ''; private $mch_id = ''; private $apiclient_cert = './wx/apiclient_cert.pem'; private $apiclient_key = './wx/apiclient_key.pem'; /* refund(支付訂單號,支付總金額,退款金額) */ public function refund($order_sn,$total_fee,$refund_fee) { $data = request()->param(); if ($refund_fee){ $out_trade_no = $order_sn;//商戶支付單號 自定義而已 $out_refund_no = 'refund'.time();//商戶退款單號 自定義而已 $common=new common;//調用公共方法 //統一下單退款參數構造 $unifiedorder = array( 'appid' => $this->appid,//appid 'mch_id' => $this->mch_id,//商戶id 'nonce_str' => $common->getNonceStr(),//公共方法的隨機字符串 'out_trade_no' => $out_trade_no, //商戶訂單號 'out_refund_no' => $out_refund_no, //商戶退款單號 'total_fee' => $total_fee, //訂單金額 需要整數 依分為單位 所以一般*100 'refund_fee' => intval(floatval($refund_fee)), //退款金額 需要整數 依分為單位 所以一般*100 ); //生成簽名并加入數組 $unifiedorder['sign'] = $common->makeSign($unifiedorder); //根據數組轉成xml生成請求數據 $xmldata = $common->array2xml($unifiedorder); $opUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //吊起請求 $res = $this->curl_post_ssl_refund($opUrl, $xmldata); if (!$res) { return json_encode(array('status' => $status, 'result' => 'fail', 'errmsg' => "Can't connect the server")); } $content = $common->xml2array($res); if (strval($content['result_code']) == "FAIL" ) { return json_encode(array("status" => $status, "result" => 'fail', "errmsg" => strval($content["err_code_des"]))) ; } if (strval($content['return_code']) == "FAIL") { return json_encode(array("status" => $status, "result" => "fail", "errmsg" => strval($content['return_msg']))) ; } return "退款成功!"; }else{ return "不符合退款訂單!"; } } //curl public function curl_post_ssl_refund($url, $vars, $second=30,$aHeader=array()) { $ch = curl_init(); //超時時間 curl_setopt($ch,CURLOPT_TIMEOUT,$second); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); //這里設置代理,如果有的話 //curl_setopt($ch,CURLOPT_PROXY, "10.206.30.98"); //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //TODO 以下兩種方式需選擇一種 /*------- --第一種方法,cert 與 key 分別屬于兩個.pem文件--------------------------------*/ //默認格式為PEM,可以注釋 //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT, $this->apiclient_cert); curl_setopt($ch,CURLOPT_SSLKEY, $this->apiclient_key); //默認格式為PEM,可以注釋 //curl_setopt($ch,CURLOPT_SSLKEYTYPE,"PEM"); //默認格式為PEM,可以注釋 //curl_setopt($ch,CURLOPT_SSLKEYTYPE,"PEM"); /** * 補充 當找不到ca根證書的時候還需要rootca.pem文件 * TODO 注意,微信給出的壓縮包中,有提示信息: * 由于絕大部分操作系統已內置了微信支付服務器證書的根CA證書, * 2018年3月6日后, 不再提供CA證書文件(rootca.pem)下載 */ /*----------第二種方式,兩個文件合成一個.pem文件----------------------------------------*/ //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem'); if( count($aHeader) >= 1 ){ curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); } curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); $data = curl_exec($ch); if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); //echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } } ``` ## 3、提現· [https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1](https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1) ```php <?php namespace real; use real\WechatPay\WechatPayCommon; //real\Wxrefund /* 支付退款類 */ class WechatWithdrawal{ private $appid = ''; private $mch_id = ''; private $key = ''; private $apiclient_cert = './wx/apiclient_cert.pem'; private $apiclient_key = './wx/apiclient_key.pem'; public function WechatWithdrawal($openId,$money) { $pub = ['app_id'=>$this->appid,'mch_id'=>$this->mch_id,'key'=>$this->key];//config('keys.payConfig'); $appid = $pub['app_id'];//商戶賬號appid $mch_id = $pub['mch_id'];//商戶號 $key = $pub['key']; $openid = $openId;//授權用戶openid $common=new \WechatPayCommon;//調用公共方法 $arr = array(); $arr['partner_trade_no'] = '123456789' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商戶訂單號 $arr['openid'] = $openid; $arr['amount'] = $money * 100;//付款金額,單位為分 $arr['mch_appid'] = $appid; $arr['mchid'] = $mch_id; $arr['nonce_str'] = $common->getNonceStr();//公共方法的隨機字符串 $arr['desc'] = "零錢提現";//描述信息 $arr['check_name'] = 'NO_CHECK';//是否驗證用戶真實姓名,這里不驗證 // $arr['spbill_create_ip'] = 'xx.xx.xx.xx';//獲取服務器的ip //封裝的關于簽名的算法 $arr['sign'] = $common->makeSign($arr,$key);//簽名 $var = $common->array2xml($arr); // dump($arr['sign'] );exit; $xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',$var,30, array(), 1); libxml_disable_entity_loader(true); //echo $xml; die; $obj1=simplexml_load_string($xml,'SimpleXMLElement'); //var_dump($obj1); die; $rdata = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)),true); // var_dump('cash_xmldata',$rdata);//eblog('cash_xmldata',$rdata); // dump($rdata);exit; $return_code = trim(strtoupper($rdata['return_code'])); $result_code = trim(strtoupper($rdata['result_code'])); if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') { $isrr = array( 'status'=>1, 'msg' => '', ); } else { // $returnmsg = $rdata['return_msg']; $err_code_des = $rdata['err_code_des']; $isrr = array( 'status' => 0, 'msg' => $err_code_des, ); } return $isrr; } protected function curl_post_ssl($url, $vars, $second = 30, $aHeader = array()) { $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設置執行最長秒數 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網頁 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務端進行驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//證書類型 // curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//證書位置 // curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中規定的私鑰的加密類型 // curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//證書位置 curl_setopt($ch,CURLOPT_SSLCERT, $this->apiclient_cert); curl_setopt($ch,CURLOPT_SSLKEY, $this->apiclient_key); // curl_setopt($ch, CURLOPT_CAINFO, 'PEM'); // curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem'); if (count($aHeader) >= 1) { curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//設置頭部 } curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部數據使用HTTP協議中的"POST"操作來發送 $data = curl_exec($ch);//執行回話 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } } ``` ## 原提現以舍棄 ```php class Cash{ public function wxcash($openId,$money) { $pub = ['app_id'=>'######','mch_id'=>'######','key'=>'######'];//config('keys.payConfig'); $appid = $pub['app_id'];//商戶賬號appid $mch_id = $pub['mch_id'];//商戶號 $key = $pub['key']; $openid = $openId;//授權用戶openid $arr = array(); $arr['mch_appid'] = $appid; $arr['mchid'] = $mch_id; $arr['nonce_str'] = md5(uniqid(microtime(true),true));//隨機字符串,不長于32位 $arr['partner_trade_no'] = '123456789' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商戶訂單號 $arr['openid'] = $openid; $arr['check_name'] = 'NO_CHECK';//是否驗證用戶真實姓名,這里不驗證 $arr['amount'] = $money * 100;//付款金額,單位為分 $arr['desc'] = "零錢提現";//描述信息 $arr['spbill_create_ip'] = 'xx.xx.xx.xx';//獲取服務器的ip //封裝的關于簽名的算法 $arr['sign'] = $this->makeSign($arr,$key);//簽名 $var = $this->arrayToXml($arr); // dump($arr['sign'] );exit; $xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',$var,30, array(), 1); libxml_disable_entity_loader(true); //echo $xml; die; $obj1=simplexml_load_string($xml,'SimpleXMLElement'); //var_dump($obj1); die; $rdata = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)),true); var_dump('cash_xmldata',$rdata);//eblog('cash_xmldata',$rdata); // dump($rdata);exit; $return_code = trim(strtoupper($rdata['return_code'])); $result_code = trim(strtoupper($rdata['result_code'])); if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') { $isrr = array( 'status'=>1, 'msg' => '', ); } else { // $returnmsg = $rdata['return_msg']; $err_code_des = $rdata['err_code_des']; $isrr = array( 'status' => 0, 'msg' => $err_code_des, ); } return $isrr; } protected function makesign($data,$key) { //獲取微信支付秘鑰 $data = array_filter($data); //簽名步驟一:按字典序排序參數 ksort($data); $string_a = http_build_query($data); $string_a = urldecode($string_a); //簽名步驟二:在string后加入KEY //$config=$this->config; $string_sign_temp = $string_a."&key=".$key; //簽名步驟三:MD5加密 $sign = md5($string_sign_temp); // 簽名步驟四:所有字符轉為大寫 $result = strtoupper($sign); // $result = strtoupper(hash_hmac("sha256",$string_sign_temp,$key)); return $result; } protected function arraytoxml($data){ $str='<xml>'; foreach($data as $k=>$v) { $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str.='</xml>'; return $str; } protected function curl_post_ssl($url, $vars, $second = 30, $aHeader = array()) { $isdir = "cert/";//APP_PATH."/common/library/php_sdk/lib/";//證書位置 $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設置執行最長秒數 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網頁 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務端進行驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//證書類型 curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//證書位置 curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中規定的私鑰的加密類型 curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//證書位置 curl_setopt($ch, CURLOPT_CAINFO, 'PEM'); curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem'); if (count($aHeader) >= 1) { curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//設置頭部 } curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部數據使用HTTP協議中的"POST"操作來發送 $data = curl_exec($ch);//執行回話 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "call faild, errorCode:$error\n"; curl_close($ch); return false; } } } $cash=new Cash(); $cash->wxcash({openid},0.01); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看