OrdersController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Order;
  3. use App\Consts\ErrorConst;
  4. use App\Libs\Utils;
  5. use App\Modules\Activity\Models\Activity;
  6. use App\Modules\SendOrder\Models\QappSendOrder;
  7. use App\Modules\Statistic\Services\AdVisitStatService;
  8. use App\Http\Controllers\QuickApp\BaseController;
  9. use App\Modules\User\Models\User;
  10. use Illuminate\Http\Request;
  11. use App\Modules\Subscribe\Services\BookOrderService;
  12. use App\Modules\Subscribe\Services\ChapterOrderService;
  13. use App\Modules\Subscribe\Services\OrderService;
  14. use App\Http\Controllers\QuickApp\Order\Transformers\BookOrderTransformer;
  15. use App\Http\Controllers\QuickApp\Order\Transformers\ChapterOrderTransformer;
  16. use App\Http\Controllers\QuickApp\Order\Transformers\ChargeListTransformer;
  17. use App\Libs\Pay\PayFactory;
  18. use Hashids;
  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\Subscribe\Models\Order;
  24. use App\Modules\Trade\Models\Order as TradeOrder;
  25. use App\Modules\Trade\Pay\OrderArousePayFactory;
  26. use App\Modules\Trade\Pay\OrderPaySuccess;
  27. use App\Modules\Trade\Services\PayMerchantService;
  28. use EasyWeChat\Support\XML;
  29. class OrdersController extends BaseController
  30. {
  31. /**
  32. * @apiDefine Order 订单
  33. */
  34. /**
  35. *
  36. */
  37. private $chargeList;
  38. public function exchangeList()
  39. {
  40. foreach ($this->chargeList as &$item) {
  41. if (!$item->switch_to) continue;
  42. $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('product_id', $item->id)->first();
  43. if ($order) {
  44. $change = ProductService::getProductSingle($item->switch_to);
  45. $item->id = $change->id;
  46. $item->price = $change->price;
  47. $item->given = $change->given;
  48. }
  49. }
  50. }
  51. /**
  52. * @apiVersion 1.0.0
  53. * @apiDescription 充值列表
  54. * @api {get} order/chargeList 充值列表
  55. * @apiHeader {String} [Authorization] token
  56. * @apiGroup Order
  57. * @apiName chargeList
  58. * @apiSuccess {int} code 状态码
  59. * @apiSuccess {String} msg 信息
  60. * @apiSuccess {object} data 结果集
  61. * @apiSuccessExample {json} Success-Response:
  62. * HTTP/1.1 200 OK
  63. * {
  64. * code: 0,
  65. * msg: "",
  66. * data: [
  67. * {
  68. * product_id: 1,
  69. * price: "30.00元",
  70. * vip: 0,
  71. * intro: [
  72. * {
  73. * label: 3000,
  74. * important: false
  75. * },
  76. * {
  77. * label: "书币",
  78. * important: true
  79. * }
  80. * ]
  81. * },
  82. * {
  83. * product_id: 2,
  84. * price: "50.00元",
  85. * vip: 1,
  86. * intro: [
  87. * {
  88. * label: 5000,
  89. * important: false
  90. * },
  91. * {
  92. * label: "1000+",
  93. * important: true
  94. * },
  95. * {
  96. * label: "书币",
  97. * important: false
  98. * }
  99. * ]
  100. * },
  101. * {
  102. * product_id: 5,
  103. * price: "365.00元",
  104. * vip: 0,
  105. * intro: [
  106. * {
  107. * label: "年费VIP会员",
  108. * important: true
  109. * }
  110. * ]
  111. * }
  112. * ]
  113. * }
  114. */
  115. public function chargeList(Request $request)
  116. {
  117. $bid = $request->input('bid', '');
  118. $temp = $bid;
  119. $template_id = PayTemplateService::getPayTemplate($this->distribution_channel_id);
  120. // 获取派单id
  121. $user = User::getUser($this->uid);
  122. $sendOrderId = getProp($user, 'send_order_id');
  123. if ($sendOrderId) {
  124. // 获取快应用账号
  125. $qappSendOrder = QappSendOrder::getSendOrderById($sendOrderId);
  126. $account = getProp($qappSendOrder, 'account');
  127. // 方夏青的派单使用,男频充值模板
  128. if ($account === 'fangxq') {
  129. $template_id = 10666;
  130. }
  131. // 蒋佳芯
  132. if (in_array($account, ['jiangjx', 'yangzh'])) {
  133. $template_id = 10679;
  134. }
  135. }
  136. $book_config = null;
  137. if ($bid) {
  138. $bid = Hashids::decode($bid)[0];
  139. $book_config = BookConfigService::getBookById($bid);
  140. }
  141. if ($template_id == 2) { //模板2只有在长篇小说过来的用户才显示
  142. //部分渠道需要2元模板不管哪个入口进来都展示
  143. $exclude_channels = explode(',', env('PRICE_TWO_SHOW_ALL_CHANNEL'));
  144. if (!in_array($this->distribution_channel_id, $exclude_channels)) {
  145. if ($book_config) {
  146. if ($book_config->charge_type == 'BOOK') $template_id = 1;
  147. }
  148. }
  149. }
  150. $res = ProductService::getChargeProduct($template_id);
  151. if (!$res) {
  152. return response()->error('WAP_SYS_ERROR');
  153. }
  154. $this->chargeList = $res;
  155. $this->exchangeList();
  156. //TODO 长篇小数才有模板2
  157. $uid = $this->uid;
  158. $is_first_recharge = OrderService::judgeUserFirstRecharge($uid);
  159. $data = [];
  160. $appad = 0;
  161. foreach ($res as $v) {
  162. if ($template_id == 7 && $book_config && $book_config->charge_type == 'BOOK' && $v->price == 2) {
  163. continue;
  164. }
  165. if ($template_id == 2 && $v->type == 'NEW_USER' && !$bid) {
  166. //2元模版,直接进充值,不出现
  167. continue;
  168. }
  169. if ($v->type == 'NEW_USER' && $is_first_recharge) {
  170. if (
  171. env('NO_NEW_USER_CHARGE') &&
  172. in_array(
  173. $this->distribution_channel_id,
  174. explode(',', env('NO_NEW_USER_CHARGE'))
  175. )
  176. ) {
  177. continue;
  178. }
  179. $temp = [
  180. 'price' => (float)$v->price . '元',
  181. 'is_year_order' => 0,
  182. 'is_month_order' => 0,
  183. 'text' => sprintf('%s+%s书币', $v->price * 100, $v->given),
  184. 'today_special' => $v->is_default,
  185. 'first_charge' => true,
  186. 'save_text' => round($v->given / 100, 1) . '元',
  187. 'product_id' => $v->id,
  188. ];
  189. $data[] = $temp;
  190. } elseif ($v->type == 'YEAR_ORDER') {
  191. if ($v->type == 'NEW_USER') {
  192. continue;
  193. }
  194. $save_text = '年费vip会员';
  195. $text = '全年免费看';
  196. $temp = [
  197. 'price' => (int)$v->price . '元',
  198. 'is_year_order' => 1,
  199. 'is_month_order' => 0,
  200. 'text' => $text,
  201. 'today_special' => $v->is_default,
  202. 'first_charge' => false,
  203. 'save_text' => $save_text,
  204. 'product_id' => $v->id,
  205. ];
  206. $data[] = $temp;
  207. } else {
  208. if ($v->type == 'NEW_USER') {
  209. continue;
  210. }
  211. $save_text = '';
  212. if ($v->given) {
  213. $save_text = round($v->given / 100, 1) . '元';
  214. $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
  215. } else {
  216. $text = sprintf('%s书币', $v->price * 100);
  217. }
  218. $temp = [
  219. 'price' => (float)$v->price . '元',
  220. 'is_year_order' => 0,
  221. 'is_month_order' => 0,
  222. 'text' => $text,
  223. 'today_special' => $v->is_default,
  224. 'first_charge' => false,
  225. 'save_text' => $save_text,
  226. 'product_id' => $v->id,
  227. ];
  228. $data[] = $temp;
  229. }
  230. }
  231. return response()->success($data);
  232. }
  233. /**
  234. * @apiVersion 1.0.0
  235. * @apiDescription 单本消费记录
  236. * @api {get} order/bookOrderList 单本消费记录
  237. * @apiHeader {String} [Authorization] token
  238. * @apiGroup Order
  239. * @apiName bookOrderList
  240. * @apiParam {String} [page_size] 分页大小
  241. * @apiParam {String} [page] 页码
  242. * @apiSuccess {int} code 状态码
  243. * @apiSuccess {String} msg 信息
  244. * @apiSuccess {object} data 结果集
  245. * @apiSuccess {Int} uid uid
  246. * @apiSuccess {Int} bid bid
  247. * @apiSuccess {Int} book_name 书名
  248. * @apiSuccess {Int} fee 钱
  249. * @apiSuccess {String} created_at 时间
  250. * @apiSuccessExample {json} Success-Response:
  251. * HTTP/1.1 200 OK
  252. * {
  253. * code: 0,
  254. * msg: "",
  255. * data: list:[
  256. * {
  257. * uid: 4,
  258. * bid: 1,
  259. * book_name: "dfsedfertrwet",
  260. * fee: 100,
  261. * created_at: "2017-12-02 16:24:54"
  262. * }
  263. * ]
  264. * meta: {
  265. * total: 1,
  266. * per_page: 15,
  267. * current_page: 1,
  268. * last_page: 1,
  269. * next_page_url: "",
  270. * prev_page_url: ""
  271. * }
  272. * }
  273. * }
  274. */
  275. public function bookOrderList(Request $request)
  276. {
  277. $page_size = $request->input('page_size', 15);
  278. $book_order = BookOrderService::getRecord($this->uid, $page_size);
  279. return response()->pagination(new BookOrderTransformer(), $book_order);
  280. }
  281. /**
  282. * @apiVersion 1.0.0
  283. * @apiDescription 章节消费记录
  284. * @api {get} order/chapterOrderList 章节消费记录
  285. * @apiHeader {String} [Authorization] token
  286. * @apiGroup Order
  287. * @apiName chapterOrderList
  288. * @apiParam {String} [page_size] 分页大小
  289. * @apiParam {String} [page] 页码
  290. * @apiSuccess {int} code 状态码
  291. * @apiSuccess {String} msg 信息
  292. * @apiSuccess {object} data 结果集
  293. * @apiSuccess {Int} uid uid
  294. * @apiSuccess {Int} bid bid
  295. * @apiSuccess {Int} cid cid
  296. * @apiSuccess {Int} chapter_name 章节名
  297. * @apiSuccess {Int} book_name 书名
  298. * @apiSuccess {Int} fee 钱
  299. * @apiSuccess {String} created_at 时间
  300. * @apiSuccessExample {json} Success-Response:
  301. * HTTP/1.1 200 OK
  302. * {
  303. * code: 0,
  304. * msg: "",
  305. * data: list:[
  306. * {
  307. * uid: 4,
  308. * bid: 1,
  309. * cid: 1,
  310. * chapter_name: "sdfsd",
  311. * book_name: "dfsedfertrwet",
  312. * fee: 100,
  313. * created_at: "2017-12-02 16:24:54"
  314. * }
  315. * ]
  316. * meta: {
  317. * total: 1,
  318. * per_page: 15,
  319. * current_page: 1,
  320. * last_page: 1,
  321. * next_page_url: "",
  322. * prev_page_url: ""
  323. * }
  324. * }
  325. */
  326. public function chapterOrderList(Request $request)
  327. {
  328. $chapter_model = new ChapterOrderService();
  329. $page_size = $request->input('page_size', 15);
  330. $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
  331. foreach ($chapter_order as $item) {
  332. if ($item->fee == 0) {
  333. $result = AdVisitStatService::getInfoV2($this->uid, $item->cid, ['UNLOCK', 'UNLOCK_2']);
  334. $item->fee = '解锁';
  335. }
  336. }
  337. return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
  338. }
  339. /**
  340. * @apiVersion 1.0.0
  341. * @apiDescription 充值记录
  342. * @api {get} order/chargeRecordLists 充值记录
  343. * @apiParam {String} [token] token
  344. * @apiHeader {String} [Authorization] token 两个token任选其一
  345. * @apiGroup Order
  346. * @apiName chargeRecordLists
  347. * @apiParam {String} [page_size] 分页大小
  348. * @apiParam {String} [page] 页码
  349. * @apiSuccess {int} code 状态码
  350. * @apiSuccess {String} msg 信息
  351. * @apiSuccess {object} data 结果集
  352. * @apiSuccess {String} data.price 价格
  353. * @apiSuccess {String} data.status 状态
  354. * @apiSuccess {String} data.trade_no 订单号
  355. * @apiSuccess {String} data.created_at 时间
  356. * @apiSuccessExample {json} Success-Response:
  357. * HTTP/1.1 200 OK
  358. * {
  359. * code: 0,
  360. * msg: "",
  361. * data: {
  362. * list: [
  363. * {
  364. * id: 134,
  365. * price: "1.00",
  366. * status: "PAID",
  367. * trade_no: "201712021915481585670623626232",
  368. * created_at: "2017-12-02 19:15:56"
  369. * }
  370. * ],
  371. * meta: {
  372. * total: 1,
  373. * per_page: 15,
  374. * current_page: 1,
  375. * last_page: 1,
  376. * next_page_url: "",
  377. * prev_page_url: ""
  378. * }
  379. * }
  380. * }
  381. */
  382. public function chargeRecordLists(Request $request)
  383. {
  384. $page_size = $request->input('page_size', 15);
  385. $res = OrderService::getSuccessOrderList($this->uid, $page_size);
  386. return response()->pagination(new ChargeListTransformer(), $res);
  387. }
  388. private function getPayParams(Request $request)
  389. {
  390. $uid = $this->uid;
  391. $product_id = $request->get('product_id', 0);
  392. $send_order_id = $this->send_order_id;
  393. if (!$product_id) {
  394. return false;
  395. }
  396. $bid = $request->get('bid', 0);
  397. if ($bid) {
  398. $from_bid = BookService::decodeBidStatic($bid);
  399. }
  400. $trade_no = date("YmdHis") . hexdec(uniqid());
  401. myLog('pay-order')->info('request', [
  402. 'params' => $request->all(),
  403. 'uid' => $uid,
  404. 'bid' => $bid,
  405. 'trade_no' => $trade_no
  406. ]);
  407. $product_info = ProductService::getProductSingle($product_id);
  408. if (!getProp($product_info, 'id')) {
  409. return false;
  410. }
  411. $distribution_channel_id = $this->distribution_channel_id;
  412. $price = $product_info->price * 100;
  413. if (in_array($uid, explode(',', env('TEST_UID')))) {
  414. $price = 1;
  415. }
  416. $from_type = 'QuickApp';
  417. if ($product_info->type == 'YEAR_ORDER') {
  418. $order_type = 'YEAR';
  419. } elseif ($product_info->type == 'QUARTER') {
  420. $order_type = 'QUARTER';
  421. } elseif ($product_info->type == 'BOOK_ORDER') {
  422. $order_type = 'BOOK';
  423. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  424. $order_type = 'RECHARGE';
  425. } else {
  426. $order_type = '';
  427. }
  428. $create_ip = _getIp();
  429. // 活动
  430. $activity_token = $request->get('activity_token', '');
  431. $activity_id = 0;
  432. if ($activity_token) {
  433. $activity = Activity::getActivityBuToken($activity_token);
  434. $activity_id = (int)getProp($activity, 'id');
  435. // 校验活动次数
  436. $settingJson = getProp($activity, 'setting');
  437. if ($settingJson) {
  438. $setting = json_decode($settingJson, true);
  439. $productInfos = getProp($setting, 'product_info', []);
  440. $productInfo = collect($productInfos)->firstWhere('product_id', $product_id);
  441. $limit = (int)getProp($productInfo, 'limit');
  442. if ($limit > 0) {
  443. // 查询用户通过该活动已经充值的次数
  444. $paidNum = TradeOrder::getActivityOrderNum([
  445. 'uid' => $uid,
  446. 'begin_time' => getProp($activity, 'start_time'),
  447. 'end_time' => getProp($activity, 'end_time'),
  448. 'status' => 'PAID',
  449. 'product_id' => $product_id,
  450. 'activity_id' => $activity_id
  451. ]);
  452. myLog('charge')->info('', compact('uid', 'product_id', 'activity', 'paidNum'));
  453. // 活动充值次数限制
  454. if ($paidNum >= $limit) {
  455. Utils::throwError(ErrorConst::ACTIVITY_CHARGE_OUT_OF_LIMIT);
  456. }
  457. }
  458. }
  459. }
  460. // 包名
  461. $package = $request->header('x-package', '');
  462. return compact(
  463. 'distribution_channel_id',
  464. 'uid',
  465. 'product_id',
  466. 'price',
  467. 'trade_no',
  468. 'create_ip',
  469. 'send_order_id',
  470. 'from_bid',
  471. 'from_type',
  472. 'order_type',
  473. 'activity_id',
  474. 'package'
  475. );
  476. }
  477. /**
  478. * @apiVersion 1.0.0
  479. * @apiDescription 支付
  480. * @api {get} goToPay 微信APP支付
  481. * @apiGroup pay
  482. * @apiName wxindex
  483. * @apiParam {Int} product_id product_id
  484. * @apiParam {Int} send_order_id send_order_id
  485. * @apiParam {String} bid bid
  486. * @apiHeader {String} [Authorization] token
  487. * @apiSuccess {int} code 状态码
  488. * @apiSuccess {String} msg 信息
  489. * @apiSuccess {Object} data 信息
  490. * @apiSuccess {Object} data.trade_no 订单号
  491. * @apiSuccess {Object} data.appId 唤起支付的appId
  492. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  493. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  494. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  495. * @apiSuccess {Object} data.sign 唤起支付的sign
  496. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  497. * @apiSuccessExample {json} Success-Response:
  498. * HTTP/1.1 200 OK
  499. * {
  500. * code: 0,
  501. * msg: "",
  502. * data: {
  503. *
  504. * }
  505. */
  506. function wxIndex(Request $request)
  507. {
  508. if ($params = $this->getPayParams($request)) {
  509. $params['pay_merchant_id'] = $this->app_pay_merchat_id;
  510. } else {
  511. return response()->error('QAPP_PARAM_ERROR');
  512. }
  513. $app = OrderArousePayFactory::wx($this->uid);
  514. // 微信支付参数
  515. $params['trade_type'] = 'APP'; //交易类型
  516. $result = $app->handle($params);
  517. myLog('wxPay')->info('wxIndex', compact('params', 'result'));
  518. if ($result) {
  519. $result['trade_no'] = $params['trade_no'];
  520. return response()->success($result);
  521. } else {
  522. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  523. }
  524. }
  525. /**
  526. * @apiVersion 1.0.0
  527. * @apiDescription 微信H5支付
  528. * @api {get} goToH5Pay 微信H5支付
  529. * @apiGroup pay
  530. * @apiName wxH5Index
  531. * @apiParam {Int} product_id product_id
  532. * @apiParam {Int} send_order_id send_order_id
  533. * @apiParam {String} bid bid
  534. * @apiHeader {String} [Authorization] token
  535. * @apiSuccess {int} code 状态码
  536. * @apiSuccess {String} msg 信息
  537. * @apiSuccess {Object} data 信息
  538. * @apiSuccess {Object} data.trade_no 订单号
  539. * @apiSuccess {Object} data.appId 唤起支付的appId
  540. * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
  541. * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
  542. * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
  543. * @apiSuccess {Object} data.sign 唤起支付的sign
  544. * @apiSuccess {Object} data.trade_type 唤起支付trade_type
  545. * @apiSuccess {Object} data.mweb_url 唤起支付mweb_url
  546. * @apiSuccessExample {json} Success-Response:
  547. * HTTP/1.1 200 OK
  548. * {
  549. * code: 0,
  550. * msg: "",
  551. * data: {
  552. *
  553. * }
  554. */
  555. function wxH5Index(Request $request)
  556. {
  557. if ($params = $this->getPayParams($request)) {
  558. $params['pay_merchant_id'] = $this->h5_pay_merchat_id;
  559. } else {
  560. return response()->error('QAPP_PARAM_ERROR');
  561. }
  562. $app = OrderArousePayFactory::wx($this->uid);
  563. // 微信支付参数
  564. $params['trade_type'] = 'MWEB'; //交易类型
  565. $result = $app->handle($params);
  566. myLog('wxPay')->info('wxH5Index', compact('params', 'result'));
  567. if ($result) {
  568. $result['trade_no'] = $params['trade_no'];
  569. return response()->success($result);
  570. } else {
  571. return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
  572. }
  573. }
  574. /**
  575. * @apiVersion 1.0.0
  576. * @apiDescription 支付宝APP支付
  577. * @api {get} goToAliPay 支付宝APP支付
  578. * @apiGroup pay
  579. * @apiName aliIndex
  580. * @apiParam {Int} product_id product_id
  581. * @apiParam {Int} send_order_id send_order_id
  582. * @apiParam {String} bid bid
  583. * @apiHeader {String} [Authorization] token
  584. * @apiSuccess {Object} data.order_info 唤起支付信息str
  585. * @apiSuccess {Object} data.trade_no 订单号
  586. * @apiSuccessExample {json} Success-Response:
  587. * HTTP/1.1 200 OK
  588. * {
  589. * code: 0,
  590. * msg: "",
  591. * data:""
  592. *
  593. */
  594. public function aliIndex(Request $request)
  595. {
  596. myLog('aliPay')->info('aliIndex-request', [$request->all()]);
  597. if ($params = $this->getPayParams($request)) {
  598. myLog('aliPay')->info('aliIndex-request1', compact('params'));
  599. $params['pay_merchant_id'] = $this->ali_pay_merchat_id;
  600. $params['type'] = 'App';
  601. myLog('aliPay')->info('aliIndex-request2', compact('params'));
  602. } else {
  603. return response()->error('QAPP_PARAM_ERROR');
  604. }
  605. $app = OrderArousePayFactory::ali($this->uid);
  606. $order_info = $app->handle($params);
  607. myLog('aliPay')->info('aliIndex', compact('params', 'order_info'));
  608. return response()->success(['trade_no' => $params['trade_no'], 'order_info' => $order_info]);
  609. }
  610. /**
  611. * @apiVersion 1.0.0
  612. * @apiDescription 订单查询
  613. * @api {get} checkOrder 订单查询
  614. * @apiGroup pay
  615. * @apiName checkOrder
  616. * @apiParam {String} [token] token
  617. * @apiHeader {String} [Authorization] token 两个token任选其一
  618. * @apiParam {String} order order
  619. * @apiSuccess {int} code 状态码
  620. * @apiSuccess {String} msg 信息
  621. * @apiSuccess {Object} data 信息
  622. * @apiSuccessExample {json} Success-Response:
  623. * HTTP/1.1 200 OK
  624. * {
  625. * code: 0,
  626. * msg: "",
  627. * data: {
  628. *
  629. * }
  630. */
  631. public function checkOrder(Request $request)
  632. {
  633. $order = $request->input('order');
  634. $order_info = OrderService::getByTradeNo($order);
  635. if ($order_info && $order_info->status == 'PAID') {
  636. return response()->success();
  637. }
  638. return response()->success($order);
  639. }
  640. /**
  641. * 官方微信回调
  642. */
  643. function wxback(Request $request)
  644. {
  645. $xml = XML::parse(strval($request->getContent()));
  646. myLog('wxpay')->info($xml);
  647. if (isset($xml['attach'])) {
  648. $pay_merchant_id = (int)$xml['attach'];
  649. $config = PayMerchantService::findPayConfig($pay_merchant_id);
  650. $app = PayFactory::official($config);
  651. $response = $app->notify()->handleNotify(function ($notify, $successful) {
  652. if (!$successful) {
  653. return 'fail';
  654. }
  655. if (OrderPaySuccess::handle($notify->out_trade_no, $notify->transaction_id)) {
  656. return true;
  657. } else {
  658. return 'fail';
  659. }
  660. });
  661. return $response;
  662. } else {
  663. return 'fail';
  664. }
  665. }
  666. /**
  667. * 支付宝支付回调
  668. */
  669. function aliback(Request $request)
  670. {
  671. $param = $request->except('_url');
  672. myLog('alipay')->info($param);
  673. $trade_no = isset($param['out_trade_no']) ? $param['out_trade_no'] : '';
  674. if ($trade_no) {
  675. $order = Order::where('trade_no', $trade_no)->first();
  676. $pay_merchant_id = $order ? $order->pay_merchant_id : 0;
  677. $pay_merchant_id = $pay_merchant_id ? $pay_merchant_id : 140;
  678. $config = PayMerchantService::findAliPayConfig($pay_merchant_id);
  679. $app = PayFactory::aliPay($config);
  680. if ($app->notify($param)) {
  681. if (OrderPaySuccess::handle($param['out_trade_no'], $param['trade_no'])) {
  682. return response('success');
  683. } else {
  684. return response('fail');
  685. }
  686. }
  687. }
  688. myLog('alipay')->info('sign fail');
  689. return response('fail');
  690. }
  691. function wait(Request $request)
  692. {
  693. $param = $request->except('_url');
  694. return view('pay.middleware')->with($param);
  695. }
  696. }