Palymat.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. namespace app\index\controller;
  3. use qqpay\QQPay;
  4. use think\DB;
  5. use think\facade\Env;
  6. use think\facade\Session;
  7. use think\Request;
  8. class Palymat extends Base {
  9. protected $config;
  10. protected function initialize() {
  11. parent::initialize();
  12. $this->config = $this->getsystems();
  13. }
  14. public function topaly(Request $request) {
  15. //接收数据
  16. $data = $request->param();
  17. //获取当前客户端IP地址
  18. $data["buyip"] = $request->ip();
  19. //查询支付开关控制
  20. $status = Db::name("pay_set")->field($data["payst"] . "_close")->find(1);
  21. //查询已购买过的产品ID
  22. $member = Db::name('Member')->where('id', Session::get('Member.id'))->field('buymat')->find();
  23. //字符串转数组
  24. if (isset($member)) {
  25. $array = explode(",", $member['buymat']);
  26. }
  27. if (in_array($data["matid"], $array)) {
  28. $this->error("请勿重复购买!", 'User/my_buylog');
  29. }
  30. $tab = Db::name("model")->field("tablename")->select();
  31. foreach ($tab as $va) {
  32. $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($data["matid"]);
  33. if ($infs != null) {
  34. $info = $infs;
  35. }
  36. }
  37. //判断该支付方式是否关闭
  38. if ($status[$data["payst"] . "_close"] !== 1) {
  39. //查询易支付的相关信息
  40. $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
  41. $epay = Db::name('pay_set')->field('epay_close')->find(1);
  42. if ($epay['epay_close'] == 1 && $data['payst'] !== "moneypay") {
  43. //发起易支付
  44. if (empty($info) || empty($data)) {
  45. $this->error("支付信息缺失,终止支付!");
  46. } else {
  47. $tradeNo = trade_no();
  48. //订单描述
  49. $parameter = [
  50. "pid" => (int) $epayInfo['epay_appid'], //商户ID
  51. "type" => $data["payst"], //支付方式
  52. "notify_url" => $request->domain() . url("Palymat/epayNotify"), //异步通知地址
  53. "return_url" => $request->domain() . url("Palymat/epayReturn"), //页面跳转同步通知页面路径
  54. "out_trade_no" => $tradeNo, //商户订单号
  55. "name" => $info['title'], //商品名称
  56. "money" => $info['moneys'], //金额
  57. "sitename" => $this->config['title'], //站点名称
  58. ];
  59. //检测易支付API是否支持SSL
  60. $stream = stream_context_create(array("ssl" => array("capture_peer_cert" => true)));
  61. $read = fopen($epayInfo['epay_url'], "rb", false, $stream);
  62. $cont = stream_context_get_params($read);
  63. $result = (isset($cont["options"]["ssl"]["peer_certificate"])) ? true : false;
  64. if ($result) {
  65. $transport = "https";
  66. } else {
  67. $transport = "http";
  68. }
  69. //易支付的配置信息
  70. $epayConfig = [
  71. 'partner' => $epayInfo['epay_appid'], //商户ID
  72. 'key' => $epayInfo['epay_key'], //商户KEY
  73. 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
  74. 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
  75. 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
  76. 'apiurl' => $epayInfo['epay_url'], //支付API地址
  77. ];
  78. //建立请求
  79. require_once Env::get('extend_path') . "/epay/lib/epay_submit.class.php";
  80. $epaySubmit = new \AlipaySubmit($epayConfig);
  81. $htmlText = $epaySubmit->buildRequestForm($parameter);
  82. if ($data['payst'] == "alipay") {
  83. $payType = "2";
  84. } elseif ($data['payst'] == "wxpay") {
  85. $payType = "1";
  86. } elseif ($data['payst'] == "qqpay") {
  87. $payType = "0";
  88. }
  89. //记录订单信息
  90. $buyInfo = [
  91. 'uid' => Session::get('Member.id'),
  92. 'order_id' => $tradeNo,
  93. 'product_id' => $info['id'],
  94. 'buy_type' => "0",
  95. 'pay_type' => $payType,
  96. 'money' => $info['moneys'],
  97. 'buy_ip' => $data["buyip"],
  98. 'create_time' => time(),
  99. 'intro' => '购买内容:' . $info['title'],
  100. 'status' => "0",
  101. ];
  102. Db::name("member_buylog")->insert($buyInfo);
  103. echo $htmlText;
  104. }
  105. } else {
  106. $this->error('该支付方式已关闭!');
  107. }
  108. } else {
  109. if (empty($info) || empty($data)) {
  110. $this->error("支付信息缺失,终止支付!");
  111. } else {
  112. if ($data["payst"] == "alipay") {
  113. $swich = Db::name("pay_set")->field("alipay")->find(1);
  114. //查询当面付配置信息
  115. $setalipay = Db::name("pay")->find(1);
  116. //等于1为官方支付宝,0为当面支付
  117. if ($swich['alipay'] == 0) {
  118. $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams();
  119. $params->sign_type = "RSA2";
  120. $params->charset = "utf-8";
  121. $params->version = "1.0";
  122. //订单号
  123. $tradeNo = trade_no();
  124. //APP ID
  125. $params->appID = $setalipay["alipayf2f_private_id"];
  126. //当面付私钥
  127. $params->appPrivateKey = $setalipay["alipayf2f_private_key"];
  128. //支付宝公钥
  129. $params->appPublicKey = $setalipay["alipayf2f_public_key"];
  130. //实例化,传入配置信息
  131. $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
  132. $requests = new \Yurun\PaySDK\AlipayApp\FTF\Params\QR\Request();
  133. // 商户订单号
  134. $requests->businessParams->out_trade_no = $tradeNo;
  135. // 价格
  136. $requests->businessParams->total_amount = $info['moneys'];
  137. // 产品标题
  138. $requests->businessParams->subject = $info['title'];
  139. //最晚付款时间,10分钟
  140. $requests->businessParams->timeout_express = "10m";
  141. try {
  142. //调用接口
  143. $payInfo = $pay->execute($requests);
  144. if ($pay->checkResult()) {
  145. //订单创建时间
  146. $createTime = time();
  147. //记录订单信息
  148. $buyInfo = [
  149. 'uid' => Session::get('Member.id'),
  150. 'order_id' => $tradeNo,
  151. 'product_id' => $info['id'],
  152. 'buy_type' => "0",
  153. 'pay_type' => "2",
  154. 'money' => $info['moneys'],
  155. 'buy_ip' => $data["buyip"],
  156. 'create_time' => $createTime,
  157. 'intro' => '购买内容:' . $info['title'],
  158. 'status' => "0",
  159. ];
  160. Db::name("member_buylog")->insert($buyInfo);
  161. $info['out_trade_no'] = $tradeNo;
  162. $info['time'] = $createTime;
  163. $info['class'] = "Palymat";
  164. $info['qr_code'] = $payInfo["alipay_trade_precreate_response"]['qr_code'];
  165. Session::set('MattersInfo', $info);
  166. $this->redirect('Palymat/facepay');
  167. }
  168. } catch (Exception $e) {
  169. $this->error($pay->response->body);
  170. }
  171. } elseif ($swich['alipay'] == 1) {
  172. //支付宝官方支付
  173. // 公共配置
  174. $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
  175. $params->appID = $setalipay['alipay_pid'];
  176. $params->md5Key = $setalipay['alipay_key'];
  177. // SDK实例化,传入公共配置
  178. $pay = new \Yurun\PaySDK\Alipay\SDK($params);
  179. $requests = new \Yurun\PaySDK\Alipay\Params\Pay\Request();
  180. $requests->businessParams->seller_id = $setalipay['alipay_pid'];
  181. //订单号
  182. $tradeNo = trade_no();
  183. // 支付后通知地址(作为支付成功回调,这个可靠)
  184. $requests->notify_url = $request->domain() . url("Palymat/aliPayNotify");
  185. // 支付后跳转返回地址
  186. $requests->return_url = $request->domain() . url("Palymat/aliPayReturn");
  187. // 商户订单号
  188. $requests->businessParams->out_trade_no = $tradeNo;
  189. // 价格
  190. $requests->businessParams->total_fee = $info['moneys'];
  191. // 商品标题
  192. $requests->businessParams->subject = $info['title'];
  193. //最晚付款时间,10分钟
  194. $requests->businessParams->it_b_pay = "10m";
  195. //记录订单信息
  196. $buyInfo = [
  197. 'uid' => Session::get('Member.id'),
  198. 'order_id' => $tradeNo,
  199. 'product_id' => $info['id'],
  200. 'buy_type' => "0",
  201. 'pay_type' => "2",
  202. 'money' => $info['moneys'],
  203. 'buy_ip' => $data["buyip"],
  204. 'create_time' => time(),
  205. 'intro' => '购买内容:' . $info['title'],
  206. 'status' => "0",
  207. ];
  208. Db::name("member_buylog")->insert($buyInfo);
  209. // 获取跳转url
  210. $pay->prepareExecute($requests, $url);
  211. $this->redirect($url);
  212. }
  213. } elseif ($data["payst"] == "wxpay") {
  214. //微信支付
  215. //查询微信配置信息
  216. $wxpayInfo = Db::name("pay")->field('wxpay_mchid,wxpay_kye,wxpay_appid')->find(1);
  217. //生成订单
  218. $tradeNo = trade_no();
  219. // 公共配置
  220. $params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
  221. // 支付平台分配给开发者的应用ID
  222. $params->appID = $wxpayInfo['wxpay_appid'];
  223. // 微信支付分配的商户号
  224. $params->mch_id = $wxpayInfo['wxpay_mchid'];
  225. // API 密钥
  226. $params->key = $wxpayInfo['wxpay_kye'];
  227. // SDK实例化,传入公共配置
  228. $pay = new \Yurun\PaySDK\Weixin\SDK($params);
  229. // 支付接口
  230. $requests = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request();
  231. // 商品描述
  232. $requests->body = $info['title'];
  233. // 订单号
  234. $requests->out_trade_no = $tradeNo;
  235. // 订单总金额,单位为:分
  236. $requests->total_fee = $info['moneys'] * 100;
  237. // 客户端ip
  238. $requests->spbill_create_ip = $data["buyip"];
  239. //异步通知地址
  240. $requests->notify_url = $request->domain() . url("Palymat/wxpay");
  241. //交易类型
  242. $requests->trade_type = "NATIVE";
  243. //商品ID
  244. $requests->product_id = $info['id'];
  245. // 调用接口
  246. $payInfo = $pay->execute($requests);
  247. //订单创建时间
  248. $createTime = time();
  249. //记录订单信息
  250. $buyInfo = [
  251. 'uid' => Session::get('Member.id'),
  252. 'order_id' => $tradeNo,
  253. 'product_id' => $info['id'],
  254. 'buy_type' => "0",
  255. 'pay_type' => "1",
  256. 'money' => $info['moneys'],
  257. 'buy_ip' => $data["buyip"],
  258. 'create_time' => $createTime,
  259. 'intro' => '购买内容:' . $info['title'],
  260. 'status' => "0",
  261. ];
  262. Db::name("member_buylog")->insert($buyInfo);
  263. $info['time'] = $createTime;
  264. $info['out_trade_no'] = $tradeNo;
  265. $info['class'] = "Palymat";
  266. //二维码链接
  267. $info['code_url'] = $payInfo['code_url'];
  268. Session::set('MattersInfo', $info);
  269. $this->redirect('Palymat/wxpay');
  270. } elseif ($data["payst"] == "qqpay") {
  271. //QQ支付
  272. $qqPayInfo = Db::name("pay")->find(1);
  273. $tradeNo = trade_no();
  274. $qqArr = [
  275. "mch_id" => $qqPayInfo['qqpay_mchid'], //商户号
  276. "notify_url" => $request->domain() . url("Palymat/qqpay"), //异步通知回调地址
  277. "key" => $qqPayInfo['qqpay_key'], //商户key
  278. ];
  279. $param = [
  280. "out_trade_no" => $tradeNo, // 订单号
  281. "trade_type" => "NATIVE", // 固定值
  282. "total_fee" => $info['moneys'], // 单位为分
  283. "body" => $info['title'], //订单标题
  284. ];
  285. //实例化
  286. $qq = new QQPay($qqArr);
  287. //下单操作
  288. $unified = $qq->unifiedOrder($param);
  289. if ($unified["return_code"] == "SUCCESS" && $unified["result_code"] == "SUCCESS") {
  290. //订单创建时间
  291. $createTime = time();
  292. //记录订单信息
  293. $buyInfo = [
  294. 'uid' => Session::get('Member.id'),
  295. 'order_id' => $tradeNo,
  296. 'product_id' => $info['id'],
  297. 'buy_type' => "0",
  298. 'pay_type' => "0",
  299. 'money' => $info['moneys'],
  300. 'buy_ip' => $data["buyip"],
  301. 'create_time' => time(),
  302. 'intro' => '购买内容:' . $info['title'],
  303. 'status' => "0",
  304. ];
  305. Db::name("member_buylog")->insert($buyInfo);
  306. $info['time'] = $createTime;
  307. $info['trade_no'] = $tradeNo;
  308. $info['class'] = "Palymat";
  309. $info['code_url'] = $unified['code_url'];
  310. Session::set('MattersInfo', $info);
  311. $this->redirect('Palymat/qqpay');
  312. } else {
  313. $this->error('请求出错!');
  314. }
  315. } elseif ($data["payst"] == "moneypay") {
  316. //余额支付
  317. //查询当前会员已购买的文章以及余额
  318. $member = Db::name('Member')->where('id', Session::get('Member.id'))->field('buymat,money')->find();
  319. if ($member['money'] < $info['moneys']) {
  320. $this->error('余额不足,请充值!');
  321. } else {
  322. //记录订单信息
  323. $buyInfo = [
  324. 'uid' => Session::get('Member.id'),
  325. 'order_id' => trade_no(),
  326. 'product_id' => $info['id'],
  327. 'buy_type' => "0",
  328. 'pay_type' => "3",
  329. 'money' => $info['moneys'],
  330. 'buy_ip' => $data["buyip"],
  331. 'create_time' => time(),
  332. 'intro' => '购买内容:' . $info['title'],
  333. 'status' => "1",
  334. ];
  335. Db::name("member_buylog")->insert($buyInfo);
  336. //扣款操作
  337. $balance = $member['money'] - $info['moneys'];
  338. $res = Db::name('Member')->where('id', Session::get('Member.id'))->update(['money' => $balance]);
  339. if ($res) {
  340. //将当前文章ID作记录
  341. if (empty($member['buymat'])) {
  342. $buyMatid = $info['id'];
  343. } else {
  344. $buyMatid = $member['buymat'] . ',' . $info['id'];
  345. }
  346. Db::name('Member')->where('id', Session::get('Member.id'))->update(['buymat' => $buyMatid]);
  347. $this->redirect('User/my_buylog');
  348. } else {
  349. $this->error('支付失败!');
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. //微信支付轮询地址
  357. public function wxpay() {
  358. $info = Session::get('MattersInfo');
  359. if (request()->isPost()) {
  360. //查询微信配置信息
  361. $wxpayInfo = Db::name("pay")->field('wxpay_mchid,wxpay_kye,wxpay_appid')->find(1);
  362. // 公共配置
  363. $params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
  364. // 支付平台分配给开发者的应用ID
  365. $params->appID = $wxpayInfo['wxpay_appid'];
  366. // 微信支付分配的商户号
  367. $params->mch_id = $wxpayInfo['wxpay_mchid'];
  368. // API 密钥
  369. $params->key = $wxpayInfo['wxpay_kye'];
  370. // SDK实例化,传入公共配置
  371. $pay = new \Yurun\PaySDK\Weixin\SDK($params);
  372. $requests = new \Yurun\PaySDK\Weixin\OrderQuery\Request;
  373. // 微信订单号,与商户订单号二选一
  374. $requests->out_trade_no = $payData['out_trade_no'];
  375. $result = $pay->execute($requests);
  376. if ($pay->checkResult()) {
  377. //支付成功
  378. if ($result['trade_state'] == "SUCCESS") {
  379. if ($result['cash_fee'] / 100 !== (float) $info['moneys']) {
  380. Session::delete('MattersInfo');
  381. return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
  382. } else {
  383. //订单更新为已付款
  384. Db::name("member_buylog")->where("order_id", $info["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
  385. //查询订单信息
  386. $buyinfo = Db::name("member_buylog")->where("order_id", $info["out_trade_no"])->find();
  387. //查询已购买过的产品ID集
  388. $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
  389. if (empty($buymat['buymat'])) {
  390. $buyMatid = $info['id'];
  391. } else {
  392. $buyMatid = $buymat['buymat'] . ',' . $info['id'];
  393. }
  394. $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  395. if ($res) {
  396. //删除session
  397. Session::delete('MattersInfo');
  398. //更新操作
  399. $this->success('支付成功!', 'User/my_buylog');
  400. }
  401. }
  402. }
  403. }
  404. }
  405. return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/wxpay', ['wxpay' => $info]);
  406. }
  407. //易支付回调地址
  408. public function epayReturn(Request $request) {
  409. if (request()->isGet()) {
  410. //接收回调
  411. $data = $request->get();
  412. $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
  413. // 判断易支付接口的协议
  414. $preg = '/^http(s)?:\\/\\/.+/';
  415. if (preg_match($preg, $epayInfo['epay_url'])) {
  416. $transport = 'https';
  417. } else {
  418. $transport = 'http';
  419. }
  420. //易支付的配置信息
  421. $epayConfig = [
  422. 'partner' => $epayInfo['epay_appid'], //商户ID
  423. 'key' => $epayInfo['epay_key'], //商户KEY
  424. 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
  425. 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
  426. 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
  427. 'apiurl' => $epayInfo['epay_url'], //支付API地址
  428. ];
  429. require_once Env::get('extend_path') . "/epay/lib/epay_notify.class.php";
  430. $epaySubmit = new \AlipayNotify($epayConfig);
  431. $verifyResult = $epaySubmit->verifyReturn();
  432. if ($verifyResult) {
  433. if ($data['trade_status'] == "TRADE_SUCCESS") {
  434. $this->redirect('User/my_buylog');
  435. } else {
  436. $this->error('回调验证失败!');
  437. }
  438. }
  439. }
  440. }
  441. //易支付异步通知地址
  442. public function epayNotify(Request $request) {
  443. if (request()->isGet()) {
  444. //接收回调
  445. $data = $request->get();
  446. $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
  447. // 判断易支付接口的协议
  448. $preg = '/^http(s)?:\\/\\/.+/';
  449. if (preg_match($preg, $epayInfo['epay_url'])) {
  450. $transport = 'https';
  451. } else {
  452. $transport = 'http';
  453. }
  454. //易支付的配置信息
  455. $epayConfig = [
  456. 'partner' => $epayInfo['epay_appid'], //商户ID
  457. 'key' => $epayInfo['epay_key'], //商户KEY
  458. 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
  459. 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
  460. 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
  461. 'apiurl' => $epayInfo['epay_url'], //支付API地址
  462. ];
  463. require_once Env::get('extend_path') . "/epay/lib/epay_notify.class.php";
  464. $epaySubmit = new \AlipayNotify($epayConfig);
  465. $verifyResult = $epaySubmit->verifyNotify();
  466. if ($verifyResult) {
  467. //支付成功
  468. if ($data['trade_status'] == "TRADE_SUCCESS") {
  469. if ($data['type'] == "alipay") {
  470. $payType = 2;
  471. } elseif ($data['type'] == "wxpay") {
  472. $payType = 1;
  473. } elseif ($data['type'] == "qqpay") {
  474. $payType = 0;
  475. } else {
  476. $payType = -1;
  477. }
  478. //订单更新为已付款
  479. Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->update(["status" => "1", "create_time" => time(), 'pay_type' => $payType]);
  480. //查询订单信息
  481. $buyinfo = Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->find();
  482. $tab = Db::name("model")->field("tablename")->select();
  483. foreach ($tab as $va) {
  484. $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($buyinfo["product_id"]);
  485. if ($infs != null) {
  486. $info = $infs;
  487. }
  488. }
  489. //查询已购买过的产品ID集
  490. $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
  491. if (empty($buymat['buymat'])) {
  492. $buyMatid = $info['id'];
  493. //更新
  494. Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  495. } else {
  496. $buyArray = explode(',', $buymat['buymat']);
  497. //防止多次回调重复存入ID值
  498. if (!in_array($info['id'], $buyArray)) {
  499. $buyMatid = $buymat['buymat'] . ',' . $info['id'];
  500. //更新
  501. Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. //QQ支付轮询地址
  509. public function qqpay() {
  510. $info = Session::get('MattersInfo');
  511. if (request()->isPost()) {
  512. $qqPayInfo = Db::name("pay")->find(1);
  513. $qqArr = [
  514. "mch_id" => $qqPayInfo['qqpay_mchid'], //商户号
  515. "key" => $qqPayInfo['qqpay_key'], //商户key
  516. ];
  517. //实例化
  518. $qq = new QQPay($qqArr);
  519. //查询订单
  520. $unified = $qq->orderQuery($info['trade_no']);
  521. //支付成功
  522. if ($unified['trade_state'] == "SUCCESS") {
  523. if ($unified['cash_fee'] / 100 !== (float) $info['moneys']) {
  524. Session::delete('MattersInfo');
  525. return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
  526. } else {
  527. //订单更新为已付款
  528. Db::name("member_buylog")->where("order_id", $info["trade_no"])->update(["status" => "1", "create_time" => time()]);
  529. //查询订单信息
  530. $buyinfo = Db::name("member_buylog")->where("order_id", $info["trade_no"])->find();
  531. //查询已购买过的产品ID集
  532. $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
  533. if (empty($buymat['buymat'])) {
  534. $buyMatid = $info['id'];
  535. } else {
  536. $buyMatid = $buymat['buymat'] . ',' . $info['id'];
  537. }
  538. $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  539. if ($res) {
  540. //删除session
  541. Session::delete('MattersInfo');
  542. //更新操作
  543. $this->success('支付成功!', 'User/my_buylog');
  544. }
  545. }
  546. }
  547. }
  548. return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/qqpay', ['qqpay' => $info]);
  549. }
  550. //支付宝支付通知地址
  551. public function aliPayNotify(Request $request) {
  552. if (request()->isPost()) {
  553. $setAlipay = Db::name("pay")->find(1);
  554. // 公共配置
  555. $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
  556. $params->appID = $setAlipay['alipay_pid'];
  557. $params->md5Key = $setAlipay['alipay_key'];
  558. // SDK实例化,传入公共配置
  559. $pay = new \Yurun\PaySDK\Alipay\SDK($params);
  560. $data = $request->post();
  561. if ($pay->verifyCallback($data)) {
  562. if ($data["trade_status"] == "TRADE_SUCCESS") {
  563. //查询订单信息
  564. $buyinfo = Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->find();
  565. //查询文章价格作比对
  566. $tab = Db::name("model")->field("tablename")->select();
  567. foreach ($tab as $va) {
  568. $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($buyinfo["product_id"]);
  569. if ($infs != null) {
  570. $info = $infs;
  571. }
  572. }
  573. if ((float) $info['moneys'] !== (float) $data["price"]) {
  574. // 支付接口
  575. $refundRequest = new \Yurun\PaySDK\Alipay\Params\Refund\Request();
  576. $refundRequest->businessParams->batch_no = Session::get('MattersInfo.payinfo.businessParams.out_trade_no'); // 退款批次号
  577. $refundRequest->businessParams->refund_date = date('Y-m-d H:i:s'); // 退款请求时间
  578. $refundRequest->businessParams->batch_num = 1; // 总笔数
  579. $refundRequest->businessParams->detail_data = "{$data['out_trade_no']}^{$data['price']}^{$data['subject']}"; // 单笔数据集
  580. // 调用接口
  581. $result = $pay->execute($refundRequest);
  582. if ('T' == $result['is_success']) {
  583. return ['msg' => "支付金额异常,终止支付!", 'code' => 0];
  584. }
  585. } else {
  586. //订单更新为已付款
  587. Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
  588. //查询已购买过的产品ID集
  589. $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat,money')->find();
  590. if (empty($buymat['buymat'])) {
  591. $buyMatid = $info['id'];
  592. //更新
  593. Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  594. } else {
  595. $buyArray = explode(',', $buymat['buymat']);
  596. //防止多次回调重复存入ID值
  597. if (!in_array($info['id'], $buyArray)) {
  598. $buyMatid = $buymat['buymat'] . ',' . $info['id'];
  599. //更新
  600. Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  601. }
  602. }
  603. }
  604. }
  605. } else {
  606. $this->error('回调验证失败!');
  607. }
  608. }
  609. }
  610. //支付宝支付回调地址
  611. public function aliPayReturn(Request $request) {
  612. if (request()->isGet()) {
  613. $setAlipay = Db::name("pay")->find(1);
  614. $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
  615. $params->appID = $setAlipay['alipay_pid'];
  616. $params->md5Key = $setAlipay['alipay_key'];
  617. // SDK实例化,传入公共配置
  618. $pay = new \Yurun\PaySDK\Alipay\SDK($params);
  619. //接收返回参数
  620. $data = $request->get();
  621. if ($pay->verifyCallback($data)) {
  622. $this->redirect('User/my_buylog');
  623. } else {
  624. $this->error('回调验证失败!');
  625. }
  626. }
  627. }
  628. //支付宝当面付轮询
  629. public function facepay(Request $request) {
  630. $info = Session::get('MattersInfo');
  631. if (request()->isPost()) {
  632. $infos = Db::name("pay")->field("alipayf2f_private_id,alipayf2f_private_key,alipayf2f_public_key")->find(1);
  633. // 公共配置
  634. $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams();
  635. $params->sign_type = "RSA2";
  636. $params->charset = "utf-8";
  637. $params->version = "1.0";
  638. //APP ID
  639. $params->appID = $infos["alipayf2f_private_id"];
  640. //支付宝公钥
  641. $params->appPublicKey = $infos["alipayf2f_public_key"];
  642. //应用私钥
  643. $params->appPrivateKey = $infos["alipayf2f_private_key"];
  644. // SDK实例化,传入公共配置
  645. $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
  646. $requests = new \Yurun\PaySDK\AlipayApp\Params\Query\Request;
  647. // 订单支付时传入的商户订单号,和支付宝交易号不能同时为空。
  648. $requests->businessParams->out_trade_no = $info['out_trade_no'];
  649. // 调用接口
  650. $result = $pay->execute($requests);
  651. if ($pay->checkResult()) {
  652. //支付成功
  653. if ($result["alipay_trade_query_response"]["trade_status"] == "TRADE_SUCCESS") {
  654. //支付金额异常,退款操作
  655. if ((float) $info['moneys'] !== (float) $result["alipay_trade_query_response"]["buyer_pay_amount"]) {
  656. $refundRequest = new \Yurun\PaySDK\AlipayApp\Params\Refund\Request;
  657. $refundRequest->businessParams->out_trade_no = $info['out_trade_no'];
  658. $refundRequest->businessParams->refund_amount = $result["alipay_trade_query_response"]["buyer_pay_amount"];
  659. $refundRequest->businessParams->refund_reason = '支付金额异常!';
  660. // 调用接口
  661. $result = $pay->execute($refundRequest);
  662. if ($pay->checkResult()) {
  663. //删除session
  664. Session::delete('MattersInfo');
  665. return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
  666. }
  667. } else {
  668. //订单更新为已付款
  669. Db::name("member_buylog")->where("order_id", $result["alipay_trade_query_response"]["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
  670. //查询订单信息
  671. $buyinfo = Db::name("member_buylog")->where("order_id", $result["alipay_trade_query_response"]["out_trade_no"])->find();
  672. //查询已购买过的产品ID集
  673. $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
  674. if (empty($buymat['buymat'])) {
  675. $buyMatid = $info['id'];
  676. } else {
  677. $buyMatid = $buymat['buymat'] . ',' . $info['id'];
  678. }
  679. //更新
  680. $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
  681. if ($res) {
  682. //删除session
  683. Session::delete('MattersInfo');
  684. //更新操作
  685. $this->success('支付成功!', 'User/my_buylog');
  686. }
  687. }
  688. } else {
  689. $this->error('扫码成功!');
  690. }
  691. } else {
  692. $this->error('等待支付中...');
  693. }
  694. }
  695. return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/facepay', ["facepay" => $info]);
  696. }
  697. }