MonthOrderController.php 12 KB

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