OrdersController.php 25 KB

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