OrdersController.php 21 KB

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