使用Payment为第三方支付SDK
首先头部引入:use Payment\Client\Transfer;
public function transfer_agent(){
$data = $this->request->param();
//处理业务逻辑
//1、空值校验
//2、根基实际业务,检查提现金额是否超过可提金额
//3、写入一条数据到提现数据库
// 1、空值校验
if(empty($data['store_id'])){
ReturnAjax('', '店铺id不能为空', 2001);
}
if(empty($data['openid'])){
ReturnAjax('', 'openid不能为空', 2001);
}
if(empty($data['true_name'])){
ReturnAjax('', '真实姓名不能为空', 2001);
}
if(empty($data['price'])){
ReturnAjax('', '金额不能为空', 2001);
}
$user_id = $this->admin_user;
$store_id = $data['store_id'];
// 2、检查提现金额是否
$allow_cash = $this->allow($user_id,$store_id);
// dump($allow_cash);die;
if($data['price'] > $allow_cash['allow_cash']){
ReturnAjax('', '金额不可超过可提现金额', 2001);
}
// 防止频繁操作
$end_time = Db::name('store_agent_cash')->where(['user_id'=>$user_id])->order('id desc')->value('create_time');
$now_time = time();
$allow_time = $now_time - $end_time;
if($allow_time < 5){
ReturnAjax('', '操作频繁,请稍后再试', 2001);
}
// dump($allow_cash);
// dump($data['price']);die;
if($data['price'] < 10){
ReturnAjax('', '提现金额不能小于10元', 2001);
}
$store_name = Db::name('store')->where(['id'=>$store_id])->value('store');
$now = time();
$transNo = $now . rand(1000, 9999);//提现订单号
$adds = [
'store_id' => $store_id,
'price' => $data['price'],
'user_id' => $user_id,
'true_name' => $data['true_name'],
'openid' => $data['openid'],
'status' => 0,
'remarks' => $user_id.'从【'.$store_name.'】提现',
'trans_no' => $transNo,
'create_time' => time(),
];
// halt($add);
$st_a_cash_id = Db::name('store_agent_cash')->insertGetId($adds);
//--------------------------------------------------------------
// halt($a);
if ($st_a_cash_id > 0) {
$config = config('wechat.wx_pay');
// halt($re_u_cash);
$datas = [
'nonce_str' => 'zhongao2020',
'channel' => 'account',
'trans_no' => $transNo, //付款订单号,自己生成
'openid' => $data['openid'],
'check_name' => 'FORCE_CHECK', //NO_CHECK:不校验真实姓名;FORCE_CHECK:强校验真实姓名
'payer_real_name' => $data['true_name'], //收款用户真实姓名。如果check_name设置为FORCE_CHECK,则必填用户真实姓名
'amount' => $data['price'], //企业付款金额,单位为元
'desc' => '从【智克艺术】-【'.$store_name.'】提现', //企业付款备注,必填。注意:备注中的敏感词会被转成字符*
'client_ip' => '47.104.64.239',
];
// halt($st_a_cash_id);
//-------------------------------------------------------------------------------------
$ret = Transfer::run(Config::WX_TRANSFER, $config, $datas);
// -------------------------------------------------------------------------------------
if (!empty($ret)) {
$res = json_encode($ret, JSON_UNESCAPED_UNICODE);
// 更换为数组
$res = json_decode($res, true);
if($res['is_success'] == 'T'){
$ups = [];
$ups['transaction_id'] = $res['response']['transaction_id'];
$ups['pay_date'] = $res['response']['pay_date'];
$ups['status'] = 1;
Db::name('store_agent_cash')->where(['id'=>$st_a_cash_id])->update($ups);
ReturnAjax($res, '提现成功', 2000);
}
if($res['is_success'] == 'F'){
Db::name('store_agent_cash')->where(['id'=>$st_a_cash_id])->update(['is_del'=>1]);
ReturnAjax($res, $res['error'], 2001);
}
}else{
Db::name('store_agent_cash')->where(['id'=>$st_a_cash_id])->update(['is_del'=>1]);
ReturnAjax($ret, '调取支付失败', 2001);
}
}
}
网友评论