Browse Source

支付与回调

lizhen_gitee 11 tháng trước cách đây
mục cha
commit
3d5c0a0491

+ 255 - 0
application/api/controller/Notify.php

@@ -0,0 +1,255 @@
+<?php
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use addons\epay\library\Service;
+/**
+ * 订单支付回调
+ */
+class Notify extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+
+
+    //充值金币 异步回调对外方法
+    public function recharge_notify_base(){
+
+        //验签
+        $paytype = input('paytype','wechat');
+        $notify_file = $this->notify_log_start($paytype);
+        $pay = Service::checkNotify($paytype);
+        if (!$pay) {
+            echo '签名错误';
+            exit;
+        }
+
+        //验证,拿订单号等信息
+        $data = $pay->verify();
+        $out_trade_no = $data['out_trade_no'];
+        //订单查询
+        $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
+
+        if(empty($info)){
+            echo $pay->success();
+            exit;
+        }
+
+        if($info['order_status'] != 0)
+        {
+            echo $pay->success();
+            exit;
+        }
+        //你可以在此编写订单逻辑
+        $rs = $this->recharge_notify_do($out_trade_no);
+        if($rs === false){
+            //不论结果都应返回success
+            echo $pay->success();
+            exit;
+        }else{
+            //不论结果都应返回success
+            echo $pay->success();
+            exit;
+        }
+
+        //默认
+        echo $pay->success();
+        exit;
+    }
+
+    //充值金币 逻辑
+    private function recharge_notify_do($out_trade_no){
+
+        Db::startTrans();
+        $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
+        if (empty($orderInfo)) {
+            Db::rollback();
+            return false;
+        }
+
+        if($orderInfo['order_status'] != 0){
+            Db::rollback();
+            return false;
+        }
+
+        //逻辑开始
+        //$args = json_decode($orderInfo['args'],true);
+        $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'money',$orderInfo['order_amount'],10, '余额充值','pay_order',$orderInfo['id']);
+        if($result['status']===false)
+        {
+            Db::rollback();
+            return false;
+        }
+        //逻辑结束
+
+
+        //状态
+        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
+        if($ros === false) {
+            Db::rollback();
+            return false;
+        }
+
+        //默认提交
+        Db::commit();
+        return true;
+    }
+
+////////////////////////////////////////////////////////////////////////
+    //充值VIP 异步回调对外方法
+    public function vip_notify_base(){
+
+        //验签
+        $paytype = input('paytype','wechat');
+        $notify_file = $this->notify_log_start($paytype);
+        $pay = Service::checkNotify($paytype);
+        if (!$pay) {
+            echo '签名错误';
+            exit;
+        }
+
+        //验证,拿订单号等信息
+        $data = $pay->verify();
+        $out_trade_no = $data['out_trade_no'];
+        //订单查询
+        $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
+
+        if(empty($info)){
+            echo $pay->success();
+            exit;
+        }
+
+        if($info['order_status'] != 0)
+        {
+            echo $pay->success();
+            exit;
+        }
+        //你可以在此编写订单逻辑
+        $rs = $this->vip_notify_do($out_trade_no);
+        if($rs === false){
+            //不论结果都应返回success
+            echo $pay->success();
+            exit;
+        }else{
+            //不论结果都应返回success
+            echo $pay->success();
+            exit;
+        }
+
+        //默认
+        echo $pay->success();
+        exit;
+    }
+
+    //充值金币 逻辑
+    private function vip_notify_do($out_trade_no){
+
+        Db::startTrans();
+        $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
+        if (empty($orderInfo)) {
+            Db::rollback();
+            return false;
+        }
+
+        if($orderInfo['order_status'] != 0){
+            Db::rollback();
+            return false;
+        }
+
+        //逻辑开始
+        //先充值
+        $args = json_decode($orderInfo['args'],true);
+        $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
+        if($user_info['vip_endtime'] < time()){
+            //过期了
+            $vip_endtime = time() + (intval($args['days']) * 86400);
+            $vip_type = 1;
+        }else{
+            //追加vip
+            $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
+            $vip_type = 2;
+        }
+        $update_data = [
+            'vip_endtime'=>$vip_endtime,
+        ];
+        $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
+
+        //记录日志
+        $log_data = [
+            'user_id' => $orderInfo['user_id'],
+            'before'  => $user_info['vip_endtime'],
+            'change_value'  => intval($args['days']) * 86400,
+            'remain'  => $vip_endtime,
+            'remark'  => '安卓购买vip',
+            'createtime'  => time(),
+            'vip_type'  => $vip_type,
+        ];
+        Db::name('user_vip_log')->insertGetId($log_data);
+
+        if($result === false)
+        {
+            Db::rollback();
+            return false;
+        }
+
+
+        //逻辑结束
+
+        //状态
+        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
+        if($ros === false) {
+            Db::rollback();
+            return false;
+        }
+
+        //默认提交
+        Db::commit();
+        return true;
+    }
+
+
+    //异步日志
+    private function notify_log_start($paytype = 'wechat'){
+        //记录支付回调数据
+        ignore_user_abort(); // run script in background
+        set_time_limit(30);
+        // 日志文件 start
+        $log_base_dir = '../epaylog/'.$paytype.'/';
+        if (!is_dir($log_base_dir))
+        {
+            mkdir($log_base_dir, 0770, true);
+            @chmod($log_base_dir, 0770);
+        }
+        $notify_file = $log_base_dir.'notify.txt';
+        if(!file_exists($notify_file)) {
+            @touch($notify_file);
+            @chmod($notify_file, 0770);
+        }
+        if(filesize($notify_file)>5242880)//大于5M自动切换
+        {
+            rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
+        }
+        if(!file_exists($notify_file)) {
+            @touch($notify_file);
+            @chmod($notify_file, 0770);
+        }
+        // 日志文件 end
+        //开始写入
+        $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
+        if($_REQUEST && $paytype == 'alipay') {
+            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
+        } else {
+            $xml = file_get_contents("php://input");
+            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
+            $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
+            file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
+        }
+
+        ini_set('display_errors','On');
+
+        return $notify_file;
+
+    }
+}

+ 274 - 0
application/api/controller/Pay.php

@@ -0,0 +1,274 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use addons\epay\library\Service;
+/**
+ * 充值配置与充值订单
+ */
+class Pay extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //充值金币 创建订单
+    public function money_recharge(){
+
+        $pay_type  = input('pay_type','wechat');
+        $platform  = 'app';
+        $freemoney = input('freemoney',0);
+        $uid = $this->auth->id;
+
+        if(!$freemoney){
+            $this->error('填写充值金额');
+        }
+
+        if(!$this->user_auth_limit()){
+            $this->error('请先完成实名认证');
+        }
+
+        //赋值money
+        /*if($rc_id){
+            $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
+            $money = $recharge_config['money'] ?: 0;
+            $gold  = $recharge_config['gold'] ?: 0;
+        }*/
+
+        //自由输入覆盖
+        if(!empty($freemoney)){
+            $rc_id = 0;
+            $money = floatval($freemoney);
+
+        }
+
+        //
+        if($money<=0)
+        {
+            $this->error('支付金额必须大于0');
+        }
+        if($money > 10000){
+            $this->error('支付金额太大');
+        }
+
+        //创建订单
+        $data = [];
+        $data['user_id'] = $uid;
+        $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
+        $data['order_amount'] = $money;
+        $data['createtime'] = time();
+
+        $data['pay_type'] = $pay_type;
+        $data['platform'] = $platform;
+        $data['order_status'] = 0;
+        $data['table_name'] = 'money_recharge';
+        $data['table_id'] = 0;
+        $data['args'] = '';
+
+        $orderid = Db::name('pay_order')->insertGetId($data);
+
+        $openid = $this->auth->wechat_openid;
+        //下单
+        $params = [
+            'type'         => $pay_type,
+            'orderid'      => $data['out_trade_no'],
+            'title'        => '支付订单',
+            'amount'       => $data['order_amount'],
+            'method'       => $platform,
+            'openid'       => $openid,
+            'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
+            'returnurl' => '',
+        ];
+
+        $res = Service::submitOrder($params);
+
+        if($pay_type == 'wechat'){
+            $this->success('success',json_decode($res,true));
+        }else{
+            $this->success('success',$res);
+        }
+    }
+
+///////////////////////////////////////////////////////////////
+
+    //金币充值
+    public function gold_config(){
+        $list = Db::name('paygold_webcon')->order('weigh asc,id asc')->select();
+        $data['goldconfig'] = $list;
+
+        $wallet = model('wallet')->getWallet($this->auth->id);
+        $data['wallet'] = $wallet;
+        $this->success('success',$data);
+    }
+
+    //充值金币 创建订单
+    public function gold_recharge(){
+
+        $rc_id     = input('rc_id',0);
+        $pay_type  = input('pay_type','wechat');
+        $platform  = 'app';
+//        $freemoney = input('freemoney',0);
+        $freemoney = 0;
+        $uid = $this->auth->id;
+
+        if(!$rc_id && !$freemoney){
+            $this->error('请选择或填写充值金额');
+        }
+
+        if(!$this->user_auth_limit()){
+            $this->error('请先完成实名认证');
+        }
+
+        //赋值money
+        if($rc_id){
+            $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
+            $money = $recharge_config['money'] ?: 0;
+            $gold  = $recharge_config['gold'] ?: 0;
+        }
+
+        //自由输入覆盖
+        /*if(!empty($freemoney)){
+            $rc_id = 0;
+            $money = floatval($freemoney);
+            $bili = config('rmb_to_gold') ?: 10;
+            $gold  = bcmul($money,$bili,0);
+        }*/
+
+        //
+        if($money<=0)
+        {
+            $this->error('支付金额必须大于0');
+        }
+        if($money > 10000){
+            $this->error('支付金额太大');
+        }
+
+        //充值上限
+        $limit_rs = $this->limit_gift($money);
+        if($limit_rs !== true){
+            $this->error($limit_rs);
+        }
+
+        //创建订单
+        $data = [];
+        $data['user_id'] = $uid;
+        $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
+        $data['order_amount'] = $money;
+        $data['createtime'] = time();
+
+        $data['pay_type'] = $pay_type;
+        $data['platform'] = $platform;
+        $data['order_status'] = 0;
+        $data['table_name'] = 'gold_recharge';
+        $data['table_id'] = 0;
+        $data['args'] = json_encode(['gold'=>$gold]);
+
+        $orderid = Db::name('pay_order')->insertGetId($data);
+
+        $openid = $this->auth->wechat_openid;
+        //下单
+        $params = [
+            'type'         => $pay_type,
+            'orderid'      => $data['out_trade_no'],
+            'title'        => '支付订单',
+            'amount'       => $data['order_amount'],
+            'method'       => $platform,
+            'openid'       => $openid,
+            'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
+            'returnurl' => '',
+        ];
+
+        $res = Service::submitOrder($params);
+
+        if($pay_type == 'wechat'){
+            $this->success('success',json_decode($res,true));
+        }else{
+            $this->success('success',$res);
+        }
+    }
+
+
+
+    //vip用的
+    public function vip_config(){
+        $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
+        $data['vipconfig'] = $list;
+        $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
+        $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
+        $data['avatar'] = localpath_to_netpath($this->auth->avatar);
+        $this->success('success',$data);
+    }
+
+    //vip用的,创建订单
+    public function vip_recharge(){
+
+        $rc_id = input('rc_id',0);
+        $pay_type = input('pay_type','wechat');
+        $platform = 'app';
+        $uid = $this->auth->id;
+
+        if(!$rc_id){
+            $this->error('请选择会员套餐');
+        }
+
+        if(!$this->user_auth_limit()){
+            $this->error('请先完成实名认证');
+        }
+
+
+        //赋值money
+        $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
+        $money = $recharge_config['money'];
+
+        if($money<=0)
+        {
+            $this->error('支付金额必须大于0');
+        }
+        if($money > 10000){
+            $this->error('支付金额太大');
+        }
+
+
+        //创建订单
+        $data = [];
+        $data['user_id'] = $uid;
+        $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
+        $data['order_amount'] = $money;
+        $data['createtime'] = time();
+
+        $data['pay_type'] = $pay_type;
+        $data['platform'] = $platform;
+        $data['order_status'] = 0;
+        $data['table_name'] = 'vip_recharge';
+        $data['table_id'] = 0;
+        $data['args'] = json_encode(['days'=>$recharge_config['days']]);
+
+        $orderid = Db::name('pay_order')->insertGetId($data);
+
+        $openid = $this->auth->wechat_openid;
+        //下单
+        $params = [
+            'type'         => $pay_type,
+            'orderid'      => $data['out_trade_no'],
+            'title'        => '支付订单',
+            'amount'       => $data['order_amount'],
+            'method'       => $platform,
+            'openid'       => $openid,
+            'notifyurl' => config('pay_notify_url').'/api/notify/vip_notify_base/paytype/'.$pay_type,
+            'returnurl' => '',
+        ];
+
+        $res = Service::submitOrder($params);
+
+        if($pay_type == 'wechat'){
+            $this->success('success',json_decode($res,true));
+        }else{
+            $this->success('success',$res);
+        }
+    }
+
+
+
+
+}

+ 5 - 5
application/api/controller/Sms.php

@@ -24,8 +24,8 @@ class Sms extends Api
      */
     public function send()
     {
-        $mobile = $this->request->post("mobile");
-        $event = $this->request->post("event");
+        $mobile = input("mobile");
+        $event = input("event");
         $event = $event ? $event : 'register';
         $event = 'default';
 
@@ -74,10 +74,10 @@ class Sms extends Api
      */
     public function check()
     {
-        $mobile = $this->request->post("mobile");
-        $event = $this->request->post("event");
+        $mobile = input("mobile");
+        $event = input("event");
         $event = $event ? $event : 'register';
-        $captcha = $this->request->post("captcha");
+        $captcha = input("captcha");
 
         if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
             $this->error(__('手机号不正确'));

+ 4 - 4
application/api/controller/Userbank.php

@@ -32,8 +32,8 @@ class Userbank extends Api
         }
 
         //
-        $bank_no = $this->request->request('bank_no');// 银行账号
-        $open_bank = $this->request->request('open_bank');// 开户行
+        $bank_no = input('bank_no');// 银行账号
+        $open_bank = input('open_bank');// 开户行
         if(!$bank_no || !$open_bank ) {
             $this->error("请将信息填写完整");
         }
@@ -96,7 +96,7 @@ class Userbank extends Api
         }
 
         //
-        $payNo = $this->request->request('pay_no');//支付宝账号
+        $payNo = input('pay_no');//支付宝账号
         if(!$payNo) {
             $this->error("请将信息填写完整");
         }
@@ -157,7 +157,7 @@ class Userbank extends Api
         }
 
         //
-        $payNo = $this->request->request('pay_no');//支付宝账号
+        $payNo = input('pay_no');//支付宝账号
         if(!$payNo) {
             $this->error("请将信息填写完整");
         }

+ 1 - 1
application/common.php

@@ -677,7 +677,7 @@ if (!function_exists('get_rand_nick_name')) {
 
     }
 }
-if (!function_exists('get_rand_nick_name')) {
+if (!function_exists('createUniqueNo')) {
     //创建订单号
     function createUniqueNo($prifix = 'P',$id = 0)
     {

+ 13 - 0
application/common/controller/Api.php

@@ -557,4 +557,17 @@ class Api
         }
         return 0;
     }
+
+    //实名认证限制功能
+    //true  不需要实名认证,不受限
+    //false 需要实名认证,受限
+    protected function user_auth_limit(){
+
+        if($this->auth->idcard_status == 1){
+            return true; //已实名,不受限
+        }else{
+            return false;
+        }
+
+    }
 }

+ 8 - 1
application/config.php

@@ -320,7 +320,7 @@ return [
         'redis_selectdb' => 0,
     ],
 
-    //阿里云短信配置,  借用tken
+    //阿里云短信配置,  借用 tken
     'alisms' =>[
         'template_cn' => 'SMS_267360101', //国内
         'sign' => 'TKEN',
@@ -328,6 +328,13 @@ return [
         'secret' => 'MK7sRjd2rpZ4PEbngf76PvKhvpTJWc',
     ],
 
+    //阿里云实名认证二要素,借用 安心聊正式数脉
+    'aliyun_auth_shumai_two' => [
+        'app_key'    => '204562981',
+        'app_secret' => '8F52sWanDJF7pXfrSZVLxIMZeDzWuuff',
+        'app_code'   => 'a4c5dc1c580a4d9dab323286c2780328',
+    ],
+
     //腾讯cos
     /*'cos'                  => [
         'appid' => '',

+ 4 - 0
application/extra/site.php

@@ -31,6 +31,7 @@ return array (
     'userios' => '用户IOS版本',
     'doctorandroid' => '医生安卓版本',
     'doctorios' => '医生IOS版本',
+    'takecash' => '提现配置',
   ),
   'mail_type' => '1',
   'mail_smtp_host' => 'smtp.qq.com',
@@ -72,4 +73,7 @@ return array (
   'doctor_ios_update_title' => '新的版本',
   'doctor_ios_update_info' => '界是你没有挽过的船新版本',
   'kefuphoneno' => '400-1234-1175',
+  'min_takecash_money' => '1',
+  'max_takecash_money' => '10000',
+  'takecash_plat_bili' => '6',
 );