OrdersController.php 66 KB

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