您好,我按照您的指示在/addons/xorpay测试,也根据https://www.dqdl.net/a/223.html,修改后仍旧提示”创建订单失败“,下面是我两处的代码:
【/addons/recharge/model/Order.php】代码:
<?php
namespace addons\recharge\model;
use app\common\library\Auth;
use app\common\model\User;
use think\Exception;
use think\Model;
/**
* 充值订单模型
*/
class Order extends Model
{
// 表名
protected $name = 'recharge_order';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = '';
// 追加属性
protected $append = [
];
/**
* 发起订单支付
* @param float $money
* @param string $paytype
*/
public static function submitOrder($money, $paytype = 'wechat')
{
$auth = Auth::instance();
$user_id = $auth->isLogin() ? $auth->id : 0;
$order = null;
$config = get_addon_config('recharge');
if ($config && $config['ordercreatelimit']) {
$order = self::where('user_id', $user_id)
->where('amount', $money)
->where('status', 'created')
->order('id', 'desc')
->find();
}
$request = \think\Request::instance();
// 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' => substr($request->server('HTTP_USER_AGENT'), 0, 255),
// '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("支付功能不存在或未开启");
}
}
}
/**
* 订单结算
* @param int $orderid
* @param string $payamount
* @param string $memo
* @return bool
* @throws \think\exception\DbException
*/
public static function settle($orderid, $payamount = null, $memo = '')
{
$order = self::getByOrderid($orderid);
if (!$order) {
return false;
}
if ($order['status'] != 'paid') {
$order->payamount = $payamount ? $payamount : $order->amount;
$order->paytime = time();
$order->status = 'paid';
$order->memo = $memo;
$order->save();
// 最新版本可直接使用User::money($order->user_id, $order->amount, '充值');来更新
// 更新会员余额
$user = User::get($order->user_id);
if ($user) {
$before = $user->money;
$after = $user->money + $order->amount;
//更新会员信息
$user->save(['money' => $after]);
//写入日志
MoneyLog::create(['user_id' => $order->user_id, 'money' => $order->amount, 'before' => $before, 'after' => $after, 'memo' => '充值']);
}
$result = \think\Hook::listen('recharge_order_settled', $order);
}
return true;
}
}
【/addons/xorpay/controller/Recharge.php】代码:
<?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;
}
}
谢谢您!
嗯嗯,是的。我目前只签约了支付宝,多次尝试都提示:”创建订单失败,请重试!“ 我在xorpay后台查看订单都有记录,但全是未付款
到这个文件里去找问题
/addons/xorpay/controller/Api.php
找到下面这个代码
在这串代码底部增加一个打印输出
print_r($outData);die;//查看返回结果
然后进充值界面,点击充值查看xorpay返回来的参数是什么。
如果返回如下面的数据
根据xorpay官方文档给出的提示是
更多返回结果到xorpay开发文档里查看
XorPay支付平台文档
@jianbs 谢谢,我按照您的指示,返回的数据是这样的:
Array ( [status] => ok [info] => Array ( [qr] => https://qr.alipay.com/bax00411ejdlkpn9442b2024 ) [expires_in] => 7200 [aoid] => a12d5dd414f44a9a854fbe550beedc7e )
还是到这个文件
/addons/xorpay/controller/Api.php
搜索
$order = Service::createOrder($price, $out_order_id, $type,$payUrl,$pay_type,$aoid,$posData['order_uid'], $posData['name'], $notifyurl, $returnurl, $extend, $more);
找到如下代码
改为如下结构
然后进充值界面,点击充值查看提示什么报错!
@jianbs 您好,代码修改之后提示数据库表不存在,我检查了数据库发现xorpay在数据库创建的表前缀是fa_,我自己建的表前缀是cc_,改过来就能支付成功了。但是唤起支付后有两个问题:
第一个是在手机端唤起支付宝支付会提示:“签名错误”,在电脑上用支付宝扫码支付则正常;
第二个是提示:count(): Parameter must be an array or an object that implements Countable
提示问题出现在if(count($order) == 0)