微信公眾號整套邏輯的支付和退款


都已經做兩遍公眾號了,還是有時候總卡殼,今天把所有的問題都遇到的差不多了,咱就好好的總結總結

先說支付問題

申請下來公眾號后,先將后台該填的都填寫好

 

需要用到的公眾號信息

 

 

 

下面就開始要進入代碼狀態了

先說支付:

 

 

 

 需要放在function.php 里的方法

吊起微信支付最重要的方法(一)

 

/** * 直接生成微信jssdk_config * @echo string $jssdk Jssdk_config * @author 5heAtMin9 <sheatming@foxmail.com> */

function wx_jssdk_config($debug='true'){ $getSignPackage = wx_getSignPackage(); $jssdk = '<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>'; $jssdk .= '<script> wx.config({ debug: '.$debug.', appId: "'.$getSignPackage['appId'].'", timestamp: "'.$getSignPackage['timestamp'].'", nonceStr: "'.$getSignPackage['nonceStr'].'", signature: "'.$getSignPackage['signature'].'", jsApiList: ["checkJsApi","onMenuShareTimeline","onMenuShareAppMessage","onMenuShareQQ", "onMenuShareWeibo","onMenuShareQZone","hideMenuItems","showMenuItems","hideAllNonBaseMenuItem","showAllNonBaseMenuItem","translateVoice","startRecord","stopRecord","onVoiceRecordEnd","playVoice","onVoicePlayEnd","pauseVoice","stopVoice","uploadVoice","downloadVoice","chooseImage","previewImage","uploadImage","downloadImage","getNetworkType","openLocation","getLocation","hideOptionMenu","showOptionMenu","closeWindow","scanQRCode","chooseWXPay","openProductSpecificView","addCard","chooseCard","openCard"], }); </script>'; echo $jssdk; }

 

 

 

 

吊起微信支付最重要的方法(二)

/** * 微信支付 * @param string $openid 支付者openid * @param string $url 回調地址 * @param string $od_title 訂單標題 * @param string $od_money 價格 * @param string $od_id 訂單編號 * @return string 狀態 * @author 5heAtMin9 <sheatming@foxmail.com> */
function wx_h5_pay($openid, $url, $od_title,  $od_money, $od_id) { if(is_null($url) || $url == ''){ $url = 'http://'. $_SERVER['HTTP_HOST'].'/weixinpay/notify_url.php'; } $pay = array( 'appid' => C('WX_APPID'),
        'mch_id' => C('WX_MCHID'),
        'nonce_str' => getRandom(32),
        'body' => $od_title,
        'out_trade_no' => $od_id,
        'total_fee' => $od_money * 100,
        'spbill_create_ip' => get_client_ip(),
        'notify_url' => $url,//通知地址
        'trade_type' => 'JSAPI',
        'openid' => $openid, ); ksort($pay); $string =  ToUrlParams($pay).'&key='.C('WX_PAYAPIKEY'); $string = md5($string); $string = strtoupper($string); $pay['sign'] = $string; $postObj = simplexml_load_string(os_post('https://api.mch.weixin.qq.com/pay/unifiedorder', arrayToXml($pay)), 'SimpleXMLElement', LIBXML_NOCDATA); $time = time(); $timeStamp = "$time"; $nonceStr = getRandom(32); $data['package'] = 'prepay_id='.$postObj -> prepay_id; $data['timestamp'] = $timeStamp; $data['nonceStr'] = $nonceStr; $val = 'appId='.C('WX_APPID').'&'; $val .='nonceStr='.$nonceStr.'&'; $val .='package='.$data['package'].'&'; $val .='signType=MD5&'; $val .='timeStamp='.$timeStamp; $stringA = $val.'&key='.C('WX_PAYAPIKEY'); $data['sign'] = strtoupper(MD5($stringA)); $data['val'] = $val; return $data; }

 

 

微信回調的方法:

<?php /** * Created by PhpStorm. * User: Kuoer * Date: 2016/4/9 * Time: 14:41 */

function payok($orderid, $alipayid, $openid, $total_fee, $type) { $postUrl =  'http://'.$_SERVER['HTTP_HOST'].'/index.php?s=/Home/Pay/weixinpay'; $curlPost = 'orderid=' . $orderid . '&alipayid=' . $alipayid . '&openid=' . $openid; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $postUrl);                 //抓取指定網頁
            curl_setopt($ch, CURLOPT_HEADER, 0);                    //設置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);            //要求結果為字符串且輸出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 終止從服務端進行驗證
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_exec($ch);                                 //運行curl
            curl_close($ch); } header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: X-Requested-With'); $xml = file_get_contents("php://input"); $xml = simplexml_load_string($xml); //return (string)$xml->prepay_id;
    $success = (string)$xml->return_code; if ($success == 'SUCCESS') { $out_trade_no = (string)$xml->out_trade_no; $trade_no = (string)$xml->transaction_id; $openid = (string)$xml->openid; $pay = (string)$xml->total_fee; echo payok($out_trade_no, $trade_no, $openid, $pay, 'weixin'); } else { $return_code = (string)$xml->return_code; $return_msg = (string)$xml->return_msg; echo '<xml> <return_code><![CDATA[' . $return_code . ']]></return_code> <return_msg><![CDATA[' . $return_msg . ']]></return_msg> </xml>'; }

 

 

微信回調方法處理方法  需放在function.php里

這兩個方法是處理微信給返回的一個支付成功的數據,這些數據都生成了一個txt文件

function quzhi( $s, $xml ) { $str = explode( '<' . $s . '>', $xml ); $str = explode( '</' . $s . '>', $str[ 1 ] ); return guolv( trim( $str[ 0 ] ) ); } function guolv( $str ) { $s = array( '!', '-', '[', 'CDATA', ']', '<', '>' ); return str_replace( $s, '', $str ); }

 

 

微信回調,我自己設置指定的那個單獨創建的控制器里的方法:

<?php namespace Home\Controller; use Think\Controller; use User\Api\UserApi; class PayController extends HomeController { protected function _initialize(){ /* 讀取站點配置 */
        $config = api('Config/lists'); C($config); //添加配置
 } public function weixinpay(){ $orderid = I('orderid'); $suijishu = rand(100000000,99999999999); $val = 'appid='.C('WX_APPID').'&'; $val .='mch_id='.C('WX_MCHID').'&'; $val .='nonce_str='.$suijishu.'&'; $val .='out_trade_no='.$orderid; $stringA = $val.'&key='.C('WX_PAYAPIKEY'); $url = 'https://api.mch.weixin.qq.com/pay/orderquery'; $post_data = '<xml> <appid>'.C('WX_APPID').'</appid> <mch_id>'.C('WX_MCHID').'</mch_id> <nonce_str>'.$suijishu.'</nonce_str> <out_trade_no>'.$orderid.'</out_trade_no> <sign>'.strtoupper(MD5($stringA)).'</sign> </xml>'; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求結果為字符串且輸出到屏幕上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不驗證證書
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不驗證證書
        $res = curl_exec($ch); curl_close($ch);
    //上面都是固定死的,下面才開始寫你要修改的數據
if(quzhi('trade_state',$res) == 'SUCCESS'){ $orderid = I('orderid'); $data['gwc_danhao'] = I('orderid'); $data['money'] = quzhi('total_fee',$res); $data['liushui_hao'] = quzhi('transaction_id',$res); $on = M('yy_pay')->where($data)->find();//沒有找到這條數據就去執行下面的添加 if(!$on){ $gwc_id['danhao'] = I('orderid'); $gwc_uid = M('yy_goumai')->where($gwc_id)->getField('uid'); $data['uid'] = $gwc_uid; $data['time'] = time(); M('yy_pay')->add($data); $price = M('yy_goumai')->where($gwc_id)->sum('zongjia'); if($price*100 != $data['money']){ $this->error('支付異常'); }else{ $gouwuche = M('yy_goumai')->where($gwc_id)->select(); foreach($gouwuche as $k=>$v){ $gwc_zt['utime']=time(); $gwc_zt['state']=1; M('yy_goumai')->where(array('id'=>$v['id']))->setField($gwc_zt); } } } } } }

 

 

像支付就這些啦,如果有沒說的地方可以聯系我QQ:507285474

 

 

 

 

檢測用戶是否登錄

/** * 檢測用戶是否登錄 * @return integer 0-未登錄,大於0-當前登錄用戶ID * @author 麥當苗兒 <zuojiazi@vip.qq.com> */
function is_login(){ $user = session('user_auth'); if (empty($user)) { return 0; } else { return session('user_auth'); } }

 

 

 檢測用戶是否注冊

 

/** * 檢測用戶是否微信登錄 * @return integer 0-未登錄,大於0-當前登錄用戶ID * @author 5heAtMin9 <sheatming@foxmail.com> */

function is_wx_login(){ $backurl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']; $puid = I('sharecode',0); if(!is_login()){ if(I('get.code')){ $code = I('get.code'); $appid = C('WX_APPID'); $secret = C('WX_APPSECRET'); $api = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code'; $json = os_get($api); $array = json_decode($json, true); //             $openid = $array['openid']; $token = $array['access_token']; //             $where['openid'] = $openid; $user = M('ucenter_member')->where($where)->find(); if(!$user){ //沒注冊
                $api = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $token . '&openid=' . $openid . '&lang=zh_CN'; $json = os_get($api); $array = json_decode($json, true); $tmp = rand(11111111111,99999999999); $access_info = array( 'type' => '2', //1.普通賬號 2.微信賬號 3.手機號 4.郵箱
                    'username' => $tmp, //微信注冊 隨機分配賬號
                    'openid' => $openid,
                    'email' => $tmp.'@rand.com',
                    'status' => 1,
                    'reg_time' => time(), ); $uid = M('ucenter_member') -> add($access_info); $user_info = array( 'uid' => $uid,
                    'openid' => $array['openid'],
                    'nickname' => $array['nickname'],
                    'sex' => $array['sex'],
                    'country' => $array['country'],
                    'city' => $array['city'],
                    'province' => $array['province'],
                    'headimgurl' => $array['headimgurl'],
                    'puid' => $puid,
                    'status' => 1 ); $re = M('member') -> add($user_info); session('user_auth',$uid); $url = str_replace('Home/', '', U('wechat.php?User/login', array('openid' => $openid,'backurl'=>urlencode($backurl),'y' => md5(time().'5heAtMin9')))); redirect($url); //             } else { session('user_auth',$user['id']); //注冊了 跳轉到登陸界面
                $url = str_replace('Home/', '', U('wechat.php?User/login', array('openid' => $openid,'backurl'=>urlencode($backurl),'y' => md5(time().'5heAtMin9')))); redirect($url); return $url; } //return $array;
        } else { $appid = C('WX_APPID'); redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($backurl).'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'); } } else { return is_login(); } }

 

 

 

我們接着說退款:

點擊退款傳過來值,處理一下

最主要的是這個 function.php里的wx_tuikaun()方法  

 public function tuikuan(){ $wher['id'] =I('id'); $data['gwc_danhao'] = M('yy_goumai')->where($wher)->getField('danhao'); $re = M('yy_pay')->where($data)->find(); if($re){ $gwc_arr['danbao'] = $re['gwc_danhao']; $gm = M('yy_goumai')->where($gwc_arr)->find();//退款需要用的商品總價 // $gwc_tui_id = M('yy_goumai')->where(array('id'=>I('id')))->find();//退款需要用的商品單價(不是加上郵費的后的總金額) // dump($gwc_tui_id);die();
            $suijishu = time().rand(111111111,999999999).$gm['danhao'];//退款需要用的隨機數 //1.退款商品的單價(不是加上郵費的后的總金額),2.支付成功后生成的流水單號,3.微信支付平台上要顯示的流水號,(用時間戳 + 隨機數 + 用戶id);4.總價格。
            
            $tuikuan = wx_tuikuan($gm['danjia'],$re['liushui_hao'],$suijishu,$gm['zongjia']); if($tuikuan -> result_code == 'SUCCESS'){ M('yy_pay')->where($data)->setField('tuikuan','1'); $gwc_zt['state'] = '-2'; $gwc_zt['utime'] = time(); $yes = M('yy_goumai')->where($wher)->setField($gwc_zt); if($yes){ $this->success('退款成功'); }else{ $this->error('退款失敗'); } } }else{ $this->error('出錯啦!訂單號參數值不對!'); } }

 

 

function.php  里的  wx_tuikaun()方法

/** * 微信退款 * @param $od_money 退款金額 * @param $od_id 訂單編號 * @return SimpleXMLElement */
function wx_tuikuan( $od_money, $od_id, $back_id, $zhifu ) { $pay = array( 'appid' => C( 'WX_APPID' ),
        'mch_id' => C( 'WX_MCHID' ),
        'nonce_str' => getRandom( 32 ),
        'op_user_id' => C( 'WX_MCHID' ),
        'out_refund_no' => $back_id,
        'transaction_id' => $od_id,
        'refund_fee' => $od_money * 100,
        'total_fee' => $zhifu * 100, ); ksort( $pay ); $string = ToUrlParams( $pay ) . '&key=' . C( 'WX_PAYAPIKEY' ); $string = md5( $string ); $string = strtoupper( $string ); $pay[ 'sign' ] = $string; $tmp = zhifu( 'https://api.mch.weixin.qq.com/secapi/pay/refund', arrayToXml( $pay ) ); // dump($tmp);die();
    $postObj = simplexml_load_string( $tmp, 'SimpleXMLElement', LIBXML_NOCDATA ); return $postObj; }

 

 

/** * 微信退款 * @param $od_money 退款金額 * @param $od_id 訂單編號 * @return SimpleXMLElement */
function wx_refund($od_money, $od_id, $back_id) { $pay = array( 'appid' => C('WX_APPID'),
        'mch_id' => C('WX_MCHID'),
        'nonce_str' => getRandom(32),
        'op_user_id' => C('WX_MCHID'),
        'out_refund_no' => $back_id,
        'out_trade_no' => $od_id,
        'refund_fee' => $od_money * 100,
        'total_fee' => $od_money * 100, ); ksort($pay); $string =  ToUrlParams($pay).'&key='.C('WX_PAYAPIKEY'); $string = md5($string); $string = strtoupper($string); $pay['sign'] = $string; $tmp = zhifu('https://api.mch.weixin.qq.com/secapi/pay/refund', arrayToXml($pay)); $postObj = simplexml_load_string($tmp, 'SimpleXMLElement', LIBXML_NOCDATA); return $postObj; }

 

 

然后好像沒有了!!!!

再見!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM