소스 검색

充值相关

zhangxiaobin 1 년 전
부모
커밋
acd982b699

+ 4 - 4
addons/epay/config.php

@@ -8,11 +8,11 @@ return [
         'content' => [],
         'value' => [
             'appid' => '',
-            'app_id' => '',
-            'app_secret' => '',
+            'app_id' => 'wx1bc47f317884cb40',
+            'app_secret' => '9d382a035255f7901b8a0f76ce129f29',
             'miniapp_id' => 'wx1bc47f317884cb40',
-            'mch_id' => '',
-            'key' => '9d382a035255f7901b8a0f76ce129f29',
+            'mch_id' => '1625774482',
+            'key' => 'DRs2yi80exLRn6tI4cBhISMfj7SPYMQG',
             'mode' => 'normal',
             'sub_mch_id' => '',
             'sub_appid' => '',

+ 141 - 2
application/api/controller/Pay.php

@@ -5,6 +5,7 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use think\Db;
 use addons\epay\library\Service;
+use think\Exception;
 /**
  * 充值配置与充值订单
  */
@@ -62,6 +63,7 @@ class Pay extends Api
 
         //验签
         $paytype = input('paytype','wechat');
+        $func = input('func','order_notify_do');
         $notify_file = $this->notify_log_start($paytype);
         $pay = Service::checkNotify($paytype);
         if (!$pay) {
@@ -85,9 +87,9 @@ class Pay extends Api
             return $pay->success()->send();
             exit;
         }
-
+        //$out_trade_no = 'R230605144624823142892';
         //你可以在此编写订单逻辑
-        $rs = $this->order_notify_do($out_trade_no);
+        $rs = $this->$func($out_trade_no);
         if($rs === false){
             //不论结果都应返回success
             return $pay->success()->send();
@@ -195,6 +197,143 @@ class Pay extends Api
 
 
 
+    //微信小程序充值
+    public function pay_recharge()
+    {
+        Db::startTrans();
+        try {
+            $pay_type = input('pay_type','wechat');
+            $platform = input('platform','miniapp');
+            $id = input('id','0');
+            $amounts = input('amounts','0.00');
+            $uid = $this->auth->id;
+            $companyId = $this->auth->company_id;
+            if (empty($id) && empty($amounts)) {
+                throw new Exception('参数错误');
+            }
+            $gift_amount = 0.00;
+            if (!empty($id)) {//验证充值配置
+                $where['status'] = 1;//上架
+                $orderinfo = Db::name('recharge_config')->where('id',$id)->where($where)->find();
+                if (empty($orderinfo)) {
+                    throw new Exception('未获取到充值套餐信息');
+                }
+                $order_amount = $orderinfo['price'];
+                $gift_amount = $orderinfo['giftprice'];
+            } else {
+                $isInt = is_int($amounts);
+                if (!$isInt) {
+                    throw new Exception('请输入整数');
+                }
+                if ($amounts < 1) {
+                    throw new Exception('充值金额有误');
+                }
+                $order_amount = $amounts;
+            }
+
+            //创建订单
+            $data['company_id'] = $companyId;
+            $data['user_id'] = $uid;
+            $data['out_trade_no'] = createUniqueNo('R',$uid); // 数据库订单号加密
+            $data['order_amount'] = $order_amount;
+            $data['gift_amount'] = $gift_amount;
+            $data['createtime'] = time();
+            $data['pay_type'] = $pay_type;
+            $data['order_status'] = 0;
+            $data['table_name'] = 'recharge_config';
+            $data['table_id'] = $id;
+
+            //$orderid = Db::name('pay_order')->insertGetId($data);
+
+            $openid = $this->auth->mini_openid;
+            $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
+            //下单
+            $params = [
+                'type'         => $pay_type,
+                'orderid'      => $data['out_trade_no'],
+                'title'        => '充值',
+                'amount'       => $data['order_amount'],
+                'method'       => $platform,
+                'openid'       => $openid,
+                'notifyurl' => $this->httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/recharge',
+                'returnurl' => '',
+            ];
+
+            $res = Service::submitOrder($params);
+            Db::commit();
+            if($pay_type == 'wechat'){
+                $this->success('success',json_decode($res,true));
+            }else{
+                $this->success('success',$res);
+            }
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 充值到账
+     * @param $out_trade_no
+     * @return bool
+     */
+    private function recharge($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;
+        }
+        $userWalletWhere['user_id'] = $orderInfo['user_id'];
+        $userWalletWhere['company_id'] = $orderInfo['company_id'];
+        $userWalletData = Db::name('user_wallet')->where($userWalletWhere)->find();
+        $before = isset($userWalletData['money']) ? $userWalletData['money'] : 0.00;
+        $changeValue = bcadd($orderInfo['order_amount'],$orderInfo['gift_amount'],2);
+        $remain = bcadd($before,$changeValue,2);
+        $time = time();
+        //逻辑开始
+        $userMoneyLogData = [
+            'user_id'      => $orderInfo['user_id'],
+            'company_id'   => $orderInfo['company_id'],
+            'log_type'     => 104, //日志类型 104
+            'before'       => $before, //之前余额
+            'change_value' => $changeValue, //变动金额
+            'remain'       => $remain, //剩余金额
+            'table'        => 'pay_order', //数据来源
+            'table_id'     => $orderInfo['id'], //数据来源ID
+            'remark'       => '充值', //remark
+            'createtime'   => $time,
+        ];
+        $userMoneyLogRes = Db::name('user_money_log')->insertGetId($userMoneyLogData);
+        if (!$userMoneyLogRes) {
+            throw new Exception('充值记录失败');
+        }
+        $update = [
+            'money' => $remain,
+            'updatetime' => $time,
+        ];
+        $rs_order = Db::name('user_wallet')->where($userWalletWhere)->update($update);
+        if(!$rs_order){
+            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;
+    }
 
 }

+ 92 - 0
application/api/controller/Recharge.php

@@ -0,0 +1,92 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use think\Exception;
+
+class Recharge extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+
+    //首页
+    public function getList(){
+
+        $where = [
+            'company_id' => $this->auth->company_id,
+            'status'     => 1,
+        ];
+        $field = 'id,price,giftprice';
+        $list = Db::name('recharge_config')->field($field)->where($where)->order('id desc')->select();
+
+        //追加赠送
+        /*if(!empty($list)){
+            $config_ids = array_column($list,'id');
+            $gift = Db::name('recharge_gift')->alias('gift')
+                ->field('gift.*,coupons.name,coupons.info,coupons.days')
+                ->join('coupons','gift.coupon_id = coupons.id','LEFT')
+                ->where('gift.config_id','IN',$config_ids)
+                ->where('coupons.status',1)
+                ->select();
+
+            foreach($list as $key => &$val){
+                $val['gift'] = [];
+                foreach($gift as $k => $v){
+                    if($val['id'] == $v['config_id']){
+                        $val['gift'][] = $v;
+                    }
+                }
+            }
+        }*/
+        $this->success(1,$list);
+    }
+
+    /**
+     * 充值
+     * @return void
+     */
+    /*public function add()
+    {
+        Db::startTrans();
+        try {
+            //验证参数
+            $id = $this->request->param('id',0);
+            $amounts = $this->request->param('amounts',0.00);
+            if (empty($id) && empty($amounts)) {
+                throw new Exception('参数错误');
+            }
+            if (!empty($id)) {
+
+            } else {
+                $isInt = is_int($amounts);
+                if (!$isInt) {
+                    throw new Exception('请输入整数');
+                }
+                if ($amounts < 1) {
+                    throw new Exception('充值金额有误');
+                }
+            }
+
+            $userId = $this->auth->id;
+            $time = time();
+            $data = [
+                'car_number' => $this->request->param('car_number', ''),
+                'car_model'  => $this->request->param('car_model', ''),
+            ];
+            $data['user_id'] = $userId;
+            $data['createtime'] = $time;
+            $res = $this->model->insertGetId($data);
+            if (!$res) {
+                throw new Exception('操作失败');
+            }
+            Db::commit();
+            $this->success('操作成功');
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+    }*/
+}

+ 43 - 0
application/api/controller/User.php

@@ -646,4 +646,47 @@ class User extends Api
             $this->error($e->getMessage());
         }
     }
+    
+    /**
+     * 获取用户信息
+     * @return void
+     */
+    public function getInfo()
+    {
+        try {
+            $userInfo = $this->auth->getUserinfo();
+            
+            $this->success('获取成功',$userInfo);
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 小程序码
+     * @return void
+     */
+    public function getMiniCode()
+    {
+        try {
+            $companyId = 3;
+            $tk = getAccessToken();
+            $urlss = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk;
+            $ch = curl_init();
+            $dataArr = ["page"=>"pages/index/index","env_version"=>"develop", "scene"=>"shopId=".$companyId];
+            $datass = json_encode($dataArr);
+            curl_setopt($ch, CURLOPT_URL, $urlss);
+            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
+            curl_setopt($ch, CURLOPT_POSTFIELDS, $datass);
+            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+            curl_setopt($ch, CURLOPT_HEADER, false);
+            $output = curl_exec($ch);
+            curl_close($ch);
+            $this->success('获取成功',$output);
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
 }

+ 55 - 0
application/api/controller/UserMoneyLog.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+use think\Exception;
+
+class UserMoneyLog extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+    protected $model = null;
+
+    public function _initialize()
+    {
+        parent::_initialize();
+        $this->model = Db::name('user_money_log');
+    }
+
+    /**
+     * 列表
+     * @return void
+     */
+    public function getList()
+    {
+        try {
+            $status = $this->request->param('status',1);//状态:1待使用,2已失效
+            $userId = $this->auth->id;
+            $companyId = $this->auth->company_id;
+            $field = 'id,coupon_name,coupon_info,endtime,number,remain';
+            $where['user_id'] = $userId;
+            $where['company_id'] = $companyId;
+            $whereOr = [];
+            if ($status == 1) {
+                $where['remain'] = ['gt',0];
+            } else {
+                $whereOr['remain'] = ['elt',0];
+                $whereOr['endtime'] = ['lt',time()];
+            }
+            $result = $this->model->field($field)->where($where)->where(function($query) use ($whereOr){
+                $query->whereOr($whereOr);
+            })->order('createtime desc')->autopage()->select();
+
+            if (!empty($result)) {
+                foreach ($result as $key => &$value) {
+                    !empty($value['endtime']) && $value['endtime'] = date('Y.m.d H:i:s',$value['endtime']);
+                }
+            }
+            $this->success('获取成功',$result);
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
+}

+ 60 - 0
application/common.php

@@ -1052,4 +1052,64 @@ if(!function_exists('getProvince')) {
         $params['full_address']  = $params['province_name'].$params['city_name'].$params['area_name'].$address;
         return $params;
     }
+}
+if(!function_exists('httpRequest')) {
+    /**
+     * CURL请求
+     * @param $url 请求url地址
+     * @param $method 请求方法 get post
+     * @param null $postfields post数据数组
+     * @param array $headers 请求header信息
+     * @param bool|false $debug 调试开启 默认false
+     * @return mixed
+     */
+    function httpRequest($url, $method, $postfields = null, $headers = array(), $debug = false)
+    {
+        $method = strtoupper($method);
+        $ci     = curl_init();
+        /* Curl settings */
+        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
+        curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
+        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
+        curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
+        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
+        switch ($method) {
+            case "POST":
+                curl_setopt($ci, CURLOPT_POST, true);
+                if (!empty($postfields)) {
+                    $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
+                    curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
+                }
+                break;
+            default:
+                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
+                break;
+        }
+        $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
+        curl_setopt($ci, CURLOPT_URL, $url);
+        if ($ssl) {
+            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
+            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
+        }
+        //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
+        //curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
+        curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
+        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
+        /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
+        $response    = curl_exec($ci);
+        $requestinfo = curl_getinfo($ci);
+        $http_code   = curl_getinfo($ci, CURLINFO_HTTP_CODE);
+        if ($debug) {
+            echo "=====post data======\r\n";
+            var_dump($postfields);
+            echo "=====info===== \r\n";
+            print_r($requestinfo);
+            echo "=====response=====\r\n";
+            print_r($response);
+        }
+        curl_close($ci);
+        return $response;
+        //return array($http_code, $response,$requestinfo);
+    }
 }

+ 1 - 1
application/common/library/Auth.php

@@ -26,7 +26,7 @@ class Auth
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score'];
+    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score','company_id'];
 
     public function __construct($options = [])
     {

+ 16 - 0
paylog/wechat/notify.txt

@@ -0,0 +1,16 @@
+
+
+2023-06-05 15:59:04 [notify][入口接收php://input流原始数据] 
+
+
+2023-06-05 15:59:04 [notify][入口接收php://input流] false
+
+2023-06-05 16:06:07 [notify][入口接收php://input流原始数据] 
+
+
+2023-06-05 16:06:07 [notify][入口接收php://input流] false
+
+2023-06-05 17:31:01 [notify][入口接收php://input流原始数据] 
+
+
+2023-06-05 17:31:01 [notify][入口接收php://input流] false