| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- /**
- * 支付宝支付
- */
- namespace app\api\services;
- use think\console\command\optimize\Schema;
- use think\Exception;
- use think\facade\Db;
- use utils\AliPay;
- /**
- * Class AliPayServices
- * @package app\services
- * @method $this data($data)
- * @method $this type(int $type)
- * @method $this trade_type(string $trade_type)
- * @method $this uid(int $uid)
- */
- class AliPayServices extends BasePayServices
- {
- /**
- * 用户id
- * @var
- */
- protected $uid;
- /**
- * 数据体
- * @var
- */
- protected $data;
- /**
- * 支付类型 app jsapi
- * @var string
- */
- protected $trade_type;
- protected static $instance = null;
- public static function instance ()
- {
- if (is_null(self::$instance)) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 统一下单
- * @return mixed
- */
- public function getUnifiedOrder ()
- {
- $uid = $this->uid;
- if (!$uid || !$this->data)
- return api_error_return('下单失败');
- list($body, $total_amount, $order_type, $remarks, $trade_type, $pay_way, $voucher_img) = $this->_payConf($uid, $this->data);
- // 如果是会员订单
- // ["开通会员","38","3","开通会员time2022-01-17 22:14:15","app","1",""]
- // 如果是商品订单
- // ["购买商品",40,"4","20220117224549208049091","app","1",""]
- $out_trade_no = rand(1000000000, 9999999999) . (string)date('ymdhis', time()) . (int)microtime(true); // 订单号
- /** @var AliPay $pay */
- $pay = app()->make(AliPay::class);
- if ($pay->pay_v == 1) {
- $rpm = $pay->unifiedOrder(compact('uid', 'body', 'out_trade_no', 'total_amount'));
- // {
- // "flag": true,
- // "msg": "调用成功",
- // "data": {
- // "result": "alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&alipay_sdk=alipay-sdk-php-2020-04-15&app_cert_sn=01ddde3e4d64f1a43931e5dff9576eed&app_id=2021003113660354&biz_content=%7B%22total_amount%22%3A%2238%22%2C%22subject%22%3A%22%E5%BC%80%E9%80%9A%E4%BC%9A%E5%91%98%22%2C%22out_trade_no%22%3A%2291485501262201171015001642428900%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fhjw.suncorex.com%3A2080%2FaliResult&sign_type=RSA2×tamp=2022-01-17+22%3A15%3A00&version=1.0&sign=kiqVqlWdOhZbbTXv1q4zaCSB3vrYSrJ2%2F9%2FqP7t9KEMrEX%2Fm5XtXYUzaLcVGCFgIL41MzvN9%2FMUdsAA6H0FwwW76diMQMz47yk0E4m2NMo0u9uQ09EAF83jPBwBhMEPTySTIR5aU1SbY%2BW2h1%2B%2BOErUgHiqdUMTeTKh8JrXE9ZsWAbHpQ47OIkke9F136C%2FKvmBmzacfsdlPwu7ikCPV0hLgVphoAp00O1BpQS%2FzwPR5gYkpiVic5HcWy2qZSqEgRTSeS%2FL4WtqzbkN3GT0L%2FmmqI7wGUZNhlacu53djiBCok1Y0TWvj%2BxhqshljuOA5IvtzIljUAFWlvUj8G1znsg%3D%3D"
- // }
- //}
- } else {
- $rpm = $pay->easyUnifiedOrder(compact('uid', 'body', 'out_trade_no', 'total_amount'));
- }
- if (!$rpm['flag']) {
- return api_error_return($rpm['msg']);
- }
- $this->setPaymentOrder(compact('body', 'out_trade_no', 'total_amount', 'remarks', 'uid', 'trade_type', 'order_type', 'pay_way', 'voucher_img'), 2);
- if (in_array($uid,[7523024,7523024])){
- sr_log('shimctest '.$uid. json_encode($rpm));
- }
- sleep('5');
- return api_succ_return(['msg'=>'请求成功', 'data'=>['alipay'=>$rpm['data']['result']]]);
- }
- /**
- * 支付回调
- * @return $this
- */
- public function getNotifyInfo ()
- {
- $pay = new AliPay();
- if ($pay->checkSign($this->data)) {
- if ($this->data['trade_status'] == 'TRADE_SUCCESS') {
- $this->notify_info = $this->data;
- $this->pay_status = true;
- $this->pay_way = 2;
- }
- }
- sr_log('签名错误');
- return $this;
- }
- /**
- * 提现
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function withdrawal ()
- {
- if (empty($this->data['money'])){
- throw new Exception('参数错误');
- }
- if (!preg_match("/^[1-9][0-9]*$/" ,$this->data['money'])){
- throw new Exception('提现金额不能有小数点,不能是负数');
- }
- $uid = $this->uid;
- if (!$uid || !$this->data)
- throw new Exception('提现失败');
- $money = $this->data['money'];
- $user = Db::name('user')->alias('a')->join('user_data b', 'a.id=b.uid')->field('money,status,alipay,real_name,is_auth,level')->where('a.id', $uid)->find();
- if ($user['status'] != 1)
- throw new Exception('账号已被禁用');
- if ($user['is_auth'] != 1)
- throw new Exception('你还未实名认证');
- if (empty($user['alipay']) || empty($user['real_name']))
- throw new Exception('你还未绑定支付宝');
- if ($money <= env('WITHDRAW.ONE_COUNT_MONEY'))
- throw new Exception('单次提现金额要大于'.env('WITHDRAW.ONE_COUNT_MONEY'));
- if ($money > $user['money'])
- throw new Exception('余额不足');
- // $pay_configs = get_config(2); // 获取支付设置
- // if (empty($pay_configs))
- // return api_error_return('后台配置错误');
- //$service = $pay_configs['TRANSFERSERVICE'] ?? 0;
- // $service = $user['vip'] ? 0 : ($user['level'] ? 1 : 3); // vip手续费0,星级达人手续费1%,普通用户3%
- $service_money = $money * (env('WITHDRAW.APP_WITHDRAW_SCALE')) / 100 + env('WITHDRAW.APP_WITHDRAW_BEGIN');
- $practical_money = $money - $service_money; // 最终金额
- // if ($money < $pay_configs['AGENT_REFLECT_MONEY'])
- // return api_error_return('单笔提现金额小于' . $pay_configs['AGENT_REFLECT_MONEY'] . '元');
- // if ($money > $pay_configs['TRANSFERMAX'])
- // return api_error_return('单笔提现金额大于' . $pay_configs['TRANSFERMAX'] . '元');
- $today = Db::name('withdraw_log')
- ->field("
- CASE WHEN id>0 THEN count(id) ELSE 0 END count,
- CASE WHEN id>0 THEN sum(apply_money) ELSE 0 END money
- ")
- ->where('uid', $uid)
- ->where('status', 1)
- ->whereTime('create_at', 'today')
- ->find();
- if ($today['count'] >= env('WITHDRAW.ONE_DAY_MAX_COUNT'))
- throw new Exception('超出今日提现次数');
- if ($today['money'] + $money > env('WITHDRAW.ONE_DAY_MAX_MONEY'))
- throw new Exception('超过今日最大转账金额' . env('WITHDRAW.ONE_DAY_MAX_MONEY') . '元');
- $moth = Db::name('withdraw_log')
- ->field("
- CASE WHEN id>0 THEN count(id) ELSE 0 END count,
- CASE WHEN id>0 THEN sum(apply_money) ELSE 0 END money
- ")
- ->where('uid', $uid)
- ->where('status', 1)
- ->whereTime('create_at', 'moth')
- ->find();
- if ($moth['count'] >= env('WITHDRAW.ONE_MONTH_MAX_COUNT'))
- throw new Exception('超出本月提现次数');
- if ($moth['money'] + $money > env('WITHDRAW.ONE_MONTH_MAX_MONEY'))
- throw new Exception('超过本月最大提现金额' . env('WITHDRAW.ONE_MONTH_MAX_MONEY') . '元');
- $insert_log = [ // 提现流水
- 'uid' => $this->uid,
- 'apply_money' => $money,
- 'status' => 0,
- 'zfb_number' => $user['alipay'],
- 'zfb_name' => $user['real_name'],
- 'channel' => 1,
- 'service_money' => $service_money,
- 'transfer_type' => 1,
- 'practical_money' => $practical_money,
- 'ip' => app('request')->ip(),
- 'mchid' => Db::name('pay_config')->where('channel', 1)->where('status', 1)->value('mchid'), // 商户号
- ];
- edit_user_money(2, $uid, $money);
- $log_id = Db::name('withdraw_log')->insertGetId($insert_log);
- }
- /**
- * 后台提现
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function adminWithdrawal ()
- {
- $uid = $this->uid;
- if (!$uid || !$this->data)
- return api_error_return('提现失败');
- $money = $this->data['apply_money']; // 申请的金额
- $practical_money = $this->data['practical_money']; // 最终金额
- if ($practical_money <= 0)
- return api_error_return('提现金额必须大于0');
- $user = Db::name('user')->field('money,status,is_auth')->where('id', $uid)->find();
- if ($user['status'] != 1)
- return api_error_return('账号已被禁用');
- // if ($user['is_auth'] != 1)
- // api_error_return->json_error('你还未实名认证');
- if ($money <= 0)
- return api_error_return('提现金额要大于0');
- // if ($money > $user['money'])
- // return api_error_return('余额不足');
- // $pay_configs = get_config(2); // 获取支付设置
- // if (empty($pay_configs))
- // return api_error_return('后台配置错误');
- // if ($money < $pay_configs['AGENT_REFLECT_MONEY'])
- // return api_error_return('单笔提现金额小于' . $pay_configs['AGENT_REFLECT_MONEY'] . '元');
- // if ($money > $pay_configs['TRANSFERMAX'])
- // return api_error_return('单笔提现金额大于' . $pay_configs['AGENT_REFLECT_MONEY'] . '元');
- // $today = Db::name('withdraw_log')
- // ->field("
- // CASE WHEN id>0 THEN count(id) ELSE 0 END count,
- // CASE WHEN id>0 THEN sum(apply_money) ELSE 0 END money
- // ")
- // ->where('uid', $uid)
- // ->where('status', 1)
- // ->whereTime('create_at', 'today')
- // ->find();
- // if ($today['count'] >= $pay_configs['TRANSFERMAXDAYNUM'])
- // return api_error_return('超出今日提现次数');
- // if ($today['money'] + $money > $pay_configs['TRANSFERMAXTODAY'])
- // return api_error_return('超过今日最大转账金额' . $pay_configs['TRANSFERMAXTODAY'] . '元');
- // $moth = Db::name('withdraw_log')
- // ->field("
- // CASE WHEN id>0 THEN count(id) ELSE 0 END count,
- // CASE WHEN id>0 THEN sum(apply_money) ELSE 0 END money
- // ")
- // ->where('uid', $uid)
- // ->where('status', 1)
- // ->whereTime('create_at', 'moth')
- // ->find();
- // if ($moth['count'] >= $pay_configs['PERFORMANCE_UPPER'])
- // return api_error_return('超出本月提现次数');
- // if ($moth['money'] + $money > $pay_configs['TRANSFERMAXMONTH'])
- // return api_error_return('超过本月最大转账金额' . $pay_configs['TRANSFERMAXMONTH'] . '元');
- try {
- /** @var AliPay $pay */
- $pay = app()->make(AliPay::class);
- // $pay = new AliPay();
- $para = ['practical_money'=>$this->data['practical_money'],
- 'zfb_name'=>$this->data['zfb_name'],
- 'zfb_number'=>$this->data['zfb_number'],
- 'uid'=>$this->data['uid'],
- 'type'=>'pay'
- ];
- // $para {"practical_money":"7.50","real_name":"王红力","alipay":"18280066008","uid":"7493314"}
- $rpm = $pay->withdrawal($para);
- sr_log('rpm'.json_encode($rpm));
- if ($rpm['flag']) {
- Db::name('withdraw_log')->where('id', $this->data['id'])->update(['status' => 1, 'out_biz_no' => $rpm['data']['out_biz_no'], 'update_at' => date('Y-m-d H:i:s', time()), 'final_transfer_type' => 1]);
- return api_succ_return('提现成功');
- } else {
- Db::name('withdraw_log')->where('id', $this->data['id'])->update(['fail_log' => $rpm['msg'], 'status' => 2]);
- return api_error_return('提现失败');
- }
- } catch (\Exception $e) {
- sr_log('eeee'.$e->getMessage());
- return api_error_return($e->getMessage());
- return api_error_return('提现失败');
- }
- }
- /**
- * 魔术方法
- * @param $name
- * @param $arguments
- * @return $this
- */
- public function __call ($name, $arguments)
- {
- // TODO: Implement __call() method.
- if ($name == 'data') {
- $this->{$name} = $arguments[0];
- } else {
- $this->{$name} = $arguments[0];
- }
- return $this;
- }
- }
|