OrdersController.php 79 KB

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