OrdersController.php 22 KB

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