OrdersController.php 65 KB

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