OrdersController.php 27 KB

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