xorpay插件充值提示”创建订单失败?“

发布于 2020-02-28 16:49:35

您好,我按照您的指示在/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;
 }
}

谢谢您!

查看更多

关注者
0
被浏览
618
jianbs
jianbs 认证专家 2020-02-28
曾梦想仗剑走天涯,谁知后来剑被偷了...
登录后查看是否有阅读权限
1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览