OrdersController.php 56 KB

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