Wallet.php 31 KB

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