OrdersController.php 27 KB

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