接口文档地址:https://opendoc.gongmall.com/
准备工作:
ip白名单:47.110.242.xxx
工猫提现确认地址
//发起请求提现前,工猫会先调用此接口去确认提现是否合法
http://api.xxx.com/app/notify/gongmao_transfer_sure
工猫提现回调地址
//提现结果通知过来,根据结果处理自己这边业务
http://api.xxx.com/app/notify/gongmao_notify
工猫电子合同返回地址
//获取合同,工猫电子合同返回地址(电签成功返回按钮跳转的地址)
http://api.xxx.com/app/notify/gongmao_contract_url
工猫电子合同回调地址
//电签成功与否,通知我们这边电签状态
http://api.xxx.com/app/notify/gongmao_contract_notify
封装service调用
* @link https://opendoc.gongmall.com/
* @version 0.0.1
* @package 工猫service
* ****************************************************** */
namespace app\common\service;
class Gongmao {
/*
* 电签链接(嵌套)
正式环境
https://openapi.gongmall.com
appKey
571a1624e5f94c91b26426c56e9e3xxx
appSecret
9e595e66101f07e2a1b29d6cc7078xxx
合同地址
https://contract.gongmall.com/url_contract.html?companyId=2VrLwM&positionId=kPxXwP&channel=kPxJzx
测试环境
https://openapi-qa.gongmall.com
appKey
1af0686fffef41a3851c350ad28c6xxx
appSecret
e80507da02572129256ba91390e24xxx
合同地址
https://contract-qa.gongmall.com/url_contract.html?companyId=WVnaGz&positionId=OV8jbM&channel=kPxJzx
*/
public $gongmao_appKey = '571a1624e5f94c91b26426c56e9e3dea';//工猫开发者唯一标识appKey
public $gongmao_appSecret = '9e595e66101f07e2a1b29d6cc70788bd';//工猫开发者密钥appSecret
public $gongmao_contractUrl = 'https://contract.gongmall.com/url_contract.html?companyId=2VrLwM&positionId=kPxXwP&channel=kPxJzx';//合同地址
public $gongmao_url = 'https://openapi.gongmall.com'; //正式地址
//加密数据
public function aes_data($contractUrl = '',$data = []){
//data为AES加密数据
$plaintext = urldecode(http_build_query($data));
//加密key由配置的appKey与appSecret生成
$key = strtoupper(md5($this->gongmao_appKey . $this->gongmao_appSecret));
//偏移量
$size = 16;$iv = str_repeat("\0",$size);
// 添加Padding,使用//PKCS5Padding
$padding = $size - strlen($plaintext) % $size;
$plaintext .= str_repeat(chr($padding), $padding);
//使用AES-192-CBC进行加密
$encrypted = openssl_encrypt($plaintext, 'AES-192-CBC',base64_decode($key),OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
//加密结果
$contractUrl = $contractUrl."&data=".base64_encode($encrypted);
return $contractUrl;
}
//自定义ascii排序
public function ASCII($params = []){
if(!empty($params)){
$p = ksort($params);
if($p){
$str = '';
foreach ($params as $k=>$val){
$arr[] = $k.'='.$val;
}
$arr[] = "appSecret=".$this->gongmao_appSecret;
return implode($arr,"&");
}
}
return '';//参数错误
}
/**
* 获取时间戳到毫秒
* @return bool|string
*/
public function getMillisecond(){
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return substr($msectime,0,13);
}
/*
* POST请求
* @access public
* @param string $url 请求地址
* @param array $data 请求的数据
* @return string $ret
*/
public function curl_post($url,$data){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$ret = curl_exec($ch);
if(!$ret) {
echo "请求出错:".curl_error($ch)."\n";
}else{
// echo "返回数据:$ret\n";
}
return $ret;
}
}
添加银行卡,要跳转电签
编辑银行卡,如果之前没有电签,要跳转电签
发送提现请求
//方法引入gongmaoService
//ajax添加银行卡和编辑银行卡,判断电签状态,如果没有电签。则跳转此方法电签。
/*
* 电子合同
*/
public function contract(){
$id = $this->request->param('id',0);
$data = $this->userBankModel->where(['id'=>$id])->find();
if(!empty($data)){//根据银行卡记录ID找到该条记录
if(!$data['status']){//未签
$post_data['name'] = $data['name'];//姓名
$post_data['mobile'] = $data['mobile'];//手机号
$post_data['certificateType'] = 1;//证件类型 1:身份证
$post_data['idNumber'] = $data['idCard'];//证件号
$post_data['bankNum'] = $data['bankNo'];//银行卡号/ 支付宝账户
$post_data['bankName'] = $data['bankName'];//银行名称,最长50位
$post_data['workNumber'] = $this->user_id;//工号,最长50位,可作为用户唯一标识
$post_data['encodeExtraParam'] = 'test';//附言参数,最长500位,由传入方提供,数据量大时建议使用jsonString格式传输。
$gongmaoService = new Gongmao();
# 电子合同
$r = $gongmaoService->aes_data($gongmaoService->gongmao_contractUrl,$post_data);
header("location:".$r);
}
}
//已经电签,则跳转提现页面
$this->success($msg = '操作成功',$url = url('app/member/cash'));
}
提现页面和动作
/**
* 提现
*/
public function cash()
{
$custom_config = $this->systemModel->where(['name'=>'custom_config'])->value('value');
$custom_config = unserialize($custom_config);
$limit = $custom_config['member_cash_limit_money'] ? $custom_config['member_cash_limit_money'] : 1;
if($this->request->isAjax()){
$money = $this->request->param('money',0);
$bank_id = $this->request->param('bank_id','');
($money < $limit) && $this->response($code = 400 ,$msg = "提现金额不得小于$limit" ,$data = []);
$share_money = $this->shareModel->where('user_id', $this->user_id)->sum('sum');
$withdraw_money = $this->withdrawModel->where(['user_id'=>$this->user_id, 'status'=>['neq', 30]])->sum('money');
$moneyall = $share_money - $withdraw_money;
($money > $moneyall) && $this->response($code = 400 ,$msg = "提现金额不得大于可提现总金额" ,$data = []);
//开始提现---写入记录
$time = time();
$bankcard = $this->userBankModel->where(['id'=>$bank_id])->find();
!$bankcard->status && $this->response($code = 400 ,$msg = "提现银行卡未实名认证,请先认证!",$data = ['url'=>url('app/member/cash_bankcard_edit',['id'=>$bank_id])]);
$data['user_id'] = $this->user_id;
$data['money'] = $money;
$data['status'] = 10;
$data['withdrawSn'] = 'PJH'.$this->user_id.time();
$data['bankNo'] = $bankcard['bankNo'];
$data['idCard'] = $bankcard['idCard'];
$data['mobile'] = $bankcard['mobile'];
$data['name'] = $bankcard['name'];
$data['create_time'] = $time;
$data['update_time'] = $time;
$this->withdrawModel->insert($data);
$this->response($code = 200, $msg = '提现申请已提交,正在审核,敬请等待',$data = ['url'=>url('app/member/earning_detail')]);
}else{
$id = $this->request->param('id',0);
$where['user_id'] = $this->user_id;
$id && $where['id'] = $id;
$user_bank = $this->userBankModel->where($where)->order(['id'=>'desc'])->find();
if (empty($user_bank)) {//提现银行卡没有设置
return $this->cash_bankcard_add();
} else {//读取可提现银行卡和金额
$user_bank['bankIcon'] = $this->userBankModel->getBankIconByName($user_bank['bankName']);
$share_money = $this->shareModel->where(['user_id' => $this->user_id])->sum('sum'); //总收益
$withdraw_money = $this->withdrawModel->where(['user_id' => $this->user_id,'status' => ['neq',30]])->sum('money');//已提现
$money = round($share_money - $withdraw_money,3);//可提现
return $this->fetch('cash',[
'user_bank' => $user_bank ? $user_bank : [],
'money' => $money,
'limit' => $limit
]);
}
}
}
/*
* 查询企业余额
*/
public function balance(){
$gongmaoService = new Gongmao();
$url = $gongmaoService->gongmao_url.'/api/company/getBalance';
$time = time();
$data['appKey'] = $gongmaoService->gongmao_appKey;//开发者唯一标识
$data['nonce'] = md5($time);//随机数,请每次随机产生,保证随机数不可预测
$data['timestamp'] = $time;//当前毫秒时间戳
$data['sign'] = strtoupper(md5($gongmaoService->ASCII($data)));
$res = $gongmaoService->curl_post($url,$data);
dump($res);die;
}
//工猫提现回调地址
public function gongmao_notify(){
$requestId = $this->request->param('requestId',0);//提现记录号
$status = $this->request->param('status',0);//1:成功;2:失败
$failReason = $this->request->param('failReason','');//失败原因
$name = $this->request->param('name','');//姓名
$mobile = $this->request->param('mobile','');//手机号
$amount = $this->request->param('amount',0);//提现金额
$currentTax = $this->request->param('currentTax','');//当次缴纳个税
$currentRealWage = $this->request->param('currentRealWage','');//实发金额
$currentManageFee = $this->request->param('currentManageFee','');//当次提现管理费
$identity = $this->request->param('identity','');//身份证号
$bankName = $this->request->param('bankName','');//银行名称
$bankAccount = $this->request->param('bankAccount','');//银行卡号
$dateTime = $this->request->param('dateTime','');//申请时间
$remark = $this->request->param('remark','');//单据描述
$payTime = $this->request->param('payTime','');//实际付款时间
try{
$where['withdrawSn'] = $requestId;
// $where['name'] = $name;
// $where['mobile'] = $mobile;
// $where['idCard'] = $identity;
// $where['bankNo'] = $bankAccount;
// $where['money'] = $amount;
$data = $this->withdrawModel->where($where)->find();
if($status == 1){
$update_data['status'] = 20;//已支付
$wechat_msg = '提现成功';
}elseif($status == 2){
$update_data['status'] = 30;//提现失败
$update_data['message'] = $failReason;
$wechat_msg = '提现失败';
file_put_contents('./gongmao_notify.php','提现记录id:'.$data['id'].'---'.json_encode($this->request->post())."\n\r",FILE_APPEND);
}else{
$wechat_msg = '状态未知';
}
$update_data['update_time'] = time();
$this->withdrawModel->where(['id'=>$data['id']])->update($update_data);
$wechat_user = $this->wxUserModel->where('userId',$data['user_id'])->order('id','desc')->find();
$url = config('api_url').url('app/member/earning_detail');
$content ="提现结果通知:\n".date('m月d日',time())."\n";
$content .="您有一笔提现申请处理结果如下\n";
$content .="提现金额:".$data['money']."元\n";
$content .="处理结果:".$wechat_msg."\n";
$content .="处理时间:".date('Y-m-d H:i:s', time())."\n";
$content .="提现结果反馈:处理".$wechat_msg."";
$content .="如有疑问,请及时联系客服\n ---------------------------";
$content .="详情";
sendCustomMessage([
'touser' => $wechat_user->wxOpenid,
'text' => [
'content' => $content
],
'msgtype' => 'text'
]);
}catch (\Exception $e){
file_put_contents('./gongmao_notify.php','提现记录id:'.$data['id'].'---'.$e->getMessage()."\n\r",FILE_APPEND);
}
return json(['code'=>200,'msg'=>'工猫提现回调成功']);
}
//工猫提现确认地址
public function gongmao_transfer_sure(){
$requestId = $this->request->param('requestId',0);//提现记录号
$name = $this->request->param('name','');//姓名
$mobile = $this->request->param('mobile','');//手机号
$amount = $this->request->param('amount',0);//提现金额
$identity = $this->request->param('identity','');//身份证号
$bankAccount = $this->request->param('bankAccount','');//银行卡号
$dateTime = $this->request->param('dateTime','');//申请时间
$remark = $this->request->param('remark','');//单据描述
$custom_config = $this->systemModel->where(['name'=>'custom_config'])->value('value');
$custom_config = unserialize($custom_config);
$limit = $custom_config['member_cash_limit_money'] ? $custom_config['member_cash_limit_money'] : 1;
# ['name'=>$name,'mobile'=>$mobile,'idCard'=>$identity,'bankNo'=>$bankAccount]
$withdraw = $this->withdrawModel->where(['withdrawSn'=>$requestId])->find();
file_put_contents('./trans_sure.php',json_encode($withdraw)."\n\r",FILE_APPEND);
if(empty($withdraw)){//接受参数,数据库回查不到数据
return json($data = ['requestId'=>$requestId,'status'=>2,'msg'=>"提现信息核对有误"]);
}
file_put_contents('./trans_sure.php',$amount.'---'.$limit."\n\r",FILE_APPEND);
if($amount < $limit) {//提现金额小于后台设置最小提现金额
return json($data = ['requestId'=>$requestId,'status'=>2,'msg'=>"提现金额不得小于$limit"]);
}
return json($data = ['requestId'=>$requestId,'status'=>1,'msg'=>"工猫提现确认ok"]);
}
//工猫电子合同返回地址(电签成功返回按钮跳转)
public function gongmao_contract_url(){
header("location:".url('app/member/cash_bankcard_list'));
#echo json_encode(['code'=>0,'msg'=>'工猫电子合同返回地址']);
}
//工猫电子合同回调地址
public function gongmao_contract_notify(){
$name = $this->request->param('name','');//姓名
$mobile = $this->request->param('mobile','');//手机号
$workNumber = $this->request->param('workNumber',0);//工号
$identity = $this->request->param('identity','');//身份证号
$salaryAccount = $this->request->param('salaryAccount','');//银行卡号
$status = $this->request->param('status','');//合同状态(1、未签 2、已签)
$bankName = $this->request->param('bankName','');//银行名称
$extraParam = $this->request->param('extraParam','');//附言参数,由传入方提供,回调时将原样返还,可以用来做用户数据标识
try{
$where['name'] = $name;
$where['mobile'] = $mobile;
$where['user_id'] = $workNumber;
$where['idCard'] = $identity;
$where['bankNo'] = $salaryAccount;
$where['bankName'] = $bankName;
$status = $status >1 ? 1 : 0;//更新状态
$r = $this->userBankModel->where($where)->update(['status'=>$status,'create_time'=>time()]);
file_put_contents('./gongmao.php','------ok------'."----".$r."\n\r",FILE_APPEND);
}catch (\Exception $e){
file_put_contents('./gongmao.php','------failed------'."----".$e->getMessage()."\n\r",FILE_APPEND);
}
return json($data = ['code'=>200,'status'=>1,'msg'=>"工猫电子合同回调ok"]);
}