OrdersController.php 77 KB

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