OrdersController.php 70 KB

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