OrdersController.php 68 KB

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