MonthOrderController.php 13 KB

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