OrdersController.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. $result = $this->createUnPayOrder([
  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. if ($request->get('suid')) {
  270. $this->createSubstituteOrder($result->id, $uid, $request->get('suid'));
  271. }
  272. Log::info($data);
  273. $config = [];
  274. if ($pay_merchant->config_info) {
  275. $config = json_decode($pay_merchant->config_info, true);
  276. }
  277. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  278. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  279. $pay_info = $wechatPay->send($data);
  280. if (!$pay_info) {
  281. $pay_info = $wechatPay->send($data);
  282. }
  283. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  284. //支付异常
  285. $this->payAlert($channel->pay_merchant_id, $trade_no, $pay_info);
  286. }
  287. $pay_info['pay_redirect_url'] = urldecode($pay_redirect_url);
  288. if ($request->has('cid')) {
  289. $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&cid=' . $request->input('cid');
  290. }
  291. // if ($hash_bid) {
  292. // $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&bid=' . $hash_bid;
  293. // }
  294. if ($suid) {
  295. $prize_fee = (int)(($product_info->price*100)*0.1);
  296. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  297. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  298. $pay_info['pay_redirect_url'] = $pay_redirect_url = sprintf(
  299. $url_format,
  300. env('PROTOCOL'),
  301. encodeDistributionChannelId($help_pay_page_channel_id),
  302. env('CUSTOM_HOST'),
  303. $prize_fee,
  304. $uid
  305. );
  306. }
  307. $pay_url_info = parse_url($pay_redirect_url);
  308. $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']);
  309. //$h5_scheme = env('H5_SCHEME','https');
  310. //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
  311. Log::info('$pay_info is');
  312. $jsSdkSign = 0;
  313. Log::info($pay_info);
  314. Log::info('jsSdkSign---- :' . $jsSdkSign);
  315. $pay_order = $trade_no;
  316. return view('pay.order.index', compact('pay_info', 'referer', 'jsSdkSign', 'pay_order'));
  317. }
  318. private function isNewUserSecondCharge($charge_type,$product_id,$uid){
  319. //Log::info('新用户只能冲一次:' .$charge_type);
  320. if($charge_type == 'NEW_USER'){
  321. $result = OrderService::getUserOrderByProductId($uid,$product_id);
  322. //Log::info('isNewUserSecondCharge-------------------------');
  323. //Log::info($result);
  324. if($result){
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. public function reportError(Request $request)
  331. {
  332. $data = $request->post('data');
  333. DB::table('pay_page_error')->insert([
  334. 'msg' => $data,
  335. 'created_at' => date('Y-m-d H:i:s'),
  336. 'updated_at' => date('Y-m-d H:i:s')
  337. ]);
  338. return response()->success();
  339. }
  340. private function jsSdkSign($pay_info, $url)
  341. {
  342. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  343. return 0;
  344. }
  345. $appid = $pay_info['appId'];
  346. $jsapi_ticket = Redis::hget($appid, 'jsapi_ticket');
  347. //如果能获取到jsapi_ticket 则直接签名
  348. if ($jsapi_ticket) {
  349. $sign_arr = [
  350. 'noncestr' => $pay_info['nonceStr'],
  351. 'jsapi_ticket' => $jsapi_ticket,
  352. 'timestamp' => $pay_info['timeStamp'],
  353. 'url' => $url
  354. ];
  355. return sha1($this->arr_to_url($sign_arr));
  356. }
  357. //获取不到jsapi_ticket
  358. $app_secret_info = DB::table('official_setting')->select('secret')->first();
  359. if ($app_secret_info && !empty($app_secret_info->secret)) {
  360. $client = new Client(['timeout' => 5]);
  361. try {
  362. //获取access_token
  363. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $app_secret_info->secret;
  364. $res = $client->request('get', $url)->getBody()->getContents();
  365. if (!$res) {
  366. return 0;
  367. }
  368. $res = json_decode($res, 1);
  369. if (!isset($res['access_token']) || empty($res['access_token'])) {
  370. return 0;
  371. }
  372. Redis::hset($appid, 'access_token', $res['access_token']);
  373. //获取jsapi_ticket
  374. $res = null;
  375. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $res['access_token'] . '&type=jsapi';
  376. $res = $client->request('get', $url)->getBody()->getContents();
  377. if (!$res) {
  378. return 0;
  379. }
  380. $res = json_decode($res, 1);
  381. if (!isset($res['ticket']) || empty($res['ticket'])) {
  382. return 0;
  383. }
  384. Redis::hset($appid, 'jsapi_ticket', $res['ticket']);
  385. Redis::EXPIRE($appid, 7000);
  386. $sign_arr = [
  387. 'noncestr' => $pay_info['nonceStr'],
  388. 'jsapi_ticket' => $res['ticket'],
  389. 'timestamp' => $pay_info['timeStamp'],
  390. 'url' => $url
  391. ];
  392. return sha1($this->arr_to_url($sign_arr));
  393. } catch (\Exception $e) {
  394. return 0;
  395. }
  396. }
  397. return 0;
  398. }
  399. public function waitPage(Request $request)
  400. {
  401. $order = $request->input('order');
  402. //$order = "201712081711401585932843356442";
  403. $order = (string)$order;
  404. $url = urldecode($request->input('redirect'));
  405. return view('pay.order.wait', compact('order', 'url'));
  406. //return view('pay.order.wait');
  407. }
  408. /**
  409. * 官方微信回调
  410. * @param Request $request
  411. * @return \Symfony\Component\HttpFoundation\Response
  412. */
  413. function wcback_official(Request $request)
  414. {
  415. $xml = XML::parse(strval($request->getContent()));
  416. Log::info('xml is');
  417. Log::info($xml);
  418. if (!$xml || !is_array($xml)) return 'fail';
  419. $remarks = json_decode($xml['attach'], true);
  420. $pay_merchant_id = $remarks['pmi'];
  421. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  422. $application = WechatPay::instance('OFFICIALPAY', json_decode($pay_merchant->config_info, true));
  423. //$app = new Application($options);
  424. $app = $application->app;
  425. $response = $app->payment->handleNotify(function ($notify, $successful) {
  426. if (!$successful) return 'fail';
  427. $trade_no = $notify->out_trade_no;
  428. $order = OrderService::getByTradeNo($trade_no);
  429. if (isset($order->status) && $order->status == 'PAID') {
  430. Log::info('has_pay:' . $trade_no);
  431. return true;
  432. }
  433. DB::beginTransaction();
  434. try {
  435. $transaction_id = $notify->transaction_id;
  436. $remarks = json_decode($notify->attach, true);
  437. $uid = $remarks['uid'];
  438. $distribution_channel_id = $remarks['dcd'];
  439. $product_id = $remarks['pd'];
  440. $product = ProductService::getProductSingle($product_id);
  441. $pay_merchant_source = $remarks['pms'];
  442. $pay_merchant_id = $remarks['pmi'];
  443. $send_order_id = $remarks['soi'];
  444. $create_ip = $remarks['cp'];
  445. $price = $product->price;
  446. $bid = $remarks['bid'];
  447. //$this->updateOrderTotal($trade_no,$transaction_id);
  448. $prize_fee = (int)(($product->price*100)*0.1);
  449. $this->substituteOrderPrice($order->id,$prize_fee);
  450. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  451. // 更新其他定制Order表
  452. if ($product->type == 'YEAR_ORDER') {
  453. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  454. $order_type = 'YEAR';
  455. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  456. $order->order_type = $order_type;
  457. $order->status = 'PAID';
  458. $order->pay_end_at = date('Y-m-d H:i:s');
  459. $order->transaction_id = $transaction_id;
  460. $order->save();
  461. //$price = $price/100;
  462. /*
  463. $this->createOrder(
  464. compact(
  465. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  466. 'pay_merchant_id','create_ip','bid','transaction_id'
  467. )
  468. );*/
  469. } elseif ($product->type == 'BOOK_ORDER') {
  470. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  471. $order_type = 'BOOK';
  472. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  473. $order->order_type = $order_type;
  474. $order->status = 'PAID';
  475. $order->pay_end_at = date('Y-m-d H:i:s');
  476. $order->transaction_id = $transaction_id;
  477. $order->save();
  478. //$price = $price/100;
  479. /*
  480. $this->createOrder(
  481. compact(
  482. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  483. 'pay_merchant_id','create_ip','bid','transaction_id'
  484. )
  485. );*/
  486. } elseif ($product->type == 'TICKET_RECHARGE') {
  487. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  488. $order_type = 'RECHARGE';
  489. $this->userCharge($product, $uid);
  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. } elseif ($product->type == 'FOREVER') {
  503. Log::info('FOREVER------------------------FOREVER');
  504. $order_type = 'FOREVER';
  505. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  506. $order->order_type = $order_type;
  507. $order->status = 'PAID';
  508. $order->pay_end_at = date('Y-m-d H:i:s');
  509. $order->transaction_id = $transaction_id;
  510. $order->save();
  511. } else {
  512. DB::rollback();
  513. return 'Order not exist.';
  514. }
  515. $this->successPayPushMsg($uid, $product, $order->id);
  516. $this->orderStatistical($order);
  517. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  518. Redis::hdel($key, $uid);
  519. DB::commit();
  520. return true;
  521. } catch (\Exception $e) {
  522. DB::rollback();
  523. return 'fail';
  524. }
  525. });
  526. return $response;
  527. }
  528. /**
  529. * 通联支付回调
  530. * @param Request $request
  531. */
  532. function wcback_allinpay(Request $request)
  533. {
  534. Log::info('wcback_allinpay_request');
  535. try {
  536. $data = $request->all();
  537. Log::info($request->all());
  538. if (!$data['trxreserved']) return 'fail';
  539. $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
  540. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  541. unset($data['_url']);
  542. if (!$pay_merchant->config_info) return 'fail';
  543. $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
  544. if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
  545. Log::info('allinpay_notify_sign_success');
  546. /**
  547. * 只有0000表示交易成功或下单成功,其他为失败
  548. * 0000:交易成功
  549. * 3045,3088:交易超时
  550. * 3008:余额不足
  551. * 3999:交易失败
  552. * 2008:交易处理中
  553. * 3050:交易已撤销
  554. */
  555. if ($data['trxstatus'] == '0000') {
  556. // 修改表比较多,开启事务
  557. DB::beginTransaction();
  558. $trade_no = $data['cusorderid'];
  559. $transaction_id = $data['chnltrxid'];
  560. //$openid = $data['acct'];
  561. $remarks = $data['trxreserved'];
  562. // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
  563. $remarks = json_decode($remarks, true);
  564. Log::info('$remarks');
  565. Log::info($remarks);
  566. // 更新Order
  567. $order = OrderService::getByTradeNo($trade_no);
  568. // 防止重复推送
  569. if (isset($order->status) && $order->status == 'PAID') {
  570. Log::info('has_pay:' . $trade_no);
  571. echo 'success';
  572. exit();
  573. }
  574. $uid = $remarks['uid'];
  575. $distribution_channel_id = $remarks['dcd'];
  576. $product_id = $remarks['pd'];
  577. $product = ProductService::getProductSingle($product_id);
  578. $pay_merchant_source = $remarks['pms'];
  579. $pay_merchant_id = $remarks['pmi'];
  580. $send_order_id = $remarks['soi'];
  581. $create_ip = $remarks['cp'];
  582. $price = $product->price;
  583. $bid = $remarks['bid'];
  584. Log::info('save_order_end');
  585. //$this->updateOrderTotal($trade_no,$transaction_id);
  586. Log::info($product);
  587. Log::info('product_type:' . $product->type);
  588. $prize_fee = (int)(($product->price*100)*0.1);
  589. $this->substituteOrderPrice($order->id,$prize_fee);
  590. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  591. // 更新其他定制Order表
  592. if ($product->type == 'YEAR_ORDER') {
  593. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  594. $order_type = 'YEAR';
  595. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  596. $order->order_type = $order_type;
  597. $order->status = 'PAID';
  598. $order->pay_end_at = date('Y-m-d H:i:s');
  599. $order->transaction_id = $transaction_id;
  600. $order->save();
  601. //$price = $price/100;
  602. /*
  603. $this->createOrder(
  604. compact(
  605. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  606. 'pay_merchant_id','create_ip','bid','transaction_id'
  607. )
  608. );*/
  609. } elseif ($product->type == 'BOOK_ORDER') {
  610. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  611. $order_type = 'BOOK';
  612. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  613. $order->order_type = $order_type;
  614. $order->status = 'PAID';
  615. $order->pay_end_at = date('Y-m-d H:i:s');
  616. $order->transaction_id = $transaction_id;
  617. $order->save();
  618. //$price = $price/100;
  619. /*
  620. $this->createOrder(
  621. compact(
  622. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  623. 'pay_merchant_id','create_ip','bid','transaction_id'
  624. )
  625. );*/
  626. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  627. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  628. $order_type = 'RECHARGE';
  629. $this->userCharge($product, $uid);
  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. } elseif ($product->type == 'FOREVER') {
  643. Log::info('FOREVER------------------------FOREVER');
  644. $order_type = 'FOREVER';
  645. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  646. $order->order_type = $order_type;
  647. $order->status = 'PAID';
  648. $order->pay_end_at = date('Y-m-d H:i:s');
  649. $order->transaction_id = $transaction_id;
  650. $order->save();
  651. }
  652. $this->successPayPushMsg($uid, $product, $order->id);
  653. $this->orderStatistical($order);
  654. DB::commit();
  655. //redis 删除未支付的uid
  656. try {
  657. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  658. Redis::hdel($key, $uid);
  659. //Redis::srem($key,$uid);
  660. } catch (\Exception $e) {
  661. Log::info('redis remote error-----------------------');
  662. Log::info($e);
  663. }
  664. }
  665. } else {
  666. Log::info('allinpay_notify_sign_fail');
  667. }
  668. } catch (\Exception $e) {
  669. DB::rollBack();
  670. Log::info('receive_allinpay_ept:' . $e->getMessage());
  671. Log::info('pay_callback_end fail');
  672. echo 'fail';
  673. return ;
  674. }
  675. Log::info('pay_callback_end');
  676. echo "success";
  677. }
  678. public function wcback_lianlianpay(Request $request)
  679. {
  680. $data = $request->getContent();
  681. Log::info('wcback_lianlianpay call back enter');
  682. Log::info($request->getContent());
  683. $data = json_decode($data, true);
  684. if ($data['result_pay'] == 'SUCCESS') {
  685. $trade_no = $data['no_order'];
  686. $order = OrderService::getByTradeNo($trade_no);
  687. if (!$order) return response()->json(['ret_code' => '-1']);
  688. $pay_merchant_id = $order->pay_merchant_id;
  689. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  690. unset($data['_url']);
  691. if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
  692. $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
  693. $uid = $order->uid;
  694. $distribution_channel_id = $order->distribution_channel_id;
  695. $price = $order->price;
  696. $send_order_id = $order->send_order_id;
  697. $product_id = $order->product_id;
  698. if (!$wechatPay->rsaCheck($data)) {
  699. Log::info('sign check error');
  700. //return response()->json(['ret_code'=>'-1']);
  701. }
  702. // 防止重复推送
  703. if (isset($order->status) && $order->status == 'PAID') {
  704. Log::info('has_pay:' . $trade_no);
  705. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  706. }
  707. DB::beginTransaction();
  708. try {
  709. $product = ProductService::getProductSingle($order->product_id);
  710. $transaction_id = $data['oid_paybill'];
  711. $prize_fee = (int)(($product->price*100));
  712. $this->substituteOrderPrice($order->id,$prize_fee);
  713. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  714. // 更新其他定制Order表
  715. if ($product->type == 'YEAR_ORDER') {
  716. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  717. $order_type = 'YEAR';
  718. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  719. $order->order_type = $order_type;
  720. $order->status = 'PAID';
  721. $order->pay_end_at = date('Y-m-d H:i:s');
  722. $order->transaction_id = $transaction_id;
  723. $order->save();
  724. } elseif ($product->type == 'BOOK_ORDER') {
  725. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  726. $order_type = 'BOOK';
  727. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  728. $order->order_type = $order_type;
  729. $order->status = 'PAID';
  730. $order->pay_end_at = date('Y-m-d H:i:s');
  731. $order->transaction_id = $transaction_id;
  732. $order->save();
  733. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  734. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  735. $order_type = 'RECHARGE';
  736. $this->userCharge($product, $uid);
  737. $order->order_type = $order_type;
  738. $order->status = 'PAID';
  739. $order->pay_end_at = date('Y-m-d H:i:s');
  740. $order->transaction_id = $transaction_id;
  741. $order->save();
  742. } elseif ($product->type == 'FOREVER') {
  743. Log::info('FOREVER------------------------FOREVER');
  744. $order_type = 'FOREVER';
  745. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  746. $order->order_type = $order_type;
  747. $order->status = 'PAID';
  748. $order->pay_end_at = date('Y-m-d H:i:s');
  749. $order->transaction_id = $transaction_id;
  750. $order->save();
  751. }
  752. $this->successPayPushMsg($uid, $product, $order->id);
  753. $this->orderStatistical($order);
  754. DB::commit();
  755. //redis 删除未支付的uid
  756. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
  757. Redis::hdel($key, $uid);
  758. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  759. } catch (\Exception $e) {
  760. DB::rollBack();
  761. Log::info('lianlian:' . $e->getMessage());
  762. echo 'fail';
  763. return ;
  764. }
  765. }
  766. echo 'success';
  767. }
  768. /**
  769. * 微众支付回调
  770. * @param Request $request
  771. */
  772. function wcback_palmpay(Request $request)
  773. {
  774. Log::info('wcback_palmpay_request');
  775. try {
  776. $data = $request->except('_url');
  777. Log::info($request->all());
  778. //订单
  779. $trade_no = $data['outTradeNo'];
  780. $order = OrderService::getByTradeNo($trade_no);
  781. if (!$order) die('failed');
  782. // 防止重复推送
  783. if (isset($order->status) && $order->status == 'PAID') {
  784. Log::info('has_pay:' . $trade_no);
  785. echo 'success';
  786. exit();
  787. }
  788. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  789. //校验签名
  790. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  791. if ($sign != $data['sign']) die('failed');
  792. $transaction_id = $data['chorderid'];
  793. if ($data['status'] == 'success') {
  794. Log::info('wcback_palmpay_notify_sign_success');
  795. if ($data['status'] == 'success') {
  796. // 修改表比较多,开启事务
  797. DB::beginTransaction();
  798. // 更新Order
  799. $order = OrderService::getByTradeNo($trade_no);
  800. $uid = $order->uid;
  801. $distribution_channel_id = $order->distribution_channel_id;
  802. $product_id = $order->product_id;
  803. $product = ProductService::getProductSingle($product_id);
  804. $send_order_id = $order->send_order_id;
  805. $price = $product->price;
  806. Log::info('save_order_end');
  807. Log::info($product);
  808. Log::info('product_type:' . $product->type);
  809. $prize_fee = (int)(($product->price*100)*0.1);
  810. $this->substituteOrderPrice($order->id,$prize_fee);
  811. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  812. // 更新其他定制Order表
  813. if ($product->type == 'YEAR_ORDER') {
  814. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  815. $order_type = 'YEAR';
  816. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  817. $order->order_type = $order_type;
  818. $order->status = 'PAID';
  819. $order->pay_end_at = date('Y-m-d H:i:s');
  820. $order->transaction_id = $transaction_id;
  821. $order->save();
  822. } elseif ($product->type == 'BOOK_ORDER') {
  823. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  824. $order_type = 'BOOK';
  825. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  826. $order->order_type = $order_type;
  827. $order->status = 'PAID';
  828. $order->pay_end_at = date('Y-m-d H:i:s');
  829. $order->transaction_id = $transaction_id;
  830. $order->save();
  831. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  832. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  833. $order_type = 'RECHARGE';
  834. $this->userCharge($product, $uid);
  835. $order->order_type = $order_type;
  836. $order->status = 'PAID';
  837. $order->pay_end_at = date('Y-m-d H:i:s');
  838. $order->transaction_id = $transaction_id;
  839. $order->save();
  840. } elseif ($product->type == 'FOREVER') {
  841. Log::info('FOREVER------------------------FOREVER');
  842. $order_type = 'FOREVER';
  843. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  844. $order->order_type = $order_type;
  845. $order->status = 'PAID';
  846. $order->pay_end_at = date('Y-m-d H:i:s');
  847. $order->transaction_id = $transaction_id;
  848. $order->save();
  849. }
  850. $this->successPayPushMsg($uid, $product, $order->id);
  851. $this->orderStatistical($order);
  852. DB::commit();
  853. //redis 删除未支付的uid
  854. try {
  855. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  856. Redis::hdel($key, $uid);
  857. } catch (\Exception $e) {
  858. Log::info('redis remote error-----------------------');
  859. Log::info($e);
  860. }
  861. }
  862. } else {
  863. Log::info('wcback_palmpay_notify_sign_fail');
  864. }
  865. } catch (\Exception $e) {
  866. var_dump($e->getMessage());
  867. DB::rollBack();
  868. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  869. echo 'fail';
  870. Log::info('pay_callback_end error');
  871. return;
  872. }
  873. Log::info('pay_callback_end');
  874. echo "success";
  875. }
  876. /**
  877. * 公众号签名@华灯初上
  878. * @param $params
  879. * @return string
  880. */
  881. protected function getSign($params, $key)
  882. {
  883. $url = $this->arr_to_url($params, false);
  884. $url = $url . '&key=' . $key;
  885. $sign = md5($url);
  886. return $sign;
  887. }
  888. /**
  889. * 公众号签名@华灯初上
  890. * @param $array
  891. * @param bool $has_sign
  892. * @return string
  893. */
  894. protected function arr_to_url($array, $has_sign = false)
  895. {
  896. ksort($array);
  897. reset($array);
  898. $arg = "";
  899. while (list ($name, $val) = each($array)) {
  900. if ($name == 'sign' && !$has_sign) continue;
  901. if (strpos($name, "_") === 0)
  902. continue;
  903. if (is_array($val))
  904. $val = join(',', $val);
  905. if ($val === "")
  906. continue;
  907. $arg .= $name . "=" . $val . "&";
  908. }
  909. $arg = substr($arg, 0, count($arg) - 2);
  910. return $arg;
  911. }
  912. /**
  913. * 单本充值会掉
  914. * @param $product_id
  915. * @param $uid
  916. * @param $send_order_id
  917. * @param $fee
  918. */
  919. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  920. {
  921. $book_conf = BookConfigService::getBookByProduct($product_id);
  922. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  923. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  924. $insert_data['uid'] = $uid;
  925. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  926. $insert_data['fee'] = $fee;
  927. $insert_data['send_order_id'] = $send_order_id;
  928. $insert_data['charge_balance'] = 0;
  929. $insert_data['reward_balance'] = 0;
  930. Log::info('start_save_book_order');
  931. Log::info($insert_data);
  932. return BookOrderService::save_book_order($insert_data);
  933. }
  934. /**
  935. * 包年
  936. * @param $uid
  937. * @param $distribution_channel_id
  938. * @param $fee
  939. * @param $send_order_id
  940. * @return mixed
  941. */
  942. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  943. {
  944. Log::info('start_save_year_order');
  945. $insert_data['uid'] = $uid;
  946. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  947. $insert_data['fee'] = $fee;
  948. $insert_data['send_order_id'] = $send_order_id;
  949. Log::info($insert_data);
  950. return YearOrderService::save_year_order($insert_data);
  951. }
  952. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  953. {
  954. Log::info('start_save_year_order');
  955. ('start_forever_Free_Order');
  956. $insert_data['uid'] = $uid;
  957. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  958. $insert_data['fee'] = $fee;
  959. $insert_data['send_order_id'] = $send_order_id;
  960. Log::info($insert_data);
  961. return YearOrderService::foreverFree($insert_data);
  962. }
  963. /**
  964. * 用户充值
  965. * @param $product
  966. * @param $uid\
  967. */
  968. protected function userCharge($product, $uid)
  969. {
  970. $total = $product->price * 100 + $product->given;
  971. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  972. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  973. }
  974. /**
  975. * 添加订单
  976. * @param array $data
  977. * @return mixed
  978. */
  979. protected function createOrder(array $data)
  980. {
  981. $insert_data = array();
  982. $insert_data['uid'] = $data['uid'];
  983. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  984. $insert_data['product_id'] = $data['product_id'];
  985. $insert_data['price'] = $data['price'];
  986. $insert_data['status'] = 'PAID';
  987. $insert_data['pay_type'] = 1;
  988. $insert_data['trade_no'] = $data['trade_no'];
  989. $insert_data['send_order_id'] = $data['send_order_id'];
  990. $insert_data['order_type'] = $data['order_type'];
  991. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  992. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  993. $insert_data['transaction_id'] = $data['transaction_id'];
  994. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  995. $insert_data['create_ip'] = $data['create_ip'];
  996. $insert_data['from_bid'] = $data['bid'];
  997. Log::info('createOrder');
  998. Log::info($insert_data);
  999. return OrderService::save_order($insert_data);
  1000. }
  1001. /**
  1002. * 添加位置付订单
  1003. * @param $data
  1004. * @return mixed
  1005. */
  1006. protected function createUnPayOrder($data)
  1007. {
  1008. $data['status'] = 'UNPAID';
  1009. $data['transaction_id'] = '';
  1010. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1011. return OrderService::save_order($data);
  1012. }
  1013. /**
  1014. * order total
  1015. * @param $data
  1016. *
  1017. */
  1018. protected function createOrderTotal($data)
  1019. {
  1020. $data['status'] = 'UNPAID';
  1021. $data['created_at'] = date('Y-m-d H:i:s');
  1022. $data['updated_at'] = date('Y-m-d H:i:s');
  1023. DB::table('orders_total')->insert($data);
  1024. }
  1025. protected function updateOrderTotal($trade_no, $transaction_id)
  1026. {
  1027. $data['status'] = 'PAID';
  1028. $data['transaction_id'] = $transaction_id;
  1029. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1030. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1031. }
  1032. protected function successPayPushMsg($uid, $product_info, $order_id)
  1033. {
  1034. try {
  1035. $force_sub_info = $this->getSubscribe($uid);
  1036. $data = UserService::getById($uid);
  1037. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1038. //return false;
  1039. }
  1040. if ($force_sub_info) {
  1041. //$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>";
  1042. if (SubstituteOrderService::getByOrderId($order_id)) {
  1043. $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>";
  1044. } else {
  1045. $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>";
  1046. }
  1047. if ($product_info->type == 'YEAR_ORDER') {
  1048. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1049. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1050. if ($product_info->given) {
  1051. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1052. } else {
  1053. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1054. }
  1055. } else {
  1056. $money_text = '未知';
  1057. }
  1058. $delay = 0;
  1059. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1060. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1061. $res['openid'] = $force_sub_info->openid;
  1062. $res['appid'] = $force_sub_info->appid;
  1063. $res['content'] = $content;
  1064. $res['type'] = 'one_task';
  1065. $res['send_time'] = date("Y-m-d H:i:s");
  1066. $res['task_id'] = md5('pay_success_push');
  1067. $send_data = array(
  1068. 'send_time' => date("Y-m-d H:i:s"),
  1069. 'data' => $res
  1070. );
  1071. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1072. }
  1073. } catch (\Exception $e) {
  1074. }
  1075. return true;
  1076. }
  1077. private function orderStatistical($order)
  1078. {
  1079. try {
  1080. $send_data = array(
  1081. 'type' => 'order_info',
  1082. 'data' => $order->toArray()
  1083. );
  1084. $send = new SendStatisticsList($send_data);
  1085. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  1086. dispatch($job);
  1087. } catch (\Exception $e) {
  1088. Log::info($e);
  1089. }
  1090. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  1091. try{
  1092. if($order->from_type){
  1093. $pv = Redis::hget('push:distribution_channel_id:allpv' ,$order->from_type);
  1094. if($pv){
  1095. Redis::sadd('push:all:paidnum:from:' . $order->from_type,$order->uid);
  1096. $old = Redis::hget('push:all:paidamount' ,$order->from_type);
  1097. if($old){
  1098. Redis::hset('push:all:paidamount' ,$order->from_type,$old+$order->price);
  1099. }else{
  1100. Redis::hset('push:all:paidamount' ,$order->from_type,$order->price);
  1101. }
  1102. }
  1103. }
  1104. }catch (\Exception $e){}
  1105. }
  1106. private function getSubscribe($uid)
  1107. {
  1108. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  1109. }
  1110. public function getSubscribeOfficialName($uid)
  1111. {
  1112. $subscribe = $this->getSubscribe($uid);
  1113. if ($subscribe && isset($subscribe->appid)) {
  1114. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  1115. if ($official && isset($official->nickname)) {
  1116. return $official->nickname;
  1117. }
  1118. }
  1119. return '';
  1120. }
  1121. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  1122. {
  1123. $change_pay_id = 9;
  1124. if ($pay_merchant_id == 9) {
  1125. //$change_pay_id = 12;
  1126. }
  1127. try {
  1128. $time = (int)date('H');
  1129. $change = false;
  1130. if ($time <= 8 || $time >= 23) {
  1131. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  1132. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  1133. $change = true;
  1134. }
  1135. }
  1136. $phone_arr = ['15868100210', '18072713392', '15088790066', '13858057394', '18668029091', '18668420256'];
  1137. //$phone_arr = ['18668029091'];
  1138. if ($n) {
  1139. $content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  1140. } else {
  1141. $content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  1142. }
  1143. if ($change) {
  1144. $content .= ',已经切换到支付通道:' . $change_pay_id;
  1145. }
  1146. foreach ($phone_arr as $phone) {
  1147. SMS::send($phone, $content);
  1148. }
  1149. file_put_contents(date('Y-m-d') . '.txt', json_encode($pay_info), FILE_APPEND);
  1150. } catch (\Exception $e) {
  1151. }
  1152. }
  1153. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  1154. {
  1155. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  1156. }
  1157. private function substituteOrderPrice($order_id,$prize_fee=500)
  1158. {
  1159. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  1160. }
  1161. private function updateUserSendOrderId(int $uid,$send_order_id){
  1162. if($send_order_id && $uid){
  1163. $user = UserService::getById($uid);
  1164. if($user && !$user->send_order_id){
  1165. UserService::updateInfo($uid,compact('send_order_id'));
  1166. }
  1167. }
  1168. }
  1169. /**
  1170. * 测书智能推送统计 付费用户数、充值金额
  1171. * @param $bid
  1172. * @param $uid
  1173. * @param $price
  1174. */
  1175. private function smartPushTestBookPaidUv($bid,$uid,$price){
  1176. $smart_bid = ReadRecordService::getSmartPush($uid);
  1177. if( $smart_bid && in_array($bid,$smart_bid) ){
  1178. try{
  1179. $key = 'smartPushTestBookPaidUv:bid:%s';
  1180. Redis::sadd(sprintf($key,$bid),$uid);
  1181. $now_amount = Redis::hget('smartPushTestBookPaidAmount',$bid);
  1182. if(!$now_amount){
  1183. $now_amount = 0;
  1184. }
  1185. Redis::hset('smartPushTestBookPaidAmount',$bid,$now_amount+$price);
  1186. }catch (\Exception $e){
  1187. }
  1188. }
  1189. }
  1190. }