123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\common\library;
- use think\Controller;
- use app\common\model\Config;
- class WxMerchPay extends Controller
- {
-
-
-
- public function __construct()
- {
- $this->appid = Config::get_values('wechat_appid');
- $this->api_key = Config::get_values('wechat_mch_key');
- $this->mch_id = Config::get_values('wechat_mch_id');
- }
-
- function xmltoarray($xml) {
-
- libxml_disable_entity_loader(true);
- $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
- $val = json_decode(json_encode($xmlstring),true);
- return $val;
- }
-
- function arraytoxml($data){
- $str='<xml>';
- foreach($data as $k=>$v) {
- $str.='<'.$k.'>'.$v.'</'.$k.'>';
- }
- $str.='</xml>';
- return $str;
- }
-
- function createNoncestr($length =32){
- $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";
- $str ="";
- for($i=0;$i<$length;$i++){
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
-
- function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
- $isdir = dirname(__DIR__) . "/wxcert/";
- $ch = curl_init();
- 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');
- 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);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
- $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;
- }
- }
-
- function sendMoney($amount,$re_openid,$partner_trade_no,$desc='测试',$check_name=''){
- $total_amount = (100) * $amount;
- $data=array(
- 'mch_appid'=> $this->appid,
- 'mchid'=> $this->mch_id,
- 'nonce_str'=>$this->createNoncestr(),
- 'partner_trade_no'=> $partner_trade_no,
- 'openid'=> $re_openid,
- 'check_name'=>'NO_CHECK',
- 're_user_name'=> $check_name,
- 'amount'=>$total_amount,
- 'desc'=> $desc,
- 'spbill_create_ip'=> request()->ip(),
- );
-
- $secrect_key=$this->api_key;
- $data=array_filter($data);
- ksort($data);
- $str='';
- foreach($data as $k=>$v) {
- $str.=$k.'='.$v.'&';
- }
- $str.='key='.$secrect_key;
- $data['sign']=md5($str);
-
- $xml=$this->arraytoxml($data);
- $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
- $res=$this->curl_post_ssl($url,$xml);
- $return=$this->xmltoarray($res);
- return $return;
-
-
-
-
- }
- }
|