OrdersController.php 22 KB

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