OrdersController.php 25 KB

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