OrdersController.php 29 KB

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