OrdersController.php 80 KB

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