OrdersController.php 71 KB

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