OrdersController.php 67 KB

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