MonthOrderController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. class MonthOrderController extends Controller
  26. {
  27. public function getH5RedirectUrl(Request $request)
  28. {
  29. $app_id = 'Aekgrv87yo5wxnl715';
  30. $app_secret = '57gwsnd158ib2sn7wluq5716brp8fzm';
  31. $key = 'sn7wluq5716brp8fzm';
  32. //$plan_id = 124768;
  33. $plan_id = 39603;
  34. $user_id = 10008;
  35. $ip = _getIp();
  36. $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
  37. $sign = strtoupper($sign);
  38. $client = new Client();
  39. Log::info(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
  40. $url = 'http://pap.manyuedu.org/h5.php?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
  41. //$url = 'http://pap.manyuedu.org/?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
  42. return $client->get($url)->getBody()->getContents();
  43. }
  44. public function getWechatRedirectUrl(Request $request)
  45. {
  46. $app_id = 'Aekgrv87yo5wxnl715';
  47. $app_secret = '57gwsnd158ib2sn7wluq5716brp8fzm';
  48. $key = 'sn7wluq5716brp8fzm';
  49. //$plan_id = 124768;
  50. $plan_id = 39603;
  51. $user_id = 10008;
  52. $ip = _getIp();
  53. $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
  54. $sign = strtoupper($sign);
  55. $client = new Client();
  56. Log::info(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
  57. //$url = 'http://pap.manyuedu.org/h5.php?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
  58. $url = 'http://pap.manyuedu.org/?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
  59. return $client->get($url)->getBody()->getContents();
  60. }
  61. public function signCallBack(Request $request)
  62. {
  63. Log::info('signCallBack------------------------------------------signCallBack');
  64. Log::info($request->all());
  65. Log::info($request->getMethod());
  66. //$key = 'sn7wluq5716brp8fzm';
  67. $key = env('MONTH_ORDER_KEY');
  68. $app_id = $request->post('app_id');
  69. $plan_id = $request->post('plan_id');
  70. $user_id = $request->post('user_id');
  71. $change_type = $request->post('change_type');
  72. if ($check = checkParam($request->except('_url'), ['app_id', 'plan_id', 'user_id', 'change_type'])) {
  73. //return response()->error('PARAM_EMPTY', ['msg' => $check]);
  74. return response('fail');
  75. }
  76. $sign = _sign(compact('app_id', 'plan_id', 'user_id', 'change_type'), $key . $key);
  77. $sign = strtoupper($sign);
  78. $user_info = UserService::getById($user_id);
  79. if (!$user_info) {
  80. Log::error('signCallBack user is not exist,uid is :' . $user_id);
  81. return response('fail');
  82. }
  83. if ($sign == $request->post('sign')) {
  84. UserMonthService::createSign($user_id, $plan_id, $change_type, $user_info->openid);
  85. }
  86. Log::info('signCallBack-------------------end-----------------------signCallBack');
  87. return response('success');
  88. }
  89. public function orderCallBackV2(Request $request)
  90. {
  91. Log::info('orderCallBack------------------------------------------orderCallBack');
  92. Log::info($request->all());
  93. Log::info($request->getMethod());
  94. $key = env('MONTH_ORDER_KEY');//'sn7wluq5716brp8fzm';
  95. $app_id = $request->post('app_id');
  96. $plan_id = $request->post('plan_id', 0);
  97. $user_id = $request->post('user_id');
  98. $total_fee = $request->post('total_fee');
  99. $trade_no = $request->post('trade_no');
  100. $out_trade_no = $request->post('out_trade_no');
  101. if ($check = checkParam($request->except('_url'), ['app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'])) {
  102. return response()->error('PARAM_EMPTY', ['msg' => $check]);
  103. }
  104. $sign = _sign(compact('app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'), $key . $key);
  105. $sign = strtoupper($sign);
  106. Log::info('self sign is: ' . $sign);
  107. $order_info = Order::find($user_id);
  108. if (!$order_info) {
  109. Log::error('month order not exist !!!order is :' . $trade_no);
  110. return response('fail');
  111. }
  112. if ($order_info->status == 'PAID') {
  113. Log::error('month order had deal !!!, order is :' . $trade_no);
  114. return response('fail');
  115. }
  116. //if($sign == $request->post('sign')){
  117. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  118. UserMonthService::createLOrder($order_info->uid, $plan_id, $total_fee, $trade_no, $out_trade_no);
  119. $order_info->transaction_id = $trade_no;
  120. $order_info->status = 'PAID';
  121. $order_info->pay_end_at = date('Y-m-d H:i:s');
  122. $order_info->save();
  123. self::addOrderAndVipV2($order_info->uid, $total_fee);
  124. }
  125. //}
  126. Log::info('orderCallBack--------------------endend----------------------orderCallBack');
  127. return response('success');
  128. }
  129. public function orderCallBack(Request $request)
  130. {
  131. Log::info('orderCallBack------------------------------------------orderCallBack');
  132. Log::info($request->all());
  133. Log::info($request->getMethod());
  134. $key = env('MONTH_ORDER_KEY');//'sn7wluq5716brp8fzm';
  135. $app_id = $request->post('app_id');
  136. $plan_id = $request->post('plan_id', 0);
  137. $user_id = $request->post('user_id');
  138. $total_fee = $request->post('total_fee');
  139. $trade_no = $request->post('trade_no');
  140. $out_trade_no = $request->post('out_trade_no');
  141. $result_code = $request->post('result_code',0);
  142. $this->recordOrderCallBack($request);
  143. if(!$result_code) return response('success');
  144. if ($check = checkParam($request->except('_url'), ['app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'])) {
  145. return response()->error('PARAM_EMPTY', ['msg' => $check]);
  146. }
  147. $sign = _sign(compact('app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'), $key . $key);
  148. $sign = strtoupper($sign);
  149. //if($sign == $request->post('sign')){
  150. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  151. UserMonthService::createLOrder($user_id, $plan_id, $total_fee, $trade_no, $out_trade_no);
  152. $this->addOrderAndVip($user_id, $out_trade_no, $trade_no, $total_fee);
  153. $this->successPayPushMsg($user_id);
  154. $this->userProperty($user_id);
  155. }
  156. //}
  157. Log::info('orderCallBack--------------------endend----------------------orderCallBack');
  158. return response('success');
  159. }
  160. private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price)
  161. {
  162. $order_info = OrderService::getByTradeNo($out_trade_no);
  163. //订单已经存在
  164. if ($order_info) {
  165. return;
  166. }
  167. $user_info = UserService::getById($uid);
  168. if (!$user_info) return;
  169. $this->createOrder($uid, $user_info->distribution_channel_id, 0, $price, $out_trade_no, $trade_no);
  170. YearOrderService::save_month_order([
  171. 'uid' => $uid,
  172. 'distribution_channel_id' => $user_info->distribution_channel_id,
  173. 'fee' => $price / 100,
  174. 'send_order_id' => 0
  175. ]);
  176. }
  177. private function addOrderAndVipV2($uid, $price)
  178. {
  179. $user_info = UserService::getById($uid);
  180. if (!$user_info) return;
  181. YearOrderService::save_month_order([
  182. 'uid' => $uid,
  183. 'distribution_channel_id' => $user_info->distribution_channel_id,
  184. 'fee' => $price / 100,
  185. 'send_order_id' => 0
  186. ]);
  187. }
  188. private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
  189. {
  190. $count = OrderService::getUserChargeTimes($uid);
  191. $count = $count + 1;
  192. $send_order_id = 0;
  193. try {
  194. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  195. } catch (\Exception $e) {
  196. }
  197. $send_order_name = '';
  198. if ($send_order_id) {
  199. $send_order_info = SendOrderService::getById($send_order_id);
  200. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  201. $send_order_name = $send_order_info->name;
  202. }
  203. }
  204. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  205. $init_order = [
  206. 'distribution_channel_id' => $distribution_channel_id,
  207. 'uid' => $uid,
  208. 'product_id' => 6826,
  209. 'price' => $price / 100,
  210. 'pay_type' => $count,
  211. 'trade_no' => $trade_no,
  212. 'pay_merchant_source' => 'MONTH',
  213. 'pay_merchant_id' => 99,
  214. 'create_ip' => '',
  215. 'send_order_id' => $send_order_id,
  216. 'send_order_name' => $send_order_name,
  217. 'order_type' => 'RECHARGE',
  218. 'from_bid' => $from_bid,
  219. 'from_type' => 'auto_month',
  220. 'activity_id' => 0,
  221. 'inner_send_order_id' => '',
  222. 'status' => 'PAID',
  223. 'transaction_id' => $transaction_id,
  224. 'pay_end_at' => date('Y-m-d H:i:s')
  225. ];
  226. OrderService::save_order($init_order);
  227. }
  228. public function wait(Request $request)
  229. {
  230. //$user_cookie = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  231. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  232. $r = $request->get('r');
  233. $r = urldecode($r);
  234. return view('pay.order.monthpaywait', ['url' => $r]);
  235. }
  236. public function issuccess(Request $request)
  237. {
  238. $uid = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  239. //$uid = $user_cookie ? decrypt($user_cookie) : null;
  240. if (!$uid) {
  241. return response()->success();
  242. }
  243. $last = UserMonthService::getLastOrder($uid);
  244. if (!$last) {
  245. return response()->error('WAP_SYS_ERROR');
  246. }
  247. if (time() - strtotime($last->created_at) < 20) {
  248. return response()->success();
  249. }
  250. return response()->error('WAP_SYS_ERROR');
  251. }
  252. private function recordOrderCallBack(Request $request){
  253. $app_id = $request->post('app_id','');
  254. $plan_id = $request->post('plan_id',0);
  255. $uid = $request->post('user_id','');
  256. $total_fee = $request->post('total_fee',0);
  257. $trade_no = $request->post('trade_no','');
  258. $out_trade_no = $request->post('out_trade_no','');
  259. $result_code = $request->post('result_code',-1);
  260. $sign = $request->post('sign',-1);
  261. $created_at = date('Y-m-d H:i:s');
  262. $updated_at = date('Y-m-d H:i:s');
  263. $origin_data = json_encode($request->except('_url'));
  264. try{
  265. DB::table('user_month_order_callback')->insert([
  266. compact('uid','app_id','plan_id','total_fee','out_trade_no','origin_data',
  267. 'trade_no','result_code','sign','created_at','updated_at'
  268. )
  269. ]);
  270. }catch (\Exception $e){
  271. Log::error('recordOrderCallBack error');
  272. }
  273. }
  274. protected function successPayPushMsg($uid)
  275. {
  276. try {
  277. $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
  278. $data = UserService::getById($uid);
  279. if (!$force_sub_info) {
  280. return;
  281. }
  282. $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> ";
  283. $delay = 0;
  284. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  285. $top_url = 'https://help.zhuishuyun.com/top.html';
  286. $content = sprintf($content_format, $url, $top_url);
  287. $res['openid'] = $force_sub_info->openid;
  288. $res['appid'] = $force_sub_info->appid;
  289. $res['content'] = $content;
  290. $res['type'] = 'one_task';
  291. $res['send_time'] = date("Y-m-d H:i:s");
  292. $res['task_id'] = md5('month_pay_success_push');
  293. $send_data = array(
  294. 'send_time' => date("Y-m-d H:i:s"),
  295. 'data' => $res
  296. );
  297. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  298. } catch (\Exception $e) {
  299. Log::error('pay_success_push error');
  300. Log::error($e);
  301. }
  302. return;
  303. }
  304. private function userProperty($uid){
  305. try{
  306. $userproperty = UserDivisionCpcPropertyService::calculateUserPropertyV2($uid);
  307. if(!$userproperty) {
  308. return ;
  309. }
  310. UserDivisionCpcPropertyService::createorUpdateV2($userproperty);
  311. }catch (\Exception $e){
  312. Log::error('month userProperty error');
  313. Log::error($e);
  314. }
  315. }
  316. }