OrdersController.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. Log::info('has_pay:' . $trade_no);
  1079. echo 'success';
  1080. exit();
  1081. }
  1082. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  1083. //校验签名
  1084. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  1085. if ($sign != $data['sign']) die('failed');
  1086. $transaction_id = $data['chorderid'];
  1087. if ($data['status'] == 'success') {
  1088. Log::info('wcback_palmpay_notify_sign_success');
  1089. if ($data['status'] == 'success') {
  1090. // 修改表比较多,开启事务
  1091. DB::beginTransaction();
  1092. // 更新Order
  1093. $order = OrderService::getByTradeNo($trade_no);
  1094. $uid = $order->uid;
  1095. $distribution_channel_id = $order->distribution_channel_id;
  1096. $product_id = $order->product_id;
  1097. $product = ProductService::getProductSingle($product_id);
  1098. $send_order_id = $order->send_order_id;
  1099. $price = $product->price;
  1100. Log::info('save_order_end');
  1101. Log::info($product);
  1102. Log::info('product_type:' . $product->type);
  1103. $prize_fee = (int)(($product->price * 100) * 0.1);
  1104. $this->substituteOrderPrice($order->id, $prize_fee);
  1105. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1106. //获取用户充值次数
  1107. $order->pay_type = $this->getChargeTimes($order->uid);
  1108. // 更新其他定制Order表
  1109. if ($product->type == 'YEAR_ORDER') {
  1110. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1111. $order_type = 'YEAR';
  1112. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1113. $order->order_type = $order_type;
  1114. $order->status = 'PAID';
  1115. $order->pay_end_at = date('Y-m-d H:i:s');
  1116. $order->transaction_id = $transaction_id;
  1117. $order->save();
  1118. } elseif ($product->type == 'BOOK_ORDER') {
  1119. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1120. $order_type = 'BOOK';
  1121. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1122. $order->order_type = $order_type;
  1123. $order->status = 'PAID';
  1124. $order->pay_end_at = date('Y-m-d H:i:s');
  1125. $order->transaction_id = $transaction_id;
  1126. $order->save();
  1127. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1128. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1129. $order_type = 'RECHARGE';
  1130. $this->userCharge($product, $uid);
  1131. $order->order_type = $order_type;
  1132. $order->status = 'PAID';
  1133. $order->pay_end_at = date('Y-m-d H:i:s');
  1134. $order->transaction_id = $transaction_id;
  1135. $order->save();
  1136. } elseif ($product->type == 'FOREVER') {
  1137. Log::info('FOREVER------------------------FOREVER');
  1138. $order_type = 'FOREVER';
  1139. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1140. $order->order_type = $order_type;
  1141. $order->status = 'PAID';
  1142. $order->pay_end_at = date('Y-m-d H:i:s');
  1143. $order->transaction_id = $transaction_id;
  1144. $order->save();
  1145. }
  1146. $this->successPayPushMsg($uid, $product, $order->id);
  1147. $this->orderStatistical($order);
  1148. DB::commit();
  1149. //redis 删除未支付的uid
  1150. try {
  1151. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1152. Redis::hdel($key, $uid);
  1153. } catch (\Exception $e) {
  1154. Log::info('redis remote error-----------------------');
  1155. Log::info($e);
  1156. }
  1157. // 注册动作-》回调订单
  1158. $action_type = 'CallBackOrder';
  1159. $param = [
  1160. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1161. 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
  1162. ];
  1163. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1164. $this->userProperty($uid);
  1165. $this->huaweiActivity($order->activity_id,$uid,$product_id);
  1166. }
  1167. } else {
  1168. Log::info('wcback_palmpay_notify_sign_fail');
  1169. }
  1170. } catch (\Exception $e) {
  1171. var_dump($e->getMessage());
  1172. DB::rollBack();
  1173. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1174. echo 'fail';
  1175. Log::info('pay_callback_end error');
  1176. return;
  1177. }
  1178. Log::info('pay_callback_end');
  1179. echo "success";
  1180. }
  1181. /**
  1182. * 公众号签名@华灯初上
  1183. * @param $params
  1184. * @return string
  1185. */
  1186. protected function getSign($params, $key)
  1187. {
  1188. $url = $this->arr_to_url($params, false);
  1189. $url = $url . '&key=' . $key;
  1190. $sign = md5($url);
  1191. return $sign;
  1192. }
  1193. /**
  1194. * 公众号签名@华灯初上
  1195. * @param $array
  1196. * @param bool $has_sign
  1197. * @return string
  1198. */
  1199. protected function arr_to_url($array, $has_sign = false)
  1200. {
  1201. ksort($array);
  1202. reset($array);
  1203. $arg = "";
  1204. while (list ($name, $val) = each($array)) {
  1205. if ($name == 'sign' && !$has_sign) continue;
  1206. if (strpos($name, "_") === 0)
  1207. continue;
  1208. if (is_array($val))
  1209. $val = join(',', $val);
  1210. if ($val === "")
  1211. continue;
  1212. $arg .= $name . "=" . $val . "&";
  1213. }
  1214. $arg = substr($arg, 0, count($arg) - 2);
  1215. return $arg;
  1216. }
  1217. /**
  1218. * 单本充值会掉
  1219. * @param $product_id
  1220. * @param $uid
  1221. * @param $send_order_id
  1222. * @param $fee
  1223. */
  1224. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  1225. {
  1226. $book_conf = BookConfigService::getBookByProduct($product_id);
  1227. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  1228. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  1229. $insert_data['uid'] = $uid;
  1230. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1231. $insert_data['fee'] = $fee;
  1232. $insert_data['send_order_id'] = $send_order_id;
  1233. $insert_data['charge_balance'] = 0;
  1234. $insert_data['reward_balance'] = 0;
  1235. Log::info('start_save_book_order');
  1236. Log::info($insert_data);
  1237. return BookOrderService::save_book_order($insert_data);
  1238. }
  1239. /**
  1240. * 包年
  1241. * @param $uid
  1242. * @param $distribution_channel_id
  1243. * @param $fee
  1244. * @param $send_order_id
  1245. * @return mixed
  1246. */
  1247. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1248. {
  1249. Log::info('start_save_year_order');
  1250. $insert_data['uid'] = $uid;
  1251. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1252. $insert_data['fee'] = $fee;
  1253. $insert_data['send_order_id'] = $send_order_id;
  1254. Log::info($insert_data);
  1255. return YearOrderService::save_year_order($insert_data);
  1256. }
  1257. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1258. {
  1259. Log::info('start_save_year_order');
  1260. ('start_forever_Free_Order');
  1261. $insert_data['uid'] = $uid;
  1262. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1263. $insert_data['fee'] = $fee;
  1264. $insert_data['send_order_id'] = $send_order_id;
  1265. Log::info($insert_data);
  1266. return YearOrderService::foreverFree($insert_data);
  1267. }
  1268. /**
  1269. * 用户充值
  1270. * @param $product
  1271. * @param $uid\
  1272. */
  1273. protected function userCharge($product, $uid)
  1274. {
  1275. $total = $product->price * 100 + $product->given;
  1276. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  1277. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  1278. }
  1279. /**
  1280. * 添加订单
  1281. * @param array $data
  1282. * @return mixed
  1283. */
  1284. protected function createOrder(array $data)
  1285. {
  1286. $insert_data = array();
  1287. $insert_data['uid'] = $data['uid'];
  1288. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  1289. $insert_data['product_id'] = $data['product_id'];
  1290. $insert_data['price'] = $data['price'];
  1291. $insert_data['status'] = 'PAID';
  1292. $insert_data['pay_type'] = 1;
  1293. $insert_data['trade_no'] = $data['trade_no'];
  1294. $insert_data['send_order_id'] = $data['send_order_id'];
  1295. $insert_data['order_type'] = $data['order_type'];
  1296. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  1297. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  1298. $insert_data['transaction_id'] = $data['transaction_id'];
  1299. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  1300. $insert_data['create_ip'] = $data['create_ip'];
  1301. $insert_data['from_bid'] = $data['bid'];
  1302. Log::info('createOrder');
  1303. Log::info($insert_data);
  1304. return OrderService::save_order($insert_data);
  1305. }
  1306. /**
  1307. * 添加位置付订单
  1308. * @param $data
  1309. * @return mixed
  1310. */
  1311. protected function createUnPayOrder($data)
  1312. {
  1313. $data['status'] = 'UNPAID';
  1314. $data['transaction_id'] = '';
  1315. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1316. return OrderService::save_order($data);
  1317. }
  1318. /**
  1319. * order total
  1320. * @param $data
  1321. *
  1322. */
  1323. protected function createOrderTotal($data)
  1324. {
  1325. $data['status'] = 'UNPAID';
  1326. $data['created_at'] = date('Y-m-d H:i:s');
  1327. $data['updated_at'] = date('Y-m-d H:i:s');
  1328. DB::table('orders_total')->insert($data);
  1329. }
  1330. protected function updateOrderTotal($trade_no, $transaction_id)
  1331. {
  1332. $data['status'] = 'PAID';
  1333. $data['transaction_id'] = $transaction_id;
  1334. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1335. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1336. }
  1337. protected function successPayPushMsg($uid, $product_info, $order_id)
  1338. {
  1339. try {
  1340. $force_sub_info = $this->getSubscribeV2($uid);
  1341. $data = UserService::getById($uid);
  1342. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1343. //return false;
  1344. }
  1345. if ($force_sub_info) {
  1346. //$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>";
  1347. if (SubstituteOrderService::getByOrderId($order_id)) {
  1348. $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>";
  1349. } else {
  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. }
  1352. if ($product_info->type == 'YEAR_ORDER') {
  1353. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1354. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1355. if ($product_info->given) {
  1356. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1357. } else {
  1358. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1359. }
  1360. } else {
  1361. $money_text = '未知';
  1362. }
  1363. $delay = 0;
  1364. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1365. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1366. foreach ($force_sub_info as $item) {
  1367. $res['openid'] = $item->openid;
  1368. $res['appid'] = $item->appid;
  1369. $res['content'] = $content;
  1370. $res['type'] = 'one_task';
  1371. $res['send_time'] = date("Y-m-d H:i:s");
  1372. $res['task_id'] = md5('pay_success_push');
  1373. $send_data = array(
  1374. 'send_time' => date("Y-m-d H:i:s"),
  1375. 'data' => $res
  1376. );
  1377. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1378. }
  1379. }
  1380. } catch (\Exception $e) {
  1381. Log::info('pay_success_push error');
  1382. Log::info($e);
  1383. }
  1384. return true;
  1385. }
  1386. private function orderStatistical($order)
  1387. {
  1388. try {
  1389. $send_data = array(
  1390. 'type' => 'order_info',
  1391. 'data' => $order->toArray()
  1392. );
  1393. $send = new SendStatisticsList($send_data);
  1394. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  1395. dispatch($job);
  1396. } catch (\Exception $e) {
  1397. Log::info($e);
  1398. }
  1399. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  1400. try {
  1401. if ($order->from_type) {
  1402. $pv = Redis::hget('push:distribution_channel_id:allpv', $order->from_type);
  1403. if ($pv) {
  1404. Redis::sadd('push:all:paidnum:from:' . $order->from_type, $order->uid);
  1405. $old = Redis::hget('push:all:paidamount', $order->from_type);
  1406. if ($old) {
  1407. Redis::hset('push:all:paidamount', $order->from_type, $old + $order->price);
  1408. } else {
  1409. Redis::hset('push:all:paidamount', $order->from_type, $order->price);
  1410. }
  1411. }
  1412. }
  1413. if (isset($order->pay_merchant_id) && $order->pay_merchant_id)//临时统计队列清0 用于报警
  1414. {
  1415. $key = 'pay_merchant:' . $order->pay_merchant_id;
  1416. Redis::hset($key, 'unpaid_num', 0);
  1417. }
  1418. } catch (\Exception $e) {
  1419. }
  1420. }
  1421. private function getSubscribe($uid)
  1422. {
  1423. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  1424. }
  1425. private function getSubscribeV2($uid)
  1426. {
  1427. return DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->get();
  1428. }
  1429. public function getSubscribeOfficialName($uid, $distribution_channel_id)
  1430. {
  1431. $subscribe = $this->getSubscribe($uid);
  1432. if ($subscribe && isset($subscribe->appid)) {
  1433. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  1434. if ($official && isset($official->nickname)) {
  1435. return $official->nickname;
  1436. }
  1437. }
  1438. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  1439. if (isset($res->nickname) && !empty($res->nickname)) {
  1440. return $res->nickname;
  1441. }
  1442. try {
  1443. $official = DB::table('official_accounts')
  1444. ->where('distribution_channel_id', $distribution_channel_id)
  1445. ->where('is_auth', 1)
  1446. ->orderBy('id', 'desc')
  1447. ->select('nickname')->first();
  1448. if ($official && isset($official->nickname)) {
  1449. return $official->nickname;
  1450. }
  1451. } catch (\Exception $e) {
  1452. }
  1453. return '';
  1454. }
  1455. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  1456. {
  1457. $change_pay_id = 40;
  1458. if ($pay_merchant_id == 40) {
  1459. $change_pay_id = 9;
  1460. }
  1461. try {
  1462. $time = (int)date('G');
  1463. $change = false;
  1464. if ($time <= 8 || $time >= 23) {
  1465. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  1466. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  1467. $change = true;
  1468. }
  1469. }
  1470. //$number,$template_type,$param=array()
  1471. $phone_arr = ['15868100210', '18072713392', '15088790066', '13858057394', '18668029091', '18668420256'];
  1472. //$phone_arr = ['18668029091'];
  1473. $param = ['pay_id' => $pay_merchant_id];
  1474. if ($n) {
  1475. $template_type = 'pay_channel_remind';
  1476. //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  1477. } else {
  1478. $template_type = 'pay_channel_remind';
  1479. //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  1480. }
  1481. if ($change) {
  1482. $template_type = 'pay_channel_change';
  1483. $param = ['pay_id' => $pay_merchant_id, 'new_pay_id' => $change_pay_id];
  1484. }
  1485. $last_alert_time = Redis::hget('autopayalert',$pay_merchant_id);
  1486. if($last_alert_time && (time() -$last_alert_time) <= 10 ){
  1487. return ;
  1488. }
  1489. Redis::hset('autopayalert',$pay_merchant_id,time());
  1490. foreach ($phone_arr as $phone) {
  1491. AliSMS::send($phone, $template_type, $param);
  1492. }
  1493. } catch (\Exception $e) {
  1494. }
  1495. }
  1496. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  1497. {
  1498. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  1499. }
  1500. private function recordOtherParam($order_id, $uid)
  1501. {
  1502. $gxhp = ReadRecordService::getByField($uid, 'gxhp');
  1503. if ($gxhp) {
  1504. OrderParamService::create($order_id, $gxhp);
  1505. }
  1506. }
  1507. private function substituteOrderPrice($order_id, $prize_fee = 500)
  1508. {
  1509. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  1510. }
  1511. private function updateUserSendOrderId(int $uid, $send_order_id)
  1512. {
  1513. if ($send_order_id && $uid) {
  1514. $user = UserService::getById($uid);
  1515. if ($user && !$user->send_order_id) {
  1516. UserService::updateInfo($uid, compact('send_order_id'));
  1517. }
  1518. }
  1519. }
  1520. /**
  1521. * 测书智能推送统计 付费用户数、充值金额
  1522. * @param $bid
  1523. * @param $uid
  1524. * @param $price
  1525. */
  1526. private function smartPushTestBookPaidUv($bid, $uid, $price)
  1527. {
  1528. $smart_bid = ReadRecordService::getSmartPush($uid);
  1529. if ($smart_bid && in_array($bid, $smart_bid)) {
  1530. try {
  1531. $key = 'smartPushTestBookPaidUv:bid:%s';
  1532. Redis::sadd(sprintf($key, $bid), $uid);
  1533. $now_amount = Redis::hget('smartPushTestBookPaidAmount', $bid);
  1534. if (!$now_amount) {
  1535. $now_amount = 0;
  1536. }
  1537. Redis::hset('smartPushTestBookPaidAmount', $bid, $now_amount + $price);
  1538. } catch (\Exception $e) {
  1539. }
  1540. }
  1541. }
  1542. /**
  1543. * 获取用户第几次充值
  1544. * @param $uid
  1545. * @return int
  1546. */
  1547. private function getChargeTimes($uid)
  1548. {
  1549. $count = OrderService::getUserChargeTimes($uid);
  1550. return $count + 1;
  1551. }
  1552. //米玩 订单转移
  1553. private function transfer($distribution_channel_id)
  1554. {
  1555. if (in_array($distribution_channel_id, explode(',', redisEnv('MIWAN_TRANSFER_CHANNEL_ID', 1)))) {
  1556. $random = random_int(1, 100);
  1557. $miwan_transfer_probability = redisEnv('MIWAN_TRANSFER_PROBABILITY', 0);
  1558. $miwan_transfer_to_channel_id = redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0);
  1559. if ($miwan_transfer_to_channel_id && $random <= $miwan_transfer_probability) {
  1560. return $miwan_transfer_to_channel_id;
  1561. }
  1562. }
  1563. return 0;
  1564. }
  1565. private function userProperty($uid){
  1566. try{
  1567. $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
  1568. if(!$userproperty) return ;
  1569. UserDivisionCpcPropertyService::createorUpdate($userproperty);
  1570. }catch (\Exception $e){}
  1571. }
  1572. private function huaweiActivity($activity_id,$uid,$product_id){
  1573. $huawei_activity_id = redisEnv('HUAWEI_ACTIVITY_ID');
  1574. if($huawei_activity_id && $activity_id && $activity_id == $huawei_activity_id){
  1575. $product_id_relate_lottery_count = ['6117'=>1,'6118'=>2,'6119'=>4,'6120'=>8];
  1576. if(isset($product_id_relate_lottery_count[$product_id])){
  1577. $info = DB::table('huaweilottery_user_counts')->where('uid',$uid)->select('count')->first();
  1578. if($info){
  1579. DB::table('huaweilottery_user_counts')->where('uid',$uid)->update([
  1580. 'uid'=>$uid,
  1581. 'count'=>$info->count+$product_id_relate_lottery_count[$product_id],
  1582. 'updated_at'=>date('Y-m-d H:i:s')
  1583. ]);
  1584. }else{
  1585. DB::table('huaweilottery_user_counts')->insert([
  1586. 'uid'=>$uid,
  1587. 'count'=>$product_id_relate_lottery_count[$product_id],
  1588. 'updated_at'=>date('Y-m-d H:i:s'),
  1589. 'created_at'=>date('Y-m-d H:i:s')
  1590. ]);
  1591. }
  1592. }
  1593. }
  1594. }
  1595. }