123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- /**
- * 2018-07-25 韩
- */
-
- namespace App\Http\Controllers;
-
- use App\Models\Corps;
- use Illuminate\Http\Request;
-
- use DB;
- use Illuminate\Support\Facades\Redis;
- use Pay;
- use Log;
- use Mail;
- use App\Common\Api;
- use App\Models\Orders;
- use App\Models\AdminVip;
- use App\Models\Activitys;
- use App\Models\VipConfig;
- use App\Models\Admin;
- use App\Models\CorpVip;
-
- class PayController extends Controller
- {
- public function __construct()
- {
-
- }
-
- /**
- * 微信支付获取二维码URL并下单
- * @param Request $request
- * @return mixed
- */
- public function wechatPay(Request $request){
- $params = $request->all();
-
- $userInfo = $request->session()->get('userInfo')->toArray();
- //若会员等级不在配置表中存在,则视为违规请求
- $vip_config = VipConfig::where('vip_level', $params['vip_level'])->first();
- if(!$vip_config){
- return Api::json(config('code.fail'), trans('msg.vip_not_exist'));
- }
-
- //配置参数
- $order = [];
- $order['out_trade_no'] = date('YmdHis').uniqid();
- $price = $vip_config['price'];
- $order['total_fee'] = $price * $params['months'];
-
- // //该管理员若存在已生效会员且未到期,则无法购买低版本会员
- // $adminVip = AdminVip::where('app_id', $params['app_id'])->where('admin_id', $userInfo['id'])->first();
- // if($adminVip){
- // if($adminVip['vip_level'] > $params['vip_level'] && $adminVip['vip_end_time'] > time()) {
- // return Api::json(config('code.fail'), trans('msg.vip_not_reduce'));
- // }
- // }
- //该企业若存在已生效会员且未到期,则无法购买低版本会员
- $CorpVip = CorpVip::where('app_id', $params['app_id'])->where('corp_id', $userInfo['corp_id'])->first();
- if($CorpVip){
- if($CorpVip['vip_level'] > $params['vip_level'] && $CorpVip['vip_end_time'] > time()) {
- return Api::json(config('code.fail'), trans('msg.vip_not_reduce'));
- }
- }
-
- if($params['vip_level'] == 1){
- $order_info = Orders::where('admin_id', $userInfo['id'])
- ->where('app_id', $params['app_id'])
- ->where('vip_level', $params['vip_level'])
- ->where('status', 1)
- ->first();
- if($order_info){
- return Api::json(config('code.fail'), trans('msg.vip_buy_again'));
- }
- $order['body'] = env('APP_NAME').'体验版('.$params['months'].'个月)';
- } elseif($params['vip_level'] == 10) {
- $order['body'] = env('APP_NAME').'专业版('.$params['months'].'个月)';
- }
-
- //获取支付相关参数
- $result = Pay::wechat()->scan($order);
-
- //业务结果成功时订单入库
- if($result['result_code'] == 'SUCCESS') {
- //订单入库
- $query = [];
- $query['admin_id'] = $userInfo['id'];
- $query['corp_id'] = $userInfo['corp_id'];
- $query['app_id'] = $params['app_id'];
- $query['vip_level'] = $params['vip_level'];
- $query['months'] = $params['months'];
- $query['money'] = $order['total_fee'];
- $query['order_id'] = $order['out_trade_no'];
- $query['create_time'] = time();
- $query['update_time'] = time();
- $res = Orders::insert($query);
- if (!$res) {
- return Api::json(config('code.fail'), trans('msg.sql_error'));
- }
-
- $data['order_id'] = $order['out_trade_no'];
- $data['code_url'] = $result['code_url'];
- return Api::json(config('code.success'), trans('msg.success'), $data);
- } else {
- return Api::json(config('code.service_error'), trans('msg.service_error'));
- }
- }
-
- /**
- * 支付通知接口
- * @param Request $request
- * @return mixed
- */
- public function wechatPayNotify(Request $request){
- $pay = Pay::wechat();
-
- try{
- $data = $pay->verify(); // 是的,验签就这么简单!
-
- Log::debug('Wechat notify', $data->all());
- //订单状态修改
- $order_info = Orders::where('order_id', $data['out_trade_no'])->first();
- if($order_info['status'] == 0){
- self::orderDetail($order_info['order_id']);
- }
- } catch (Exception $e) {
- // $e->getMessage();
- }
-
- return $pay->success(); // laravel 框架中请直接 `return $pay->success()`
- }
-
- /**
- * 获取订单支付状态
- * @param Request $request
- * @return string
- */
- public function getOrderStatus(Request $request){
- $params = $request->all();
- if(empty($params['order_id'])){
- return Api::json(config('code.params_error'), trans('msg.params_error'));
- }
-
- $order_info = Orders::where('order_id', $params['order_id'])->first();
- if($order_info) {
- //若订单状态为0(未支付),则获取微信订单状态
- if($order_info['status'] == 0){
- //查询订单状态 参数对照:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
- $order_query = Pay::wechat()->find($params['order_id']);
- if($order_query['trade_state'] == 'SUCCESS'){ //SUCCESS 支付成功
- self::orderDetail($params['order_id']);
-
- $data['status'] = 1;
- } else {
- $data['status'] = $order_info['status'];
- }
- } else {
- $data['status'] = $order_info['status'];
- }
- return Api::json(config('code.success'), trans('msg.success'), $data);
- } else {
- return Api::json(config('code.fail'), trans('msg.order_not_exist'));
- }
- }
-
- /**
- * 获取历史订单
- * @param Request $request
- * @return string
- */
- public function getHistoryOrders(Request $request){
- $params = $request->all();
-
- $userInfo = $request->session()->get('userInfo');
- $userInfo = collect($userInfo)->toArray();
-
- $page_size = empty($params['page_size'])?10:$params['page_size'];
-
- $res = Orders::where('corp_id', $userInfo['corp_id'])
- ->where('admin_id', $userInfo['id'])
- ->where('status', 1)
- ->paginate($page_size);
- foreach($res as $k=>$v){
- $vipConfig = VipConfig::where('vip_level', $v['vip_level'])->first();
- $res[$k]['vip_name'] = $vipConfig['vip_name'];
- $res[$k]['money'] = $v['money']/100;
- $res[$k]['price'] = $vipConfig['price']/100;
- $res[$k]['cost_price'] = $vipConfig['cost_price']/100;
- $res[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
- $res[$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
- }
-
- $res = Api::dateFormat($res);
- $data = Api::page($res);
- return Api::json(config('code.success'), trans('msg.success'), $data);
- }
-
- /**
- * 获取VIP配置
- * @param Request $request
- * @return string
- */
- public function getVipConfig(Request $request){
- $res = VipConfig::get()->toArray();
- foreach($res as $k=>$v){
- $res[$k]['price'] = $v['price']/100;
- $res[$k]['cost_price'] = $v['cost_price']/100;
- }
- return Api::json(config('code.success'), trans('msg.success'), $res);
- }
-
- /**
- * 订单处理
- * @param $order_id
- * @return bool
- */
- public function orderDetail($order_id){
- $order_info = Orders::where('order_id', $order_id)->first();
- //支付成功,修改订单及相关记录状态
- Orders::where('order_id', $order_id)->update(['status'=>1,'update_time'=>time()]);
- $is_admin_vip = AdminVip::where('app_id', $order_info['app_id'])->where('admin_id', $order_info['admin_id'])->first();
- $result = Redis::setnx($order_id, 1); //流程控制,确保不会二次修改订单
- if($result) {
- Redis::expire($order_id, 3600);
- if (!$is_admin_vip) {
- //同步订单信息到AdminVip表
- $query = [];
- $query['admin_id'] = $order_info['admin_id'];
- $query['app_id'] = $order_info['app_id'];
- $query['vip_level'] = $order_info['vip_level'];
- $query['vip_start_time'] = $order_info['create_time'];
- $query['vip_end_time'] = $order_info['create_time'] + ($order_info['months'] * 30 * 3600 * 24);
- $query['create_time'] = time();
- $query['update_time'] = time();
- AdminVip::insert($query);
- } else {
- $query = [];
- if ($is_admin_vip['vip_level'] == $order_info['vip_level']) {
- if ($is_admin_vip['vip_end_time'] > time()) {
- $query['vip_end_time'] = $is_admin_vip['vip_end_time'] + ($order_info['months'] * 30 * 3600 * 24);
- } else {
- $query['vip_end_time'] = time() + ($order_info['months'] * 30 * 3600 * 24);
- }
- } else {
- $query['vip_start_time'] = time();
- $query['vip_end_time'] = time() + ($order_info['months'] * 30 * 3600 * 24);
- }
- $query['vip_level'] = $order_info['vip_level'];
- AdminVip::where('app_id', $order_info['app_id'])->where('admin_id', $order_info['admin_id'])->update($query);
- }
- //更新活动表中该管理员创建的活动的vip_level
- if (!$is_admin_vip || $is_admin_vip['vip_level'] < $order_info['vip_level']) {
- Activitys::where('corp_id', $order_info['corp_id'])->where('creator', $order_info['admin_id'])->where('app_id', $order_info['app_id'])->update(['vip_level' => $order_info['vip_level']]);
- }
-
- //发送邮件通知
- $corp_info = Corps::where('corp_id', $order_info['corp_id'])->first();
- $admin_info = Admin::where('id', $order_info['admin_id'])->first();
- $vip_config_info = VipConfig::where('vip_level', $order_info['vip_level'])->first();
- $content="购买公司:".$corp_info['corp_name']."\n"
- .'购买用户:'.$admin_info['name']."\n"
- .'会员等级:'.$vip_config_info['vip_name']."\n"
- .'购买期限:'.$order_info['months']."个月\n"
- .'付款金额:'.($order_info['money']/100)."元\n"
- .'订 单 号:'.$order_info['order_id']."\n";
- Log::info($content);
- Mail::raw("$content",function ($message){
- $mail_receive = collect(config('mail.mail_receive'))->toArray();
- foreach($mail_receive as $k=>$v){
- $message->to($k, $v)->subject('会员购买通知');
- }
- });
- }
-
- return true;
- }
- }
- ?>
|