OrdersController.php 23 KB

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