OrdersController.php 22 KB

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