OrdersController.php 79 KB

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