MonthOrderController.php 13 KB

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