OrdersController.php 70 KB

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