OrdersController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. if (!getProp($product_info, 'id')) {
  381. return false;
  382. }
  383. $distribution_channel_id = $this->distribution_channel_id;
  384. $price = $product_info->price * 100;
  385. if (in_array($uid, explode(',', env('TEST_UID')))) {
  386. $price = 1;
  387. }
  388. $from_type = 'QuickApp';
  389. if ($product_info->type == 'YEAR_ORDER') {
  390. $order_type = 'YEAR';
  391. } elseif ($product_info->type == 'BOOK_ORDER') {
  392. $order_type = 'BOOK';
  393. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  394. $order_type = 'RECHARGE';
  395. } else {
  396. $order_type = '';
  397. }
  398. $create_ip = _getIp();
  399. return compact(
  400. 'distribution_channel_id',
  401. 'uid',
  402. 'product_id',
  403. 'price',
  404. 'trade_no',
  405. 'create_ip',
  406. 'send_order_id',
  407. 'from_bid',
  408. 'from_type',
  409. 'order_type'
  410. );
  411. }
  412. /**
  413. * @apiVersion 1.0.0
  414. * @apiDescription 支付
  415. * @api {get} goToPay 微信APP支付
  416. * @apiGroup pay
  417. * @apiName wxindex
  418. * @apiParam {Int} product_id product_id
  419. * @apiParam {Int} send_order_id send_order_id
  420. * @apiParam {String} bid bid
  421. * @apiHeader {String} [Authorization] token
  422. * @apiSuccess {int} code 状态码
  423. * @apiSuccess {String} msg 信息
  424. * @apiSuccess {Object} data 信息
  425. * @apiSuccess {Object} data.trade_no 订单号
  426. * @apiSuccess {Object} data.appId 唤起支付的appId
  427. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  428. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  429. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  430. * @apiSuccess {Object} data.sign 唤起支付的sign
  431. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  432. * @apiSuccessExample {json} Success-Response:
  433. * HTTP/1.1 200 OK
  434. * {
  435. * code: 0,
  436. * msg: "",
  437. * data: {
  438. *
  439. * }
  440. */
  441. function wxIndex(Request $request)
  442. {
  443. if ($params = $this->getPayParams($request)) {
  444. $params['pay_merchant_id'] = $this->app_pay_merchat_id;
  445. } else {
  446. return response()->error('QAPP_PARAM_ERROR');
  447. }
  448. $app = OrderArousePayFactory::wx($this->uid);
  449. // 微信支付参数
  450. $params['trade_type'] = 'APP'; //交易类型
  451. $result = $app->handle($params);
  452. if ($result) {
  453. $result['trade_no'] = $params['trade_no'];
  454. return response()->success($result);
  455. } else {
  456. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  457. }
  458. }
  459. /**
  460. * @apiVersion 1.0.0
  461. * @apiDescription 微信H5支付
  462. * @api {get} goToH5Pay 微信H5支付
  463. * @apiGroup pay
  464. * @apiName wxH5Index
  465. * @apiParam {Int} product_id product_id
  466. * @apiParam {Int} send_order_id send_order_id
  467. * @apiParam {String} bid bid
  468. * @apiHeader {String} [Authorization] token
  469. * @apiSuccess {int} code 状态码
  470. * @apiSuccess {String} msg 信息
  471. * @apiSuccess {Object} data 信息
  472. * @apiSuccess {Object} data.trade_no 订单号
  473. * @apiSuccess {Object} data.appId 唤起支付的appId
  474. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  475. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  476. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  477. * @apiSuccess {Object} data.sign 唤起支付的sign
  478. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  479. * @apiSuccess {Object} data.mweb_url 唤起支付mweb_url
  480. * @apiSuccessExample {json} Success-Response:
  481. * HTTP/1.1 200 OK
  482. * {
  483. * code: 0,
  484. * msg: "",
  485. * data: {
  486. *
  487. * }
  488. */
  489. function wxH5Index(Request $request)
  490. {
  491. if ($params = $this->getPayParams($request)) {
  492. $params['pay_merchant_id'] = $this->h5_pay_merchat_id;
  493. } else {
  494. return response()->error('QAPP_PARAM_ERROR');
  495. }
  496. $app = OrderArousePayFactory::wx($this->uid);
  497. // 微信支付参数
  498. $params['trade_type'] = 'MWEB'; //交易类型
  499. $result = $app->handle($params);
  500. if ($result) {
  501. $result['trade_no'] = $params['trade_no'];
  502. return response()->success($result);
  503. } else {
  504. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  505. }
  506. }
  507. /**
  508. * @apiVersion 1.0.0
  509. * @apiDescription 支付宝APP支付
  510. * @api {get} goToAliPay 支付宝APP支付
  511. * @apiGroup pay
  512. * @apiName aliIndex
  513. * @apiParam {Int} product_id product_id
  514. * @apiParam {Int} send_order_id send_order_id
  515. * @apiParam {String} bid bid
  516. * @apiHeader {String} [Authorization] token
  517. * @apiSuccess {Object} data.order_info 唤起支付信息str
  518. * @apiSuccess {Object} data.trade_no 订单号
  519. * @apiSuccessExample {json} Success-Response:
  520. * HTTP/1.1 200 OK
  521. * {
  522. * code: 0,
  523. * msg: "",
  524. * data:""
  525. *
  526. */
  527. public function aliIndex(Request $request)
  528. {
  529. if ($params = $this->getPayParams($request)) {
  530. $params['pay_merchant_id'] = $this->ali_pay_merchat_id;
  531. $params['type'] = 'App';
  532. } else {
  533. return response()->error('QAPP_PARAM_ERROR');
  534. }
  535. $app = OrderArousePayFactory::ali($this->uid);
  536. $order_info = $app->handle($params);
  537. return response()->success(['trade_no' => $params['trade_no'], 'order_info' => $order_info]);
  538. }
  539. /**
  540. * @apiVersion 1.0.0
  541. * @apiDescription 订单查询
  542. * @api {get} checkOrder 订单查询
  543. * @apiGroup pay
  544. * @apiName checkOrder
  545. * @apiParam {String} [token] token
  546. * @apiHeader {String} [Authorization] token 两个token任选其一
  547. * @apiParam {String} order order
  548. * @apiSuccess {int} code 状态码
  549. * @apiSuccess {String} msg 信息
  550. * @apiSuccess {Object} data 信息
  551. * @apiSuccessExample {json} Success-Response:
  552. * HTTP/1.1 200 OK
  553. * {
  554. * code: 0,
  555. * msg: "",
  556. * data: {
  557. *
  558. * }
  559. */
  560. public function checkOrder(Request $request)
  561. {
  562. $order = $request->input('order');
  563. $order_info = OrderService::getByTradeNo($order);
  564. if ($order_info && $order_info->status == 'PAID') {
  565. return response()->success();
  566. }
  567. return response()->success($order);
  568. }
  569. /**
  570. * 官方微信回调
  571. */
  572. function wxback(Request $request)
  573. {
  574. $xml = XML::parse(strval($request->getContent()));
  575. myLog('wxpay')->info($xml);
  576. if (isset($xml['attach'])) {
  577. $pay_merchant_id = (int)$xml['attach'];
  578. $config = PayMerchantService::findPayConfig($pay_merchant_id);
  579. $app = PayFactory::official($config);
  580. $response = $app->notify()->handleNotify(function ($notify, $successful) {
  581. if (!$successful) {
  582. return 'fail';
  583. }
  584. if (OrderPaySuccess::handle($notify->out_trade_no, $notify->transaction_id)) {
  585. return true;
  586. } else {
  587. return 'fail';
  588. }
  589. });
  590. return $response;
  591. } else {
  592. return 'fail';
  593. }
  594. }
  595. /**
  596. * 支付宝支付回调
  597. */
  598. function aliback(Request $request)
  599. {
  600. $param = $request->except('_url');
  601. myLog('alipay')->info($param);
  602. $trade_no = isset($param['out_trade_no']) ? $param['out_trade_no'] : '';
  603. if ($trade_no) {
  604. $order = Order::where('trade_no', $trade_no)->first();
  605. $pay_merchant_id = $order ? $order->pay_merchant_id : 0;
  606. $pay_merchant_id = $pay_merchant_id ? $pay_merchant_id : 140;
  607. $config = PayMerchantService::findAliPayConfig($pay_merchant_id);
  608. $app = PayFactory::aliPay($config);
  609. if ($app->notify($param)) {
  610. if (OrderPaySuccess::handle($param['out_trade_no'], $param['trade_no'])) {
  611. return response('success');
  612. } else {
  613. return response('fail');
  614. }
  615. }
  616. }
  617. myLog('alipay')->info('sign fail');
  618. return response('fail');
  619. }
  620. function wait(Request $request)
  621. {
  622. $param = $request->except('_url');
  623. return view('pay.middleware')->with($param);
  624. }
  625. }