OrdersController.php 80 KB

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