Api.php 17 KB

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