OrdersController.php 80 KB

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