OrdersController.php 55 KB

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