123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- <?php
- namespace app\index\controller;
- use qqpay\QQPay;
- use think\DB;
- use think\facade\Env;
- use think\facade\Session;
- use think\Request;
- class Palymat extends Base {
- protected $config;
- protected function initialize() {
- parent::initialize();
- $this->config = $this->getsystems();
- }
- public function topaly(Request $request) {
- //接收数据
- $data = $request->param();
- //获取当前客户端IP地址
- $data["buyip"] = $request->ip();
- //查询支付开关控制
- $status = Db::name("pay_set")->field($data["payst"] . "_close")->find(1);
- //查询已购买过的产品ID
- $member = Db::name('Member')->where('id', Session::get('Member.id'))->field('buymat')->find();
- //字符串转数组
- if (isset($member)) {
- $array = explode(",", $member['buymat']);
- }
- if (in_array($data["matid"], $array)) {
- $this->error("请勿重复购买!", 'User/my_buylog');
- }
- $tab = Db::name("model")->field("tablename")->select();
- foreach ($tab as $va) {
- $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($data["matid"]);
- if ($infs != null) {
- $info = $infs;
- }
- }
- //判断该支付方式是否关闭
- if ($status[$data["payst"] . "_close"] !== 1) {
- //查询易支付的相关信息
- $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
- $epay = Db::name('pay_set')->field('epay_close')->find(1);
- if ($epay['epay_close'] == 1 && $data['payst'] !== "moneypay") {
- //发起易支付
- if (empty($info) || empty($data)) {
- $this->error("支付信息缺失,终止支付!");
- } else {
- $tradeNo = trade_no();
- //订单描述
- $parameter = [
- "pid" => (int) $epayInfo['epay_appid'], //商户ID
- "type" => $data["payst"], //支付方式
- "notify_url" => $request->domain() . url("Palymat/epayNotify"), //异步通知地址
- "return_url" => $request->domain() . url("Palymat/epayReturn"), //页面跳转同步通知页面路径
- "out_trade_no" => $tradeNo, //商户订单号
- "name" => $info['title'], //商品名称
- "money" => $info['moneys'], //金额
- "sitename" => $this->config['title'], //站点名称
- ];
- //检测易支付API是否支持SSL
- $stream = stream_context_create(array("ssl" => array("capture_peer_cert" => true)));
- $read = fopen($epayInfo['epay_url'], "rb", false, $stream);
- $cont = stream_context_get_params($read);
- $result = (isset($cont["options"]["ssl"]["peer_certificate"])) ? true : false;
- if ($result) {
- $transport = "https";
- } else {
- $transport = "http";
- }
- //易支付的配置信息
- $epayConfig = [
- 'partner' => $epayInfo['epay_appid'], //商户ID
- 'key' => $epayInfo['epay_key'], //商户KEY
- 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
- 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
- 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
- 'apiurl' => $epayInfo['epay_url'], //支付API地址
- ];
- //建立请求
- require_once Env::get('extend_path') . "/epay/lib/epay_submit.class.php";
- $epaySubmit = new \AlipaySubmit($epayConfig);
- $htmlText = $epaySubmit->buildRequestForm($parameter);
- if ($data['payst'] == "alipay") {
- $payType = "2";
- } elseif ($data['payst'] == "wxpay") {
- $payType = "1";
- } elseif ($data['payst'] == "qqpay") {
- $payType = "0";
- }
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => $tradeNo,
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => $payType,
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => time(),
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "0",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- echo $htmlText;
- }
- } else {
- $this->error('该支付方式已关闭!');
- }
- } else {
- if (empty($info) || empty($data)) {
- $this->error("支付信息缺失,终止支付!");
- } else {
- if ($data["payst"] == "alipay") {
- $swich = Db::name("pay_set")->field("alipay")->find(1);
- //查询当面付配置信息
- $setalipay = Db::name("pay")->find(1);
- //等于1为官方支付宝,0为当面支付
- if ($swich['alipay'] == 0) {
- $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams();
- $params->sign_type = "RSA2";
- $params->charset = "utf-8";
- $params->version = "1.0";
- //订单号
- $tradeNo = trade_no();
- //APP ID
- $params->appID = $setalipay["alipayf2f_private_id"];
- //当面付私钥
- $params->appPrivateKey = $setalipay["alipayf2f_private_key"];
- //支付宝公钥
- $params->appPublicKey = $setalipay["alipayf2f_public_key"];
- //实例化,传入配置信息
- $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
- $requests = new \Yurun\PaySDK\AlipayApp\FTF\Params\QR\Request();
- // 商户订单号
- $requests->businessParams->out_trade_no = $tradeNo;
- // 价格
- $requests->businessParams->total_amount = $info['moneys'];
- // 产品标题
- $requests->businessParams->subject = $info['title'];
- //最晚付款时间,10分钟
- $requests->businessParams->timeout_express = "10m";
- try {
- //调用接口
- $payInfo = $pay->execute($requests);
- if ($pay->checkResult()) {
- //订单创建时间
- $createTime = time();
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => $tradeNo,
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => "2",
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => $createTime,
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "0",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- $info['out_trade_no'] = $tradeNo;
- $info['time'] = $createTime;
- $info['class'] = "Palymat";
- $info['qr_code'] = $payInfo["alipay_trade_precreate_response"]['qr_code'];
- Session::set('MattersInfo', $info);
- $this->redirect('Palymat/facepay');
- }
- } catch (Exception $e) {
- $this->error($pay->response->body);
- }
- } elseif ($swich['alipay'] == 1) {
- //支付宝官方支付
- // 公共配置
- $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
- $params->appID = $setalipay['alipay_pid'];
- $params->md5Key = $setalipay['alipay_key'];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\Alipay\SDK($params);
- $requests = new \Yurun\PaySDK\Alipay\Params\Pay\Request();
- $requests->businessParams->seller_id = $setalipay['alipay_pid'];
- //订单号
- $tradeNo = trade_no();
- // 支付后通知地址(作为支付成功回调,这个可靠)
- $requests->notify_url = $request->domain() . url("Palymat/aliPayNotify");
- // 支付后跳转返回地址
- $requests->return_url = $request->domain() . url("Palymat/aliPayReturn");
- // 商户订单号
- $requests->businessParams->out_trade_no = $tradeNo;
- // 价格
- $requests->businessParams->total_fee = $info['moneys'];
- // 商品标题
- $requests->businessParams->subject = $info['title'];
- //最晚付款时间,10分钟
- $requests->businessParams->it_b_pay = "10m";
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => $tradeNo,
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => "2",
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => time(),
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "0",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- // 获取跳转url
- $pay->prepareExecute($requests, $url);
- $this->redirect($url);
- }
- } elseif ($data["payst"] == "wxpay") {
- //微信支付
- //查询微信配置信息
- $wxpayInfo = Db::name("pay")->field('wxpay_mchid,wxpay_kye,wxpay_appid')->find(1);
- //生成订单
- $tradeNo = trade_no();
- // 公共配置
- $params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
- // 支付平台分配给开发者的应用ID
- $params->appID = $wxpayInfo['wxpay_appid'];
- // 微信支付分配的商户号
- $params->mch_id = $wxpayInfo['wxpay_mchid'];
- // API 密钥
- $params->key = $wxpayInfo['wxpay_kye'];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\Weixin\SDK($params);
- // 支付接口
- $requests = new \Yurun\PaySDK\Weixin\Native\Params\Pay\Request();
- // 商品描述
- $requests->body = $info['title'];
- // 订单号
- $requests->out_trade_no = $tradeNo;
- // 订单总金额,单位为:分
- $requests->total_fee = $info['moneys'] * 100;
- // 客户端ip
- $requests->spbill_create_ip = $data["buyip"];
- //异步通知地址
- $requests->notify_url = $request->domain() . url("Palymat/wxpay");
- //交易类型
- $requests->trade_type = "NATIVE";
- //商品ID
- $requests->product_id = $info['id'];
- // 调用接口
- $payInfo = $pay->execute($requests);
- //订单创建时间
- $createTime = time();
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => $tradeNo,
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => "1",
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => $createTime,
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "0",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- $info['time'] = $createTime;
- $info['out_trade_no'] = $tradeNo;
- $info['class'] = "Palymat";
- //二维码链接
- $info['code_url'] = $payInfo['code_url'];
- Session::set('MattersInfo', $info);
- $this->redirect('Palymat/wxpay');
- } elseif ($data["payst"] == "qqpay") {
- //QQ支付
- $qqPayInfo = Db::name("pay")->find(1);
- $tradeNo = trade_no();
- $qqArr = [
- "mch_id" => $qqPayInfo['qqpay_mchid'], //商户号
- "notify_url" => $request->domain() . url("Palymat/qqpay"), //异步通知回调地址
- "key" => $qqPayInfo['qqpay_key'], //商户key
- ];
- $param = [
- "out_trade_no" => $tradeNo, // 订单号
- "trade_type" => "NATIVE", // 固定值
- "total_fee" => $info['moneys'], // 单位为分
- "body" => $info['title'], //订单标题
- ];
- //实例化
- $qq = new QQPay($qqArr);
- //下单操作
- $unified = $qq->unifiedOrder($param);
- if ($unified["return_code"] == "SUCCESS" && $unified["result_code"] == "SUCCESS") {
- //订单创建时间
- $createTime = time();
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => $tradeNo,
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => "0",
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => time(),
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "0",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- $info['time'] = $createTime;
- $info['trade_no'] = $tradeNo;
- $info['class'] = "Palymat";
- $info['code_url'] = $unified['code_url'];
- Session::set('MattersInfo', $info);
- $this->redirect('Palymat/qqpay');
- } else {
- $this->error('请求出错!');
- }
- } elseif ($data["payst"] == "moneypay") {
- //余额支付
- //查询当前会员已购买的文章以及余额
- $member = Db::name('Member')->where('id', Session::get('Member.id'))->field('buymat,money')->find();
- if ($member['money'] < $info['moneys']) {
- $this->error('余额不足,请充值!');
- } else {
- //记录订单信息
- $buyInfo = [
- 'uid' => Session::get('Member.id'),
- 'order_id' => trade_no(),
- 'product_id' => $info['id'],
- 'buy_type' => "0",
- 'pay_type' => "3",
- 'money' => $info['moneys'],
- 'buy_ip' => $data["buyip"],
- 'create_time' => time(),
- 'intro' => '购买内容:' . $info['title'],
- 'status' => "1",
- ];
- Db::name("member_buylog")->insert($buyInfo);
- //扣款操作
- $balance = $member['money'] - $info['moneys'];
- $res = Db::name('Member')->where('id', Session::get('Member.id'))->update(['money' => $balance]);
- if ($res) {
- //将当前文章ID作记录
- if (empty($member['buymat'])) {
- $buyMatid = $info['id'];
- } else {
- $buyMatid = $member['buymat'] . ',' . $info['id'];
- }
- Db::name('Member')->where('id', Session::get('Member.id'))->update(['buymat' => $buyMatid]);
- $this->redirect('User/my_buylog');
- } else {
- $this->error('支付失败!');
- }
- }
- }
- }
- }
- }
- //微信支付轮询地址
- public function wxpay() {
- $info = Session::get('MattersInfo');
- if (request()->isPost()) {
- //查询微信配置信息
- $wxpayInfo = Db::name("pay")->field('wxpay_mchid,wxpay_kye,wxpay_appid')->find(1);
- // 公共配置
- $params = new \Yurun\PaySDK\Weixin\Params\PublicParams();
- // 支付平台分配给开发者的应用ID
- $params->appID = $wxpayInfo['wxpay_appid'];
- // 微信支付分配的商户号
- $params->mch_id = $wxpayInfo['wxpay_mchid'];
- // API 密钥
- $params->key = $wxpayInfo['wxpay_kye'];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\Weixin\SDK($params);
- $requests = new \Yurun\PaySDK\Weixin\OrderQuery\Request;
- // 微信订单号,与商户订单号二选一
- $requests->out_trade_no = $payData['out_trade_no'];
- $result = $pay->execute($requests);
- if ($pay->checkResult()) {
- //支付成功
- if ($result['trade_state'] == "SUCCESS") {
- if ($result['cash_fee'] / 100 !== (float) $info['moneys']) {
- Session::delete('MattersInfo');
- return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
- } else {
- //订单更新为已付款
- Db::name("member_buylog")->where("order_id", $info["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
- //查询订单信息
- $buyinfo = Db::name("member_buylog")->where("order_id", $info["out_trade_no"])->find();
- //查询已购买过的产品ID集
- $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
- if (empty($buymat['buymat'])) {
- $buyMatid = $info['id'];
- } else {
- $buyMatid = $buymat['buymat'] . ',' . $info['id'];
- }
- $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- if ($res) {
- //删除session
- Session::delete('MattersInfo');
- //更新操作
- $this->success('支付成功!', 'User/my_buylog');
- }
- }
- }
- }
- }
- return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/wxpay', ['wxpay' => $info]);
- }
- //易支付回调地址
- public function epayReturn(Request $request) {
- if (request()->isGet()) {
- //接收回调
- $data = $request->get();
- $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
- // 判断易支付接口的协议
- $preg = '/^http(s)?:\\/\\/.+/';
- if (preg_match($preg, $epayInfo['epay_url'])) {
- $transport = 'https';
- } else {
- $transport = 'http';
- }
- //易支付的配置信息
- $epayConfig = [
- 'partner' => $epayInfo['epay_appid'], //商户ID
- 'key' => $epayInfo['epay_key'], //商户KEY
- 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
- 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
- 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
- 'apiurl' => $epayInfo['epay_url'], //支付API地址
- ];
- require_once Env::get('extend_path') . "/epay/lib/epay_notify.class.php";
- $epaySubmit = new \AlipayNotify($epayConfig);
- $verifyResult = $epaySubmit->verifyReturn();
- if ($verifyResult) {
- if ($data['trade_status'] == "TRADE_SUCCESS") {
- $this->redirect('User/my_buylog');
- } else {
- $this->error('回调验证失败!');
- }
- }
- }
- }
- //易支付异步通知地址
- public function epayNotify(Request $request) {
- if (request()->isGet()) {
- //接收回调
- $data = $request->get();
- $epayInfo = Db::name('pay')->field('epay_url,epay_appid,epay_key')->find(1);
- // 判断易支付接口的协议
- $preg = '/^http(s)?:\\/\\/.+/';
- if (preg_match($preg, $epayInfo['epay_url'])) {
- $transport = 'https';
- } else {
- $transport = 'http';
- }
- //易支付的配置信息
- $epayConfig = [
- 'partner' => $epayInfo['epay_appid'], //商户ID
- 'key' => $epayInfo['epay_key'], //商户KEY
- 'sign_type' => strtoupper('MD5'), //签名方式 不需修改
- 'input_charset' => strtolower('gbk'), //字符编码格式 目前支持 gbk 或 utf-8
- 'transport' => $transport, //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
- 'apiurl' => $epayInfo['epay_url'], //支付API地址
- ];
- require_once Env::get('extend_path') . "/epay/lib/epay_notify.class.php";
- $epaySubmit = new \AlipayNotify($epayConfig);
- $verifyResult = $epaySubmit->verifyNotify();
- if ($verifyResult) {
- //支付成功
- if ($data['trade_status'] == "TRADE_SUCCESS") {
- if ($data['type'] == "alipay") {
- $payType = 2;
- } elseif ($data['type'] == "wxpay") {
- $payType = 1;
- } elseif ($data['type'] == "qqpay") {
- $payType = 0;
- } else {
- $payType = -1;
- }
- //订单更新为已付款
- Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->update(["status" => "1", "create_time" => time(), 'pay_type' => $payType]);
- //查询订单信息
- $buyinfo = Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->find();
- $tab = Db::name("model")->field("tablename")->select();
- foreach ($tab as $va) {
- $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($buyinfo["product_id"]);
- if ($infs != null) {
- $info = $infs;
- }
- }
- //查询已购买过的产品ID集
- $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
- if (empty($buymat['buymat'])) {
- $buyMatid = $info['id'];
- //更新
- Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- } else {
- $buyArray = explode(',', $buymat['buymat']);
- //防止多次回调重复存入ID值
- if (!in_array($info['id'], $buyArray)) {
- $buyMatid = $buymat['buymat'] . ',' . $info['id'];
- //更新
- Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- }
- }
- }
- }
- }
- }
- //QQ支付轮询地址
- public function qqpay() {
- $info = Session::get('MattersInfo');
- if (request()->isPost()) {
- $qqPayInfo = Db::name("pay")->find(1);
- $qqArr = [
- "mch_id" => $qqPayInfo['qqpay_mchid'], //商户号
- "key" => $qqPayInfo['qqpay_key'], //商户key
- ];
- //实例化
- $qq = new QQPay($qqArr);
- //查询订单
- $unified = $qq->orderQuery($info['trade_no']);
- //支付成功
- if ($unified['trade_state'] == "SUCCESS") {
- if ($unified['cash_fee'] / 100 !== (float) $info['moneys']) {
- Session::delete('MattersInfo');
- return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
- } else {
- //订单更新为已付款
- Db::name("member_buylog")->where("order_id", $info["trade_no"])->update(["status" => "1", "create_time" => time()]);
- //查询订单信息
- $buyinfo = Db::name("member_buylog")->where("order_id", $info["trade_no"])->find();
- //查询已购买过的产品ID集
- $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
- if (empty($buymat['buymat'])) {
- $buyMatid = $info['id'];
- } else {
- $buyMatid = $buymat['buymat'] . ',' . $info['id'];
- }
- $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- if ($res) {
- //删除session
- Session::delete('MattersInfo');
- //更新操作
- $this->success('支付成功!', 'User/my_buylog');
- }
- }
- }
- }
- return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/qqpay', ['qqpay' => $info]);
- }
- //支付宝支付通知地址
- public function aliPayNotify(Request $request) {
- if (request()->isPost()) {
- $setAlipay = Db::name("pay")->find(1);
- // 公共配置
- $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
- $params->appID = $setAlipay['alipay_pid'];
- $params->md5Key = $setAlipay['alipay_key'];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\Alipay\SDK($params);
- $data = $request->post();
- if ($pay->verifyCallback($data)) {
- if ($data["trade_status"] == "TRADE_SUCCESS") {
- //查询订单信息
- $buyinfo = Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->find();
- //查询文章价格作比对
- $tab = Db::name("model")->field("tablename")->select();
- foreach ($tab as $va) {
- $infs = Db::name($va["tablename"])->field("id,title,moneys")->find($buyinfo["product_id"]);
- if ($infs != null) {
- $info = $infs;
- }
- }
- if ((float) $info['moneys'] !== (float) $data["price"]) {
- // 支付接口
- $refundRequest = new \Yurun\PaySDK\Alipay\Params\Refund\Request();
- $refundRequest->businessParams->batch_no = Session::get('MattersInfo.payinfo.businessParams.out_trade_no'); // 退款批次号
- $refundRequest->businessParams->refund_date = date('Y-m-d H:i:s'); // 退款请求时间
- $refundRequest->businessParams->batch_num = 1; // 总笔数
- $refundRequest->businessParams->detail_data = "{$data['out_trade_no']}^{$data['price']}^{$data['subject']}"; // 单笔数据集
- // 调用接口
- $result = $pay->execute($refundRequest);
- if ('T' == $result['is_success']) {
- return ['msg' => "支付金额异常,终止支付!", 'code' => 0];
- }
- } else {
- //订单更新为已付款
- Db::name("member_buylog")->where("order_id", $data["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
- //查询已购买过的产品ID集
- $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat,money')->find();
- if (empty($buymat['buymat'])) {
- $buyMatid = $info['id'];
- //更新
- Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- } else {
- $buyArray = explode(',', $buymat['buymat']);
- //防止多次回调重复存入ID值
- if (!in_array($info['id'], $buyArray)) {
- $buyMatid = $buymat['buymat'] . ',' . $info['id'];
- //更新
- Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- }
- }
- }
- }
- } else {
- $this->error('回调验证失败!');
- }
- }
- }
- //支付宝支付回调地址
- public function aliPayReturn(Request $request) {
- if (request()->isGet()) {
- $setAlipay = Db::name("pay")->find(1);
- $params = new \Yurun\PaySDK\Alipay\Params\PublicParams();
- $params->appID = $setAlipay['alipay_pid'];
- $params->md5Key = $setAlipay['alipay_key'];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\Alipay\SDK($params);
- //接收返回参数
- $data = $request->get();
- if ($pay->verifyCallback($data)) {
- $this->redirect('User/my_buylog');
- } else {
- $this->error('回调验证失败!');
- }
- }
- }
- //支付宝当面付轮询
- public function facepay(Request $request) {
- $info = Session::get('MattersInfo');
- if (request()->isPost()) {
- $infos = Db::name("pay")->field("alipayf2f_private_id,alipayf2f_private_key,alipayf2f_public_key")->find(1);
- // 公共配置
- $params = new \Yurun\PaySDK\AlipayApp\Params\PublicParams();
- $params->sign_type = "RSA2";
- $params->charset = "utf-8";
- $params->version = "1.0";
- //APP ID
- $params->appID = $infos["alipayf2f_private_id"];
- //支付宝公钥
- $params->appPublicKey = $infos["alipayf2f_public_key"];
- //应用私钥
- $params->appPrivateKey = $infos["alipayf2f_private_key"];
- // SDK实例化,传入公共配置
- $pay = new \Yurun\PaySDK\AlipayApp\SDK($params);
- $requests = new \Yurun\PaySDK\AlipayApp\Params\Query\Request;
- // 订单支付时传入的商户订单号,和支付宝交易号不能同时为空。
- $requests->businessParams->out_trade_no = $info['out_trade_no'];
- // 调用接口
- $result = $pay->execute($requests);
- if ($pay->checkResult()) {
- //支付成功
- if ($result["alipay_trade_query_response"]["trade_status"] == "TRADE_SUCCESS") {
- //支付金额异常,退款操作
- if ((float) $info['moneys'] !== (float) $result["alipay_trade_query_response"]["buyer_pay_amount"]) {
- $refundRequest = new \Yurun\PaySDK\AlipayApp\Params\Refund\Request;
- $refundRequest->businessParams->out_trade_no = $info['out_trade_no'];
- $refundRequest->businessParams->refund_amount = $result["alipay_trade_query_response"]["buyer_pay_amount"];
- $refundRequest->businessParams->refund_reason = '支付金额异常!';
- // 调用接口
- $result = $pay->execute($refundRequest);
- if ($pay->checkResult()) {
- //删除session
- Session::delete('MattersInfo');
- return ['msg' => "支付金额异常,终止支付!", 'code' => 2, 'url' => url('User/index')];
- }
- } else {
- //订单更新为已付款
- Db::name("member_buylog")->where("order_id", $result["alipay_trade_query_response"]["out_trade_no"])->update(["status" => "1", "create_time" => time()]);
- //查询订单信息
- $buyinfo = Db::name("member_buylog")->where("order_id", $result["alipay_trade_query_response"]["out_trade_no"])->find();
- //查询已购买过的产品ID集
- $buymat = Db::name('Member')->where('id', $buyinfo["uid"])->field('buymat')->find();
- if (empty($buymat['buymat'])) {
- $buyMatid = $info['id'];
- } else {
- $buyMatid = $buymat['buymat'] . ',' . $info['id'];
- }
- //更新
- $res = Db::name("member")->where("id", $buyinfo["uid"])->update(["buymat" => $buyMatid]);
- if ($res) {
- //删除session
- Session::delete('MattersInfo');
- //更新操作
- $this->success('支付成功!', 'User/my_buylog');
- }
- }
- } else {
- $this->error('扫码成功!');
- }
- } else {
- $this->error('等待支付中...');
- }
- }
- return $this->fetch('/home_temp/' . $this->config["home_temp"] . '/facepay', ["facepay" => $info]);
- }
- }
|