OrdersController.php 76 KB

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