OrdersController.php 65 KB

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