OrdersController.php 80 KB

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