MonthOrderController.php 12 KB

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