OrdersController.php 69 KB

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