OrdersController.php 69 KB

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