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