OrdersController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Order;
  3. use App\Modules\Statistic\Services\AdVisitStatService;
  4. use App\Http\Controllers\QuickApp\BaseController;
  5. use Illuminate\Http\Request;
  6. use App\Modules\Subscribe\Services\BookOrderService;
  7. use App\Modules\Subscribe\Services\ChapterOrderService;
  8. use App\Modules\Subscribe\Services\OrderService;
  9. use App\Http\Controllers\QuickApp\Order\Transformers\BookOrderTransformer;
  10. use App\Http\Controllers\QuickApp\Order\Transformers\ChapterOrderTransformer;
  11. use App\Http\Controllers\QuickApp\Order\Transformers\ChargeListTransformer;
  12. use App\Libs\Pay\PayFactory;
  13. use App\Libs\Pay\WechatPay;
  14. use App\Modules\Subscribe\Services\YearOrderService;
  15. use App\Modules\User\Services\UserService;
  16. use DB;
  17. use Hashids;
  18. use EasyWeChat\Foundation\Application;
  19. use App\Modules\Product\Services\ProductService;
  20. use App\Modules\Book\Services\BookConfigService;
  21. use App\Modules\Book\Services\BookService;
  22. use App\Modules\Channel\Services\PayTemplateService;
  23. use App\Modules\Trade\Pay\OrderArousePayFactory;
  24. use App\Modules\Trade\Pay\OrderPaySuccess;
  25. use Exception;
  26. use Log;
  27. class OrdersController extends BaseController
  28. {
  29. /**
  30. * @apiDefine Order 订单
  31. */
  32. /**
  33. *
  34. */
  35. private $chargeList;
  36. public function exchangeList()
  37. {
  38. foreach ($this->chargeList as &$item) {
  39. if (!$item->switch_to) continue;
  40. $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('product_id', $item->id)->first();
  41. if ($order) {
  42. $change = ProductService::getProductSingle($item->switch_to);
  43. $item->id = $change->id;
  44. $item->price = $change->price;
  45. $item->given = $change->given;
  46. }
  47. }
  48. }
  49. /**
  50. * @apiVersion 1.0.0
  51. * @apiDescription 充值列表
  52. * @api {get} order/chargeList 充值列表
  53. * @apiHeader {String} [Authorization] token
  54. * @apiGroup Order
  55. * @apiName chargeList
  56. * @apiSuccess {int} code 状态码
  57. * @apiSuccess {String} msg 信息
  58. * @apiSuccess {object} data 结果集
  59. * @apiSuccessExample {json} Success-Response:
  60. * HTTP/1.1 200 OK
  61. * {
  62. * code: 0,
  63. * msg: "",
  64. * data: [
  65. * {
  66. * product_id: 1,
  67. * price: "30.00元",
  68. * vip: 0,
  69. * intro: [
  70. * {
  71. * label: 3000,
  72. * important: false
  73. * },
  74. * {
  75. * label: "书币",
  76. * important: true
  77. * }
  78. * ]
  79. * },
  80. * {
  81. * product_id: 2,
  82. * price: "50.00元",
  83. * vip: 1,
  84. * intro: [
  85. * {
  86. * label: 5000,
  87. * important: false
  88. * },
  89. * {
  90. * label: "1000+",
  91. * important: true
  92. * },
  93. * {
  94. * label: "书币",
  95. * important: false
  96. * }
  97. * ]
  98. * },
  99. * {
  100. * product_id: 5,
  101. * price: "365.00元",
  102. * vip: 0,
  103. * intro: [
  104. * {
  105. * label: "年费VIP会员",
  106. * important: true
  107. * }
  108. * ]
  109. * }
  110. * ]
  111. * }
  112. */
  113. public function chargeList(Request $request)
  114. {
  115. $bid = $request->input('bid', '');
  116. $temp = $bid;
  117. $template_id = PayTemplateService::getPayTemplate($this->distribution_channel_id);
  118. $book_config = null;
  119. if ($bid) {
  120. $bid = Hashids::decode($bid)[0];
  121. $book_config = BookConfigService::getBookById($bid);
  122. }
  123. if ($template_id == 2) { //模板2只有在长篇小说过来的用户才显示
  124. //部分渠道需要2元模板不管哪个入口进来都展示
  125. $exclude_channels = explode(',', env('PRICE_TWO_SHOW_ALL_CHANNEL'));
  126. if (!in_array($this->distribution_channel_id, $exclude_channels)) {
  127. if ($book_config) {
  128. if ($book_config->charge_type == 'BOOK') $template_id = 1;
  129. }
  130. }
  131. }
  132. $res = ProductService::getChargeProduct($template_id);
  133. if (!$res) {
  134. return response()->error('WAP_SYS_ERROR');
  135. }
  136. $this->chargeList = $res;
  137. $this->exchangeList();
  138. //TODO 长篇小数才有模板2
  139. $uid = $this->uid;
  140. $is_first_recharge = OrderService::judgeUserFirstRecharge($uid);
  141. $data = [];
  142. $appad = 0;
  143. foreach ($res as $v) {
  144. if ($template_id == 7 && $book_config && $book_config->charge_type == 'BOOK' && $v->price == 2) {
  145. continue;
  146. }
  147. if ($template_id == 2 && $v->type == 'NEW_USER' && !$bid) {
  148. //2元模版,直接进充值,不出现
  149. continue;
  150. }
  151. if ($v->type == 'NEW_USER' && $is_first_recharge) {
  152. if (
  153. env('NO_NEW_USER_CHARGE') &&
  154. in_array(
  155. $this->distribution_channel_id,
  156. explode(',', env('NO_NEW_USER_CHARGE'))
  157. )
  158. ) {
  159. continue;
  160. }
  161. $temp = [
  162. 'price' => (float) $v->price . '元',
  163. 'is_year_order' => 0,
  164. 'is_month_order' => 0,
  165. 'text' => sprintf('%s+%s书币', $v->price * 100, $v->given),
  166. 'today_special' => $v->is_default,
  167. 'first_charge' => true,
  168. 'save_text' => round($v->given / 100, 1) . '元',
  169. 'product_id' => $v->id,
  170. ];
  171. $data[] = $temp;
  172. } elseif ($v->type == 'YEAR_ORDER') {
  173. if ($v->type == 'NEW_USER') {
  174. continue;
  175. }
  176. $save_text = '年费vip会员';
  177. $text = '每天1元,全年免费看';
  178. $temp = [
  179. 'price' => (int) $v->price . '元',
  180. 'is_year_order' => 1,
  181. 'is_month_order' => 0,
  182. 'text' => $text,
  183. 'today_special' => $v->is_default,
  184. 'first_charge' => false,
  185. 'save_text' => $save_text,
  186. 'product_id' => $v->id,
  187. ];
  188. $data[] = $temp;
  189. } else {
  190. if ($v->type == 'NEW_USER') {
  191. continue;
  192. }
  193. $save_text = '';
  194. if ($v->given) {
  195. $save_text = round($v->given / 100, 1) . '元';
  196. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  197. } else {
  198. $text = sprintf('%s书币', $v->price * 100);
  199. }
  200. $temp = [
  201. 'price' => (float) $v->price . '元',
  202. 'is_year_order' => 0,
  203. 'is_month_order' => 0,
  204. 'text' => $text,
  205. 'today_special' => $v->is_default,
  206. 'first_charge' => false,
  207. 'save_text' => $save_text,
  208. 'product_id' => $v->id,
  209. ];
  210. $data[] = $temp;
  211. }
  212. }
  213. return response()->success($data);
  214. }
  215. /**
  216. * @apiVersion 1.0.0
  217. * @apiDescription 单本消费记录
  218. * @api {get} order/bookOrderList 单本消费记录
  219. * @apiHeader {String} [Authorization] token
  220. * @apiGroup Order
  221. * @apiName bookOrderList
  222. * @apiParam {String} [page_size] 分页大小
  223. * @apiParam {String} [page] 页码
  224. * @apiSuccess {int} code 状态码
  225. * @apiSuccess {String} msg 信息
  226. * @apiSuccess {object} data 结果集
  227. * @apiSuccess {Int} uid uid
  228. * @apiSuccess {Int} bid bid
  229. * @apiSuccess {Int} book_name 书名
  230. * @apiSuccess {Int} fee 钱
  231. * @apiSuccess {String} created_at 时间
  232. * @apiSuccessExample {json} Success-Response:
  233. * HTTP/1.1 200 OK
  234. * {
  235. * code: 0,
  236. * msg: "",
  237. * data: list:[
  238. * {
  239. * uid: 4,
  240. * bid: 1,
  241. * book_name: "dfsedfertrwet",
  242. * fee: 100,
  243. * created_at: "2017-12-02 16:24:54"
  244. * }
  245. * ]
  246. * meta: {
  247. * total: 1,
  248. * per_page: 15,
  249. * current_page: 1,
  250. * last_page: 1,
  251. * next_page_url: "",
  252. * prev_page_url: ""
  253. * }
  254. * }
  255. * }
  256. */
  257. public function bookOrderList(Request $request)
  258. {
  259. $page_size = $request->input('page_size', 15);
  260. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  261. return response()->pagination(new BookOrderTransformer(), $book_order);
  262. }
  263. /**
  264. * @apiVersion 1.0.0
  265. * @apiDescription 章节消费记录
  266. * @api {get} order/chapterOrderList 章节消费记录
  267. * @apiHeader {String} [Authorization] token
  268. * @apiGroup Order
  269. * @apiName chapterOrderList
  270. * @apiParam {String} [page_size] 分页大小
  271. * @apiParam {String} [page] 页码
  272. * @apiSuccess {int} code 状态码
  273. * @apiSuccess {String} msg 信息
  274. * @apiSuccess {object} data 结果集
  275. * @apiSuccess {Int} uid uid
  276. * @apiSuccess {Int} bid bid
  277. * @apiSuccess {Int} cid cid
  278. * @apiSuccess {Int} chapter_name 章节名
  279. * @apiSuccess {Int} book_name 书名
  280. * @apiSuccess {Int} fee 钱
  281. * @apiSuccess {String} created_at 时间
  282. * @apiSuccessExample {json} Success-Response:
  283. * HTTP/1.1 200 OK
  284. * {
  285. * code: 0,
  286. * msg: "",
  287. * data: list:[
  288. * {
  289. * uid: 4,
  290. * bid: 1,
  291. * cid: 1,
  292. * chapter_name: "sdfsd",
  293. * book_name: "dfsedfertrwet",
  294. * fee: 100,
  295. * created_at: "2017-12-02 16:24:54"
  296. * }
  297. * ]
  298. * meta: {
  299. * total: 1,
  300. * per_page: 15,
  301. * current_page: 1,
  302. * last_page: 1,
  303. * next_page_url: "",
  304. * prev_page_url: ""
  305. * }
  306. * }
  307. */
  308. public function chapterOrderList(Request $request)
  309. {
  310. $chapter_model = new ChapterOrderService();
  311. $page_size = $request->input('page_size', 15);
  312. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  313. foreach ($chapter_order as $item) {
  314. if ($item->fee == 0) {
  315. $result = AdVisitStatService::getInfoV2($this->uid, $item->cid, ['UNLOCK', 'UNLOCK_2']);
  316. $item->fee = '解锁';
  317. }
  318. }
  319. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  320. }
  321. /**
  322. * @apiVersion 1.0.0
  323. * @apiDescription 充值记录
  324. * @api {get} order/chargeRecordLists 充值记录
  325. * @apiParam {String} [token] token
  326. * @apiHeader {String} [Authorization] token 两个token任选其一
  327. * @apiGroup Order
  328. * @apiName chargeRecordLists
  329. * @apiParam {String} [page_size] 分页大小
  330. * @apiParam {String} [page] 页码
  331. * @apiSuccess {int} code 状态码
  332. * @apiSuccess {String} msg 信息
  333. * @apiSuccess {object} data 结果集
  334. * @apiSuccess {String} data.price 价格
  335. * @apiSuccess {String} data.status 状态
  336. * @apiSuccess {String} data.trade_no 订单号
  337. * @apiSuccess {String} data.created_at 时间
  338. * @apiSuccessExample {json} Success-Response:
  339. * HTTP/1.1 200 OK
  340. * {
  341. * code: 0,
  342. * msg: "",
  343. * data: {
  344. * list: [
  345. * {
  346. * id: 134,
  347. * price: "1.00",
  348. * status: "PAID",
  349. * trade_no: "201712021915481585670623626232",
  350. * created_at: "2017-12-02 19:15:56"
  351. * }
  352. * ],
  353. * meta: {
  354. * total: 1,
  355. * per_page: 15,
  356. * current_page: 1,
  357. * last_page: 1,
  358. * next_page_url: "",
  359. * prev_page_url: ""
  360. * }
  361. * }
  362. * }
  363. */
  364. public function chargeRecordLists(Request $request)
  365. {
  366. $page_size = $request->input('page_size', 15);
  367. $res = OrderService::getOrderList($this->uid, $page_size);
  368. return response()->pagination(new ChargeListTransformer(), $res);
  369. }
  370. //订单是否成功
  371. public function isSuccess(Request $request)
  372. {
  373. $order = $request->input('order');
  374. $order_info = OrderService::getByTradeNo($order);
  375. if ($order_info && $order_info->status == 'PAID') {
  376. return response()->success();
  377. }
  378. return response()->error('QAPP_SYS_ERROR');
  379. }
  380. private function getPayParams(Request $request)
  381. {
  382. $product_id = $request->get('product_id', 0);
  383. $send_order_id = $request->get('send_order_id', 0);
  384. if (!$product_id) {
  385. return false;
  386. }
  387. $bid = $request->get('bid', 0);
  388. if ($bid) {
  389. $from_bid = BookService::decodeBidStatic($bid);
  390. }
  391. $trade_no = date("YmdHis") . hexdec(uniqid());
  392. $product_info = ProductService::getProductSingle($product_id);
  393. $uid = $this->uid;
  394. $distribution_channel_id = $this->distribution_channel_id;
  395. $price = $product_info->price * 100;
  396. if (in_array($uid, explode(',', env('TEST_UID')))) {
  397. $price = 1;
  398. }
  399. $from_type = 'QuickApp';
  400. if ($product_info->type == 'YEAR_ORDER') {
  401. $order_type = 'YEAR';
  402. } elseif ($product_info->type == 'BOOK_ORDER') {
  403. $order_type = 'BOOK';
  404. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  405. $order_type = 'RECHARGE';
  406. } else {
  407. $order_type = '';
  408. }
  409. $create_ip = _getIp();
  410. return compact(
  411. 'distribution_channel_id',
  412. 'uid',
  413. 'product_id',
  414. 'price',
  415. 'trade_no',
  416. 'create_ip',
  417. 'send_order_id',
  418. 'from_bid',
  419. 'from_type',
  420. 'order_type'
  421. );
  422. }
  423. /**
  424. * @apiVersion 1.0.0
  425. * @apiDescription 支付
  426. * @api {get} goToPay 微信APP支付
  427. * @apiGroup pay
  428. * @apiName wxindex
  429. * @apiParam {Int} product_id product_id
  430. * @apiParam {Int} send_order_id send_order_id
  431. * @apiParam {String} bid bid
  432. * @apiHeader {String} [Authorization] token
  433. * @apiSuccess {int} code 状态码
  434. * @apiSuccess {String} msg 信息
  435. * @apiSuccess {Object} data 信息
  436. * @apiSuccess {Object} data.appId 唤起支付的appId
  437. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  438. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  439. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  440. * @apiSuccess {Object} data.sign 唤起支付的sign
  441. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  442. * @apiSuccessExample {json} Success-Response:
  443. * HTTP/1.1 200 OK
  444. * {
  445. * code: 0,
  446. * msg: "",
  447. * data: {
  448. *
  449. * }
  450. */
  451. function wxindex(Request $request)
  452. {
  453. if ($params = $this->getPayParams($request)) {
  454. $params['pay_merchant_source'] = 'QuickAppWxPay';
  455. } else {
  456. return response()->error('QAPP_PARAM_ERROR');
  457. }
  458. $app = OrderArousePayFactory::wxApp($this->uid);
  459. $result = $app->handle($params);
  460. if ($result) {
  461. return response()->success($result);
  462. } else {
  463. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  464. }
  465. }
  466. /**
  467. * @apiVersion 1.0.0
  468. * @apiDescription 支付宝APP支付
  469. * @api {get} goToAliPay 支付宝APP支付
  470. * @apiGroup pay
  471. * @apiName aliIndex
  472. * @apiParam {Int} product_id product_id
  473. * @apiParam {Int} send_order_id send_order_id
  474. * @apiParam {String} bid bid
  475. * @apiHeader {String} [Authorization] token
  476. * @apiSuccessExample {json} Success-Response:
  477. * HTTP/1.1 200 OK
  478. * {
  479. * code: 0,
  480. * msg: "",
  481. * data:""
  482. *
  483. */
  484. public function aliIndex(Request $request)
  485. {
  486. if ($params = $this->getPayParams($request)) {
  487. $params['pay_merchant_source'] = 'QuickAppALiPay';
  488. $params['type'] = 'App';
  489. } else {
  490. return response()->error('QAPP_PARAM_ERROR');
  491. }
  492. $app = OrderArousePayFactory::ali($this->uid);
  493. return response()->success($app->handle($params));
  494. }
  495. /**
  496. * @apiVersion 1.0.0
  497. * @apiDescription 订单查询
  498. * @api {get} checkOrder 订单查询
  499. * @apiGroup pay
  500. * @apiName checkOrder
  501. * @apiParam {String} [token] token
  502. * @apiHeader {String} [Authorization] token 两个token任选其一
  503. * @apiParam {String} order order
  504. * @apiSuccess {int} code 状态码
  505. * @apiSuccess {String} msg 信息
  506. * @apiSuccess {Object} data 信息
  507. * @apiSuccessExample {json} Success-Response:
  508. * HTTP/1.1 200 OK
  509. * {
  510. * code: 0,
  511. * msg: "",
  512. * data: {
  513. *
  514. * }
  515. */
  516. public function checkOrder(Request $request)
  517. {
  518. $order = $request->input('order', '');
  519. $i = 0;
  520. $uid = $this->uid;
  521. while ($i <= 10) {
  522. $order_info = OrderService::getByTradeNo($order);
  523. if (!$order_info) {
  524. return response()->error('QAPP_SYS_ERROR');
  525. break;
  526. }
  527. if (isset($order_info->status) && $order_info->status == 'PAID') {
  528. $data['balance'] = 0;
  529. $user = UserService::getById($uid);
  530. if ($user) {
  531. $data['balance'] = $user->balance;
  532. }
  533. return response()->success($data);
  534. }
  535. sleep(1);
  536. $i++;
  537. }
  538. return response()->error('QAPP_SYS_ERROR');
  539. }
  540. /**
  541. * 官方微信回调
  542. */
  543. function wxback(Request $request)
  544. {
  545. $param = $request->except('_url');
  546. myLog('wxpay')->info($param);
  547. $config = [
  548. // 微信支付参数
  549. 'appid' => 'wx7ff41112e6dfb2d7', // 应用ID
  550. 'merchant_id' => '1500977641', // 微信支付商户号
  551. 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
  552. 'trade_type' => 'APP',
  553. ];
  554. $app = PayFactory::official($config);
  555. $response = $app->notify()->handleNotify(function ($notify, $successful) {
  556. if (!$successful) {
  557. return 'fail';
  558. }
  559. if (OrderPaySuccess::handle($notify->out_trade_no, $notify->transaction_id)) {
  560. return true;
  561. } else {
  562. return 'fail';
  563. }
  564. });
  565. return $response;
  566. }
  567. /**
  568. * 支付宝支付回调
  569. */
  570. function aliback(Request $request)
  571. {
  572. $param = $request->except('_url');
  573. $app = PayFactory::aliPay();
  574. myLog('alipay')->info($param);
  575. if ($app->notify($param)) {
  576. if (OrderPaySuccess::handle($param['out_trade_no'], $param['trade_no'])) {
  577. return response('success');
  578. } else {
  579. return response('fail');
  580. }
  581. return response('success');
  582. } else {
  583. return response('fail');
  584. }
  585. }
  586. }