OrdersController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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\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\WechatPay;
  13. use App\Modules\Subscribe\Services\YearOrderService;
  14. use App\Modules\User\Services\UserService;
  15. use DB;
  16. use Redis;
  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 Log;
  23. class OrdersController extends BaseController
  24. {
  25. /**
  26. * @apiDefine Order 订单
  27. */
  28. /**
  29. * @apiVersion 1.0.0
  30. * @apiDescription 充值列表
  31. * @api {get} order/chargeList 充值列表
  32. * @apiParam {String} [token] token
  33. * @apiHeader {String} [Authorization] token 两个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. * @apiParam {String} [token] token
  167. * @apiHeader {String} [Authorization] token 两个token任选其一
  168. * @apiGroup Order
  169. * @apiName bookOrderList
  170. * @apiSuccess {int} code 状态码
  171. * @apiSuccess {String} msg 信息
  172. * @apiSuccess {object} data 结果集
  173. * @apiSuccess {Int} uid uid
  174. * @apiSuccess {Int} bid bid
  175. * @apiSuccess {Int} book_name 书名
  176. * @apiSuccess {Int} fee 钱
  177. * @apiSuccess {String} created_at 时间
  178. * @apiSuccessExample {json} Success-Response:
  179. * HTTP/1.1 200 OK
  180. * {
  181. * code: 0,
  182. * msg: "",
  183. * data: list:[
  184. * {
  185. * uid: 4,
  186. * bid: 1,
  187. * book_name: "dfsedfertrwet",
  188. * fee: 100,
  189. * created_at: "2017-12-02 16:24:54"
  190. * }
  191. * ]
  192. * meta: {
  193. * total: 1,
  194. * per_page: 15,
  195. * current_page: 1,
  196. * last_page: 1,
  197. * next_page_url: "",
  198. * prev_page_url: ""
  199. * }
  200. * }
  201. */
  202. public function bookOrderList(Request $request)
  203. {
  204. $page_size = $request->input('page_size', 15);
  205. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  206. return response()->pagination(new BookOrderTransformer(), $book_order);
  207. }
  208. /**
  209. * @apiVersion 1.0.0
  210. * @apiDescription 章节消费记录
  211. * @api {get} order/chapterOrderList 章节消费记录
  212. * @apiParam {String} [token] token
  213. * @apiHeader {String} [Authorization] token 两个token任选其一
  214. * @apiGroup Order
  215. * @apiName chapterOrderList
  216. * @apiSuccess {int} code 状态码
  217. * @apiSuccess {String} msg 信息
  218. * @apiSuccess {object} data 结果集
  219. * @apiSuccess {Int} uid uid
  220. * @apiSuccess {Int} bid bid
  221. * @apiSuccess {Int} cid cid
  222. * @apiSuccess {Int} chapter_name 章节名
  223. * @apiSuccess {Int} book_name 书名
  224. * @apiSuccess {Int} fee 钱
  225. * @apiSuccess {String} created_at 时间
  226. * @apiSuccessExample {json} Success-Response:
  227. * HTTP/1.1 200 OK
  228. * {
  229. * code: 0,
  230. * msg: "",
  231. * data: list:[
  232. * {
  233. * uid: 4,
  234. * bid: 1,
  235. * cid: 1,
  236. * chapter_name: "sdfsd",
  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. public function chapterOrderList(Request $request)
  253. {
  254. $chapter_model = new ChapterOrderService();
  255. $page_size = $request->input('page_size', 15);
  256. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  257. foreach ($chapter_order as $item) {
  258. if ($item->fee == 0) {
  259. $result = AdVisitStatService::getInfoV2($this->uid, $item->cid, ['UNLOCK', 'UNLOCK_2']);
  260. //if ($result)$item->fee = '解锁';
  261. $item->fee = '解锁';
  262. }
  263. }
  264. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  265. }
  266. /**
  267. * @apiVersion 1.0.0
  268. * @apiDescription 充值记录
  269. * @api {get} order/chargeRecordLists 充值记录
  270. * @apiParam {String} [token] token
  271. * @apiHeader {String} [Authorization] token 两个token任选其一
  272. * @apiGroup Order
  273. * @apiName chargeRecordLists
  274. * @apiSuccess {int} code 状态码
  275. * @apiSuccess {String} msg 信息
  276. * @apiSuccess {object} data 结果集
  277. * @apiSuccess {String} data.price 价格
  278. * @apiSuccess {String} data.status 状态
  279. * @apiSuccess {String} data.trade_no 订单号
  280. * @apiSuccess {String} data.created_at 时间
  281. * @apiSuccessExample {json} Success-Response:
  282. * HTTP/1.1 200 OK
  283. * {
  284. * code: 0,
  285. * msg: "",
  286. * data: {
  287. * list: [
  288. * {
  289. * id: 134,
  290. * price: "1.00",
  291. * status: "PAID",
  292. * trade_no: "201712021915481585670623626232",
  293. * created_at: "2017-12-02 19:15:56"
  294. * }
  295. * ],
  296. * meta: {
  297. * total: 1,
  298. * per_page: 15,
  299. * current_page: 1,
  300. * last_page: 1,
  301. * next_page_url: "",
  302. * prev_page_url: ""
  303. * }
  304. * }
  305. * }
  306. */
  307. public function chargeRecordLists(Request $request)
  308. {
  309. $page_size = $request->input('page_size', 15);
  310. $res = OrderService::getOrderList($this->uid, $page_size);
  311. return response()->pagination(new ChargeListTransformer(), $res);
  312. }
  313. //订单是否成功
  314. public function isSuccess(Request $request)
  315. {
  316. $order = $request->input('order');
  317. $order_info = OrderService::getByTradeNo($order);
  318. if ($order_info && $order_info->status == 'PAID') {
  319. return response()->success();
  320. }
  321. return response()->error('QAPP_SYS_ERROR');
  322. }
  323. /**
  324. * @apiVersion 1.0.0
  325. * @apiDescription 支付
  326. * @api {get} goToPay 支付
  327. * @apiGroup pay
  328. * @apiName wxindex
  329. * @apiParam {Int} product_id product_id
  330. * @apiParam {String} [token] token
  331. * @apiHeader {String} [Authorization] token 两个token任选其一
  332. * @apiParam {String} bid bid
  333. * @apiParam {String} sign 签名
  334. * @apiSuccess {int} code 状态码
  335. * @apiSuccess {String} msg 信息
  336. * @apiSuccess {Object} data 信息
  337. * @apiSuccess {Object} data.appId 唤起支付的appId
  338. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  339. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  340. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  341. * @apiSuccess {Object} data.sign 唤起支付的sign
  342. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  343. * @apiSuccessExample {json} Success-Response:
  344. * HTTP/1.1 200 OK
  345. * {
  346. * code: 0,
  347. * msg: "",
  348. * data: {
  349. *
  350. * }
  351. */
  352. function wxindex(Request $request)
  353. {
  354. $product_id = $request->get('product_id', 0);
  355. $send_order_id = $request->get('send_order_id', 0);
  356. if (!$product_id) {
  357. return response()->error('QAPP_PARAM_ERROR');
  358. }
  359. $bid = $request->get('bid', 0);
  360. if ($bid) {
  361. $bid = BookService::decodeBidStatic($bid);
  362. }
  363. $trade_no = date("YmdHis") . hexdec(uniqid());
  364. $product_info = ProductService::getProductSingle($product_id);
  365. $uid = $this->uid;
  366. $distribution_channel_id = $this->distribution_channel_id;
  367. $price = $product_info->price * 100;
  368. if (in_array($uid, explode(',', env('TEST_UID')))) {
  369. $price = 1;
  370. }
  371. if ($product_info->type == 'YEAR_ORDER') {
  372. $order_type = 'YEAR';
  373. } elseif ($product_info->type == 'BOOK_ORDER') {
  374. $order_type = 'BOOK';
  375. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  376. $order_type = 'RECHARGE';
  377. } else {
  378. $order_type = '';
  379. }
  380. $this->createUnPayOrder([
  381. 'distribution_channel_id' => $distribution_channel_id,
  382. 'uid' => $uid,
  383. 'product_id' => $product_id,
  384. 'price' => $price / 100,
  385. 'pay_type' => 1,
  386. 'trade_no' => $trade_no,
  387. 'pay_merchant_source' => 'QuickApp',
  388. 'pay_merchant_id' => 0,
  389. 'create_ip' => $request->getClientIp(),
  390. 'send_order_id' => $send_order_id,
  391. 'order_type' => $order_type,
  392. 'from_bid' => $bid,
  393. 'from_type' => 'QuickApp',
  394. 'activity_id' => 0
  395. ]);
  396. $config = [
  397. // 微信支付参数
  398. 'appid' => 'wxf065f7364b078a73', // 应用ID
  399. 'merchant_id' => '1500977641', // 微信支付商户号
  400. 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
  401. 'trade_type' => 'APP',
  402. ];
  403. $pay = WechatPay::Official('OFFICIALPAY', $config);
  404. try {
  405. $payOrder = [
  406. 'trade_no' => $trade_no, // 订单号
  407. 'price' => $price, // 订单金额,**单位:分**
  408. 'body' => '快应用 小说', // 订单描述
  409. 'create_ip' => _getIp(), // 支付人的 IP
  410. 'remark' => 'QuickApp'
  411. ];
  412. $result = $pay->send($payOrder);
  413. if ($result) {
  414. return response()->success($result);
  415. } else {
  416. Log::error("创建微信订单失败," . json_encode($result));
  417. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  418. }
  419. } catch (Exception $e) {
  420. Log::error("创建微信订单失败," . $e->getMessage());
  421. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  422. }
  423. }
  424. /**
  425. * @apiVersion 1.0.0
  426. * @apiDescription 订单查询
  427. * @api {get} checkOrder 订单查询
  428. * @apiGroup pay
  429. * @apiName checkOrder
  430. * @apiParam {String} [token] token
  431. * @apiHeader {String} [Authorization] token 两个token任选其一
  432. * @apiParam {String} order order
  433. * @apiSuccess {int} code 状态码
  434. * @apiSuccess {String} msg 信息
  435. * @apiSuccess {Object} data 信息
  436. * @apiSuccessExample {json} Success-Response:
  437. * HTTP/1.1 200 OK
  438. * {
  439. * code: 0,
  440. * msg: "",
  441. * data: {
  442. *
  443. * }
  444. */
  445. public function checkOrder(Request $request)
  446. {
  447. $order = $request->input('order', '');
  448. $i = 0;
  449. $uid = $this->uid;
  450. while ($i <= 10) {
  451. $order_info = OrderService::getByTradeNo($order);
  452. if (!$order_info) {
  453. return response()->error('QAPP_SYS_ERROR');
  454. break;
  455. }
  456. if (isset($order_info->status) && $order_info->status == 'PAID') {
  457. $data['balance'] = 0;
  458. $user = UserService::getById($uid);
  459. if ($user) {
  460. $data['balance'] = $user->balance;
  461. }
  462. return response()->success($data);
  463. }
  464. sleep(1);
  465. $i++;
  466. }
  467. return response()->error('QAPP_SYS_ERROR');
  468. }
  469. /**
  470. * 官方微信回调
  471. * @param Request $request
  472. * @return
  473. */
  474. function wxback(Request $request)
  475. {
  476. $options = [
  477. 'app_id' => 'wxf065f7364b078a73',
  478. 'payment' => [
  479. 'merchant_id' => 1500977641,
  480. 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E',
  481. ]
  482. ];
  483. $app = new Application($options);
  484. $response = $app->payment->handleNotify(function ($notify, $successful) {
  485. if (!$successful) return 'fail';
  486. $trade_no = $notify->out_trade_no;
  487. $order = OrderService::getByTradeNo($trade_no);
  488. if (!$order) {
  489. return 'fail';
  490. }
  491. if (isset($order->status) && $order->status == 'PAID') {
  492. Log::info('has_pay:' . $trade_no);
  493. return true;
  494. }
  495. DB::beginTransaction();
  496. try {
  497. $transaction_id = $notify->transaction_id;
  498. $uid = $order->uid;
  499. $distribution_channel_id = $order->distribution_channel_id;
  500. $product_id = $order->product_id;
  501. $product = ProductService::getProductSingle($product_id);
  502. $send_order_id = 0;
  503. $price = $product->price;
  504. // 更新其他定制Order表
  505. if ($product->type == 'YEAR_ORDER') {
  506. $order_type = 'YEAR';
  507. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  508. $order->order_type = $order_type;
  509. $order->status = 'PAID';
  510. $order->pay_end_at = date('Y-m-d H:i:s');
  511. $order->transaction_id = $transaction_id;
  512. $order->save();
  513. } elseif ($product->type == 'BOOK_ORDER') {
  514. $order_type = 'BOOK';
  515. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  516. $order->order_type = $order_type;
  517. $order->status = 'PAID';
  518. $order->pay_end_at = date('Y-m-d H:i:s');
  519. $order->transaction_id = $transaction_id;
  520. $order->save();
  521. } elseif ($product->type == 'TICKET_RECHARGE') {
  522. $order_type = 'RECHARGE';
  523. $this->userCharge($product, $uid);
  524. $order->order_type = $order_type;
  525. $order->status = 'PAID';
  526. $order->pay_end_at = date('Y-m-d H:i:s');
  527. $order->transaction_id = $transaction_id;
  528. $order->save();
  529. } else {
  530. DB::rollback();
  531. return 'Order not exist.';
  532. }
  533. DB::commit();
  534. return true;
  535. } catch (\Exception $e) {
  536. DB::rollback();
  537. return 'fail';
  538. }
  539. });
  540. return $response;
  541. }
  542. /**
  543. * 单本充值会掉
  544. * @param $product_id
  545. * @param $uid
  546. * @param $send_order_id
  547. * @param $fee
  548. */
  549. private function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  550. {
  551. $book_conf = BookConfigService::getBookByProduct($product_id);
  552. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  553. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  554. $insert_data['uid'] = $uid;
  555. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  556. $insert_data['fee'] = $fee;
  557. $insert_data['send_order_id'] = $send_order_id;
  558. $insert_data['charge_balance'] = 0;
  559. $insert_data['reward_balance'] = 0;
  560. return BookOrderService::save_book_order($insert_data);
  561. }
  562. /**
  563. * 包年
  564. * @param $uid
  565. * @param $distribution_channel_id
  566. * @param $fee
  567. * @param $send_order_id
  568. * @return mixed
  569. */
  570. private function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  571. {
  572. $insert_data['uid'] = $uid;
  573. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  574. $insert_data['fee'] = $fee;
  575. $insert_data['send_order_id'] = $send_order_id;
  576. return YearOrderService::save_year_order($insert_data);
  577. }
  578. /**
  579. * 用户充值
  580. * @param $product
  581. * @param $uid\
  582. */
  583. private function userCharge($product, $uid)
  584. {
  585. $total = $product->price * 100 + $product->given;
  586. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  587. }
  588. /**
  589. * 添加位置付订单
  590. * @param $data
  591. * @return mixed
  592. */
  593. private function createUnPayOrder($data)
  594. {
  595. $data['status'] = 'UNPAID';
  596. $data['transaction_id'] = '';
  597. $data['pay_end_at'] = '0000-00-00 00:00:00';
  598. return OrderService::save_order($data);
  599. }
  600. }