CMS使用的是企业微信/支付宝接口如何使用XorPay?
需要修改到的文件:
/addons/cms/model/Order.php
需要增加的文件:/addons/xorpay/controller/Cms.php
Order.php修改内容
将下面代码增加内容:
if (!$order || (time() - $order->createtime) > 1800) {
$orderid = date("YmdHis") . mt_rand(100000, 999999);
$data = [
'user_id' => $auth->id ? $auth->id : 0,
'orderid' => $orderid,
'archives_id' => $archives->id,
'title' => "付费阅读",
'amount' => $archives->price,
'payamount' => 0,
'paytype' => $paytype,
'ip' => $request->ip(),
'useragent' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
'status' => 'created'
];
$order = Order::create($data);
} else {
if ($order->amount != $archives->price) {
$order->amount = $archives->price;
$order->save();
}
}
修改为如下:
if (!$order || (time() - $order->createtime) > 1800) {
$orderid = date("YmdHis") . mt_rand(100000, 999999);
$data = [
'user_id' => $auth->id ? $auth->id : 0,
'orderid' => $orderid,
'archives_id' => $archives->id,
'title' => "付费阅读",
'amount' => $archives->price,
'payamount' => 0,
'paytype' => $paytype,
'ip' => $request->ip(),
'useragent' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
'status' => 'created'
];
$order = Order::create($data);
} else {
if ($order->amount != $archives->price) {
$order->amount = $archives->price;
$order->save();
}
//存在订单的话更新一下订单号 jianbs 19.9.14
$orderid = $orderid = date("YmdHis") . mt_rand(100000, 999999);
Order::where('id',$order['id'])->update(['orderid'=>$orderid]);
$order['orderid'] = $orderid;
}
将下面代码注释:
/*$epay = get_addon_info('epay');
if ($epay && $epay['state']) {
$notifyurl = $request->root(true) . '/addons/cms/order/epay/type/notify/paytype/' . $paytype;
$returnurl = $request->root(true) . '/addons/cms/order/epay/type/return/paytype/' . $paytype . '/orderid/' . $order->orderid;
\addons\epay\library\Service::submitOrder($order->amount, $order->orderid, $paytype, "支付{$order->amount}元", $notifyurl, $returnurl);
} else {
$result = \think\Hook::listen('cms_order_submit', $order);
if (!$result) {
throw new OrderException("请先在后台安装配置微信支付宝整合插件");
}
}*/
注释后在代码下面新增代码:
//使用xorpay 支付
$xorpay = get_addon_info('xorpay');
if ($xorpay && $xorpay['state']) {
$notifyurl = addon_url("xorpay/cms/notifyit", [], true, true);
$returnurl = addon_url("xorpay/cms/returnit", [], true, true);
//自定义附加传递的信息,例如可以用来传递会员ID、会员账号、商品ID等等
$extend = 'article';//充值类型
$more = $order->archives_id;//文章ID
$order_uid = $auth->id;
//发起支付,并跳转到支付页面
\addons\xorpay\library\Service::submitOrder($order->amount, $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/新增一个Cms.php控制器
Cms.php文件代码如下:
php
namespace addons\xorpay\controller;
use addons\xorpay\model\Order;
use think\addons\Controller;
/**
* 文章支付处理类
*
* Class Index
* @package addons\xorpay\controller
*/
class Cms 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);
//更新CMS 订单
\addons\cms\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();
}
$order = \addons\cms\model\Order::getByOrderid($order['out_order_id']);
if (!$order->archives) {
$this->error('未找到文档信息!');
}
//你可以在这里定义你的提示信息,但切记不可在此编写逻辑
$this->redirect($order->archives->url);
return;
}
}
4年前 回复TA
新版本cms好像找不到