OrdersController.php 22 KB

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