PayController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * 2018-07-25 韩
  4. */
  5. namespace App\Http\Controllers;
  6. use App\Models\Corps;
  7. use Illuminate\Http\Request;
  8. use DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Pay;
  11. use Log;
  12. use Mail;
  13. use App\Common\Api;
  14. use App\Models\Orders;
  15. use App\Models\AdminVip;
  16. use App\Models\Activitys;
  17. use App\Models\VipConfig;
  18. use App\Models\Admin;
  19. use App\Models\CorpVip;
  20. class PayController extends Controller
  21. {
  22. public function __construct()
  23. {
  24. }
  25. /**
  26. * 微信支付获取二维码URL并下单
  27. * @param Request $request
  28. * @return mixed
  29. */
  30. public function wechatPay(Request $request){
  31. $params = $request->all();
  32. $userInfo = $request->session()->get('userInfo')->toArray();
  33. //若会员等级不在配置表中存在,则视为违规请求
  34. $vip_config = VipConfig::where('vip_level', $params['vip_level'])->first();
  35. if(!$vip_config){
  36. return Api::json(config('code.fail'), trans('msg.vip_not_exist'));
  37. }
  38. //配置参数
  39. $order = [];
  40. $order['out_trade_no'] = date('YmdHis').uniqid();
  41. $price = $vip_config['price'];
  42. $order['total_fee'] = $price * $params['months'];
  43. // //该管理员若存在已生效会员且未到期,则无法购买低版本会员
  44. // $adminVip = AdminVip::where('app_id', $params['app_id'])->where('admin_id', $userInfo['id'])->first();
  45. // if($adminVip){
  46. // if($adminVip['vip_level'] > $params['vip_level'] && $adminVip['vip_end_time'] > time()) {
  47. // return Api::json(config('code.fail'), trans('msg.vip_not_reduce'));
  48. // }
  49. // }
  50. //该企业若存在已生效会员且未到期,则无法购买低版本会员
  51. $CorpVip = CorpVip::where('app_id', $params['app_id'])->where('corp_id', $userInfo['corp_id'])->first();
  52. if($CorpVip){
  53. if($CorpVip['vip_level'] > $params['vip_level'] && $CorpVip['vip_end_time'] > time()) {
  54. return Api::json(config('code.fail'), trans('msg.vip_not_reduce'));
  55. }
  56. }
  57. if($params['vip_level'] == 1){
  58. $order_info = Orders::where('admin_id', $userInfo['id'])
  59. ->where('app_id', $params['app_id'])
  60. ->where('vip_level', $params['vip_level'])
  61. ->where('status', 1)
  62. ->first();
  63. if($order_info){
  64. return Api::json(config('code.fail'), trans('msg.vip_buy_again'));
  65. }
  66. $order['body'] = env('APP_NAME').'体验版('.$params['months'].'个月)';
  67. } elseif($params['vip_level'] == 10) {
  68. $order['body'] = env('APP_NAME').'专业版('.$params['months'].'个月)';
  69. }
  70. //获取支付相关参数
  71. $result = Pay::wechat()->scan($order);
  72. //业务结果成功时订单入库
  73. if($result['result_code'] == 'SUCCESS') {
  74. //订单入库
  75. $query = [];
  76. $query['admin_id'] = $userInfo['id'];
  77. $query['corp_id'] = $userInfo['corp_id'];
  78. $query['app_id'] = $params['app_id'];
  79. $query['vip_level'] = $params['vip_level'];
  80. $query['months'] = $params['months'];
  81. $query['money'] = $order['total_fee'];
  82. $query['order_id'] = $order['out_trade_no'];
  83. $query['create_time'] = time();
  84. $query['update_time'] = time();
  85. $res = Orders::insert($query);
  86. if (!$res) {
  87. return Api::json(config('code.fail'), trans('msg.sql_error'));
  88. }
  89. $data['order_id'] = $order['out_trade_no'];
  90. $data['code_url'] = $result['code_url'];
  91. return Api::json(config('code.success'), trans('msg.success'), $data);
  92. } else {
  93. return Api::json(config('code.service_error'), trans('msg.service_error'));
  94. }
  95. }
  96. /**
  97. * 支付通知接口
  98. * @param Request $request
  99. * @return mixed
  100. */
  101. public function wechatPayNotify(Request $request){
  102. $pay = Pay::wechat();
  103. try{
  104. $data = $pay->verify(); // 是的,验签就这么简单!
  105. Log::debug('Wechat notify', $data->all());
  106. //订单状态修改
  107. $order_info = Orders::where('order_id', $data['out_trade_no'])->first();
  108. if($order_info['status'] == 0){
  109. self::orderDetail($order_info['order_id']);
  110. }
  111. } catch (Exception $e) {
  112. // $e->getMessage();
  113. }
  114. return $pay->success(); // laravel 框架中请直接 `return $pay->success()`
  115. }
  116. /**
  117. * 获取订单支付状态
  118. * @param Request $request
  119. * @return string
  120. */
  121. public function getOrderStatus(Request $request){
  122. $params = $request->all();
  123. if(empty($params['order_id'])){
  124. return Api::json(config('code.params_error'), trans('msg.params_error'));
  125. }
  126. $order_info = Orders::where('order_id', $params['order_id'])->first();
  127. if($order_info) {
  128. //若订单状态为0(未支付),则获取微信订单状态
  129. if($order_info['status'] == 0){
  130. //查询订单状态 参数对照:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
  131. $order_query = Pay::wechat()->find($params['order_id']);
  132. if($order_query['trade_state'] == 'SUCCESS'){ //SUCCESS 支付成功
  133. self::orderDetail($params['order_id']);
  134. $data['status'] = 1;
  135. } else {
  136. $data['status'] = $order_info['status'];
  137. }
  138. } else {
  139. $data['status'] = $order_info['status'];
  140. }
  141. return Api::json(config('code.success'), trans('msg.success'), $data);
  142. } else {
  143. return Api::json(config('code.fail'), trans('msg.order_not_exist'));
  144. }
  145. }
  146. /**
  147. * 获取历史订单
  148. * @param Request $request
  149. * @return string
  150. */
  151. public function getHistoryOrders(Request $request){
  152. $params = $request->all();
  153. $userInfo = $request->session()->get('userInfo');
  154. $userInfo = collect($userInfo)->toArray();
  155. $page_size = empty($params['page_size'])?10:$params['page_size'];
  156. $res = Orders::where('corp_id', $userInfo['corp_id'])
  157. ->where('admin_id', $userInfo['id'])
  158. ->where('status', 1)
  159. ->paginate($page_size);
  160. foreach($res as $k=>$v){
  161. $vipConfig = VipConfig::where('vip_level', $v['vip_level'])->first();
  162. $res[$k]['vip_name'] = $vipConfig['vip_name'];
  163. $res[$k]['money'] = $v['money']/100;
  164. $res[$k]['price'] = $vipConfig['price']/100;
  165. $res[$k]['cost_price'] = $vipConfig['cost_price']/100;
  166. $res[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  167. $res[$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
  168. }
  169. $res = Api::dateFormat($res);
  170. $data = Api::page($res);
  171. return Api::json(config('code.success'), trans('msg.success'), $data);
  172. }
  173. /**
  174. * 获取VIP配置
  175. * @param Request $request
  176. * @return string
  177. */
  178. public function getVipConfig(Request $request){
  179. $res = VipConfig::get()->toArray();
  180. foreach($res as $k=>$v){
  181. $res[$k]['price'] = $v['price']/100;
  182. $res[$k]['cost_price'] = $v['cost_price']/100;
  183. }
  184. return Api::json(config('code.success'), trans('msg.success'), $res);
  185. }
  186. /**
  187. * 订单处理
  188. * @param $order_id
  189. * @return bool
  190. */
  191. public function orderDetail($order_id){
  192. $order_info = Orders::where('order_id', $order_id)->first();
  193. //支付成功,修改订单及相关记录状态
  194. Orders::where('order_id', $order_id)->update(['status'=>1,'update_time'=>time()]);
  195. $is_admin_vip = AdminVip::where('app_id', $order_info['app_id'])->where('admin_id', $order_info['admin_id'])->first();
  196. $result = Redis::setnx($order_id, 1); //流程控制,确保不会二次修改订单
  197. if($result) {
  198. Redis::expire($order_id, 3600);
  199. if (!$is_admin_vip) {
  200. //同步订单信息到AdminVip表
  201. $query = [];
  202. $query['admin_id'] = $order_info['admin_id'];
  203. $query['app_id'] = $order_info['app_id'];
  204. $query['vip_level'] = $order_info['vip_level'];
  205. $query['vip_start_time'] = $order_info['create_time'];
  206. $query['vip_end_time'] = $order_info['create_time'] + ($order_info['months'] * 30 * 3600 * 24);
  207. $query['create_time'] = time();
  208. $query['update_time'] = time();
  209. AdminVip::insert($query);
  210. } else {
  211. $query = [];
  212. if ($is_admin_vip['vip_level'] == $order_info['vip_level']) {
  213. if ($is_admin_vip['vip_end_time'] > time()) {
  214. $query['vip_end_time'] = $is_admin_vip['vip_end_time'] + ($order_info['months'] * 30 * 3600 * 24);
  215. } else {
  216. $query['vip_end_time'] = time() + ($order_info['months'] * 30 * 3600 * 24);
  217. }
  218. } else {
  219. $query['vip_start_time'] = time();
  220. $query['vip_end_time'] = time() + ($order_info['months'] * 30 * 3600 * 24);
  221. }
  222. $query['vip_level'] = $order_info['vip_level'];
  223. AdminVip::where('app_id', $order_info['app_id'])->where('admin_id', $order_info['admin_id'])->update($query);
  224. }
  225. //更新活动表中该管理员创建的活动的vip_level
  226. if (!$is_admin_vip || $is_admin_vip['vip_level'] < $order_info['vip_level']) {
  227. 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']]);
  228. }
  229. //发送邮件通知
  230. $corp_info = Corps::where('corp_id', $order_info['corp_id'])->first();
  231. $admin_info = Admin::where('id', $order_info['admin_id'])->first();
  232. $vip_config_info = VipConfig::where('vip_level', $order_info['vip_level'])->first();
  233. $content="购买公司:".$corp_info['corp_name']."\n"
  234. .'购买用户:'.$admin_info['name']."\n"
  235. .'会员等级:'.$vip_config_info['vip_name']."\n"
  236. .'购买期限:'.$order_info['months']."个月\n"
  237. .'付款金额:'.($order_info['money']/100)."元\n"
  238. .'订 单 号:'.$order_info['order_id']."\n";
  239. Log::info($content);
  240. Mail::raw("$content",function ($message){
  241. $mail_receive = collect(config('mail.mail_receive'))->toArray();
  242. foreach($mail_receive as $k=>$v){
  243. $message->to($k, $v)->subject('会员购买通知');
  244. }
  245. });
  246. }
  247. return true;
  248. }
  249. }
  250. ?>