MonthOrderController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/7/25
  6. * Time: 10:29
  7. */
  8. namespace App\Http\Controllers\Wap\Pay;
  9. use App\Jobs\SendTexts;
  10. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  11. use App\Modules\SendOrder\Services\SendOrderService;
  12. use App\Modules\Subscribe\Models\Order;
  13. use App\Modules\Subscribe\Services\OrderService;
  14. use App\Modules\Subscribe\Services\YearOrderService;
  15. use App\Modules\User\Services\ReadRecordService;
  16. use App\Modules\User\Services\UserDivisionCpcPropertyService;
  17. use App\Modules\User\Services\UserMonthService;
  18. use App\Modules\User\Services\UserService;
  19. use GuzzleHttp\Client;
  20. use Illuminate\Routing\Controller;
  21. use Illuminate\Http\Request;
  22. use Log;
  23. use Redis;
  24. use DB;
  25. use EasyWeChat\Support\XML;
  26. class MonthOrderController extends Controller
  27. {
  28. public function signCallBack(Request $request)
  29. {
  30. Log::info('signCallBack------------------------------------------signCallBack');
  31. Log::info($request->all());
  32. Log::info($request->getMethod());
  33. //$key = 'sn7wluq5716brp8fzm';
  34. $key = env('MONTH_ORDER_KEY');
  35. $app_id = $request->post('app_id');
  36. $plan_id = $request->post('plan_id');
  37. $type = 'MONTH';
  38. if($plan_id == env('MONTH_ORDER_PLAN_ID')){
  39. $type = 'MONTH';
  40. }
  41. if($plan_id == env('MONTH_WEEK_ORDER_PLAN_ID')){
  42. $type = 'WEEK';
  43. }
  44. $user_id = $request->post('user_id');
  45. $change_type = $request->post('change_type');
  46. if ($check = checkParam($request->except('_url'), ['app_id', 'plan_id', 'user_id', 'change_type'])) {
  47. //return response()->error('PARAM_EMPTY', ['msg' => $check]);
  48. return response('fail');
  49. }
  50. $sign = _sign(compact('app_id', 'plan_id', 'user_id', 'change_type'), $key . $key);
  51. $sign = strtoupper($sign);
  52. $user_info = UserService::getById($user_id);
  53. if (!$user_info) {
  54. Log::error('signCallBack user is not exist,uid is :' . $user_id);
  55. return response('fail');
  56. }
  57. if ($sign == $request->post('sign')) {
  58. UserMonthService::createSign($user_id, $plan_id, $change_type, $user_info->openid,$type);
  59. }
  60. Log::info('signCallBack-------------------end-----------------------signCallBack');
  61. return response('success');
  62. }
  63. public function orderCallBack(Request $request)
  64. {
  65. Log::info('orderCallBack------------------------------------------orderCallBack');
  66. Log::info($request->all());
  67. Log::info($request->getMethod());
  68. $key = env('MONTH_ORDER_KEY');//'sn7wluq5716brp8fzm';
  69. $app_id = $request->post('app_id');
  70. $plan_id = $request->post('plan_id', 0);
  71. $user_id = $request->post('user_id');
  72. $total_fee = $request->post('total_fee');
  73. $trade_no = $request->post('trade_no');
  74. $out_trade_no = $request->post('out_trade_no');
  75. $result_code = $request->post('result_code',0);
  76. $type = 'MONTH';
  77. $product_id = 6826;
  78. if($plan_id == env('MONTH_ORDER_PLAN_ID')){
  79. $type = 'MONTH';
  80. $product_id = 6826;
  81. }
  82. if($plan_id == env('MONTH_WEEK_ORDER_PLAN_ID')){
  83. $type = 'WEEK';
  84. $product_id =7394;
  85. }
  86. $this->recordOrderCallBack($request,$type);
  87. if(!$result_code) return response('success');
  88. if ($check = checkParam($request->except('_url'), ['app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'])) {
  89. return response()->error('PARAM_EMPTY', ['msg' => $check]);
  90. }
  91. $sign = _sign(compact('app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'), $key . $key);
  92. $sign = strtoupper($sign);
  93. //if($sign == $request->post('sign')){
  94. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  95. UserMonthService::createLOrder($user_id, $plan_id, $total_fee, $trade_no, $out_trade_no,$type);
  96. $this->addOrderAndVip($user_id, $out_trade_no, $trade_no, $total_fee,$product_id,$type);
  97. $this->successPayPushMsg($user_id,$type);
  98. $this->userProperty($user_id);
  99. }
  100. //}
  101. Log::info('orderCallBack--------------------endend----------------------orderCallBack');
  102. return response('success');
  103. }
  104. private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price,$product_id,$type)
  105. {
  106. $order_info = OrderService::getByTradeNo($out_trade_no);
  107. //订单已经存在
  108. if ($order_info) {
  109. return;
  110. }
  111. $user_info = UserService::getById($uid);
  112. if (!$user_info) return;
  113. $this->createOrder($uid, $user_info->distribution_channel_id, $product_id, $price, $out_trade_no, $trade_no,$type);
  114. if($type == 'MONTH'){
  115. YearOrderService::save_month_order([
  116. 'uid' => $uid,
  117. 'distribution_channel_id' => $user_info->distribution_channel_id,
  118. 'fee' => $price / 100,
  119. 'send_order_id' => 0
  120. ]);
  121. }
  122. if($type == 'WEEK'){
  123. YearOrderService::save_vip_order_daynum([
  124. 'uid' => $uid,
  125. 'distribution_channel_id' => $user_info->distribution_channel_id,
  126. 'fee' => $price / 100,
  127. 'send_order_id' => 0,
  128. 'day_num'=>7
  129. ]);
  130. }
  131. if($type == 'QUARTER'){
  132. YearOrderService::save_vip_order_daynum([
  133. 'uid' => $uid,
  134. 'distribution_channel_id' => $user_info->distribution_channel_id,
  135. 'fee' => $price / 100,
  136. 'send_order_id' => 0,
  137. 'day_num'=>92
  138. ]);
  139. }
  140. }
  141. private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id,$type)
  142. {
  143. $count = OrderService::getUserChargeTimes($uid);
  144. $count = $count + 1;
  145. $send_order_id = 0;
  146. try {
  147. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  148. } catch (\Exception $e) {
  149. }
  150. $send_order_name = '';
  151. if ($send_order_id) {
  152. $send_order_info = SendOrderService::getById($send_order_id);
  153. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  154. $send_order_name = $send_order_info->name;
  155. }
  156. }
  157. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  158. $init_order = [
  159. 'distribution_channel_id' => $distribution_channel_id,
  160. 'uid' => $uid,
  161. 'product_id' => $product_id,
  162. 'price' => $price / 100,
  163. 'pay_type' => $count,
  164. 'trade_no' => $trade_no,
  165. 'pay_merchant_source' => $type,
  166. 'pay_merchant_id' => 99,
  167. 'create_ip' => '',
  168. 'send_order_id' => $send_order_id,
  169. 'send_order_name' => $send_order_name,
  170. 'order_type' => 'RECHARGE',
  171. 'from_bid' => $from_bid,
  172. 'from_type' => 'auto_'.strtolower($type),
  173. 'activity_id' => 0,
  174. 'inner_send_order_id' => '',
  175. 'status' => 'PAID',
  176. 'transaction_id' => $transaction_id,
  177. 'pay_end_at' => date('Y-m-d H:i:s')
  178. ];
  179. OrderService::save_order($init_order);
  180. }
  181. public function wait(Request $request)
  182. {
  183. //$user_cookie = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  184. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  185. $r = $request->get('r');
  186. $r = urldecode($r);
  187. return view('pay.order.monthpaywait', ['url' => $r]);
  188. }
  189. public function issuccess(Request $request)
  190. {
  191. $uid = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  192. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  193. if (!$uid) {
  194. return response()->success();
  195. }
  196. $last = UserMonthService::getLastOrder($uid);
  197. if (!$last) {
  198. return response()->error('WAP_SYS_ERROR');
  199. }
  200. if (time() - strtotime($last->created_at) < 20) {
  201. return response()->success();
  202. }
  203. return response()->error('WAP_SYS_ERROR');
  204. }
  205. private function recordOrderCallBack(Request $request,$type){
  206. $app_id = $request->post('app_id','');
  207. $plan_id = $request->post('plan_id',0);
  208. $uid = $request->post('user_id','');
  209. $total_fee = $request->post('total_fee',0);
  210. $trade_no = $request->post('trade_no','');
  211. $out_trade_no = $request->post('out_trade_no','');
  212. $result_code = $request->post('result_code',-1);
  213. $sign = $request->post('sign',-1);
  214. $created_at = date('Y-m-d H:i:s');
  215. $updated_at = date('Y-m-d H:i:s');
  216. $origin_data = json_encode($request->except('_url'));
  217. try{
  218. DB::table('user_month_order_callback')->insert([
  219. compact('uid','app_id','plan_id','total_fee','out_trade_no','origin_data',
  220. 'trade_no','result_code','sign','created_at','updated_at','type'
  221. )
  222. ]);
  223. }catch (\Exception $e){Log::error('recordOrderCallBack error');}
  224. }
  225. protected function successPayPushMsg($uid,$type)
  226. {
  227. try {
  228. $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
  229. $data = UserService::getById($uid);
  230. if (!$force_sub_info) {
  231. return;
  232. }
  233. $content_format = '';
  234. if($type == 'MONTH'){
  235. $content_format = "包月购买成功通知:\r\n购买类型: 30元包月\r\n><a href='%s'>点击继续上次阅读</a>\r\n包月特权:\r\n 💝 全站所有作品免费阅读\r\n 💝 全站无广告\r\n 💝 阅读管家1对1服务\r\n为保障包月权益,请<a href='%s'>置顶公众号</a> ";
  236. }
  237. if($type == 'WEEK'){
  238. $content_format = "包周购买成功通知:\r\n购买类型: 7元包月\r\n><a href='%s'>点击继续上次阅读</a>\r\n包月特权:\r\n 💝 全站所有作品免费阅读\r\n 💝 全站无广告\r\n 💝 阅读管家1对1服务\r\n为保障包周权益,请<a href='%s'>置顶公众号</a> ";
  239. }
  240. $delay = 0;
  241. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  242. $top_url = 'https://help.zhuishuyun.com/top.html';
  243. $content = sprintf($content_format, $url, $top_url);
  244. $res['openid'] = $force_sub_info->openid;
  245. $res['appid'] = $force_sub_info->appid;
  246. $res['content'] = $content;
  247. $res['type'] = 'one_task';
  248. $res['send_time'] = date("Y-m-d H:i:s");
  249. $res['task_id'] = md5('month_pay_success_push');
  250. $send_data = array(
  251. 'send_time' => date("Y-m-d H:i:s"),
  252. 'data' => $res
  253. );
  254. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  255. } catch (\Exception $e) {
  256. Log::error('pay_success_push error');
  257. Log::error($e);
  258. }
  259. return;
  260. }
  261. private function userProperty($uid){
  262. try{
  263. $userproperty = UserDivisionCpcPropertyService::calculateUserPropertyV2($uid);
  264. if(!$userproperty) return ;
  265. UserDivisionCpcPropertyService::createorUpdateV2($userproperty);
  266. }catch (\Exception $e){
  267. Log::error('month userProperty error');
  268. Log::error($e);
  269. }
  270. }
  271. public function signCallBackV2(Request $request)
  272. {
  273. Log::info('V2--signCallBack--------------------------------------V2----signCallBack');
  274. Log::info($request->all());
  275. Log::info($request->getMethod());
  276. $content = $request->getContent();
  277. Log::info('$request->getContent() is :');
  278. Log::info($content);
  279. libxml_disable_entity_loader(true);
  280. $xml = XML::parse(strval($content));
  281. Log::info('xml is');
  282. Log::info($xml);
  283. if (!$xml || !is_array($xml)) return response(XML::build(['return_code'=>'fail','return_msg'=>'fail']));
  284. if($xml['return_code'] != 'SUCCESS' && $xml['result_code'] != 'SUCCESS'){
  285. $result = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
  286. return response(XML::build($result));
  287. }
  288. $sign = _sign($xml,UserMonthService::MONTH_PAY_KEY);
  289. if(strtoupper($sign) != strtoupper($xml['sign'])){
  290. Log::info('sign error');
  291. Log::info('request sign is: '.$xml['sign'].',my sign is :'.$sign);
  292. $result = ['return_code'=>'fail','return_msg'=>'sign fail'];
  293. return response(XML::build($result));
  294. }
  295. $plan_id = $xml['plan_id'];
  296. $type = 'MONTH';
  297. $fee = 0;
  298. if($plan_id == env('MONTH_ORDER_PLAN_ID')){
  299. $fee = 3000;
  300. $type = 'MONTH';
  301. }
  302. if($plan_id == env('MONTH_WEEK_ORDER_PLAN_ID')){
  303. $fee = 700;
  304. $type = 'WEEK';
  305. }
  306. if($plan_id == env('MONTH_QUARTER_ORDER_PLAN_ID')){
  307. $fee = 10000;
  308. $type = 'QUARTER';
  309. }
  310. //$fee = 1;
  311. $user_id = $xml['contract_code'];
  312. $change_type = $xml['change_type'];
  313. $user_info = UserService::getById($user_id);
  314. if (!$user_info) {
  315. Log::error('signCallBack user is not exist,uid is :' . $user_id);
  316. $result = ['return_code'=>'fail','return_msg'=>'user not exist'];
  317. return response(XML::build($result));
  318. }
  319. UserMonthService::monthOrderSignRecord([
  320. 'uid'=>$user_id,
  321. 'plan_id'=>$plan_id,
  322. 'contract_code'=>$xml['contract_code'],
  323. 'openid'=>$xml['openid'],
  324. 'change_type'=>$change_type,
  325. 'operate_time'=>$xml['operate_time'],
  326. 'contract_id'=>$xml['contract_id'],
  327. 'contract_expired_time'=>$xml['contract_expired_time'],
  328. 'contract_termination_mode'=>isset($xml['contract_termination_mode'])?$xml['contract_termination_mode']:0,
  329. 'request_serial'=>$xml['request_serial']
  330. ]);
  331. UserMonthService::createSign($user_id, $plan_id, $change_type, $user_info->openid,$type);
  332. if($change_type == 'ADD'){
  333. UserMonthService::monthPayApplypap($user_id,$xml['contract_id'],$fee,'追书云包季扣款','47.97.95.151','sign_back',\GuzzleHttp\json_encode(['uid'=>$user_id,'plan_id'=>$plan_id]));
  334. }
  335. Log::info('signCallBack-------------------end-----------------------signCallBack');
  336. $result = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
  337. return response(XML::build($result));
  338. }
  339. public function orderCallBackV2(Request $request)
  340. {
  341. Log::info('V2--------orderCallBack-----------------------------------v2-------orderCallBack');
  342. $content = $request->getContent();
  343. Log::info('orderCallBackV2 $request->getContent() is :');
  344. Log::info($content);
  345. libxml_disable_entity_loader(true);
  346. $xml = XML::parse(strval($content));
  347. Log::info('orderCallBackV2 xml is:');
  348. Log::info($xml);
  349. if (!$xml || !is_array($xml)) return response(XML::build(['return_code'=>'fail','return_msg'=>'fail']));
  350. UserMonthService::monthCallBackRecordV2([
  351. 'openid'=>isset($xml['openid'])?$xml['openid']:'',
  352. 'bank_type'=>isset($xml['bank_type'])?$xml['bank_type']:'',
  353. 'total_fee'=>isset($xml['total_fee'])?$xml['total_fee']:0,
  354. 'cash_fee'=>isset($xml['cash_fee'])?$xml['cash_fee']:0,
  355. 'trade_state'=>isset($xml['trade_state'])?$xml['trade_state']:'',
  356. 'transaction_id'=>isset($xml['transaction_id'])?$xml['transaction_id']:'',
  357. 'out_trade_no'=>isset($xml['out_trade_no'])?$xml['out_trade_no']:'',
  358. 'attach'=>isset($xml['attach'])?$xml['attach']:'',
  359. 'time_end'=>isset($xml['time_end'])?$xml['time_end']:'',
  360. 'contract_id'=>isset($xml['contract_id'])?$xml['contract_id']:'',
  361. 'result_code'=>isset($xml['result_code'])?$xml['result_code']:'',
  362. 'err_code'=>isset($xml['err_code'])?$xml['err_code']:'',
  363. 'result'=>json_encode($xml),
  364. 'uid'=>isset($xml['attach']) && !empty($xml['attach']) ? json_decode($xml['attach'],1)['uid']:0
  365. ]);
  366. $sign = _sign($xml,UserMonthService::MONTH_PAY_KEY);
  367. if(strtoupper($sign) != strtoupper($xml['sign'])){
  368. Log::info('orderCallBackV2 sign error');
  369. Log::info('orderCallBackV2 request sign is: '.$xml['sign'].',my sign is :'.$sign);
  370. $result = ['return_code'=>'fail','return_msg'=>'sign fail'];
  371. return response(XML::build($result));
  372. }
  373. if($xml['return_code'] == 'SUCCESS' && $xml['result_code'] == 'SUCCESS') {
  374. $attach = json_decode($xml['attach'], 1);
  375. $plan_id = $attach['plan_id'];
  376. $user_id = $attach['uid'];
  377. $total_fee = $xml['total_fee'];
  378. $trade_no = $xml['transaction_id'];
  379. $out_trade_no = $xml['out_trade_no'];
  380. $type = 'MONTH';
  381. $product_id = 6826;
  382. if ($plan_id == env('MONTH_ORDER_PLAN_ID')) {
  383. $type = 'MONTH';
  384. $product_id = 6826;
  385. }
  386. if ($plan_id == env('MONTH_WEEK_ORDER_PLAN_ID')) {
  387. $type = 'WEEK';
  388. $product_id = 7394;
  389. }
  390. if($plan_id == env('MONTH_QUARTER_ORDER_PLAN_ID')){
  391. $type = 'QUARTER';
  392. }
  393. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  394. UserMonthService::createLOrder($user_id, $plan_id, $total_fee, $trade_no, $out_trade_no, $type);
  395. $this->addOrderAndVip($user_id, $out_trade_no, $trade_no, $total_fee, $product_id, $type);
  396. $this->successPayPushMsg($user_id, $type);
  397. $this->userProperty($user_id);
  398. }
  399. }
  400. Log::info('orderCallBack--------------------endend----------------------orderCallBack');
  401. $result = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
  402. return response(XML::build($result));
  403. }
  404. }