温馨提示:
本文最后更新于
2025-04-20 12:46:11
,某些文章具有时效性,若有错误或已失效,请在下方留言
本文最后更新于:
一、项目目录结构
├── app/ // 应用目录
│ ├── controller/ // 控制器
│ │ └── Payment.php // 支付控制器
│ ├── model/ // 模型
│ │ ├── Transaction.php // 交易模型
│ │ └── Config.php // 系统配置模型
│ ├── view/ // 视图
│ │ └── payment/ // 支付相关模板
│ │ ├── index.html // 支付页
│ │ └── qrcode.html // 二维码页
│ └── route/ // 路由定义
│ └── app.php
├── config/ // 配置文件
│ ├── database.php // 数据库配置
│ └── route.php // 路由配置
├── public/ // 入口目录
│ ├── static/ // 静态资源
│ │ ├── css/
│ │ ├── js/
│ │ └── img/
│ ├── .htaccess // Apache伪静态规则
│ └── index.php // 入口文件
├── extend/ // 扩展类库
│ └── payment/ // 支付驱动
│ ├── WechatPay.php
│ └── Alipay.php
├── database/ // 数据库文件
│ ├── appreciation.sql // 数据库结构
│ └── seeds.sql // 初始数据
└── runtime/ // 运行时目录
二、数据库文件 (database/appreciation.sql)
-- 创建数据库
CREATE DATABASE IF NOT EXISTS `appreciation_db`
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `appreciation_db`;
-- 交易记录表
CREATE TABLE `transactions` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`trade_no` varchar(32) NOT NULL COMMENT '商户订单号',
`amount` decimal(10,2) NOT NULL COMMENT '金额',
`payment_type` enum('wechat','alipay') NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0-待支付 1-已支付',
`create_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `trade_no` (`trade_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 系统配置表
CREATE TABLE `system_config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`min_amount` decimal(10,2) NOT NULL DEFAULT '1.00',
`max_amount` decimal(10,2) NOT NULL DEFAULT '1000.00',
`rate_limit` int(11) NOT NULL DEFAULT '60' COMMENT '每分钟请求限制',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 初始数据
INSERT INTO `system_config` (`min_amount`, `max_amount`) VALUES (1.00, 1000.00);
三、核心代码实现
1. 支付控制器 (app/controller/Payment.php)
<?php
namespace app\controller;
use think\Request;
use app\model\Transaction;
use app\model\Config;
class Payment
{
// 支付入口
public function index(Request $request)
{
return view('payment/index');
}
// 生成支付二维码
public function create(Request $request)
{
$amount = $request->param('amount');
$type = $request->param('type', 'wechat');
// 验证金额范围
$config = Config::find(1);
if ($amount < $config->min_amount || $amount > $config->max_amount) {
return json(['code' => 400, 'msg' => '金额超出允许范围']);
}
// 生成订单
$tradeNo = date('YmdHis').mt_rand(1000,9999);
$transaction = new Transaction;
$transaction->save([
'trade_no' => $tradeNo,
'amount' => $amount,
'payment_type' => $type,
'create_time' => date('Y-m-d H:i:s')
]);
// 调用支付驱动
$driver = $this->getPaymentDriver($type);
$qrCode = $driver->generateQR($tradeNo, $amount);
return view('payment/qrcode', [
'qr_url' => $qrCode,
'trade_no' => $tradeNo
]);
}
// 支付回调处理
public function notify()
{
$tradeNo = input('trade_no');
$transaction = Transaction::where('trade_no', $tradeNo)->find();
// 验证签名逻辑
if ($this->verifySign()) {
$transaction->status = 1;
$transaction->save();
return 'SUCCESS';
}
return 'FAIL';
}
private function getPaymentDriver($type)
{
$class = '\\extend\\payment\\'.ucfirst($type);
return new $class();
}
}
2. 支付宝支付驱动 (extend/payment/Alipay.php)
<?php
namespace extend\payment;
class Alipay
{
private $config = [
'app_id' => '2021000123456789',
'gateway' => 'https://openapi.alipay.com/gateway.do'
];
public function generateQR($tradeNo, $amount)
{
// 调用支付宝API生成二维码
$params = [
'out_trade_no' => $tradeNo,
'total_amount' => $amount,
'subject' => '内容赞赏'
];
// 实际开发需实现签名逻辑
return $this->config['gateway'].'?'.http_build_query($params);
}
}
四、ThinkPHP伪静态配置
1. Apache配置 (.htaccess)
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
2. Nginx配置
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?/$1 last;
}
}
五、系统安装步骤
1. 环境准备
# 安装PHP扩展
sudo apt-get install php7.4 php7.4-mysql php7.4-curl
# 安装Composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
2.创建项目
# 通过Composer创建ThinkPHP项目
composer create-project topthink/think appreciation-system
# 进入项目目录
cd appreciation-system
3.导入数据库
mysql -u root -p appreciation_db < database/appreciation.sql
4.配置数据库(config/database.php)
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'appreciation_db',
'username' => 'root',
'password' => 'your_password',
'charset' => 'utf8mb4',
],
],
];
5. 配置路由 (config/route.php)
use think\facade\Route;
Route::get('pay', 'payment/index');
Route::post('create-payment', 'payment/create');
Route::post('notify', 'payment/notify');
六、安全增强措施
1. 输入过滤中间件
// app/middleware/InputFilter.php
namespace app\middleware;
class InputFilter
{
public function handle($request, \Closure $next)
{
$input = $request->param();
array_walk_recursive($input, function(&$item){
$item = htmlspecialchars(strip_tags($item));
});
$request->withParam($input);
return $next($request);
}
}
2. 请求频率限制
// 在控制器中增加限流
use think\facade\Cache;
if (Cache::get('api_limit_'.request()->ip()) > 60) {
abort(429, '请求过于频繁');
}
Cache::inc('api_limit_'.request()->ip(), 1, 60);
七、运行验证
1. 访问支付页面
http://localhost/pay
2. 支付成功后可在数据库查看记录
SELECT * FROM transactions WHERE status=1;
八、扩展功能建议
1. **支付结果WebSocket推送
// 使用Workerman集成
$worker = new Worker('websocket://0.0.0.0:2346');
$worker->onMessage = function($connection, $data){
$connection->send(json_encode([
'trade_no' => '123456',
'status' => 'paid'
]
温馨提示:
本文最后更新于
2025-04-20 12:46:11
,某些文章具有时效性,若有错误或已失效,请在下方留言
本文最后更新于:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容