OrdersController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 EasyWeChat\Payment\Order as Wxorder;
  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. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  258. }
  259. /**
  260. * @apiVersion 1.0.0
  261. * @apiDescription 充值记录
  262. * @api {get} order/chargeRecordLists 充值记录
  263. * @apiParam {String} [token] token
  264. * @apiHeader {String} [Authorization] token 两个token任选其一
  265. * @apiGroup Order
  266. * @apiName chargeRecordLists
  267. * @apiSuccess {int} code 状态码
  268. * @apiSuccess {String} msg 信息
  269. * @apiSuccess {object} data 结果集
  270. * @apiSuccess {String} data.price 价格
  271. * @apiSuccess {String} data.status 状态
  272. * @apiSuccess {String} data.trade_no 订单号
  273. * @apiSuccess {String} data.created_at 时间
  274. * @apiSuccessExample {json} Success-Response:
  275. * HTTP/1.1 200 OK
  276. * {
  277. * code: 0,
  278. * msg: "",
  279. * data: {
  280. * list: [
  281. * {
  282. * id: 134,
  283. * price: "1.00",
  284. * status: "PAID",
  285. * trade_no: "201712021915481585670623626232",
  286. * created_at: "2017-12-02 19:15:56"
  287. * }
  288. * ],
  289. * meta: {
  290. * total: 1,
  291. * per_page: 15,
  292. * current_page: 1,
  293. * last_page: 1,
  294. * next_page_url: "",
  295. * prev_page_url: ""
  296. * }
  297. * }
  298. * }
  299. */
  300. public function chargeRecordLists(Request $request)
  301. {
  302. $page_size = $request->input('page_size', 15);
  303. $res = OrderService::getOrderList($this->uid, $page_size);
  304. return response()->pagination(new ChargeListTransformer(), $res);
  305. }
  306. //订单是否成功
  307. public function isSuccess(Request $request)
  308. {
  309. $order = $request->input('order');
  310. $order_info = OrderService::getByTradeNo($order);
  311. if ($order_info && $order_info->status == 'PAID') {
  312. return response()->success();
  313. }
  314. return response()->error('QAPP_SYS_ERROR');
  315. }
  316. /**
  317. * @apiVersion 1.0.0
  318. * @apiDescription 支付
  319. * @api {get} goToPay 支付
  320. * @apiGroup pay
  321. * @apiName wxindex
  322. * @apiParam {Int} product_id product_id
  323. * @apiParam {String} [token] token
  324. * @apiHeader {String} [Authorization] token 两个token任选其一
  325. * @apiParam {String} bid bid
  326. * @apiParam {String} sign 签名
  327. * @apiSuccess {int} code 状态码
  328. * @apiSuccess {String} msg 信息
  329. * @apiSuccess {Object} data 信息
  330. * @apiSuccess {Object} data.appId 唤起支付的appId
  331. * @apiSuccess {Object} data.package 唤起支付的package
  332. * @apiSuccess {Object} data.nonceStr 唤起支付的nonceStr
  333. * @apiSuccess {Object} data.timeStamp 唤起支付的timeStamp
  334. * @apiSuccess {Object} data.signType 唤起支付的signType
  335. * @apiSuccess {Object} data.paySign 唤起支付sign
  336. * @apiSuccessExample {json} Success-Response:
  337. * HTTP/1.1 200 OK
  338. * {
  339. * code: 0,
  340. * msg: "",
  341. * data: {
  342. *
  343. * }
  344. */
  345. function wxindex(Request $request)
  346. {
  347. $product_id = $request->get('product_id', 0);
  348. $send_order_id = $request->get('send_order_id', 0);
  349. if (empty($product_id) || empty($sign)) {
  350. return response()->error('QAPP_PARAM_ERROR');
  351. }
  352. $bid = $request->get('bid', 0);
  353. if ($bid) {
  354. $bid = BookService::decodeBidStatic($bid);
  355. }
  356. $trade_no = date("YmdHis") . hexdec(uniqid());
  357. $product_info = ProductService::getProductSingle($product_id);
  358. $uid = $this->uid;
  359. $distribution_channel_id = $this->distribution_channel_id;
  360. $price = $product_info->price * 100;
  361. if (in_array($uid, explode(',', env('TEST_UID')))) {
  362. $price = 1;
  363. }
  364. if ($product_info->type == 'YEAR_ORDER') {
  365. $order_type = 'YEAR';
  366. } elseif ($product_info->type == 'BOOK_ORDER') {
  367. $order_type = 'BOOK';
  368. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  369. $order_type = 'RECHARGE';
  370. } else {
  371. $order_type = '';
  372. }
  373. $res = $this->createUnPayOrder([
  374. 'distribution_channel_id' => $distribution_channel_id,
  375. 'uid' => $uid,
  376. 'product_id' => $product_id,
  377. 'price' => $price / 100,
  378. 'pay_type' => 1,
  379. 'trade_no' => $trade_no,
  380. 'pay_merchant_source' => 'QuickApp',
  381. 'pay_merchant_id' => 0,
  382. 'create_ip' => $request->getClientIp(),
  383. 'send_order_id' => $send_order_id,
  384. 'order_type' => $order_type,
  385. 'from_bid' => $bid,
  386. 'from_type' => 'QuickApp',
  387. 'activity_id' => 0
  388. ]);
  389. $config = [
  390. // 微信支付参数
  391. 'app_id' => 'wxf065f7364b078a73', // 应用ID
  392. 'mch_id' => '1500977641', // 微信支付商户号
  393. 'mch_key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
  394. ];
  395. $pay = WechatPay::Official('OFFICIALPAY', $config);
  396. try {
  397. $payOrder = [
  398. 'out_trade_no' => $trade_no, // 订单号
  399. 'total_fee' => $price, // 订单金额,**单位:分**
  400. 'body' => '阅读云阅 小说', // 订单描述
  401. 'spbill_create_ip' => _getIp(), // 支付人的 IP
  402. 'notify_url' => env('WECHART_APP_PAY_CALL_BACK_URL'), // 定义通知URL
  403. 'attach' => 'QuickApp'
  404. ];
  405. $result = $pay->send($payOrder);
  406. if ($result && isset($result['prepayid'])) {
  407. $data['appid'] = $result['appid'];
  408. $data['partnerid'] = $result['partnerid'];
  409. $data['prepayid'] = $result['prepayid'];
  410. $data['timestamp'] = $result['timestamp'];
  411. $data['noncestr'] = $result['noncestr'];
  412. $data['packagInfo'] = $result['package'];
  413. $data['sign'] = $result['sign'];
  414. } else {
  415. Log::error("创建微信订单失败," . json_encode($result));
  416. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  417. }
  418. } catch (Exception $e) {
  419. Log::error("创建微信订单失败," . $e->getMessage());
  420. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  421. }
  422. }
  423. /**
  424. * @apiVersion 1.0.0
  425. * @apiDescription 订单查询
  426. * @api {get} checkOrder 订单查询
  427. * @apiGroup pay
  428. * @apiName checkOrder
  429. * @apiParam {String} [token] token
  430. * @apiHeader {String} [Authorization] token 两个token任选其一
  431. * @apiParam {String} order order
  432. * @apiSuccess {int} code 状态码
  433. * @apiSuccess {String} msg 信息
  434. * @apiSuccess {Object} data 信息
  435. * @apiSuccessExample {json} Success-Response:
  436. * HTTP/1.1 200 OK
  437. * {
  438. * code: 0,
  439. * msg: "",
  440. * data: {
  441. *
  442. * }
  443. */
  444. public function checkOrder(Request $request)
  445. {
  446. $order = $request->input('order', '');
  447. $i = 0;
  448. $uid = $this->uid;
  449. while ($i <= 10) {
  450. $order_info = OrderService::getByTradeNo($order);
  451. if (!$order_info) {
  452. return response()->error('QAPP_SYS_ERROR');
  453. break;
  454. }
  455. if (isset($order_info->status) && $order_info->status == 'PAID') {
  456. $data['balance'] = 0;
  457. $user = UserService::getById($uid);
  458. if ($user) {
  459. $data['balance'] = $user->balance;
  460. }
  461. return response()->success($data);
  462. }
  463. sleep(1);
  464. $i++;
  465. }
  466. return response()->error('QAPP_SYS_ERROR');
  467. }
  468. /**
  469. * 官方微信回调
  470. * @param Request $request
  471. * @return
  472. */
  473. function wxback_xcx(Request $request)
  474. {
  475. Log::info('-------------en----------------');
  476. $options = [
  477. 'app_id' => 'wxa0c8331eba3b34d5',
  478. 'payment' => [
  479. 'merchant_id' => 1501944981,
  480. 'key' => 'ee245088b93ba88008279d95f6d30413',
  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. Log::info('start_save_book_order');
  561. Log::info($insert_data);
  562. return BookOrderService::save_book_order($insert_data);
  563. }
  564. /**
  565. * 包年
  566. * @param $uid
  567. * @param $distribution_channel_id
  568. * @param $fee
  569. * @param $send_order_id
  570. * @return mixed
  571. */
  572. private function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  573. {
  574. Log::info('start_save_year_order');
  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. Log::info($insert_data);
  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. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  592. }
  593. /**
  594. * 添加位置付订单
  595. * @param $data
  596. * @return mixed
  597. */
  598. private function createUnPayOrder($data)
  599. {
  600. $data['status'] = 'UNPAID';
  601. $data['transaction_id'] = '';
  602. $data['pay_end_at'] = '0000-00-00 00:00:00';
  603. return OrderService::save_order($data);
  604. }
  605. }