OrdersController.php 30 KB

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