会员余额充值插件对接XorPay接口
FastAdmin 会员余额充值插件快速对接xorpay支付插件的方法
需要修改的文件:
/addons/recharge/model/Order.php
需要新增的文件:/addons/xorpay/controller/Recharge.php
Order.php修改内容
在下面的代码里增加内容:
if (!$order) {
$orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
$data = [
'orderid' => $orderid,
'user_id' => $user_id,
'amount' => $money,
'payamount' => 0,
'paytype' => $paytype,
'ip' => $request->ip(),
'useragent' => $request->server('HTTP_USER_AGENT'),
'status' => 'created'
];
$order = self::create($data);
}
修改为如下代码:
if (!$order) {
$orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
$data = [
'orderid' => $orderid,
'user_id' => $user_id,
'amount' => $money,
'payamount' => 0,
'paytype' => $paytype,
'ip' => $request->ip(),
'useragent' => $request->server('HTTP_USER_AGENT'),
'status' => 'created'
];
$order = self::create($data);
}else{
//存在订单的话更新一下订单号 jianbs 19.9.14
$orderid = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
Order::where('id',$order['id'])->update(['orderid'=>$orderid]);
$order['orderid'] = $orderid;
}
将下面代码注释:
/*$epay = get_addon_info('epay');
if ($epay && $epay['state']) {
$notifyurl = $request->root(true) . '/index/recharge/epay/type/notify/paytype/' . $paytype;
$returnurl = $request->root(true) . '/index/recharge/epay/type/return/paytype/' . $paytype;
\addons\epay\library\Service::submitOrder($money, $order->orderid, $paytype, "充值{$money}元", $notifyurl, $returnurl);
exit;
} else {
$result = \think\Hook::listen('recharge_order_submit', $order);
if (!$result) {
throw new Exception("请先在后台安装并配置微信支付宝整合插件");
}
}*/
在注释的代码底部增加如下代码:
//使用xorpay 支付
$xorpay = get_addon_info('xorpay');
if ($xorpay && $xorpay['state']) {
$notifyurl = addon_url("xorpay/recharge/notifyit", [], true, true);
$returnurl = addon_url("xorpay/recharge/returnit", [], true, true);
//自定义附加传递的信息,例如可以用来传递会员ID、会员账号、商品ID等等
$extend = 'recharge';//充值类型
$more = '';
$order_uid = $auth->id;
//发起支付,并跳转到支付页面
\addons\xorpay\library\Service::submitOrder($money, $order->orderid,'余额充值', $paytype, $notifyurl, $returnurl, $extend,$more,$order_uid);
exit;
} else {
$result = \think\Hook::listen('recharge_order_submit', $order);
if (!$result) {
throw new Exception("支付功能不存在或未开启");
}
}
在/addons/xorpay/controller/新增一个Recharge.php控制器
Recharge.php文件代码如下:
namespace addons\xorpay\controller;
use addons\xorpay\model\Order;
use think\addons\Controller;
/**
* 充值处理类
*
* Class Index
* @package addons\xorpay\controller
*/
class Recharge extends Controller
{
protected $layout = 'default';
protected $config = [];
public function _initialize()
{
parent::_initialize();
$this->config = get_addon_config('xorpay');
}
public function index()
{
echo 'Is OK!';die;
}
/**
* 通知回调
*/
public function notifyit()
{
$aoid = $this->request->request('aoid', '');
$order_id = $this->request->request('order_id', '');
$pay_price = $this->request->request('pay_price', '');
$pay_time = $this->request->request('pay_time', '');
$sign = $this->request->request('sign', '');
$secret = $this->config['app_secret'];
//请求xorpay 的签名 和回调回来的签名方式是不同的
//回调签名方式:签名, 参数 aoid + order_id + pay_price + pay_time + app secret 顺序拼接后 MD5
if ($sign != md5(join('',array($aoid, $order_id, $pay_price, $pay_time, $secret)))) {
$this->error('签名错误');
exit();
}
// 签名验证成功,更新订单数据
try {
$params = [
'paytime'=>$pay_time,
'status'=>'settled',
];
if($aoid)
{
$params['aoid'] = $aoid;
}
Order::where('out_order_id',$order_id)->update($params);
//更新会员充值订单
\addons\recharge\model\Order::settle($order_id, $pay_price);
} catch (Exception $e) {
}
//注意只能输出一个success
echo "success";
return;
}
/**
* 支付成功的返回
*/
public function returnit()
{
$order_id = $this->request->request('order_id', '');
$sign = $this->request->request('sign', '');
$config = get_addon_config('xorpay');
$order = Order::get($order_id);
if(count($order) == 0)
{
$this->error('查询订单失败');
exit();
}
if ($sign != md5(join('',array($order['name'], $order['pay_type'], $order['price'], $order['out_order_id'], $order['notifyurl'], $config['app_secret'])))) {
$this->error('签名错误');
exit();
}
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
$this->success("恭喜你!充值成功!", url("index/recharge/recharge"));
return;
}
}
发表评论 取消回复