應用場景: 有這麼一個返現的系統,當前端客戶發起提現的時候,後端就要通過審核這筆返現訂單,才可以返現到客戶的賬號里。 來看看下麵的截圖 這裡的業務場景就是經過兩輪審核:銷售審核,財務審核都通過後,後端就會付款一筆錢到客戶,當然,這裡財務審核會有很多種情況,不通過與通過,通過後直接付款又有很多種情況, ...
應用場景:
有這麼一個返現的系統,當前端客戶發起提現的時候,後端就要通過審核這筆返現訂單,才可以返現到客戶的賬號里。
來看看下麵的截圖
這裡的業務場景就是經過兩輪審核:銷售審核,財務審核都通過後,後端就會付款一筆錢到客戶,當然,這裡財務審核會有很多種情況,不通過與通過,通過後直接付款又有很多種情況,詳細可以查看微信付款到零錢的文檔。下麵就來看看具體你的代碼實現
微信支付配置
1,數據表大概如下
CREATE TABLE `zmq_weixin_config` ( `id` int(1) unsigned NOT NULL AUTO_INCREMENT COMMENT '微信公眾平臺編號,自增id', `weixin_name` varchar(10) NOT NULL COMMENT '微信公眾平臺名稱', `token` varchar(100) NOT NULL, `appid` char(18) NOT NULL, `appsecret` char(32) NOT NULL, `access_token` varchar(300) NOT NULL, `api_ticket` varchar(300) DEFAULT NULL COMMENT '微信卡包api_ticket', `api_ticket_expired_at` datetime DEFAULT NULL COMMENT '微信卡包api_ticket過期時間', `mchid` varchar(20) DEFAULT NULL COMMENT '商戶號', `mchkey` varchar(50) DEFAULT NULL COMMENT '支付密鑰', `expired_at` timestamp NULL DEFAULT NULL COMMENT 'access_token過期時間,會自動更新', `updated_at` timestamp NULL DEFAULT NULL COMMENT '記錄更新時間', `created_at` timestamp NULL DEFAULT NULL COMMENT '創建時間', `sort_order` smallint(5) NOT NULL DEFAULT '0' COMMENT '排序', `points_url` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='微信配置表';
2 .config的配置方法
/** * 獲取微信支付配置 * 這裡我是把配置信息存儲在數據表裡,方便調用 * $param是從controller里傳值過來:weixin_config_id,notify_url * @return array */ public function getWechatConfig($param) { $weixin = WeixinConfigBaseModel::find($param['weixin_config_id']); if (empty($weixin)) { throw new Exception('微信配置ID錯誤'); } return [ 'wechat' =>[ $app_id => $weixin->appid, 'mch_id' => $weixin->mchid, 'notify_url' => empty($param['notify_url']) ? '' : $param['notify_url'], //回調url 'key' => $weixin->mchkey, 'cert_client' => resource_path().'/wechat/'.$weixin->id.'/apiclient_cert.pem', //證書與key 'cert_key' => resource_path().'/wechat/'.$weixin->id.'/apiclient_key.pem', ] ]; }
企業付款到個人零錢核心代碼
/** * Function:企業付款到個人零錢 * Author:cyw0413 * @param $openid * @param $trade_no * @param $money * @param $desc * @return array */ public function weixinPay($input){ $config = $this->getWechatConfig($input); $params["mch_appid"]= $config['wechat']['app_id']; $params["mchid"] = $config['wechat']['mch_id']; $params["nonce_str"]= date("YmdHis").mt_rand(100,999); $params["partner_trade_no"] = $input['trade_no']; //商戶訂單號 $params["amount"] = $input['amount']; $params["desc"] = $input['desc']; $params["openid"] = $input['openid']; $params["check_name"]= 'NO_CHECK'; $params['spbill_create_ip'] = $_SERVER['SERVER_ADDR']; //生成簽名 $str = 'amount='.$params["amount"].'&check_name='.$params["check_name"].'&desc='.$params["desc"].'&mch_appid='.$params["mch_appid"].'&mchid='.$params["mchid"].'&nonce_str='.$params["nonce_str"].'&openid='.$params["openid"].'&partner_trade_no='.$params["partner_trade_no"].'&spbill_create_ip='.$params['spbill_create_ip'].'&key='.$config['wechat']['key']; //md5加密 轉換成大寫 $sign = strtoupper(md5($str)); //生成簽名 $params['sign'] = $sign; //構造XML數據 $xmldata = $this->array_to_xml($params); //數組轉XML $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //發送post請求 $res = $this->curl_post_ssl($url, $xmldata, $input['weixin_config_id']); //curl請求 if(!$res){ throw new \Exception("伺服器連接失敗"); } //付款結果分析 $content = $this->xml_to_array($res); //xml轉數組 return $content; } /** * curl請求 **/ public function curl_post_ssl($url, $xmldata, $weixin_config_id,$second=30,$aHeader=[]){ $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); //預設格式為PEM,可以註釋 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); //絕對地址可使用 dirname(__DIR__)列印,如果不是絕對地址會報 58 錯誤 curl_setopt($ch,CURLOPT_SSLCERT, resource_path().'/wechat/'.$weixin_config_id.'/apiclient_cert.pem'); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY,resource_path().'/wechat/'.$weixin_config_id.'/apiclient_key.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"; //die(); curl_close($ch); return false; } } /** * array 轉 xml * 用於生成簽名 */ public function array_to_xml($arr){ $str='<xml>'; foreach($arr as $k=>$v) { $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str.='</xml>'; return $str; } /** * xml 轉化為array */ public function xml_to_array($xml){ //禁止引用外部xml實體 libxml_disable_entity_loader(true); $xmlString = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlString),true); return $val; }
財務審核,也就是微信返現到零錢,這個時候會返回成功結果,或者是各種不成功的結果
這裡我用一個方法封裝
//財務審核 if($param['status'] == 2){ //判斷返現金額與修改後的金額 if($before_rebate_amount != $param['rebate_amount']){ //返現金額不相等,則出款金額改變 $out_amount = $param['rebate_amount'] - $before_rebate_amount ; $this->outMount($business->business_id,$out_amount); } if($param['rebate_status'] == 9){ //財務拒絕通過 $business->audit_status = $param['rebate_status']; $business->rebate_amount = $param['rebate_amount']; $business->status = 6; $business->save(); //生成日誌 $this->insertWithdrawLog($param['withdraw_id'], $business->status, $business->audit_status, $param['rebate_remark'], $param['admin_id']); }else{ //提現的各種返回結果 $this->payReturnResult($business,$param); } }
/** * Function:微信提現返回的各種結果 * Author:cyw0413 * @param $res * @param $business * @param $param */ public function payReturnResult($business,$param) { $input = [ 'weixin_config_id' => 20 , 'openid' => $business->business->open_id, 'amount' => $param['rebate_amount'] * 100, 'trade_no' => $business->order_sn, //商戶訂單號 'desc' => "微信提現" ]; $pay = new PayLogBaseService(); $res = $pay->weixinPay($input); if($res['result_code']=="SUCCESS"){ //提現成功 $business->audit_status = 4; $business->status = 4; $business->rebate_amount = $param['rebate_amount']; $param['rebate_remark'] = "已付款(".$param['rebate_amount'].")"; }elseif ($res['err_code'] == "MONEY_LIMIT"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,已達到付款給此用戶額度上限"; //throw new \Exception($param['rebate_remark']); }elseif ($res['err_code'] == "AMOUNT_LIMIT"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,低於最低付款金額或者高於最高付款金額"; //throw new \Exception($param['rebate_remark']); egdf }elseif ($res['err_code'] == "NOTENOUGH"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,付款帳號餘額不足或資金未到賬"; //throw new \Exception($param['rebate_remark']); }elseif ($res['err_code'] == "SIGN_ERROR"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,簽名錯誤"; }elseif ($res['err_code'] == "PARAM_ERROR"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,參數錯誤"; }elseif ($res['err_code'] == "OPENID_ERROR"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,Openid錯誤"; }elseif ($res['err_code'] == "FATAL_ERROR"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,兩次請求參數不一致"; }elseif ($res['err_code'] == "CA_ERROR"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,商戶API證書校驗出錯"; }elseif ($res['err_code'] == "V2_ACCOUNT_SIMPLE_BAN"){ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,無法給非實名用戶付款"; }else{ $business->audit_status = 3; $business->status = 3; $param['rebate_remark'] = "提現失敗,伺服器繁忙,請稍後再試"; //throw new \Exception($param['rebate_remark']); } $business->save(); }
當微信平臺餘額不足或者出現各種錯誤而提現失敗的時候,這裡還有支持重新付款的功能:其實就是點擊按鈕後重新調用付款到零錢的功能,知道成功付款
/** * Function:重新付款 * Author:cyw0413 * @param $param * @throws \Exception */ public function repay($param) { if(empty($param)){ throw new \Exception("參數錯誤"); } $business = GroupBusinessWithdrawBaseModel::find($param['withdraw_id']); if(empty($business)){ throw new \Exception("不存在!"); } if($business->audit_status != 3){ throw new \Exception("狀態有錯誤"); } //提現的各種返回結果 $this->payReturnResult($business,$param); }