OrdersController.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. <?php
  2. namespace App\Http\Controllers\Wap\Pay;
  3. use App\Admin\Models\User;
  4. use App\Modules\Book\Services\ChapterService;
  5. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  6. use App\Modules\Subscribe\Services\OrderParamService;
  7. use App\Modules\Subscribe\Services\SubstituteOrderService;
  8. use App\Modules\User\Services\ReadRecordService;
  9. use App\Modules\User\Services\UserDivisionCpcPropertyService;
  10. use Illuminate\Routing\Controller;
  11. use App\Libs\Pay\WechatPay;
  12. use Illuminate\Http\Request;
  13. use App\Modules\Subscribe\Services\YearOrderService;
  14. use App\Modules\Subscribe\Services\BookOrderService;
  15. use App\Modules\Subscribe\Services\OrderService;
  16. use App\Modules\Product\Services\ProductService;
  17. use App\Modules\Book\Services\BookConfigService;
  18. use App\Modules\User\Services\UserService;
  19. use App\Modules\Channel\Services\ChannelService;
  20. use Log;
  21. use DB;
  22. use Cookie;
  23. use Redis;
  24. use Hashids;
  25. use EasyWeChat\Foundation\Application;
  26. use EasyWeChat\Support\XML;
  27. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  28. use App\Jobs\SendTexts;
  29. use App\Jobs\SendStatisticsList;
  30. use App\Libs\AliSMS;
  31. use App\Modules\Activity\Services\ActivityService;
  32. use App\Modules\SendOrder\Services\SendOrderService;
  33. use GuzzleHttp\Client;
  34. class OrdersController extends Controller
  35. {
  36. /**
  37. * @apiDefine pay 支付
  38. */
  39. /**
  40. * 支付唤起入口
  41. * 功能:授权、请求支付参数、并跳转微信支付
  42. * TODO 暂时不创建订单
  43. * tail -f /var/www/ydy_wap_backend/storage/logs/laravel-2017-12-01.log
  44. * zwap/goToPay?distribution_channel_id=3&price=1&uid=3
  45. * @apiVersion 1.0.0
  46. * @apiDescription 支付
  47. * @api {get} http://pay.aizhuishu.com/goToPay 支付
  48. * @apiGroup pay
  49. * @apiName wxindex
  50. * @apiParam {Int} product_id product_id
  51. * @apiParam {Int} uid uid
  52. * @apiParam {Int} distribution_channel_id distribution_channel_id
  53. * @apiParam {String} pay_redirect_url pay_redirect_url
  54. * @apiSuccess {int} code 状态码
  55. * @apiSuccessExample {json} Success-Response:
  56. * HTTP/1.1 200 OK
  57. * {
  58. * code: 0,
  59. * msg: "",
  60. * data: {
  61. * }
  62. */
  63. function wxindex(Request $request)
  64. {
  65. $product_id = $request->has('product_id') ? $request->get('product_id') : '';
  66. $uid = $request->has('uid') ? $request->get('uid') : '';
  67. $distribution_channel_id = $request->has('distribution_channel_id') ? $request->get('distribution_channel_id') : '';
  68. $p_channel_id = $request->has('p_channel_id') ? $request->get('p_channel_id') : 0;
  69. $pay_redirect_url = $request->has('pay_redirect_url') ? $request->get('pay_redirect_url') : '';
  70. $send_order_id = $request->has('send_order_id') ? $request->get('send_order_id') : 0;
  71. $activity_id = $request->has('activity_id') ? $request->get('activity_id') : 0;
  72. $ip = $request->has('ip') ? $request->get('ip') : '';
  73. $n = $request->has('n') ? $request->get('n') : 0;
  74. $crm = $request->has('crm') ? $request->get('crm') : 0;
  75. if ($send_order_id && strlen($send_order_id) > 50) {
  76. try {
  77. $send_order_id = decrypt($send_order_id);
  78. } catch (\Exception $e) {
  79. $send_order_id = 0;
  80. }
  81. }
  82. if ($send_order_id && strlen($send_order_id) > 50) {
  83. $send_order_id = 0;
  84. }
  85. //活动判断
  86. if ($activity_id) {
  87. $limit = $request->get('limit', 0);
  88. if ($limit && $limit == 1) {
  89. $order = OrderService::userIsParticipateActivity($uid, $activity_id);
  90. if ($order) {
  91. $activity = ActivityService::getById($activity_id);
  92. if ($activity) {
  93. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com' . $activity->activity_page;
  94. return redirect()->to($url);
  95. }
  96. return '';
  97. }
  98. }
  99. }
  100. $suid = $request->get('suid', 0);
  101. if ($suid) {
  102. $user_info = UserService::getById($uid);
  103. $distribution_channel_id = $user_info->distribution_channel_id;
  104. $pay_redirect_url = 1;
  105. }
  106. if (empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)) {
  107. return response()->error('WAP_PARAM_ERROR');
  108. }
  109. if ($suid && $suid == $uid) {
  110. return back();
  111. }
  112. $openid = $request->has('openid') ? $request->get('openid') : '';
  113. $bid = $request->has('bid') ? $request->get('bid') : 0;
  114. $hash_bid = $bid;
  115. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from');
  116. if ($fromtype && strlen($fromtype) > 50) {
  117. try {
  118. $fromtype = decrypt($fromtype);
  119. } catch (\Exception $e) {
  120. $fromtype = 'main';
  121. }
  122. }
  123. if ($fromtype && strlen($fromtype) > 50) {
  124. $fromtype = 'main';
  125. }
  126. if (!$openid && !$p_channel_id && !$suid) {
  127. $transfer_channel_id = $this->transfer($distribution_channel_id);
  128. if ($transfer_channel_id) {
  129. $p_channel_id = $distribution_channel_id;
  130. $distribution_channel_id = $transfer_channel_id;
  131. }
  132. }
  133. //根据分校id获取支付配置id
  134. Log::info($request->all());
  135. $channel = ChannelService::getById($distribution_channel_id);
  136. if (!$channel || !$channel->pay_merchant_id) {
  137. return response()->error('WAP_PARAM_ERROR');
  138. }
  139. //获取支付类型
  140. $pay_merchant = DB::table('pay_merchants')->select('id', 'appid', 'source', 'config_info')->where('id', $channel->pay_merchant_id)->where('is_enabled', 1)->first();
  141. if (!$pay_merchant || !$pay_merchant->appid || !$pay_merchant->source) return response()->error('WAP_PARAM_ERROR');
  142. if ($pay_merchant->source == 'PALMPAYV2') {
  143. return redirect()->to($this->paympay($request, $pay_merchant));
  144. }
  145. //重定向 获取用户信息的次数
  146. $n++;
  147. if ($n >= 5) {
  148. //重定向次数过多,则授权公众号出问题
  149. $this->payAlert($channel->pay_merchant_id, '', '', $n);
  150. $back_url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . '/pay';
  151. return redirect()->to($back_url);
  152. }
  153. if (empty($openid)) {
  154. $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
  155. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  156. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  157. } else {
  158. $ip = get_client_ip();
  159. }
  160. $params = compact('uid', 'product_id', 'distribution_channel_id', 'send_order_id', 'bid', 'trade_no', 'pay_redirect_url', 'fromtype', 'activity_id', 'n', 'suid', 'ip', 'p_channel_id','crm');
  161. $redirect_url = env('CREATE_PAY_URL') . '?' . http_build_query($params);
  162. //$redirect_url = env('CREATE_PAY_URL').'?uid='.$uid.'&product_id='.$product_id.'&distribution_channel_id='
  163. // .$distribution_channel_id.'&send_order_id='.$send_order_id.'&bid='.$bid.'&pay_redirect_url='.urlencode($pay_redirect_url)
  164. $auth_url = env('AUTH_URL') . '?gzh_app_id=' . $pay_merchant->appid . '&redirect_url=' . urlencode($redirect_url);
  165. Log::info('redirect_auth_url:' . $auth_url);
  166. header("Location:" . $auth_url);
  167. exit();
  168. }
  169. $trade_no = $request->input('trade_no');
  170. $order_info = OrderService::getByTradeNo($trade_no);
  171. if ($order_info) return response()->error('WAP_SYS_ERROR');
  172. $cid = $request->has('cid') ? $request->get('cid') : '';
  173. $product_info = ProductService::getProductSingle($product_id, false);
  174. //新用户只能冲一次
  175. //Log::info('新用户只能冲一次:' .$product_info->type);
  176. if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
  177. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  178. return redirect()->to($url);
  179. }
  180. $price = $product_info->price * 100;
  181. if ($uid < 32) {
  182. $price = 1;
  183. }
  184. try {
  185. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  186. } catch (\Exception $e) {
  187. }
  188. $this->updateUserSendOrderId($uid, $send_order_id);
  189. if (in_array($uid, explode(',', env('TEST_UID')))) {
  190. $price = 1;
  191. }
  192. if ($bid) {
  193. try {
  194. $bid = Hashids::decode($bid)[0];
  195. } catch (\Exception $e) {
  196. $bid = 0;
  197. }
  198. }
  199. try {
  200. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  201. Redis::hset($key, $uid, time());
  202. $date = date('Y-m-d');
  203. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  204. if ($send_order_id) {
  205. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  206. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  207. }
  208. } catch (\Exception $e) {
  209. }
  210. $data = [];
  211. $data['price'] = $price;
  212. $data['create_ip'] = $ip;
  213. $data['openid'] = $openid;//
  214. $data['body'] = 'novel read';
  215. $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
  216. if ($official_name) {
  217. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  218. }
  219. $data['detail'] = 'novel read';
  220. $data['trade_no'] = $trade_no;
  221. $data['remark'] = json_encode(
  222. [
  223. 'uid' => $uid,
  224. 'dcd' => $distribution_channel_id,
  225. 'bid' => $bid,
  226. 'cp' => $ip,
  227. 'pms' => $pay_merchant->source,
  228. 'pmi' => $channel->pay_merchant_id,
  229. 'pd' => $product_id,
  230. 'soi' => $send_order_id,
  231. ]
  232. );
  233. if ($product_info->type == 'YEAR_ORDER') {
  234. $order_type = 'YEAR';
  235. } elseif ($product_info->type == 'BOOK_ORDER') {
  236. $order_type = 'BOOK';
  237. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  238. $order_type = 'RECHARGE';
  239. } elseif ($product_info->type == 'NEW_USER') {
  240. $order_type = 'RECHARGE';
  241. } else {
  242. $order_type = 'UNKNOWN';
  243. }
  244. /*
  245. $this->createOrderTotal([
  246. 'distribution_channel_id'=>$distribution_channel_id,
  247. 'uid'=>$uid,
  248. 'product_id'=>$product_id,
  249. 'price'=>$price/100,
  250. 'pay_type'=>1,
  251. 'trade_no'=>$data['trade_no'],
  252. 'pay_merchant_source'=>$pay_merchant->source,
  253. 'pay_merchant_id'=>$channel->pay_merchant_id,
  254. 'create_ip'=>$request->getClientIp(),
  255. 'send_order_id'=>$send_order_id,
  256. 'order_type'=>$order_type,
  257. 'from_bid'=>$bid
  258. ]);*/
  259. $send_order_name = '';
  260. if ($send_order_id) {
  261. $send_order_info = SendOrderService::getById($send_order_id);
  262. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  263. $send_order_name = $send_order_info->name;
  264. }
  265. if(redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0) == $distribution_channel_id){
  266. $send_order_id = -$send_order_id;
  267. }
  268. }
  269. if ($suid) {
  270. $user_info = UserService::getById($uid);
  271. $distribution_channel_id = $user_info->distribution_channel_id;
  272. }
  273. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  274. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  275. $init_order = [
  276. 'distribution_channel_id' => $distribution_channel_id,
  277. 'uid' => $uid,
  278. 'product_id' => $product_id,
  279. 'price' => $price / 100,
  280. 'pay_type' => 1,
  281. 'trade_no' => $data['trade_no'],
  282. 'pay_merchant_source' => $pay_merchant->source,
  283. 'pay_merchant_id' => $channel->pay_merchant_id,
  284. 'create_ip' => $ip,
  285. 'send_order_id' => $send_order_id,
  286. 'send_order_name' => $send_order_name,
  287. 'order_type' => $order_type,
  288. 'from_bid' => $from_bid,
  289. 'from_type' => $fromtype,
  290. 'activity_id' => $activity_id,
  291. 'inner_send_order_id' => $inner_send_order_id
  292. ];
  293. $result = $this->createUnPayOrder($init_order);
  294. //订单创建成功后增加统计
  295. if ($result) {
  296. $this->orderCreated($init_order);
  297. $this->recordOtherParam($result->id, $uid);
  298. if($p_channel_id){
  299. //miwa 转换
  300. OrderParamService::createByParam([
  301. 'order_id'=>$result->id,'gxhp'=>'mw_transfer','appid'=>'','bid'=>$bid,'data_hour_key'=>'','times'=>'','distribution_channel_id'=>$p_channel_id
  302. ]);
  303. }
  304. if($crm){
  305. OrderParamService::createByParam([
  306. 'order_id'=>$result->id,'gxhp'=>'crm','appid'=>'','bid'=>$bid,'data_hour_key'=>$crm,'times'=>'','distribution_channel_id'=>$distribution_channel_id
  307. ]);
  308. }
  309. }
  310. if ($request->get('suid')) {
  311. $this->createSubstituteOrder($result->id, $uid, $request->get('suid'));
  312. }
  313. Log::info($data);
  314. $config = [];
  315. if ($pay_merchant->config_info) {
  316. $config = json_decode($pay_merchant->config_info, true);
  317. }
  318. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  319. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  320. $pay_info = $wechatPay->send($data);
  321. if (!$pay_info) {
  322. $pay_info = $wechatPay->send($data);
  323. }
  324. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  325. //支付异常
  326. myLog('order')->info('pay error start---------------------------------');
  327. myLog('order')->info($pay_info);
  328. myLog('order')->info($request->all());
  329. myLog('order')->info('order info is');
  330. myLog('order')->info($init_order);
  331. myLog('order')->info('pay error end-----------------------------------');
  332. $this->payAlert($channel->pay_merchant_id, $trade_no, $pay_info);
  333. }
  334. $pay_info['pay_redirect_url'] = urldecode($pay_redirect_url);
  335. if ($request->has('cid')) {
  336. $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&cid=' . $request->input('cid');
  337. }
  338. // if ($hash_bid) {
  339. // $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&bid=' . $hash_bid;
  340. // }
  341. if ($suid) {
  342. $prize_fee = (int)(($product_info->price * 100) * 0.1);
  343. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  344. $help_pay_page_channel_id = $distribution_channel_id;
  345. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  346. $pay_info['pay_redirect_url'] = $pay_redirect_url = sprintf(
  347. $url_format,
  348. env('PROTOCOL'),
  349. encodeDistributionChannelId($help_pay_page_channel_id),
  350. env('CUSTOM_HOST'),
  351. $prize_fee,
  352. $uid
  353. );
  354. }
  355. $pay_url_info = parse_url($pay_redirect_url);
  356. $pay_info['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/wait?order=' . $data['trade_no'] . '&redirect=' . urlencode($pay_info['pay_redirect_url']);
  357. //$h5_scheme = env('H5_SCHEME','https');
  358. //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
  359. Log::info('$pay_info is');
  360. $jsSdkSign = 0;
  361. Log::info($pay_info);
  362. Log::info('jsSdkSign---- :' . $jsSdkSign);
  363. $pay_order = $trade_no;
  364. // 注册动作-》创建订单
  365. $action_type = 'CreateOrder';
  366. $param = [
  367. 'openid' => isset($openid) ? $openid : '0',
  368. 'uid' => isset($uid) ? $uid : '0',
  369. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  370. 'amount' => isset($price) ? $price : '0',
  371. ];
  372. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  373. return view('pay.order.index', compact('pay_info', 'referer', 'jsSdkSign', 'pay_order'));
  374. }
  375. private function paympay(Request $request, $pay_merchant)
  376. {
  377. $product_id = $request->get('product_id');
  378. $uid = $request->get('uid');
  379. $distribution_channel_id = $request->get('distribution_channel_id');
  380. $pay_redirect_url = $request->get('pay_redirect_url');
  381. $send_order_id = $request->get('send_order_id', 0);
  382. $activity_id = $request->get('activity_id', 0);
  383. $suid = $request->get('suid', 0);
  384. $bid = $request->has('bid') ? $request->get('bid') : 0;
  385. $crm = $request->has('crm') ? $request->get('crm') : '';
  386. $hash_bid = $bid;
  387. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from', 'main');
  388. //根据分校id获取支付配置id
  389. Log::info($request->all());
  390. $trade_no = $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
  391. $order_info = OrderService::getByTradeNo($trade_no);
  392. if ($order_info) return response()->error('WAP_SYS_ERROR');
  393. $cid = $request->has('cid') ? $request->get('cid') : '';
  394. $product_info = ProductService::getProductSingle($product_id, false);
  395. //新用户只能冲一次
  396. //Log::info('新用户只能冲一次:' .$product_info->type);
  397. if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
  398. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  399. return redirect()->to($url);
  400. }
  401. $price = $product_info->price * 100;
  402. if ($uid < 32) {
  403. $price = 1;
  404. }
  405. if (!$send_order_id) {
  406. try {
  407. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  408. } catch (\Exception $e) {
  409. }
  410. }
  411. $this->updateUserSendOrderId($uid, $send_order_id);
  412. if (in_array($uid, explode(',', env('TEST_UID')))) {
  413. $price = 1;
  414. }
  415. try {
  416. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  417. Redis::hset($key, $uid, time());
  418. $date = date('Y-m-d');
  419. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  420. if ($send_order_id) {
  421. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  422. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  423. }
  424. } catch (\Exception $e) {
  425. }
  426. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  427. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  428. } else {
  429. $ip = get_client_ip();
  430. }
  431. $data = [];
  432. $data['price'] = $price;
  433. $data['create_ip'] = $ip;
  434. $data['body'] = 'novel read';
  435. $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
  436. if ($official_name) {
  437. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  438. }
  439. $data['detail'] = 'novel read';
  440. $data['trade_no'] = $trade_no;
  441. if ($product_info->type == 'YEAR_ORDER') {
  442. $order_type = 'YEAR';
  443. } elseif ($product_info->type == 'BOOK_ORDER') {
  444. $order_type = 'BOOK';
  445. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  446. $order_type = 'RECHARGE';
  447. } elseif ($product_info->type == 'NEW_USER') {
  448. $order_type = 'RECHARGE';
  449. } else {
  450. $order_type = 'UNKNOWN';
  451. }
  452. $send_order_name = '';
  453. if ($send_order_id) {
  454. $send_order_info = SendOrderService::getById($send_order_id);
  455. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  456. $send_order_name = $send_order_info->name;
  457. }
  458. }
  459. if ($suid) {
  460. $user_info = UserService::getById($uid);
  461. $distribution_channel_id = $user_info->distribution_channel_id;
  462. }
  463. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  464. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  465. $init_order = [
  466. 'distribution_channel_id' => $distribution_channel_id,
  467. 'uid' => $uid,
  468. 'product_id' => $product_id,
  469. 'price' => $price / 100,
  470. 'pay_type' => 1,
  471. 'trade_no' => $data['trade_no'],
  472. 'pay_merchant_source' => 'PALMPAY',
  473. 'pay_merchant_id' => $pay_merchant->id,
  474. 'create_ip' => $ip,
  475. 'send_order_id' => $send_order_id,
  476. 'send_order_name' => $send_order_name,
  477. 'order_type' => $order_type,
  478. 'from_bid' => $from_bid,
  479. 'from_type' => $fromtype,
  480. 'activity_id' => $activity_id,
  481. 'inner_send_order_id' => $inner_send_order_id
  482. ];
  483. $result = $this->createUnPayOrder($init_order);
  484. //订单创建成功后增加统计
  485. if ($result) {
  486. $this->orderCreated($init_order);
  487. $this->recordOtherParam($result->id, $uid);
  488. if($crm){
  489. OrderParamService::createByParam([
  490. 'order_id'=>$result->id,'gxhp'=>'crm','appid'=>'','bid'=>$bid,'data_hour_key'=>$crm,'times'=>'','distribution_channel_id'=>$distribution_channel_id
  491. ]);
  492. }
  493. }
  494. if ($suid) {
  495. $this->createSubstituteOrder($result->id, $uid, $suid);
  496. }
  497. if ($suid) {
  498. $prize_fee = (int)(($product_info->price * 100) * 0.1);
  499. //$help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  500. $help_pay_page_channel_id = $distribution_channel_id;
  501. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  502. $pay_redirect_url = sprintf(
  503. $url_format,
  504. env('PROTOCOL'),
  505. encodeDistributionChannelId($help_pay_page_channel_id),
  506. env('CUSTOM_HOST'),
  507. $prize_fee,
  508. $uid
  509. );
  510. }
  511. $de_pay_redirect_url = urldecode($pay_redirect_url);
  512. /*if ($request->has('cid')) {
  513. $de_pay_redirect_url = $de_pay_redirect_url . '&cid=' . $request->get('cid');
  514. }*/
  515. $pay_url_info = parse_url($de_pay_redirect_url);
  516. $data['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/waitv2?order=' . $data['trade_no'] . '&redirect=' . urlencode($de_pay_redirect_url);
  517. Log::info($data);
  518. $config = [];
  519. if ($pay_merchant->config_info) {
  520. $config = json_decode($pay_merchant->config_info, true);
  521. }
  522. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  523. \Log::info('$wechatPay is');
  524. //\Log::info($wechatPay);
  525. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  526. \Log::info('$wechatPay ok-------------------');
  527. $pay_info = $wechatPay->send($data);
  528. \Log::info('send ok-------------------');
  529. // 注册动作-》创建订单
  530. $action_type = 'CreateOrder';
  531. $param = [
  532. 'openid' => isset($openid) ? $openid : '0',
  533. 'uid' => isset($uid) ? $uid : '0',
  534. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  535. 'amount' => isset($price) ? $price : '0',
  536. ];
  537. \Log::info('FUCK_PALMPAYV2');
  538. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  539. return $pay_info;
  540. }
  541. public function waitPageV2(Request $request)
  542. {
  543. $order = $request->get('order');
  544. $order = (string)$order;
  545. $url = urldecode($request->get('redirect'));
  546. if (substr_count($url, '?') > 1) {
  547. $url = str_replace_last('?', '&', $url);
  548. }
  549. $url_info = parse_url($url);
  550. if (isset($url_info['query']) && !empty($url_info['query'])) {
  551. parse_str($url_info['query'], $path);
  552. if (isset($path['success']) && $path['success'] == 0) {
  553. if (isset($url_info['path']) && str_contains($url_info['path'], 'reader') && isset($path['cid']) && !empty($path['cid'])) {
  554. $chapter_info = ChapterService::getChapterNameByIdNoCheck($path['cid']);
  555. $path['cid'] = $chapter_info->prev_cid;
  556. $url = sprintf('%s://%s/reader?%s', $url_info['scheme'], $url_info['host'], http_build_query($path));
  557. }
  558. return redirect($url);
  559. }
  560. }
  561. if (isset($url_info['path']) && str_contains($url_info['path'], 'pay')) {
  562. $url = sprintf('%s://%s/recent', $url_info['scheme'], $url_info['host']);
  563. return redirect($url);
  564. }
  565. return view('pay.order.wait', compact('order', 'url'));
  566. }
  567. private function isNewUserSecondCharge($charge_type, $product_id, $uid)
  568. {
  569. //Log::info('新用户只能冲一次:' .$charge_type);
  570. if ($charge_type == 'NEW_USER') {
  571. $result = OrderService::getUserOrderByProductId($uid, $product_id);
  572. //Log::info('isNewUserSecondCharge-------------------------');
  573. //Log::info($result);
  574. if ($result) {
  575. return true;
  576. }
  577. }
  578. return false;
  579. }
  580. //订单创建后统计
  581. private function orderCreated($init_order)
  582. {
  583. try {
  584. if (isset($init_order['pay_merchant_id'])) {
  585. $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
  586. Redis::hincrby($key, 'unpaid_num', 1);
  587. Redis::hset($key, 'last_create_time', time());
  588. }
  589. } catch (\Exception $e) {
  590. }
  591. }
  592. //订单回调后统计
  593. private function orderPaid($init_order)
  594. {
  595. try {
  596. if (isset($init_order['pay_merchant_id'])) {
  597. $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
  598. Redis::hset($key, 'unpaid_num', 0);
  599. }
  600. } catch (\Exception $e) {
  601. }
  602. }
  603. public function reportError(Request $request)
  604. {
  605. $data = $request->post('data');
  606. DB::table('pay_page_error')->insert([
  607. 'msg' => $data,
  608. 'created_at' => date('Y-m-d H:i:s'),
  609. 'updated_at' => date('Y-m-d H:i:s')
  610. ]);
  611. return response()->success();
  612. }
  613. private function jsSdkSign($pay_info, $url)
  614. {
  615. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  616. return 0;
  617. }
  618. $appid = $pay_info['appId'];
  619. $jsapi_ticket = Redis::hget($appid, 'jsapi_ticket');
  620. //如果能获取到jsapi_ticket 则直接签名
  621. if ($jsapi_ticket) {
  622. $sign_arr = [
  623. 'noncestr' => $pay_info['nonceStr'],
  624. 'jsapi_ticket' => $jsapi_ticket,
  625. 'timestamp' => $pay_info['timeStamp'],
  626. 'url' => $url
  627. ];
  628. return sha1($this->arr_to_url($sign_arr));
  629. }
  630. //获取不到jsapi_ticket
  631. $app_secret_info = DB::table('official_setting')->select('secret')->first();
  632. if ($app_secret_info && !empty($app_secret_info->secret)) {
  633. $client = new Client(['timeout' => 5]);
  634. try {
  635. //获取access_token
  636. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $app_secret_info->secret;
  637. $res = $client->request('get', $url)->getBody()->getContents();
  638. if (!$res) {
  639. return 0;
  640. }
  641. $res = json_decode($res, 1);
  642. if (!isset($res['access_token']) || empty($res['access_token'])) {
  643. return 0;
  644. }
  645. Redis::hset($appid, 'access_token', $res['access_token']);
  646. //获取jsapi_ticket
  647. $res = null;
  648. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $res['access_token'] . '&type=jsapi';
  649. $res = $client->request('get', $url)->getBody()->getContents();
  650. if (!$res) {
  651. return 0;
  652. }
  653. $res = json_decode($res, 1);
  654. if (!isset($res['ticket']) || empty($res['ticket'])) {
  655. return 0;
  656. }
  657. Redis::hset($appid, 'jsapi_ticket', $res['ticket']);
  658. Redis::EXPIRE($appid, 7000);
  659. $sign_arr = [
  660. 'noncestr' => $pay_info['nonceStr'],
  661. 'jsapi_ticket' => $res['ticket'],
  662. 'timestamp' => $pay_info['timeStamp'],
  663. 'url' => $url
  664. ];
  665. return sha1($this->arr_to_url($sign_arr));
  666. } catch (\Exception $e) {
  667. return 0;
  668. }
  669. }
  670. return 0;
  671. }
  672. public function waitPage(Request $request)
  673. {
  674. $order = $request->input('order');
  675. //$order = "201712081711401585932843356442";
  676. $order = (string)$order;
  677. $url = urldecode($request->input('redirect'));
  678. return view('pay.order.wait', compact('order', 'url'));
  679. //return view('pay.order.wait');
  680. }
  681. /**
  682. * 官方微信回调
  683. * @param Request $request
  684. * @return \Symfony\Component\HttpFoundation\Response
  685. */
  686. function wcback_official(Request $request)
  687. {
  688. libxml_disable_entity_loader(true);
  689. $xml = XML::parse(strval($request->getContent()));
  690. Log::info('xml is');
  691. Log::info($xml);
  692. if (!$xml || !is_array($xml)) return 'fail';
  693. $remarks = json_decode($xml['attach'], true);
  694. $pay_merchant_id = $remarks['pmi'];
  695. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  696. $application = WechatPay::instance('OFFICIALPAY', json_decode($pay_merchant->config_info, true));
  697. //$app = new Application($options);
  698. $app = $application->app;
  699. $response = $app->payment->handleNotify(function ($notify, $successful) {
  700. if (!$successful) return 'fail';
  701. $trade_no = $notify->out_trade_no;
  702. $order = OrderService::getByTradeNo($trade_no);
  703. if (isset($order->status) && $order->status == 'PAID') {
  704. Log::info('has_pay:' . $trade_no);
  705. return true;
  706. }
  707. DB::beginTransaction();
  708. try {
  709. $transaction_id = $notify->transaction_id;
  710. $remarks = json_decode($notify->attach, true);
  711. $uid = $remarks['uid'];
  712. $distribution_channel_id = $remarks['dcd'];
  713. $product_id = $remarks['pd'];
  714. $product = ProductService::getProductSingle($product_id);
  715. $pay_merchant_source = $remarks['pms'];
  716. $pay_merchant_id = $remarks['pmi'];
  717. $send_order_id = $remarks['soi'];
  718. $create_ip = $remarks['cp'];
  719. $price = $product->price;
  720. $bid = $remarks['bid'];
  721. //$this->updateOrderTotal($trade_no,$transaction_id);
  722. $prize_fee = (int)(($product->price * 100) * 0.1);
  723. $this->substituteOrderPrice($order->id, $prize_fee);
  724. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  725. //获取用户充值次数
  726. $order->pay_type = $this->getChargeTimes($order->uid);
  727. // 更新其他定制Order表
  728. if ($product->type == 'YEAR_ORDER') {
  729. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  730. $order_type = 'YEAR';
  731. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  732. $order->order_type = $order_type;
  733. $order->status = 'PAID';
  734. $order->pay_end_at = date('Y-m-d H:i:s');
  735. $order->transaction_id = $transaction_id;
  736. $order->save();
  737. //$price = $price/100;
  738. /*
  739. $this->createOrder(
  740. compact(
  741. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  742. 'pay_merchant_id','create_ip','bid','transaction_id'
  743. )
  744. );*/
  745. } elseif ($product->type == 'BOOK_ORDER') {
  746. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  747. $order_type = 'BOOK';
  748. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  749. $order->order_type = $order_type;
  750. $order->status = 'PAID';
  751. $order->pay_end_at = date('Y-m-d H:i:s');
  752. $order->transaction_id = $transaction_id;
  753. $order->save();
  754. //$price = $price/100;
  755. /*
  756. $this->createOrder(
  757. compact(
  758. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  759. 'pay_merchant_id','create_ip','bid','transaction_id'
  760. )
  761. );*/
  762. } elseif ($product->type == 'TICKET_RECHARGE') {
  763. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  764. $order_type = 'RECHARGE';
  765. $this->userCharge($product, $uid);
  766. $order->order_type = $order_type;
  767. $order->status = 'PAID';
  768. $order->pay_end_at = date('Y-m-d H:i:s');
  769. $order->transaction_id = $transaction_id;
  770. $order->save();
  771. //$price = $price/100;
  772. /*
  773. $this->createOrder(
  774. compact(
  775. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  776. 'pay_merchant_id','create_ip','bid','transaction_id'
  777. ));*/
  778. } elseif ($product->type == 'FOREVER') {
  779. Log::info('FOREVER------------------------FOREVER');
  780. $order_type = 'FOREVER';
  781. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  782. $order->order_type = $order_type;
  783. $order->status = 'PAID';
  784. $order->pay_end_at = date('Y-m-d H:i:s');
  785. $order->transaction_id = $transaction_id;
  786. $order->save();
  787. } else {
  788. DB::rollback();
  789. return 'Order not exist.';
  790. }
  791. $this->successPayPushMsg($uid, $product, $order->id);
  792. $this->orderStatistical($order);
  793. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  794. Redis::hdel($key, $uid);
  795. DB::commit();
  796. $this->huaweiActivity($order->activity_id,$uid,$product_id);
  797. // 注册动作-》回调订单
  798. $action_type = 'CallBackOrder';
  799. $param = [
  800. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  801. 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
  802. ];
  803. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  804. //用户属性
  805. $this->userProperty($uid);
  806. return true;
  807. } catch (\Exception $e) {
  808. DB::rollback();
  809. return 'fail';
  810. }
  811. });
  812. return $response;
  813. }
  814. /**
  815. * 通联支付回调
  816. * @param Request $request
  817. */
  818. function wcback_allinpay(Request $request)
  819. {
  820. Log::info('wcback_allinpay_request');
  821. try {
  822. $data = $request->all();
  823. Log::info($request->all());
  824. if (!$data['trxreserved']) return 'fail';
  825. $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
  826. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  827. unset($data['_url']);
  828. if (!$pay_merchant->config_info) return 'fail';
  829. $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
  830. if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
  831. Log::info('allinpay_notify_sign_success');
  832. /**
  833. * 只有0000表示交易成功或下单成功,其他为失败
  834. * 0000:交易成功
  835. * 3045,3088:交易超时
  836. * 3008:余额不足
  837. * 3999:交易失败
  838. * 2008:交易处理中
  839. * 3050:交易已撤销
  840. */
  841. if ($data['trxstatus'] == '0000') {
  842. // 修改表比较多,开启事务
  843. DB::beginTransaction();
  844. $trade_no = $data['cusorderid'];
  845. $transaction_id = $data['chnltrxid'];
  846. //$openid = $data['acct'];
  847. $remarks = $data['trxreserved'];
  848. // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
  849. $remarks = json_decode($remarks, true);
  850. Log::info('$remarks');
  851. Log::info($remarks);
  852. // 更新Order
  853. $order = OrderService::getByTradeNo($trade_no);
  854. // 防止重复推送
  855. if (isset($order->status) && $order->status == 'PAID') {
  856. Log::info('has_pay:' . $trade_no);
  857. echo 'success';
  858. exit();
  859. }
  860. $uid = $remarks['uid'];
  861. $distribution_channel_id = $remarks['dcd'];
  862. $product_id = $remarks['pd'];
  863. $product = ProductService::getProductSingle($product_id);
  864. $pay_merchant_source = $remarks['pms'];
  865. $pay_merchant_id = $remarks['pmi'];
  866. $send_order_id = $remarks['soi'];
  867. $create_ip = $remarks['cp'];
  868. $price = $product->price;
  869. $bid = $remarks['bid'];
  870. Log::info('save_order_end');
  871. //$this->updateOrderTotal($trade_no,$transaction_id);
  872. Log::info($product);
  873. Log::info('product_type:' . $product->type);
  874. $prize_fee = (int)(($product->price * 100) * 0.1);
  875. $this->substituteOrderPrice($order->id, $prize_fee);
  876. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  877. //获取用户充值次数
  878. $order->pay_type = $this->getChargeTimes($order->uid);
  879. // 更新其他定制Order表
  880. if ($product->type == 'YEAR_ORDER') {
  881. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  882. $order_type = 'YEAR';
  883. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  884. $order->order_type = $order_type;
  885. $order->status = 'PAID';
  886. $order->pay_end_at = date('Y-m-d H:i:s');
  887. $order->transaction_id = $transaction_id;
  888. $order->save();
  889. //$price = $price/100;
  890. /*
  891. $this->createOrder(
  892. compact(
  893. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  894. 'pay_merchant_id','create_ip','bid','transaction_id'
  895. )
  896. );*/
  897. } elseif ($product->type == 'BOOK_ORDER') {
  898. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  899. $order_type = 'BOOK';
  900. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  901. $order->order_type = $order_type;
  902. $order->status = 'PAID';
  903. $order->pay_end_at = date('Y-m-d H:i:s');
  904. $order->transaction_id = $transaction_id;
  905. $order->save();
  906. //$price = $price/100;
  907. /*
  908. $this->createOrder(
  909. compact(
  910. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  911. 'pay_merchant_id','create_ip','bid','transaction_id'
  912. )
  913. );*/
  914. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  915. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  916. $order_type = 'RECHARGE';
  917. $this->userCharge($product, $uid);
  918. $order->order_type = $order_type;
  919. $order->status = 'PAID';
  920. $order->pay_end_at = date('Y-m-d H:i:s');
  921. $order->transaction_id = $transaction_id;
  922. $order->save();
  923. //$price = $price/100;
  924. /*
  925. $this->createOrder(
  926. compact(
  927. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  928. 'pay_merchant_id','create_ip','bid','transaction_id'
  929. ));*/
  930. } elseif ($product->type == 'FOREVER') {
  931. Log::info('FOREVER------------------------FOREVER');
  932. $order_type = 'FOREVER';
  933. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  934. $order->order_type = $order_type;
  935. $order->status = 'PAID';
  936. $order->pay_end_at = date('Y-m-d H:i:s');
  937. $order->transaction_id = $transaction_id;
  938. $order->save();
  939. }
  940. $this->successPayPushMsg($uid, $product, $order->id);
  941. $this->orderStatistical($order);
  942. DB::commit();
  943. $this->userProperty($uid);
  944. $this->huaweiActivity($order->activity_id,$uid,$product_id);
  945. //redis 删除未支付的uid
  946. try {
  947. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  948. Redis::hdel($key, $uid);
  949. //Redis::srem($key,$uid);
  950. } catch (\Exception $e) {
  951. Log::info('redis remote error-----------------------');
  952. Log::info($e);
  953. }
  954. }
  955. } else {
  956. Log::info('allinpay_notify_sign_fail');
  957. }
  958. } catch (\Exception $e) {
  959. DB::rollBack();
  960. Log::info('receive_allinpay_ept:' . $e->getMessage());
  961. Log::info('pay_callback_end fail');
  962. echo 'fail';
  963. return;
  964. }
  965. Log::info('pay_callback_end');
  966. echo "success";
  967. }
  968. public function wcback_lianlianpay(Request $request)
  969. {
  970. $data = $request->getContent();
  971. Log::info('wcback_lianlianpay call back enter');
  972. Log::info($request->getContent());
  973. $data = json_decode($data, true);
  974. if ($data['result_pay'] == 'SUCCESS') {
  975. $trade_no = $data['no_order'];
  976. $order = OrderService::getByTradeNo($trade_no);
  977. if (!$order) return response()->json(['ret_code' => '-1']);
  978. $pay_merchant_id = $order->pay_merchant_id;
  979. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  980. unset($data['_url']);
  981. if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
  982. $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
  983. $uid = $order->uid;
  984. $distribution_channel_id = $order->distribution_channel_id;
  985. $price = $order->price;
  986. $send_order_id = $order->send_order_id;
  987. $product_id = $order->product_id;
  988. if (!$wechatPay->rsaCheck($data)) {
  989. Log::info('sign check error');
  990. //return response()->json(['ret_code'=>'-1']);
  991. }
  992. // 防止重复推送
  993. if (isset($order->status) && $order->status == 'PAID') {
  994. Log::info('has_pay:' . $trade_no);
  995. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  996. }
  997. DB::beginTransaction();
  998. try {
  999. $product = ProductService::getProductSingle($order->product_id);
  1000. $transaction_id = $data['oid_paybill'];
  1001. $prize_fee = (int)(($product->price * 100));
  1002. $this->substituteOrderPrice($order->id, $prize_fee);
  1003. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1004. //获取用户充值次数
  1005. $order->pay_type = $this->getChargeTimes($order->uid);
  1006. // 更新其他定制Order表
  1007. if ($product->type == 'YEAR_ORDER') {
  1008. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1009. $order_type = 'YEAR';
  1010. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1011. $order->order_type = $order_type;
  1012. $order->status = 'PAID';
  1013. $order->pay_end_at = date('Y-m-d H:i:s');
  1014. $order->transaction_id = $transaction_id;
  1015. $order->save();
  1016. } elseif ($product->type == 'BOOK_ORDER') {
  1017. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1018. $order_type = 'BOOK';
  1019. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1020. $order->order_type = $order_type;
  1021. $order->status = 'PAID';
  1022. $order->pay_end_at = date('Y-m-d H:i:s');
  1023. $order->transaction_id = $transaction_id;
  1024. $order->save();
  1025. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1026. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1027. $order_type = 'RECHARGE';
  1028. $this->userCharge($product, $uid);
  1029. $order->order_type = $order_type;
  1030. $order->status = 'PAID';
  1031. $order->pay_end_at = date('Y-m-d H:i:s');
  1032. $order->transaction_id = $transaction_id;
  1033. $order->save();
  1034. } elseif ($product->type == 'FOREVER') {
  1035. Log::info('FOREVER------------------------FOREVER');
  1036. $order_type = 'FOREVER';
  1037. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1038. $order->order_type = $order_type;
  1039. $order->status = 'PAID';
  1040. $order->pay_end_at = date('Y-m-d H:i:s');
  1041. $order->transaction_id = $transaction_id;
  1042. $order->save();
  1043. }
  1044. $this->successPayPushMsg($uid, $product, $order->id);
  1045. $this->orderStatistical($order);
  1046. DB::commit();
  1047. $this->userProperty($uid);
  1048. $this->huaweiActivity($order->activity_id,$uid,$product_id);
  1049. //redis 删除未支付的uid
  1050. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
  1051. Redis::hdel($key, $uid);
  1052. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  1053. } catch (\Exception $e) {
  1054. DB::rollBack();
  1055. Log::info('lianlian:' . $e->getMessage());
  1056. echo 'fail';
  1057. return;
  1058. }
  1059. }
  1060. echo 'success';
  1061. }
  1062. /**
  1063. * 微众支付回调
  1064. * @param Request $request
  1065. */
  1066. function wcback_palmpay(Request $request)
  1067. {
  1068. Log::info('wcback_palmpay_request');
  1069. try {
  1070. $data = $request->except('_url');
  1071. Log::info($request->all());
  1072. //订单
  1073. $trade_no = $data['outTradeNo'];
  1074. $order = OrderService::getByTradeNo($trade_no);
  1075. if (!$order) die('failed');
  1076. // 防止重复推送
  1077. if (isset($order->status) && $order->status == 'PAID') {
  1078. if($data['chorderid'] != $order->transaction_id){
  1079. Log::info('palmpay order repeat');
  1080. $this->palmpaySpecial($data['chorderid'],$trade_no);
  1081. }
  1082. Log::info('has_pay:' . $trade_no);
  1083. echo 'success';
  1084. exit();
  1085. }
  1086. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  1087. //校验签名
  1088. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  1089. if ($sign != $data['sign']) die('failed');
  1090. $transaction_id = $data['chorderid'];
  1091. if ($data['status'] == 'success') {
  1092. Log::info('wcback_palmpay_notify_sign_success');
  1093. if ($data['status'] == 'success') {
  1094. // 修改表比较多,开启事务
  1095. DB::beginTransaction();
  1096. // 更新Order
  1097. $order = OrderService::getByTradeNo($trade_no);
  1098. $uid = $order->uid;
  1099. $distribution_channel_id = $order->distribution_channel_id;
  1100. $product_id = $order->product_id;
  1101. $product = ProductService::getProductSingle($product_id);
  1102. $send_order_id = $order->send_order_id;
  1103. $price = $product->price;
  1104. Log::info('save_order_end');
  1105. Log::info($product);
  1106. Log::info('product_type:' . $product->type);
  1107. $prize_fee = (int)(($product->price * 100) * 0.1);
  1108. $this->substituteOrderPrice($order->id, $prize_fee);
  1109. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1110. //获取用户充值次数
  1111. $order->pay_type = $this->getChargeTimes($order->uid);
  1112. // 更新其他定制Order表
  1113. if ($product->type == 'YEAR_ORDER') {
  1114. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1115. $order_type = 'YEAR';
  1116. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1117. $order->order_type = $order_type;
  1118. $order->status = 'PAID';
  1119. $order->pay_end_at = date('Y-m-d H:i:s');
  1120. $order->transaction_id = $transaction_id;
  1121. $order->save();
  1122. } elseif ($product->type == 'BOOK_ORDER') {
  1123. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1124. $order_type = 'BOOK';
  1125. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1126. $order->order_type = $order_type;
  1127. $order->status = 'PAID';
  1128. $order->pay_end_at = date('Y-m-d H:i:s');
  1129. $order->transaction_id = $transaction_id;
  1130. $order->save();
  1131. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1132. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1133. $order_type = 'RECHARGE';
  1134. $this->userCharge($product, $uid);
  1135. $order->order_type = $order_type;
  1136. $order->status = 'PAID';
  1137. $order->pay_end_at = date('Y-m-d H:i:s');
  1138. $order->transaction_id = $transaction_id;
  1139. $order->save();
  1140. } elseif ($product->type == 'FOREVER') {
  1141. Log::info('FOREVER------------------------FOREVER');
  1142. $order_type = 'FOREVER';
  1143. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1144. $order->order_type = $order_type;
  1145. $order->status = 'PAID';
  1146. $order->pay_end_at = date('Y-m-d H:i:s');
  1147. $order->transaction_id = $transaction_id;
  1148. $order->save();
  1149. }
  1150. $this->successPayPushMsg($uid, $product, $order->id);
  1151. $this->orderStatistical($order);
  1152. DB::commit();
  1153. //redis 删除未支付的uid
  1154. try {
  1155. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1156. Redis::hdel($key, $uid);
  1157. } catch (\Exception $e) {
  1158. Log::info('redis remote error-----------------------');
  1159. Log::info($e);
  1160. }
  1161. // 注册动作-》回调订单
  1162. $action_type = 'CallBackOrder';
  1163. $param = [
  1164. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1165. 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
  1166. ];
  1167. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1168. $this->userProperty($uid);
  1169. $this->huaweiActivity($order->activity_id,$uid,$product_id);
  1170. }
  1171. } else {
  1172. Log::info('wcback_palmpay_notify_sign_fail');
  1173. }
  1174. } catch (\Exception $e) {
  1175. var_dump($e->getMessage());
  1176. DB::rollBack();
  1177. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1178. echo 'fail';
  1179. Log::info('pay_callback_end error');
  1180. return;
  1181. }
  1182. Log::info('pay_callback_end');
  1183. echo "success";
  1184. }
  1185. /**
  1186. * 公众号签名@华灯初上
  1187. * @param $params
  1188. * @return string
  1189. */
  1190. protected function getSign($params, $key)
  1191. {
  1192. $url = $this->arr_to_url($params, false);
  1193. $url = $url . '&key=' . $key;
  1194. $sign = md5($url);
  1195. return $sign;
  1196. }
  1197. /**
  1198. * 公众号签名@华灯初上
  1199. * @param $array
  1200. * @param bool $has_sign
  1201. * @return string
  1202. */
  1203. protected function arr_to_url($array, $has_sign = false)
  1204. {
  1205. ksort($array);
  1206. reset($array);
  1207. $arg = "";
  1208. while (list ($name, $val) = each($array)) {
  1209. if ($name == 'sign' && !$has_sign) continue;
  1210. if (strpos($name, "_") === 0)
  1211. continue;
  1212. if (is_array($val))
  1213. $val = join(',', $val);
  1214. if ($val === "")
  1215. continue;
  1216. $arg .= $name . "=" . $val . "&";
  1217. }
  1218. $arg = substr($arg, 0, count($arg) - 2);
  1219. return $arg;
  1220. }
  1221. /**
  1222. * 单本充值会掉
  1223. * @param $product_id
  1224. * @param $uid
  1225. * @param $send_order_id
  1226. * @param $fee
  1227. */
  1228. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  1229. {
  1230. $book_conf = BookConfigService::getBookByProduct($product_id);
  1231. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  1232. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  1233. $insert_data['uid'] = $uid;
  1234. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1235. $insert_data['fee'] = $fee;
  1236. $insert_data['send_order_id'] = $send_order_id;
  1237. $insert_data['charge_balance'] = 0;
  1238. $insert_data['reward_balance'] = 0;
  1239. Log::info('start_save_book_order');
  1240. Log::info($insert_data);
  1241. return BookOrderService::save_book_order($insert_data);
  1242. }
  1243. /**
  1244. * 包年
  1245. * @param $uid
  1246. * @param $distribution_channel_id
  1247. * @param $fee
  1248. * @param $send_order_id
  1249. * @return mixed
  1250. */
  1251. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1252. {
  1253. Log::info('start_save_year_order');
  1254. $insert_data['uid'] = $uid;
  1255. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1256. $insert_data['fee'] = $fee;
  1257. $insert_data['send_order_id'] = $send_order_id;
  1258. Log::info($insert_data);
  1259. return YearOrderService::save_year_order($insert_data);
  1260. }
  1261. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1262. {
  1263. Log::info('start_save_year_order');
  1264. ('start_forever_Free_Order');
  1265. $insert_data['uid'] = $uid;
  1266. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1267. $insert_data['fee'] = $fee;
  1268. $insert_data['send_order_id'] = $send_order_id;
  1269. Log::info($insert_data);
  1270. return YearOrderService::foreverFree($insert_data);
  1271. }
  1272. /**
  1273. * 用户充值
  1274. * @param $product
  1275. * @param $uid\
  1276. */
  1277. protected function userCharge($product, $uid)
  1278. {
  1279. $total = $product->price * 100 + $product->given;
  1280. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  1281. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  1282. }
  1283. /**
  1284. * 添加订单
  1285. * @param array $data
  1286. * @return mixed
  1287. */
  1288. protected function createOrder(array $data)
  1289. {
  1290. $insert_data = array();
  1291. $insert_data['uid'] = $data['uid'];
  1292. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  1293. $insert_data['product_id'] = $data['product_id'];
  1294. $insert_data['price'] = $data['price'];
  1295. $insert_data['status'] = 'PAID';
  1296. $insert_data['pay_type'] = 1;
  1297. $insert_data['trade_no'] = $data['trade_no'];
  1298. $insert_data['send_order_id'] = $data['send_order_id'];
  1299. $insert_data['order_type'] = $data['order_type'];
  1300. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  1301. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  1302. $insert_data['transaction_id'] = $data['transaction_id'];
  1303. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  1304. $insert_data['create_ip'] = $data['create_ip'];
  1305. $insert_data['from_bid'] = $data['bid'];
  1306. Log::info('createOrder');
  1307. Log::info($insert_data);
  1308. return OrderService::save_order($insert_data);
  1309. }
  1310. /**
  1311. * 添加位置付订单
  1312. * @param $data
  1313. * @return mixed
  1314. */
  1315. protected function createUnPayOrder($data)
  1316. {
  1317. $data['status'] = 'UNPAID';
  1318. $data['transaction_id'] = '';
  1319. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1320. return OrderService::save_order($data);
  1321. }
  1322. /**
  1323. * order total
  1324. * @param $data
  1325. *
  1326. */
  1327. protected function createOrderTotal($data)
  1328. {
  1329. $data['status'] = 'UNPAID';
  1330. $data['created_at'] = date('Y-m-d H:i:s');
  1331. $data['updated_at'] = date('Y-m-d H:i:s');
  1332. DB::table('orders_total')->insert($data);
  1333. }
  1334. protected function updateOrderTotal($trade_no, $transaction_id)
  1335. {
  1336. $data['status'] = 'PAID';
  1337. $data['transaction_id'] = $transaction_id;
  1338. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1339. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1340. }
  1341. protected function successPayPushMsg($uid, $product_info, $order_id)
  1342. {
  1343. try {
  1344. $force_sub_info = $this->getSubscribeV2($uid);
  1345. $data = UserService::getById($uid);
  1346. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1347. //return false;
  1348. }
  1349. if ($force_sub_info) {
  1350. //$content_format = "您好,你已经成功充值\r\n\r\n会员:%s\r\n会员ID:%s\r\n充值金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1351. if (SubstituteOrderService::getByOrderId($order_id)) {
  1352. $content_format = "代充值客服消息:\r\n\r\n你好,您的好友为你代付成功\r\n会员:%s\r\n会员ID:%s\r\n代付金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1353. } else {
  1354. $content_format = "您好,你已经成功充值\r\n\r\n会员:%s\r\n会员ID:%s\r\n充值金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1355. }
  1356. if ($product_info->type == 'YEAR_ORDER') {
  1357. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1358. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1359. if ($product_info->given) {
  1360. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1361. } else {
  1362. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1363. }
  1364. } else {
  1365. $money_text = '未知';
  1366. }
  1367. $delay = 0;
  1368. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1369. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1370. foreach ($force_sub_info as $item) {
  1371. $res['openid'] = $item->openid;
  1372. $res['appid'] = $item->appid;
  1373. $res['content'] = $content;
  1374. $res['type'] = 'one_task';
  1375. $res['send_time'] = date("Y-m-d H:i:s");
  1376. $res['task_id'] = md5('pay_success_push');
  1377. $send_data = array(
  1378. 'send_time' => date("Y-m-d H:i:s"),
  1379. 'data' => $res
  1380. );
  1381. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1382. }
  1383. }
  1384. } catch (\Exception $e) {
  1385. Log::info('pay_success_push error');
  1386. Log::info($e);
  1387. }
  1388. return true;
  1389. }
  1390. private function orderStatistical($order)
  1391. {
  1392. try {
  1393. $send_data = array(
  1394. 'type' => 'order_info',
  1395. 'data' => $order->toArray()
  1396. );
  1397. $send = new SendStatisticsList($send_data);
  1398. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  1399. dispatch($job);
  1400. } catch (\Exception $e) {
  1401. Log::info($e);
  1402. }
  1403. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  1404. try {
  1405. if ($order->from_type) {
  1406. $pv = Redis::hget('push:distribution_channel_id:allpv', $order->from_type);
  1407. if ($pv) {
  1408. Redis::sadd('push:all:paidnum:from:' . $order->from_type, $order->uid);
  1409. $old = Redis::hget('push:all:paidamount', $order->from_type);
  1410. if ($old) {
  1411. Redis::hset('push:all:paidamount', $order->from_type, $old + $order->price);
  1412. } else {
  1413. Redis::hset('push:all:paidamount', $order->from_type, $order->price);
  1414. }
  1415. }
  1416. }
  1417. if (isset($order->pay_merchant_id) && $order->pay_merchant_id)//临时统计队列清0 用于报警
  1418. {
  1419. $key = 'pay_merchant:' . $order->pay_merchant_id;
  1420. Redis::hset($key, 'unpaid_num', 0);
  1421. }
  1422. } catch (\Exception $e) {
  1423. }
  1424. }
  1425. private function getSubscribe($uid)
  1426. {
  1427. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  1428. }
  1429. private function getSubscribeV2($uid)
  1430. {
  1431. return DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->get();
  1432. }
  1433. public function getSubscribeOfficialName($uid, $distribution_channel_id)
  1434. {
  1435. $subscribe = $this->getSubscribe($uid);
  1436. if ($subscribe && isset($subscribe->appid)) {
  1437. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  1438. if ($official && isset($official->nickname)) {
  1439. return $official->nickname;
  1440. }
  1441. }
  1442. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  1443. if (isset($res->nickname) && !empty($res->nickname)) {
  1444. return $res->nickname;
  1445. }
  1446. try {
  1447. $official = DB::table('official_accounts')
  1448. ->where('distribution_channel_id', $distribution_channel_id)
  1449. ->where('is_auth', 1)
  1450. ->orderBy('id', 'desc')
  1451. ->select('nickname')->first();
  1452. if ($official && isset($official->nickname)) {
  1453. return $official->nickname;
  1454. }
  1455. } catch (\Exception $e) {
  1456. }
  1457. return '';
  1458. }
  1459. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  1460. {
  1461. $change_pay_id = 40;
  1462. if ($pay_merchant_id == 40) {
  1463. $change_pay_id = 9;
  1464. }
  1465. try {
  1466. $time = (int)date('G');
  1467. $change = false;
  1468. if ($time <= 8 || $time >= 23) {
  1469. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  1470. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  1471. $change = true;
  1472. }
  1473. }
  1474. //$number,$template_type,$param=array()
  1475. $phone_arr = ['15868100210', '15088790066', '13858057394', '18668029091', '18668420256'];
  1476. //$phone_arr = ['18668029091'];
  1477. $param = ['pay_id' => $pay_merchant_id];
  1478. if ($n) {
  1479. $template_type = 'pay_channel_remind';
  1480. //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  1481. } else {
  1482. $template_type = 'pay_channel_remind';
  1483. //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  1484. }
  1485. if ($change) {
  1486. $template_type = 'pay_channel_change';
  1487. $param = ['pay_id' => $pay_merchant_id, 'new_pay_id' => $change_pay_id];
  1488. }
  1489. $last_alert_time = Redis::hget('autopayalert',$pay_merchant_id);
  1490. if($last_alert_time && (time() -$last_alert_time) <= 3600 ){
  1491. return ;
  1492. }
  1493. Redis::hset('autopayalert',$pay_merchant_id,time());
  1494. foreach ($phone_arr as $phone) {
  1495. AliSMS::send($phone, $template_type, $param);
  1496. }
  1497. } catch (\Exception $e) {
  1498. }
  1499. }
  1500. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  1501. {
  1502. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  1503. }
  1504. private function recordOtherParam($order_id, $uid)
  1505. {
  1506. $gxhp = ReadRecordService::getByField($uid, 'gxhp');
  1507. if ($gxhp) {
  1508. OrderParamService::create($order_id, $gxhp);
  1509. }
  1510. }
  1511. private function substituteOrderPrice($order_id, $prize_fee = 500)
  1512. {
  1513. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  1514. }
  1515. private function updateUserSendOrderId(int $uid, $send_order_id)
  1516. {
  1517. if ($send_order_id && $uid) {
  1518. $user = UserService::getById($uid);
  1519. if ($user && !$user->send_order_id) {
  1520. UserService::updateInfo($uid, compact('send_order_id'));
  1521. }
  1522. }
  1523. }
  1524. /**
  1525. * 测书智能推送统计 付费用户数、充值金额
  1526. * @param $bid
  1527. * @param $uid
  1528. * @param $price
  1529. */
  1530. private function smartPushTestBookPaidUv($bid, $uid, $price)
  1531. {
  1532. $smart_bid = ReadRecordService::getSmartPush($uid);
  1533. if ($smart_bid && in_array($bid, $smart_bid)) {
  1534. try {
  1535. $key = 'smartPushTestBookPaidUv:bid:%s';
  1536. Redis::sadd(sprintf($key, $bid), $uid);
  1537. $now_amount = Redis::hget('smartPushTestBookPaidAmount', $bid);
  1538. if (!$now_amount) {
  1539. $now_amount = 0;
  1540. }
  1541. Redis::hset('smartPushTestBookPaidAmount', $bid, $now_amount + $price);
  1542. } catch (\Exception $e) {
  1543. }
  1544. }
  1545. }
  1546. /**
  1547. * 获取用户第几次充值
  1548. * @param $uid
  1549. * @return int
  1550. */
  1551. private function getChargeTimes($uid)
  1552. {
  1553. $count = OrderService::getUserChargeTimes($uid);
  1554. return $count + 1;
  1555. }
  1556. //米玩 订单转移
  1557. private function transfer($distribution_channel_id)
  1558. {
  1559. if (in_array($distribution_channel_id, explode(',', redisEnv('MIWAN_TRANSFER_CHANNEL_ID', 1)))) {
  1560. $random = random_int(1, 100);
  1561. $miwan_transfer_probability = redisEnv('MIWAN_TRANSFER_PROBABILITY', 0);
  1562. $miwan_transfer_to_channel_id = redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0);
  1563. if ($miwan_transfer_to_channel_id && $random <= $miwan_transfer_probability) {
  1564. return $miwan_transfer_to_channel_id;
  1565. }
  1566. }
  1567. return 0;
  1568. }
  1569. private function userProperty($uid){
  1570. try{
  1571. $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
  1572. if(!$userproperty) return ;
  1573. UserDivisionCpcPropertyService::createorUpdate($userproperty);
  1574. }catch (\Exception $e){}
  1575. }
  1576. private function huaweiActivity($activity_id,$uid,$product_id){
  1577. $huawei_activity_id = redisEnv('HUAWEI_ACTIVITY_ID');
  1578. if($huawei_activity_id && $activity_id && $activity_id == $huawei_activity_id){
  1579. $product_id_relate_lottery_count = ['6117'=>1,'6118'=>2,'6119'=>4,'6120'=>8];
  1580. if(isset($product_id_relate_lottery_count[$product_id])){
  1581. $info = DB::table('huaweilottery_user_counts')->where('uid',$uid)->select('count')->first();
  1582. if($info){
  1583. DB::table('huaweilottery_user_counts')->where('uid',$uid)->update([
  1584. 'uid'=>$uid,
  1585. 'count'=>$info->count+$product_id_relate_lottery_count[$product_id],
  1586. 'updated_at'=>date('Y-m-d H:i:s')
  1587. ]);
  1588. }else{
  1589. DB::table('huaweilottery_user_counts')->insert([
  1590. 'uid'=>$uid,
  1591. 'count'=>$product_id_relate_lottery_count[$product_id],
  1592. 'updated_at'=>date('Y-m-d H:i:s'),
  1593. 'created_at'=>date('Y-m-d H:i:s')
  1594. ]);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. private function palmpaySpecial($transaction_id,$trade_no)
  1600. {
  1601. try{
  1602. $order = OrderService::getByTradeNo($trade_no);
  1603. $product_id = $order->product_id;
  1604. $product = ProductService::getProductSingle($product_id);
  1605. $init_order = [
  1606. 'distribution_channel_id' => $order->distribution_channel_id,
  1607. 'uid' => $order->uid,
  1608. 'product_id' => $product_id,
  1609. 'price' => $order->price,
  1610. 'pay_type' => 1,
  1611. 'trade_no' => $order->uid.'-'.$order->id,
  1612. 'pay_merchant_source' => $order->pay_merchant_source,
  1613. 'pay_merchant_id' => $order->pay_merchant_id,
  1614. 'create_ip' => $order->create_ip,
  1615. 'send_order_id' => $order->send_order_id,
  1616. 'send_order_name' => $order->send_order_name,
  1617. 'order_type' => $order->order_type,
  1618. 'from_bid' => $order->from_bid,
  1619. 'from_type' => $order->fromtype,
  1620. 'activity_id' => $order->activity_id,
  1621. 'inner_send_order_id' => $order->inner_send_order_id,
  1622. 'status'=>'PAID',
  1623. 'transaction_id'=>$transaction_id,
  1624. 'pay_end_at'=>date('Y-m-d H:i:s')
  1625. ];
  1626. OrderService::save_order($init_order);
  1627. if ($product->type == 'YEAR_ORDER') {
  1628. Log::info('palmpaySpecial YEAR_ORDER');
  1629. $this->yearOrder($order->uid, $order->distribution_channel_id, $order->price, $order->send_order_id);
  1630. } elseif ($product->type == 'BOOK_ORDER') {
  1631. Log::info('palmpaySpecial BOOK_ORDER');
  1632. $this->bookOrder($product_id, $order->ui, $order->send_order_id, $order->price, $order->distribution_channel_id);
  1633. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1634. Log::info('palmpaySpecial TICKET_RECHARGE');
  1635. $this->userCharge($product, $order->uid);
  1636. } elseif ($product->type == 'FOREVER') {
  1637. Log::info('palmpaySpecial-------FOREVER');
  1638. $this->foreverFreeOrder($order->uid, $order->distribution_channel_id, $order->price, $order->send_order_id);
  1639. }
  1640. }catch (\Exception $e){
  1641. }
  1642. }
  1643. }