Api.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace addons\epay\controller;
  3. use addons\epay\library\QRCode;
  4. use addons\epay\library\Service;
  5. use addons\epay\library\Wechat;
  6. use addons\third\model\Third;
  7. use app\api\model\Delivery;
  8. use app\api\model\DeliveryTrade;
  9. use app\common\library\Auth;
  10. use think\addons\Controller;
  11. use think\Response;
  12. use think\Session;
  13. use Yansongda\Pay\Exceptions\GatewayException;
  14. use Yansongda\Pay\Pay;
  15. use app\api\model\Banner;
  16. use app\api\model\Category;
  17. use app\api\model\CoinRecord;
  18. use app\api\model\Detail;
  19. use app\api\model\Goods;
  20. use app\api\model\MoneyRecord;
  21. use app\api\model\Order;
  22. use app\api\model\PriceRange;
  23. use app\api\model\Prizerecord;
  24. use app\api\model\RechargeList;
  25. use app\api\model\RechargeOrder;
  26. use app\api\model\SearchHistory;
  27. use app\api\model\Setting;
  28. use app\api\model\Box;
  29. use app\api\model\Star;
  30. use app\api\model\Text;
  31. //use app\common\controller\Api;
  32. use think\Db;
  33. use think\db\exception\DataNotFoundException;
  34. use think\db\exception\ModelNotFoundException;
  35. use think\Exception;
  36. use think\exception\DbException;
  37. use app\api\library\Retail;
  38. /**
  39. * API接口控制器
  40. *
  41. * @package addons\epay\controller
  42. */
  43. class Api extends Controller
  44. {
  45. protected $layout = 'default';
  46. protected $config = [];
  47. /**
  48. * 默认方法
  49. */
  50. public function index()
  51. {
  52. return;
  53. }
  54. /**
  55. * 外部提交
  56. */
  57. public function submit()
  58. {
  59. $this->request->filter('trim');
  60. $out_trade_no = $this->request->request("out_trade_no");
  61. $title = $this->request->request("title");
  62. $amount = $this->request->request('amount');
  63. $type = $this->request->request('type');
  64. $method = $this->request->request('method', 'web');
  65. $openid = $this->request->request('openid', '');
  66. $auth_code = $this->request->request('auth_code', '');
  67. $notifyurl = $this->request->request('notifyurl', '');
  68. $returnurl = $this->request->request('returnurl', '');
  69. if (!$amount || $amount < 0) {
  70. $this->error("支付金额必须大于0");
  71. }
  72. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  73. $this->error("支付类型错误");
  74. }
  75. $params = [
  76. 'type' => $type,
  77. 'out_trade_no' => $out_trade_no,
  78. 'title' => $title,
  79. 'amount' => $amount,
  80. 'method' => $method,
  81. 'openid' => $openid,
  82. 'auth_code' => $auth_code,
  83. 'notifyurl' => $notifyurl,
  84. 'returnurl' => $returnurl,
  85. ];
  86. return Service::submitOrder($params);
  87. }
  88. /**
  89. * 微信支付(公众号支付&PC扫码支付)
  90. * @return string
  91. */
  92. public function wechat()
  93. {
  94. $config = Service::getConfig('wechat');
  95. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  96. $isMobile = $this->request->isMobile();
  97. $this->view->assign("isWechat", $isWechat);
  98. $this->view->assign("isMobile", $isMobile);
  99. //发起PC支付(Scan支付)(PC扫码模式)
  100. if ($this->request->isAjax()) {
  101. $pay = Pay::wechat($config);
  102. $orderid = $this->request->post("orderid");
  103. try {
  104. $result = $pay->find($orderid);
  105. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  106. $this->success("", "", ['status' => $result['trade_state']]);
  107. } else {
  108. $this->error("查询失败");
  109. }
  110. } catch (GatewayException $e) {
  111. $this->error("查询失败");
  112. }
  113. }
  114. $orderData = Session::get("wechatorderdata");
  115. if (!$orderData) {
  116. $this->error("请求参数错误");
  117. }
  118. if ($isWechat) {
  119. //发起公众号(jsapi支付),openid必须
  120. //如果没有openid,则自动去获取openid
  121. if (!isset($orderData['openid']) || !$orderData['openid']) {
  122. $orderData['openid'] = Service::getOpenid();
  123. }
  124. $orderData['method'] = 'mp';
  125. $type = 'jsapi';
  126. $payData = Service::submitOrder($orderData);
  127. if (!isset($payData['paySign'])) {
  128. $this->error("创建订单失败,请返回重试", "");
  129. }
  130. } else {
  131. $orderData['method'] = 'scan';
  132. $type = 'pc';
  133. $payData = Service::submitOrder($orderData);
  134. if (!isset($payData['code_url'])) {
  135. $this->error("创建订单失败,请返回重试", "");
  136. }
  137. }
  138. $this->view->assign("orderData", $orderData);
  139. $this->view->assign("payData", $payData);
  140. $this->view->assign("type", $type);
  141. $this->view->assign("title", "微信支付");
  142. return $this->view->fetch();
  143. }
  144. /**
  145. * 支付宝支付(PC扫码支付)
  146. * @return string
  147. */
  148. public function alipay()
  149. {
  150. $config = Service::getConfig('alipay');
  151. $isWechat = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
  152. $isMobile = $this->request->isMobile();
  153. $this->view->assign("isWechat", $isWechat);
  154. $this->view->assign("isMobile", $isMobile);
  155. if ($this->request->isAjax()) {
  156. $orderid = $this->request->post("orderid");
  157. $pay = Pay::alipay($config);
  158. try {
  159. $result = $pay->find($orderid);
  160. if ($result['code'] == '10000' && $result['trade_status'] == 'TRADE_SUCCESS') {
  161. $this->success("", "", ['status' => $result['trade_status']]);
  162. } else {
  163. $this->error("查询失败");
  164. }
  165. } catch (GatewayException $e) {
  166. $this->error("查询失败");
  167. }
  168. }
  169. //发起PC支付(Scan支付)(PC扫码模式)
  170. $orderData = Session::get("alipayorderdata");
  171. if (!$orderData) {
  172. $this->error("请求参数错误");
  173. }
  174. $orderData['method'] = 'scan';
  175. $payData = Service::submitOrder($orderData);
  176. if (!isset($payData['qr_code'])) {
  177. $this->error("创建订单失败,请返回重试");
  178. }
  179. $type = 'pc';
  180. $this->view->assign("orderData", $orderData);
  181. $this->view->assign("payData", $payData);
  182. $this->view->assign("type", $type);
  183. $this->view->assign("title", "支付宝支付");
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 支付成功回调
  188. */
  189. public function notifyx()
  190. {
  191. $type = $this->request->param('type');
  192. if (!Service::checkNotify($type)) {
  193. echo '签名错误';
  194. return;
  195. }
  196. //你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
  197. //下面这句必须要执行,且在此之前不能有任何输出
  198. echo "success";
  199. return;
  200. }
  201. /**
  202. * 支付成功返回
  203. */
  204. public function returnx()
  205. {
  206. $type = $this->request->param('type');
  207. if (Service::checkReturn($type)) {
  208. echo '签名错误';
  209. return;
  210. }
  211. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  212. $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  213. return;
  214. }
  215. // 支付盲盒订单的异步
  216. function notifyx212313()
  217. {
  218. $res = $this->request->param();
  219. // // $order_id=$this->request->param('out_trade_no');
  220. // // file_put_contents('./12347.txt',json_encode($data));
  221. // $data = '{"pid":"1019","trade_no":"Y2022012921422516786","out_trade_no":"202201292233419043907","type":"wxpay","name":"# 202201292142221315781 \u5728\u7ebf\u652f\u4ed8","money":"25.00","trade_status":"TRADE_SUCCESS","sign":"1a1a9836c134ad3234b81e67ab3d7a54","sign_type":"MD5","addon":"epay","controller":"api","action":"notifyx212313"}';
  222. // $res = json_decode($data,true);
  223. $order = Db::table('box_order')->where('out_trade_no', $res['out_trade_no'])->find();
  224. // print_r($order);die;
  225. // Db::table('box_order') ->where('out_trade_no', $res['out_trade_no'])->setField('status','unused');
  226. if ($order && $order['status'] == 'unpay') {
  227. if ($res['trade_status'] == 'TRADE_SUCCESS') {
  228. Db::table('box_order')->where('out_trade_no', $res['out_trade_no'])->setField('status', 'unused');
  229. $user = Db::table('box_user')->where('id', $order['user_id'])->find();
  230. $users = Db::table('box_user')->where('id', $user['pid'])->find();
  231. $lou = Db::table('box_setting')->where('id', 1)->find();
  232. $kou = $lou['kou'];
  233. if ($users['recharnum'] == 1) {
  234. Db::table('box_user')->where('id', $users['id'])->setInc("recharnum", 1);
  235. Retail::giveMoneys($order);
  236. } else {
  237. Db::table('box_user')->where('id', $users['id'])->setInc("recharnum", 1);
  238. $userarr = Db::table('box_user')->where('id', $users['id'])->find();
  239. if (bcmod($userarr['recharnum'], $kou) == 0) {
  240. } else {
  241. Retail::giveMoneys($order);
  242. };
  243. }
  244. echo "success";
  245. }
  246. }
  247. }
  248. /**
  249. * 支付成功回调
  250. */
  251. public function notifyx2()
  252. {
  253. /* $type = $this->request->param('type');
  254. if (!Service::checkNotify($type)) {
  255. echo '签名错误';
  256. return;
  257. }
  258. */
  259. $pay = $this->request->param();
  260. // file_put_contents('./5.txt', json_encode($pay));
  261. $p = $this->request->param();
  262. $order_id = $this->request->param();
  263. $xml = simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA);
  264. foreach ($xml as $k => $v) {
  265. $data[(string) $k] = (string) $v;
  266. }
  267. // print_r($order_id);
  268. $u = Db::table('box_recharge_order')->where('out_trade_no', $data['out_trade_no'])->find();
  269. // print_r($u);
  270. $order = Db::table('box_recharge_order')->where('out_trade_no', $data['out_trade_no'])->find();
  271. // file_put_contents('./5.txt', json_encode($order));
  272. if ($order && $order['status'] == 'unpay') {
  273. if ($data['return_code'] == 'SUCCESS') {
  274. // echo 'zfcg';
  275. $user = Db::table('box_user')->where('id', $order['user_id'])->find();
  276. if ($user) {
  277. $lou = Db::table('box_setting')->where('id', 1)->find();
  278. // print_r($user);
  279. $yq = $user['coin'];
  280. $jq = $order['coin_amount'] * $lou['one_rmb_to_coin_num'];
  281. $hq = $yq + $jq;
  282. //加钱
  283. Db::table('box_user')->where(['id' => $user['id']])->setField('coin', $hq);
  284. Db::table('box_recharge_order')->where(['id' => $order['id']])->setField('status', 'paid');
  285. //加记录
  286. Db::table('box_coin_record')->insertGetId([
  287. 'user_id' => $user['id'],
  288. 'coin' => $jq,
  289. 'before' => $yq,
  290. 'after' => $hq,
  291. 'type' => 'recharge',
  292. 'create_time' => time()
  293. ]);
  294. Db::table('box_user_score_log')->insertGetId([
  295. 'user_id' => $user['id'],
  296. 'score' => $jq,
  297. 'before' => $yq,
  298. 'after' => $hq,
  299. 'memo' => '前台充值',
  300. 'createtime' => time()
  301. ]);
  302. // $kou = $lou['kou'];
  303. // //加已充值成功次数
  304. // Db::table('box_user') ->where('id', $order['user_id'])->setInc("recharnum", 1);
  305. // $users= Db::table('box_user') ->where('id', $order['user_id'])->find();
  306. // if($users['recharnum']<=$kou){
  307. // Retail::giveMoney($order);
  308. // }
  309. // 发放分销佣金
  310. echo "success";
  311. } else {
  312. echo 'fail';
  313. }
  314. } else {
  315. echo 'fail';
  316. }
  317. } else {
  318. echo 'fail';
  319. }
  320. // print_r($p);
  321. //你可以在这里你的业务处理逻辑,比如处理你的订单状态、给会员加余额等等功能
  322. //下面这句必须要执行,且在此之前不能有任何输出
  323. return;
  324. }
  325. public function notifyx22()
  326. {
  327. file_put_contents('./xu.txt', json_encode($this->request->param()));
  328. $p = $this->request->param('trade_status');
  329. $order_id = $this->request->param('out_trade_no');
  330. $order = Db::table('box_delivery_trade')->where('out_trade_no', $order_id)->find();
  331. if ($order && $order['status'] == 'unpay') {
  332. if ($p == 'TRADE_SUCCESS') {
  333. $user = Db::table('box_user')->where('id', $order['user_id'])->find();
  334. if ($user) {
  335. $yq = $user['coin'];
  336. $jq = $order['coin_amount'];
  337. $hq = $yq + $jq;
  338. // 先将支付订单变更为 已支付
  339. Db::table('box_delivery_trade')->where(['id' => $order['id']])->setField('status', 'paid');
  340. Db::table('box_user_score_log')->insertGetId([
  341. 'user_id' => $user['id'],
  342. 'score' => $jq,
  343. 'before' => $yq,
  344. 'after' => $hq,
  345. 'memo' => '商品发货',
  346. 'createtime' => time()
  347. ]);
  348. $resarr = Db::table('box_delivery_order')->where(['delivery_trade_id' => $order['id']])->select();
  349. Db::table('box_delivery_order')->where(['delivery_trade_id' => $order['id']])->setField('status', 'undelivered');
  350. foreach ($resarr as $v) {
  351. Db::table('box_prize_record')->where(['id' => $v['prize_id']])->setField('status', 'delivery');
  352. }
  353. // Db::table('box_prize_record')->wherein(['id'=>$resarr['prize_id']])->setField('status','delivery');
  354. echo "success";
  355. } else {
  356. echo 'fail';
  357. }
  358. } else {
  359. echo 'fail';
  360. }
  361. } else {
  362. echo 'fail';
  363. }
  364. return;
  365. }
  366. /**
  367. * 支付成功返回
  368. */
  369. public function returnx2()
  370. {
  371. // file_put_contents('./345.txt','daozhelile');
  372. $type = $this->request->param('type');
  373. $p = $this->request->param('trade_status');
  374. $order_id = $this->request->param('out_trade_no');
  375. $data = $this->request->param();
  376. file_put_contents('./123.txt', json_encode($data));
  377. /* if (Service::checkReturn($type)) {
  378. echo '签名错误';
  379. return;
  380. }
  381. */
  382. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑?type=0
  383. // $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  384. $this->success("恭喜你!支付成功!", "/h5/#?out_trade_no=$order_id&type=$type");
  385. return;
  386. }
  387. public function returnx2222222()
  388. {
  389. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑?type=0
  390. // $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  391. $this->success("恭喜你!支付成功!", '/h5/#/');
  392. return;
  393. }
  394. public function returnx22()
  395. {
  396. $type = $this->request->param('type');
  397. /* if (Service::checkReturn($type)) {
  398. echo '签名错误';
  399. return;
  400. }
  401. */
  402. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  403. // $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));
  404. $this->success("恭喜你!支付成功!", '/h5/#/pages/me/order?type=0');
  405. return;
  406. }
  407. /**
  408. * 生成二维码
  409. */
  410. public function qrcode()
  411. {
  412. $text = $this->request->get('text', 'hello world');
  413. //如果有安装二维码插件,则调用插件的生成方法
  414. if (class_exists("\addons\qrcode\library\Service") && get_addon_info('qrcode')['state']) {
  415. $qrCode = \addons\qrcode\library\Service::qrcode(['text' => $text]);
  416. $response = Response::create()->header("Content-Type", "image/png");
  417. header('Content-Type: ' . $qrCode->getContentType());
  418. $response->content($qrCode->writeString());
  419. return $response;
  420. } else {
  421. $qr = QRCode::getMinimumQRCode($text);
  422. $im = $qr->createImage(8, 5);
  423. header("Content-type: image/png");
  424. imagepng($im);
  425. imagedestroy($im);
  426. return;
  427. }
  428. }
  429. /**
  430. * 支付成功回调
  431. */
  432. public function notifyxde()
  433. {
  434. /* $type = $this->request->param('type');
  435. if (!Service::checkNotify($type)) {
  436. echo '签名错误';
  437. return;
  438. }
  439. */
  440. $xml = simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA);
  441. foreach ($xml as $k => $v) {
  442. $data[(string) $k] = (string) $v;
  443. }
  444. // print_r($order_id);
  445. $trade = DeliveryTrade::where('out_trade_no', $data['out_trade_no'])->where('status', 'unpay')->find();
  446. if ($trade) {
  447. if ($data['return_code'] == 'SUCCESS') {
  448. // 更发货交易订单状态
  449. $trade->save([
  450. 'pay_method' => 'alipay',
  451. 'pay_rmb' => $data['total_amount'],
  452. 'alipay_trade_no' => $data['trade_no'],
  453. 'pay_time' => time(),
  454. 'status' => 'paid'
  455. ]);
  456. // 变更发货订单状态
  457. $deliveryOrder = Delivery::where('delivery_trade_id', $trade->id)->select();
  458. $prizeIds = [];
  459. foreach ($deliveryOrder as $order) {
  460. $order->save(['status' => 'undelivered']);
  461. $prizeIds[] = $order->prize_id;
  462. }
  463. // 变更奖品状态
  464. Prizerecord::whereIn('id', $prizeIds)->update(['status' => 'delivery', 'delivery_time' => time()]);
  465. } else {
  466. echo 'fail';
  467. }
  468. } else {
  469. echo 'fail';
  470. }
  471. return;
  472. }
  473. public function deliverypaysuccess()
  474. {
  475. $this->success("恭喜你!支付成功!", '/h5/#/');
  476. }
  477. }