OrdersController.php 27 KB

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