OrdersController.php 64 KB

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