OrdersController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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\Xcx\Services\XcxOrderService as OrderService;
  9. use App\Modules\Subscribe\Services\OrderService;
  10. use App\Http\Controllers\QuickApp\Order\Transformers\BookOrderTransformer;
  11. use App\Http\Controllers\QuickApp\Order\Transformers\ChapterOrderTransformer;
  12. use App\Http\Controllers\QuickApp\Order\Transformers\ChargeListTransformer;
  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 Redis;
  18. use Hashids;
  19. use EasyWeChat\Foundation\Application;
  20. use App\Modules\Product\Services\ProductService;
  21. use App\Modules\Book\Services\BookConfigService;
  22. use App\Modules\Book\Services\BookService;
  23. use Log;
  24. class OrdersController extends BaseController
  25. {
  26. /**
  27. * @apiDefine Order 订单
  28. */
  29. /**
  30. * @apiVersion 1.0.0
  31. * @apiDescription 充值列表
  32. * @api {get} order/chargeList 充值列表
  33. * @apiHeader {String} [Authorization] token
  34. * @apiGroup Order
  35. * @apiName chargeList
  36. * @apiSuccess {int} code 状态码
  37. * @apiSuccess {String} msg 信息
  38. * @apiSuccess {object} data 结果集
  39. * @apiSuccessExample {json} Success-Response:
  40. * HTTP/1.1 200 OK
  41. * {
  42. * code: 0,
  43. * msg: "",
  44. * data: [
  45. * {
  46. * product_id: 1,
  47. * price: "30.00元",
  48. * vip: 0,
  49. * intro: [
  50. * {
  51. * label: 3000,
  52. * important: false
  53. * },
  54. * {
  55. * label: "书币",
  56. * important: true
  57. * }
  58. * ]
  59. * },
  60. * {
  61. * product_id: 2,
  62. * price: "50.00元",
  63. * vip: 1,
  64. * intro: [
  65. * {
  66. * label: 5000,
  67. * important: false
  68. * },
  69. * {
  70. * label: "1000+",
  71. * important: true
  72. * },
  73. * {
  74. * label: "书币",
  75. * important: false
  76. * }
  77. * ]
  78. * },
  79. * {
  80. * product_id: 5,
  81. * price: "365.00元",
  82. * vip: 0,
  83. * intro: [
  84. * {
  85. * label: "年费VIP会员",
  86. * important: true
  87. * }
  88. * ]
  89. * }
  90. * ]
  91. * }
  92. */
  93. public function chargeList(Request $request)
  94. {
  95. $res = ProductService::getChargeProduct();
  96. if (!$res->isEmpty()) {
  97. $data = [];
  98. foreach ($res as $v) {
  99. $intro = [];
  100. if ($v->given > 0 && $v->type == 'TICKET_RECHARGE') {
  101. $intro = [
  102. [
  103. 'label' => ($v->price * 100) . "+",
  104. 'important' => false,
  105. ],
  106. [
  107. 'label' => $v->given,
  108. 'important' => true,
  109. ],
  110. [
  111. 'label' => '书币',
  112. 'important' => false,
  113. ]
  114. ];
  115. $intro2 = [
  116. ['label' => '多送', 'important' => false],
  117. ['label' => (int) ($v->given / 100), 'important' => true],
  118. ['label' => '元', 'important' => false],
  119. ];
  120. $v->vip = 0;
  121. }
  122. if ($v->given == 0 && $v->type == 'TICKET_RECHARGE') {
  123. $intro = [
  124. [
  125. 'label' => $v->price * 100,
  126. 'important' => false,
  127. ],
  128. [
  129. 'label' => '书币',
  130. 'important' => false,
  131. ]
  132. ];
  133. $v->vip = 0;
  134. $intro2 = [];
  135. }
  136. if ($v->given == 0 && $v->type == 'YEAR_ORDER') {
  137. $intro = [
  138. [
  139. 'label' => '年费VIP会员',
  140. 'important' => true,
  141. ]
  142. ];
  143. $v->vip = 1;
  144. $intro2 = [
  145. ['label' => '每天1元,全年免费看', 'important' => false],
  146. ];
  147. }
  148. $data[] = [
  149. 'product_id' => $v->id,
  150. 'price' => (int) $v->price . '元',
  151. 'vip' => $v->vip,
  152. 'intro' => $intro,
  153. 'intro2' => $intro2,
  154. 'is_default' => $v->is_default,
  155. ];
  156. }
  157. return response()->success($data);
  158. } else {
  159. return response()->error('QAPP_SYS_ERROR');
  160. }
  161. }
  162. /**
  163. * @apiVersion 1.0.0
  164. * @apiDescription 单本消费记录
  165. * @api {get} order/bookOrderList 单本消费记录
  166. * @apiHeader {String} [Authorization] token
  167. * @apiGroup Order
  168. * @apiName bookOrderList
  169. * @apiParam {String} [page_size] 分页大小
  170. * @apiParam {String} [page] 页码
  171. * @apiSuccess {int} code 状态码
  172. * @apiSuccess {String} msg 信息
  173. * @apiSuccess {object} data 结果集
  174. * @apiSuccess {Int} uid uid
  175. * @apiSuccess {Int} bid bid
  176. * @apiSuccess {Int} book_name 书名
  177. * @apiSuccess {Int} fee 钱
  178. * @apiSuccess {String} created_at 时间
  179. * @apiSuccessExample {json} Success-Response:
  180. * HTTP/1.1 200 OK
  181. * {
  182. * code: 0,
  183. * msg: "",
  184. * data: list:[
  185. * {
  186. * uid: 4,
  187. * bid: 1,
  188. * book_name: "dfsedfertrwet",
  189. * fee: 100,
  190. * created_at: "2017-12-02 16:24:54"
  191. * }
  192. * ]
  193. * meta: {
  194. * total: 1,
  195. * per_page: 15,
  196. * current_page: 1,
  197. * last_page: 1,
  198. * next_page_url: "",
  199. * prev_page_url: ""
  200. * }
  201. * }
  202. * }
  203. */
  204. public function bookOrderList(Request $request)
  205. {
  206. $page_size = $request->input('page_size', 15);
  207. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  208. return response()->pagination(new BookOrderTransformer(), $book_order);
  209. }
  210. /**
  211. * @apiVersion 1.0.0
  212. * @apiDescription 章节消费记录
  213. * @api {get} order/chapterOrderList 章节消费记录
  214. * @apiHeader {String} [Authorization] token
  215. * @apiGroup Order
  216. * @apiName chapterOrderList
  217. * @apiParam {String} [page_size] 分页大小
  218. * @apiParam {String} [page] 页码
  219. * @apiSuccess {int} code 状态码
  220. * @apiSuccess {String} msg 信息
  221. * @apiSuccess {object} data 结果集
  222. * @apiSuccess {Int} uid uid
  223. * @apiSuccess {Int} bid bid
  224. * @apiSuccess {Int} cid cid
  225. * @apiSuccess {Int} chapter_name 章节名
  226. * @apiSuccess {Int} book_name 书名
  227. * @apiSuccess {Int} fee 钱
  228. * @apiSuccess {String} created_at 时间
  229. * @apiSuccessExample {json} Success-Response:
  230. * HTTP/1.1 200 OK
  231. * {
  232. * code: 0,
  233. * msg: "",
  234. * data: list:[
  235. * {
  236. * uid: 4,
  237. * bid: 1,
  238. * cid: 1,
  239. * chapter_name: "sdfsd",
  240. * book_name: "dfsedfertrwet",
  241. * fee: 100,
  242. * created_at: "2017-12-02 16:24:54"
  243. * }
  244. * ]
  245. * meta: {
  246. * total: 1,
  247. * per_page: 15,
  248. * current_page: 1,
  249. * last_page: 1,
  250. * next_page_url: "",
  251. * prev_page_url: ""
  252. * }
  253. * }
  254. */
  255. public function chapterOrderList(Request $request)
  256. {
  257. $chapter_model = new ChapterOrderService();
  258. $page_size = $request->input('page_size', 15);
  259. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  260. foreach ($chapter_order as $item) {
  261. if ($item->fee == 0) {
  262. $result = AdVisitStatService::getInfoV2($this->uid, $item->cid, ['UNLOCK', 'UNLOCK_2']);
  263. $item->fee = '解锁';
  264. }
  265. }
  266. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  267. }
  268. /**
  269. * @apiVersion 1.0.0
  270. * @apiDescription 充值记录
  271. * @api {get} order/chargeRecordLists 充值记录
  272. * @apiParam {String} [token] token
  273. * @apiHeader {String} [Authorization] token 两个token任选其一
  274. * @apiGroup Order
  275. * @apiName chargeRecordLists
  276. * @apiParam {String} [page_size] 分页大小
  277. * @apiParam {String} [page] 页码
  278. * @apiSuccess {int} code 状态码
  279. * @apiSuccess {String} msg 信息
  280. * @apiSuccess {object} data 结果集
  281. * @apiSuccess {String} data.price 价格
  282. * @apiSuccess {String} data.status 状态
  283. * @apiSuccess {String} data.trade_no 订单号
  284. * @apiSuccess {String} data.created_at 时间
  285. * @apiSuccessExample {json} Success-Response:
  286. * HTTP/1.1 200 OK
  287. * {
  288. * code: 0,
  289. * msg: "",
  290. * data: {
  291. * list: [
  292. * {
  293. * id: 134,
  294. * price: "1.00",
  295. * status: "PAID",
  296. * trade_no: "201712021915481585670623626232",
  297. * created_at: "2017-12-02 19:15:56"
  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. */
  311. public function chargeRecordLists(Request $request)
  312. {
  313. $page_size = $request->input('page_size', 15);
  314. $res = OrderService::getOrderList($this->uid, $page_size);
  315. return response()->pagination(new ChargeListTransformer(), $res);
  316. }
  317. //订单是否成功
  318. public function isSuccess(Request $request)
  319. {
  320. $order = $request->input('order');
  321. $order_info = OrderService::getByTradeNo($order);
  322. if ($order_info && $order_info->status == 'PAID') {
  323. return response()->success();
  324. }
  325. return response()->error('QAPP_SYS_ERROR');
  326. }
  327. /**
  328. * @apiVersion 1.0.0
  329. * @apiDescription 支付
  330. * @api {get} goToPay 支付
  331. * @apiGroup pay
  332. * @apiName wxindex
  333. * @apiParam {Int} product_id product_id
  334. * @apiParam {String} [token] token
  335. * @apiHeader {String} [Authorization] token 两个token任选其一
  336. * @apiParam {String} bid bid
  337. * @apiParam {String} sign 签名
  338. * @apiSuccess {int} code 状态码
  339. * @apiSuccess {String} msg 信息
  340. * @apiSuccess {Object} data 信息
  341. * @apiSuccess {Object} data.appId 唤起支付的appId
  342. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  343. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  344. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  345. * @apiSuccess {Object} data.sign 唤起支付的sign
  346. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  347. * @apiSuccessExample {json} Success-Response:
  348. * HTTP/1.1 200 OK
  349. * {
  350. * code: 0,
  351. * msg: "",
  352. * data: {
  353. *
  354. * }
  355. */
  356. function wxindex(Request $request)
  357. {
  358. $product_id = $request->get('product_id', 0);
  359. $send_order_id = $request->get('send_order_id', 0);
  360. if (!$product_id) {
  361. return response()->error('QAPP_PARAM_ERROR');
  362. }
  363. $bid = $request->get('bid', 0);
  364. if ($bid) {
  365. $bid = BookService::decodeBidStatic($bid);
  366. }
  367. $trade_no = date("YmdHis") . hexdec(uniqid());
  368. $product_info = ProductService::getProductSingle($product_id);
  369. $uid = $this->uid;
  370. $distribution_channel_id = $this->distribution_channel_id;
  371. $price = $product_info->price * 100;
  372. if (in_array($uid, explode(',', env('TEST_UID')))) {
  373. $price = 1;
  374. }
  375. if ($product_info->type == 'YEAR_ORDER') {
  376. $order_type = 'YEAR';
  377. } elseif ($product_info->type == 'BOOK_ORDER') {
  378. $order_type = 'BOOK';
  379. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  380. $order_type = 'RECHARGE';
  381. } else {
  382. $order_type = '';
  383. }
  384. $this->createUnPayOrder([
  385. 'distribution_channel_id' => $distribution_channel_id,
  386. 'uid' => $uid,
  387. 'product_id' => $product_id,
  388. 'price' => $price / 100,
  389. 'pay_type' => 1,
  390. 'trade_no' => $trade_no,
  391. 'pay_merchant_source' => 'QuickApp',
  392. 'pay_merchant_id' => 0,
  393. 'create_ip' => $request->getClientIp(),
  394. 'send_order_id' => $send_order_id,
  395. 'order_type' => $order_type,
  396. 'from_bid' => $bid,
  397. 'from_type' => 'QuickApp',
  398. 'activity_id' => 0
  399. ]);
  400. $config = [
  401. // 微信支付参数
  402. 'appid' => 'wxf065f7364b078a73', // 应用ID
  403. 'merchant_id' => '1500977641', // 微信支付商户号
  404. 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
  405. 'trade_type' => 'APP',
  406. ];
  407. $pay = WechatPay::Official('OFFICIALPAY', $config);
  408. try {
  409. $payOrder = [
  410. 'trade_no' => $trade_no, // 订单号
  411. 'price' => $price, // 订单金额,**单位:分**
  412. 'body' => '快应用 小说', // 订单描述
  413. 'create_ip' => _getIp(), // 支付人的 IP
  414. 'remark' => 'QuickApp'
  415. ];
  416. $result = $pay->send($payOrder);
  417. if ($result) {
  418. return response()->success($result);
  419. } else {
  420. Log::error("创建微信订单失败," . json_encode($result));
  421. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  422. }
  423. } catch (Exception $e) {
  424. Log::error("创建微信订单失败," . $e->getMessage());
  425. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  426. }
  427. }
  428. /**
  429. * @apiVersion 1.0.0
  430. * @apiDescription 订单查询
  431. * @api {get} checkOrder 订单查询
  432. * @apiGroup pay
  433. * @apiName checkOrder
  434. * @apiParam {String} [token] token
  435. * @apiHeader {String} [Authorization] token 两个token任选其一
  436. * @apiParam {String} order order
  437. * @apiSuccess {int} code 状态码
  438. * @apiSuccess {String} msg 信息
  439. * @apiSuccess {Object} data 信息
  440. * @apiSuccessExample {json} Success-Response:
  441. * HTTP/1.1 200 OK
  442. * {
  443. * code: 0,
  444. * msg: "",
  445. * data: {
  446. *
  447. * }
  448. */
  449. public function checkOrder(Request $request)
  450. {
  451. $order = $request->input('order', '');
  452. $i = 0;
  453. $uid = $this->uid;
  454. while ($i <= 10) {
  455. $order_info = OrderService::getByTradeNo($order);
  456. if (!$order_info) {
  457. return response()->error('QAPP_SYS_ERROR');
  458. break;
  459. }
  460. if (isset($order_info->status) && $order_info->status == 'PAID') {
  461. $data['balance'] = 0;
  462. $user = UserService::getById($uid);
  463. if ($user) {
  464. $data['balance'] = $user->balance;
  465. }
  466. return response()->success($data);
  467. }
  468. sleep(1);
  469. $i++;
  470. }
  471. return response()->error('QAPP_SYS_ERROR');
  472. }
  473. /**
  474. * 官方微信回调
  475. * @param Request $request
  476. * @return
  477. */
  478. function wxback(Request $request)
  479. {
  480. $options = [
  481. 'app_id' => 'wxf065f7364b078a73',
  482. 'payment' => [
  483. 'merchant_id' => 1500977641,
  484. 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E',
  485. ]
  486. ];
  487. $app = new Application($options);
  488. $response = $app->payment->handleNotify(function ($notify, $successful) {
  489. if (!$successful) return 'fail';
  490. $trade_no = $notify->out_trade_no;
  491. $order = OrderService::getByTradeNo($trade_no);
  492. if (!$order) {
  493. return 'fail';
  494. }
  495. if (isset($order->status) && $order->status == 'PAID') {
  496. Log::info('has_pay:' . $trade_no);
  497. return true;
  498. }
  499. DB::beginTransaction();
  500. try {
  501. $transaction_id = $notify->transaction_id;
  502. $uid = $order->uid;
  503. $distribution_channel_id = $order->distribution_channel_id;
  504. $product_id = $order->product_id;
  505. $product = ProductService::getProductSingle($product_id);
  506. $send_order_id = 0;
  507. $price = $product->price;
  508. // 更新其他定制Order表
  509. if ($product->type == 'YEAR_ORDER') {
  510. $order_type = 'YEAR';
  511. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  512. $order->order_type = $order_type;
  513. $order->status = 'PAID';
  514. $order->pay_end_at = date('Y-m-d H:i:s');
  515. $order->transaction_id = $transaction_id;
  516. $order->save();
  517. } elseif ($product->type == 'BOOK_ORDER') {
  518. $order_type = 'BOOK';
  519. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  520. $order->order_type = $order_type;
  521. $order->status = 'PAID';
  522. $order->pay_end_at = date('Y-m-d H:i:s');
  523. $order->transaction_id = $transaction_id;
  524. $order->save();
  525. } elseif ($product->type == 'TICKET_RECHARGE') {
  526. $order_type = 'RECHARGE';
  527. $this->userCharge($product, $uid);
  528. $order->order_type = $order_type;
  529. $order->status = 'PAID';
  530. $order->pay_end_at = date('Y-m-d H:i:s');
  531. $order->transaction_id = $transaction_id;
  532. $order->save();
  533. } else {
  534. DB::rollback();
  535. return 'Order not exist.';
  536. }
  537. DB::commit();
  538. return true;
  539. } catch (\Exception $e) {
  540. DB::rollback();
  541. return 'fail';
  542. }
  543. });
  544. return $response;
  545. }
  546. /**
  547. * 单本充值会掉
  548. * @param $product_id
  549. * @param $uid
  550. * @param $send_order_id
  551. * @param $fee
  552. */
  553. private function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  554. {
  555. $book_conf = BookConfigService::getBookByProduct($product_id);
  556. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  557. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  558. $insert_data['uid'] = $uid;
  559. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  560. $insert_data['fee'] = $fee;
  561. $insert_data['send_order_id'] = $send_order_id;
  562. $insert_data['charge_balance'] = 0;
  563. $insert_data['reward_balance'] = 0;
  564. return BookOrderService::save_book_order($insert_data);
  565. }
  566. /**
  567. * 包年
  568. * @param $uid
  569. * @param $distribution_channel_id
  570. * @param $fee
  571. * @param $send_order_id
  572. * @return mixed
  573. */
  574. private function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  575. {
  576. $insert_data['uid'] = $uid;
  577. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  578. $insert_data['fee'] = $fee;
  579. $insert_data['send_order_id'] = $send_order_id;
  580. return YearOrderService::save_year_order($insert_data);
  581. }
  582. /**
  583. * 用户充值
  584. * @param $product
  585. * @param $uid\
  586. */
  587. private function userCharge($product, $uid)
  588. {
  589. $total = $product->price * 100 + $product->given;
  590. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  591. }
  592. /**
  593. * 添加位置付订单
  594. * @param $data
  595. * @return mixed
  596. */
  597. private function createUnPayOrder($data)
  598. {
  599. $data['status'] = 'UNPAID';
  600. $data['transaction_id'] = '';
  601. $data['pay_end_at'] = '0000-00-00 00:00:00';
  602. return OrderService::save_order($data);
  603. }
  604. }