OrdersController.php 66 KB

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