OrdersController.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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. // 注册动作-》回调订单
  797. $action_type = 'CallBackOrder';
  798. $param = [
  799. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  800. 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
  801. ];
  802. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  803. //用户属性
  804. $this->userProperty($uid);
  805. return true;
  806. } catch (\Exception $e) {
  807. DB::rollback();
  808. return 'fail';
  809. }
  810. });
  811. return $response;
  812. }
  813. /**
  814. * 通联支付回调
  815. * @param Request $request
  816. */
  817. function wcback_allinpay(Request $request)
  818. {
  819. Log::info('wcback_allinpay_request');
  820. try {
  821. $data = $request->all();
  822. Log::info($request->all());
  823. if (!$data['trxreserved']) return 'fail';
  824. $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
  825. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  826. unset($data['_url']);
  827. if (!$pay_merchant->config_info) return 'fail';
  828. $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
  829. if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
  830. Log::info('allinpay_notify_sign_success');
  831. /**
  832. * 只有0000表示交易成功或下单成功,其他为失败
  833. * 0000:交易成功
  834. * 3045,3088:交易超时
  835. * 3008:余额不足
  836. * 3999:交易失败
  837. * 2008:交易处理中
  838. * 3050:交易已撤销
  839. */
  840. if ($data['trxstatus'] == '0000') {
  841. // 修改表比较多,开启事务
  842. DB::beginTransaction();
  843. $trade_no = $data['cusorderid'];
  844. $transaction_id = $data['chnltrxid'];
  845. //$openid = $data['acct'];
  846. $remarks = $data['trxreserved'];
  847. // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
  848. $remarks = json_decode($remarks, true);
  849. Log::info('$remarks');
  850. Log::info($remarks);
  851. // 更新Order
  852. $order = OrderService::getByTradeNo($trade_no);
  853. // 防止重复推送
  854. if (isset($order->status) && $order->status == 'PAID') {
  855. Log::info('has_pay:' . $trade_no);
  856. echo 'success';
  857. exit();
  858. }
  859. $uid = $remarks['uid'];
  860. $distribution_channel_id = $remarks['dcd'];
  861. $product_id = $remarks['pd'];
  862. $product = ProductService::getProductSingle($product_id);
  863. $pay_merchant_source = $remarks['pms'];
  864. $pay_merchant_id = $remarks['pmi'];
  865. $send_order_id = $remarks['soi'];
  866. $create_ip = $remarks['cp'];
  867. $price = $product->price;
  868. $bid = $remarks['bid'];
  869. Log::info('save_order_end');
  870. //$this->updateOrderTotal($trade_no,$transaction_id);
  871. Log::info($product);
  872. Log::info('product_type:' . $product->type);
  873. $prize_fee = (int)(($product->price * 100) * 0.1);
  874. $this->substituteOrderPrice($order->id, $prize_fee);
  875. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  876. //获取用户充值次数
  877. $order->pay_type = $this->getChargeTimes($order->uid);
  878. // 更新其他定制Order表
  879. if ($product->type == 'YEAR_ORDER') {
  880. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  881. $order_type = 'YEAR';
  882. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  883. $order->order_type = $order_type;
  884. $order->status = 'PAID';
  885. $order->pay_end_at = date('Y-m-d H:i:s');
  886. $order->transaction_id = $transaction_id;
  887. $order->save();
  888. //$price = $price/100;
  889. /*
  890. $this->createOrder(
  891. compact(
  892. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  893. 'pay_merchant_id','create_ip','bid','transaction_id'
  894. )
  895. );*/
  896. } elseif ($product->type == 'BOOK_ORDER') {
  897. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  898. $order_type = 'BOOK';
  899. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  900. $order->order_type = $order_type;
  901. $order->status = 'PAID';
  902. $order->pay_end_at = date('Y-m-d H:i:s');
  903. $order->transaction_id = $transaction_id;
  904. $order->save();
  905. //$price = $price/100;
  906. /*
  907. $this->createOrder(
  908. compact(
  909. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  910. 'pay_merchant_id','create_ip','bid','transaction_id'
  911. )
  912. );*/
  913. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  914. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  915. $order_type = 'RECHARGE';
  916. $this->userCharge($product, $uid);
  917. $order->order_type = $order_type;
  918. $order->status = 'PAID';
  919. $order->pay_end_at = date('Y-m-d H:i:s');
  920. $order->transaction_id = $transaction_id;
  921. $order->save();
  922. //$price = $price/100;
  923. /*
  924. $this->createOrder(
  925. compact(
  926. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  927. 'pay_merchant_id','create_ip','bid','transaction_id'
  928. ));*/
  929. } elseif ($product->type == 'FOREVER') {
  930. Log::info('FOREVER------------------------FOREVER');
  931. $order_type = 'FOREVER';
  932. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  933. $order->order_type = $order_type;
  934. $order->status = 'PAID';
  935. $order->pay_end_at = date('Y-m-d H:i:s');
  936. $order->transaction_id = $transaction_id;
  937. $order->save();
  938. }
  939. $this->successPayPushMsg($uid, $product, $order->id);
  940. $this->orderStatistical($order);
  941. DB::commit();
  942. $this->userProperty($uid);
  943. //redis 删除未支付的uid
  944. try {
  945. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  946. Redis::hdel($key, $uid);
  947. //Redis::srem($key,$uid);
  948. } catch (\Exception $e) {
  949. Log::info('redis remote error-----------------------');
  950. Log::info($e);
  951. }
  952. }
  953. } else {
  954. Log::info('allinpay_notify_sign_fail');
  955. }
  956. } catch (\Exception $e) {
  957. DB::rollBack();
  958. Log::info('receive_allinpay_ept:' . $e->getMessage());
  959. Log::info('pay_callback_end fail');
  960. echo 'fail';
  961. return;
  962. }
  963. Log::info('pay_callback_end');
  964. echo "success";
  965. }
  966. public function wcback_lianlianpay(Request $request)
  967. {
  968. $data = $request->getContent();
  969. Log::info('wcback_lianlianpay call back enter');
  970. Log::info($request->getContent());
  971. $data = json_decode($data, true);
  972. if ($data['result_pay'] == 'SUCCESS') {
  973. $trade_no = $data['no_order'];
  974. $order = OrderService::getByTradeNo($trade_no);
  975. if (!$order) return response()->json(['ret_code' => '-1']);
  976. $pay_merchant_id = $order->pay_merchant_id;
  977. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  978. unset($data['_url']);
  979. if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
  980. $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
  981. $uid = $order->uid;
  982. $distribution_channel_id = $order->distribution_channel_id;
  983. $price = $order->price;
  984. $send_order_id = $order->send_order_id;
  985. $product_id = $order->product_id;
  986. if (!$wechatPay->rsaCheck($data)) {
  987. Log::info('sign check error');
  988. //return response()->json(['ret_code'=>'-1']);
  989. }
  990. // 防止重复推送
  991. if (isset($order->status) && $order->status == 'PAID') {
  992. Log::info('has_pay:' . $trade_no);
  993. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  994. }
  995. DB::beginTransaction();
  996. try {
  997. $product = ProductService::getProductSingle($order->product_id);
  998. $transaction_id = $data['oid_paybill'];
  999. $prize_fee = (int)(($product->price * 100));
  1000. $this->substituteOrderPrice($order->id, $prize_fee);
  1001. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1002. //获取用户充值次数
  1003. $order->pay_type = $this->getChargeTimes($order->uid);
  1004. // 更新其他定制Order表
  1005. if ($product->type == 'YEAR_ORDER') {
  1006. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1007. $order_type = 'YEAR';
  1008. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1009. $order->order_type = $order_type;
  1010. $order->status = 'PAID';
  1011. $order->pay_end_at = date('Y-m-d H:i:s');
  1012. $order->transaction_id = $transaction_id;
  1013. $order->save();
  1014. } elseif ($product->type == 'BOOK_ORDER') {
  1015. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1016. $order_type = 'BOOK';
  1017. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1018. $order->order_type = $order_type;
  1019. $order->status = 'PAID';
  1020. $order->pay_end_at = date('Y-m-d H:i:s');
  1021. $order->transaction_id = $transaction_id;
  1022. $order->save();
  1023. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1024. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1025. $order_type = 'RECHARGE';
  1026. $this->userCharge($product, $uid);
  1027. $order->order_type = $order_type;
  1028. $order->status = 'PAID';
  1029. $order->pay_end_at = date('Y-m-d H:i:s');
  1030. $order->transaction_id = $transaction_id;
  1031. $order->save();
  1032. } elseif ($product->type == 'FOREVER') {
  1033. Log::info('FOREVER------------------------FOREVER');
  1034. $order_type = 'FOREVER';
  1035. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1036. $order->order_type = $order_type;
  1037. $order->status = 'PAID';
  1038. $order->pay_end_at = date('Y-m-d H:i:s');
  1039. $order->transaction_id = $transaction_id;
  1040. $order->save();
  1041. }
  1042. $this->successPayPushMsg($uid, $product, $order->id);
  1043. $this->orderStatistical($order);
  1044. DB::commit();
  1045. $this->userProperty($uid);
  1046. //redis 删除未支付的uid
  1047. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
  1048. Redis::hdel($key, $uid);
  1049. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  1050. } catch (\Exception $e) {
  1051. DB::rollBack();
  1052. Log::info('lianlian:' . $e->getMessage());
  1053. echo 'fail';
  1054. return;
  1055. }
  1056. }
  1057. echo 'success';
  1058. }
  1059. /**
  1060. * 微众支付回调
  1061. * @param Request $request
  1062. */
  1063. function wcback_palmpay(Request $request)
  1064. {
  1065. Log::info('wcback_palmpay_request');
  1066. try {
  1067. $data = $request->except('_url');
  1068. Log::info($request->all());
  1069. //订单
  1070. $trade_no = $data['outTradeNo'];
  1071. $order = OrderService::getByTradeNo($trade_no);
  1072. if (!$order) die('failed');
  1073. // 防止重复推送
  1074. if (isset($order->status) && $order->status == 'PAID') {
  1075. Log::info('has_pay:' . $trade_no);
  1076. echo 'success';
  1077. exit();
  1078. }
  1079. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  1080. //校验签名
  1081. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  1082. if ($sign != $data['sign']) die('failed');
  1083. $transaction_id = $data['chorderid'];
  1084. if ($data['status'] == 'success') {
  1085. Log::info('wcback_palmpay_notify_sign_success');
  1086. if ($data['status'] == 'success') {
  1087. // 修改表比较多,开启事务
  1088. DB::beginTransaction();
  1089. // 更新Order
  1090. $order = OrderService::getByTradeNo($trade_no);
  1091. $uid = $order->uid;
  1092. $distribution_channel_id = $order->distribution_channel_id;
  1093. $product_id = $order->product_id;
  1094. $product = ProductService::getProductSingle($product_id);
  1095. $send_order_id = $order->send_order_id;
  1096. $price = $product->price;
  1097. Log::info('save_order_end');
  1098. Log::info($product);
  1099. Log::info('product_type:' . $product->type);
  1100. $prize_fee = (int)(($product->price * 100) * 0.1);
  1101. $this->substituteOrderPrice($order->id, $prize_fee);
  1102. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1103. //获取用户充值次数
  1104. $order->pay_type = $this->getChargeTimes($order->uid);
  1105. // 更新其他定制Order表
  1106. if ($product->type == 'YEAR_ORDER') {
  1107. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1108. $order_type = 'YEAR';
  1109. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1110. $order->order_type = $order_type;
  1111. $order->status = 'PAID';
  1112. $order->pay_end_at = date('Y-m-d H:i:s');
  1113. $order->transaction_id = $transaction_id;
  1114. $order->save();
  1115. } elseif ($product->type == 'BOOK_ORDER') {
  1116. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1117. $order_type = 'BOOK';
  1118. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1119. $order->order_type = $order_type;
  1120. $order->status = 'PAID';
  1121. $order->pay_end_at = date('Y-m-d H:i:s');
  1122. $order->transaction_id = $transaction_id;
  1123. $order->save();
  1124. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1125. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1126. $order_type = 'RECHARGE';
  1127. $this->userCharge($product, $uid);
  1128. $order->order_type = $order_type;
  1129. $order->status = 'PAID';
  1130. $order->pay_end_at = date('Y-m-d H:i:s');
  1131. $order->transaction_id = $transaction_id;
  1132. $order->save();
  1133. } elseif ($product->type == 'FOREVER') {
  1134. Log::info('FOREVER------------------------FOREVER');
  1135. $order_type = 'FOREVER';
  1136. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1137. $order->order_type = $order_type;
  1138. $order->status = 'PAID';
  1139. $order->pay_end_at = date('Y-m-d H:i:s');
  1140. $order->transaction_id = $transaction_id;
  1141. $order->save();
  1142. }
  1143. $this->successPayPushMsg($uid, $product, $order->id);
  1144. $this->orderStatistical($order);
  1145. DB::commit();
  1146. //redis 删除未支付的uid
  1147. try {
  1148. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1149. Redis::hdel($key, $uid);
  1150. } catch (\Exception $e) {
  1151. Log::info('redis remote error-----------------------');
  1152. Log::info($e);
  1153. }
  1154. // 注册动作-》回调订单
  1155. $action_type = 'CallBackOrder';
  1156. $param = [
  1157. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1158. 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
  1159. ];
  1160. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1161. $this->userProperty($uid);
  1162. }
  1163. } else {
  1164. Log::info('wcback_palmpay_notify_sign_fail');
  1165. }
  1166. } catch (\Exception $e) {
  1167. var_dump($e->getMessage());
  1168. DB::rollBack();
  1169. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1170. echo 'fail';
  1171. Log::info('pay_callback_end error');
  1172. return;
  1173. }
  1174. Log::info('pay_callback_end');
  1175. echo "success";
  1176. }
  1177. /**
  1178. * 公众号签名@华灯初上
  1179. * @param $params
  1180. * @return string
  1181. */
  1182. protected function getSign($params, $key)
  1183. {
  1184. $url = $this->arr_to_url($params, false);
  1185. $url = $url . '&key=' . $key;
  1186. $sign = md5($url);
  1187. return $sign;
  1188. }
  1189. /**
  1190. * 公众号签名@华灯初上
  1191. * @param $array
  1192. * @param bool $has_sign
  1193. * @return string
  1194. */
  1195. protected function arr_to_url($array, $has_sign = false)
  1196. {
  1197. ksort($array);
  1198. reset($array);
  1199. $arg = "";
  1200. while (list ($name, $val) = each($array)) {
  1201. if ($name == 'sign' && !$has_sign) continue;
  1202. if (strpos($name, "_") === 0)
  1203. continue;
  1204. if (is_array($val))
  1205. $val = join(',', $val);
  1206. if ($val === "")
  1207. continue;
  1208. $arg .= $name . "=" . $val . "&";
  1209. }
  1210. $arg = substr($arg, 0, count($arg) - 2);
  1211. return $arg;
  1212. }
  1213. /**
  1214. * 单本充值会掉
  1215. * @param $product_id
  1216. * @param $uid
  1217. * @param $send_order_id
  1218. * @param $fee
  1219. */
  1220. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  1221. {
  1222. $book_conf = BookConfigService::getBookByProduct($product_id);
  1223. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  1224. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  1225. $insert_data['uid'] = $uid;
  1226. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1227. $insert_data['fee'] = $fee;
  1228. $insert_data['send_order_id'] = $send_order_id;
  1229. $insert_data['charge_balance'] = 0;
  1230. $insert_data['reward_balance'] = 0;
  1231. Log::info('start_save_book_order');
  1232. Log::info($insert_data);
  1233. return BookOrderService::save_book_order($insert_data);
  1234. }
  1235. /**
  1236. * 包年
  1237. * @param $uid
  1238. * @param $distribution_channel_id
  1239. * @param $fee
  1240. * @param $send_order_id
  1241. * @return mixed
  1242. */
  1243. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1244. {
  1245. Log::info('start_save_year_order');
  1246. $insert_data['uid'] = $uid;
  1247. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1248. $insert_data['fee'] = $fee;
  1249. $insert_data['send_order_id'] = $send_order_id;
  1250. Log::info($insert_data);
  1251. return YearOrderService::save_year_order($insert_data);
  1252. }
  1253. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1254. {
  1255. Log::info('start_save_year_order');
  1256. ('start_forever_Free_Order');
  1257. $insert_data['uid'] = $uid;
  1258. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1259. $insert_data['fee'] = $fee;
  1260. $insert_data['send_order_id'] = $send_order_id;
  1261. Log::info($insert_data);
  1262. return YearOrderService::foreverFree($insert_data);
  1263. }
  1264. /**
  1265. * 用户充值
  1266. * @param $product
  1267. * @param $uid\
  1268. */
  1269. protected function userCharge($product, $uid)
  1270. {
  1271. $total = $product->price * 100 + $product->given;
  1272. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  1273. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  1274. }
  1275. /**
  1276. * 添加订单
  1277. * @param array $data
  1278. * @return mixed
  1279. */
  1280. protected function createOrder(array $data)
  1281. {
  1282. $insert_data = array();
  1283. $insert_data['uid'] = $data['uid'];
  1284. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  1285. $insert_data['product_id'] = $data['product_id'];
  1286. $insert_data['price'] = $data['price'];
  1287. $insert_data['status'] = 'PAID';
  1288. $insert_data['pay_type'] = 1;
  1289. $insert_data['trade_no'] = $data['trade_no'];
  1290. $insert_data['send_order_id'] = $data['send_order_id'];
  1291. $insert_data['order_type'] = $data['order_type'];
  1292. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  1293. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  1294. $insert_data['transaction_id'] = $data['transaction_id'];
  1295. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  1296. $insert_data['create_ip'] = $data['create_ip'];
  1297. $insert_data['from_bid'] = $data['bid'];
  1298. Log::info('createOrder');
  1299. Log::info($insert_data);
  1300. return OrderService::save_order($insert_data);
  1301. }
  1302. /**
  1303. * 添加位置付订单
  1304. * @param $data
  1305. * @return mixed
  1306. */
  1307. protected function createUnPayOrder($data)
  1308. {
  1309. $data['status'] = 'UNPAID';
  1310. $data['transaction_id'] = '';
  1311. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1312. return OrderService::save_order($data);
  1313. }
  1314. /**
  1315. * order total
  1316. * @param $data
  1317. *
  1318. */
  1319. protected function createOrderTotal($data)
  1320. {
  1321. $data['status'] = 'UNPAID';
  1322. $data['created_at'] = date('Y-m-d H:i:s');
  1323. $data['updated_at'] = date('Y-m-d H:i:s');
  1324. DB::table('orders_total')->insert($data);
  1325. }
  1326. protected function updateOrderTotal($trade_no, $transaction_id)
  1327. {
  1328. $data['status'] = 'PAID';
  1329. $data['transaction_id'] = $transaction_id;
  1330. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1331. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1332. }
  1333. protected function successPayPushMsg($uid, $product_info, $order_id)
  1334. {
  1335. try {
  1336. $force_sub_info = $this->getSubscribeV2($uid);
  1337. $data = UserService::getById($uid);
  1338. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1339. //return false;
  1340. }
  1341. if ($force_sub_info) {
  1342. //$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>";
  1343. if (SubstituteOrderService::getByOrderId($order_id)) {
  1344. $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>";
  1345. } else {
  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. }
  1348. if ($product_info->type == 'YEAR_ORDER') {
  1349. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1350. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1351. if ($product_info->given) {
  1352. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1353. } else {
  1354. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1355. }
  1356. } else {
  1357. $money_text = '未知';
  1358. }
  1359. $delay = 0;
  1360. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1361. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1362. foreach ($force_sub_info as $item) {
  1363. $res['openid'] = $item->openid;
  1364. $res['appid'] = $item->appid;
  1365. $res['content'] = $content;
  1366. $res['type'] = 'one_task';
  1367. $res['send_time'] = date("Y-m-d H:i:s");
  1368. $res['task_id'] = md5('pay_success_push');
  1369. $send_data = array(
  1370. 'send_time' => date("Y-m-d H:i:s"),
  1371. 'data' => $res
  1372. );
  1373. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1374. }
  1375. }
  1376. } catch (\Exception $e) {
  1377. Log::info('pay_success_push error');
  1378. Log::info($e);
  1379. }
  1380. return true;
  1381. }
  1382. private function orderStatistical($order)
  1383. {
  1384. try {
  1385. $send_data = array(
  1386. 'type' => 'order_info',
  1387. 'data' => $order->toArray()
  1388. );
  1389. $send = new SendStatisticsList($send_data);
  1390. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  1391. dispatch($job);
  1392. } catch (\Exception $e) {
  1393. Log::info($e);
  1394. }
  1395. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  1396. try {
  1397. if ($order->from_type) {
  1398. $pv = Redis::hget('push:distribution_channel_id:allpv', $order->from_type);
  1399. if ($pv) {
  1400. Redis::sadd('push:all:paidnum:from:' . $order->from_type, $order->uid);
  1401. $old = Redis::hget('push:all:paidamount', $order->from_type);
  1402. if ($old) {
  1403. Redis::hset('push:all:paidamount', $order->from_type, $old + $order->price);
  1404. } else {
  1405. Redis::hset('push:all:paidamount', $order->from_type, $order->price);
  1406. }
  1407. }
  1408. }
  1409. if (isset($order->pay_merchant_id) && $order->pay_merchant_id)//临时统计队列清0 用于报警
  1410. {
  1411. $key = 'pay_merchant:' . $order->pay_merchant_id;
  1412. Redis::hset($key, 'unpaid_num', 0);
  1413. }
  1414. } catch (\Exception $e) {
  1415. }
  1416. }
  1417. private function getSubscribe($uid)
  1418. {
  1419. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  1420. }
  1421. private function getSubscribeV2($uid)
  1422. {
  1423. return DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->get();
  1424. }
  1425. public function getSubscribeOfficialName($uid, $distribution_channel_id)
  1426. {
  1427. $subscribe = $this->getSubscribe($uid);
  1428. if ($subscribe && isset($subscribe->appid)) {
  1429. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  1430. if ($official && isset($official->nickname)) {
  1431. return $official->nickname;
  1432. }
  1433. }
  1434. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  1435. if (isset($res->nickname) && !empty($res->nickname)) {
  1436. return $res->nickname;
  1437. }
  1438. try {
  1439. $official = DB::table('official_accounts')
  1440. ->where('distribution_channel_id', $distribution_channel_id)
  1441. ->where('is_auth', 1)
  1442. ->orderBy('id', 'desc')
  1443. ->select('nickname')->first();
  1444. if ($official && isset($official->nickname)) {
  1445. return $official->nickname;
  1446. }
  1447. } catch (\Exception $e) {
  1448. }
  1449. return '';
  1450. }
  1451. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  1452. {
  1453. $change_pay_id = 40;
  1454. if ($pay_merchant_id == 40) {
  1455. $change_pay_id = 9;
  1456. }
  1457. try {
  1458. $time = (int)date('G');
  1459. $change = false;
  1460. if ($time <= 8 || $time >= 23) {
  1461. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  1462. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  1463. $change = true;
  1464. }
  1465. }
  1466. //$number,$template_type,$param=array()
  1467. $phone_arr = ['15868100210', '18072713392', '15088790066', '13858057394', '18668029091', '18668420256'];
  1468. //$phone_arr = ['18668029091'];
  1469. $param = ['pay_id' => $pay_merchant_id];
  1470. if ($n) {
  1471. $template_type = 'pay_channel_remind';
  1472. //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  1473. } else {
  1474. $template_type = 'pay_channel_remind';
  1475. //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  1476. }
  1477. if ($change) {
  1478. $template_type = 'pay_channel_change';
  1479. $param = ['pay_id' => $pay_merchant_id, 'new_pay_id' => $change_pay_id];
  1480. }
  1481. foreach ($phone_arr as $phone) {
  1482. AliSMS::send($phone, $template_type, $param);
  1483. }
  1484. } catch (\Exception $e) {
  1485. }
  1486. }
  1487. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  1488. {
  1489. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  1490. }
  1491. private function recordOtherParam($order_id, $uid)
  1492. {
  1493. $gxhp = ReadRecordService::getByField($uid, 'gxhp');
  1494. if ($gxhp) {
  1495. OrderParamService::create($order_id, $gxhp);
  1496. }
  1497. }
  1498. private function substituteOrderPrice($order_id, $prize_fee = 500)
  1499. {
  1500. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  1501. }
  1502. private function updateUserSendOrderId(int $uid, $send_order_id)
  1503. {
  1504. if ($send_order_id && $uid) {
  1505. $user = UserService::getById($uid);
  1506. if ($user && !$user->send_order_id) {
  1507. UserService::updateInfo($uid, compact('send_order_id'));
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * 测书智能推送统计 付费用户数、充值金额
  1513. * @param $bid
  1514. * @param $uid
  1515. * @param $price
  1516. */
  1517. private function smartPushTestBookPaidUv($bid, $uid, $price)
  1518. {
  1519. $smart_bid = ReadRecordService::getSmartPush($uid);
  1520. if ($smart_bid && in_array($bid, $smart_bid)) {
  1521. try {
  1522. $key = 'smartPushTestBookPaidUv:bid:%s';
  1523. Redis::sadd(sprintf($key, $bid), $uid);
  1524. $now_amount = Redis::hget('smartPushTestBookPaidAmount', $bid);
  1525. if (!$now_amount) {
  1526. $now_amount = 0;
  1527. }
  1528. Redis::hset('smartPushTestBookPaidAmount', $bid, $now_amount + $price);
  1529. } catch (\Exception $e) {
  1530. }
  1531. }
  1532. }
  1533. /**
  1534. * 获取用户第几次充值
  1535. * @param $uid
  1536. * @return int
  1537. */
  1538. private function getChargeTimes($uid)
  1539. {
  1540. $count = OrderService::getUserChargeTimes($uid);
  1541. return $count + 1;
  1542. }
  1543. //米玩 订单转移
  1544. private function transfer($distribution_channel_id)
  1545. {
  1546. if (in_array($distribution_channel_id, explode(',', redisEnv('MIWAN_TRANSFER_CHANNEL_ID', 1)))) {
  1547. $random = random_int(1, 100);
  1548. $miwan_transfer_probability = redisEnv('MIWAN_TRANSFER_PROBABILITY', 0);
  1549. $miwan_transfer_to_channel_id = redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0);
  1550. if ($miwan_transfer_to_channel_id && $random <= $miwan_transfer_probability) {
  1551. return $miwan_transfer_to_channel_id;
  1552. }
  1553. }
  1554. return 0;
  1555. }
  1556. private function userProperty($uid){
  1557. try{
  1558. $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
  1559. if(!$userproperty) return ;
  1560. UserDivisionCpcPropertyService::createorUpdate($userproperty);
  1561. }catch (\Exception $e){}
  1562. }
  1563. }