OrdersController.php 66 KB

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