PHP实现财务审核通过后返现金额到客户
程序员文章站
2022-04-15 14:43:27
应用场景: 有这么一个返现的系统,当前端客户发起提现的时候,后端就要通过审核这笔返现订单,才可以返现到客户的账号里。 来看看下面的截图 这里的业务场景就是经过两轮审核:销售审核,财务审核都通过后,后端就会付款一笔钱到客户,当然,这里财务审核会有很多种情况,不通过与通过,通过后直接付款又有很多种情况, ......
应用场景:
有这么一个返现的系统,当前端客户发起提现的时候,后端就要通过审核这笔返现订单,才可以返现到客户的账号里。
来看看下面的截图
这里的业务场景就是经过两轮审核:销售审核,财务审核都通过后,后端就会付款一笔钱到客户,当然,这里财务审核会有很多种情况,不通过与通过,通过后直接付款又有很多种情况,详细可以查看微信付款到零钱的文档。下面就来看看具体你的代码实现
微信支付配置
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); }