OrdersController.php 70 KB

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