OrdersController.php 80 KB

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