OrdersController.php 71 KB

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