OrdersController.php 78 KB

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