文章目录[隐藏]
WordPress网络传媒柔性收益分成插件开发详解
引言:为什么需要柔性收益分成插件
在当今的网络传媒行业中,内容创作者、平台运营者和广告商之间的收益分配是一个复杂而关键的问题。传统的固定比例分成模式往往无法满足多样化的合作需求。WordPress作为全球最流行的内容管理系统,拥有超过40%的网站市场份额,开发一款柔性收益分成插件能够帮助网络传媒平台实现更灵活、更公平的收益分配机制。
本文将详细介绍如何开发一款功能完整的WordPress柔性收益分成插件,从需求分析到代码实现,涵盖插件架构设计、数据库设计、核心功能实现等关键环节。
插件需求分析与功能规划
核心需求
- 支持多角色收益分配(作者、编辑、平台、合作伙伴等)
- 可配置的分成规则和条件
- 实时收益统计与报表
- 安全可靠的支付集成
- 用户友好的管理界面
功能模块设计
- 收益规则管理模块
- 用户收益账户模块
- 分成计算引擎模块
- 支付处理模块
- 数据统计与报表模块
数据库设计与表结构
<?php
/**
* 插件数据库表结构
*/
global $wpdb;
// 定义表名
define('FRS_EARNINGS_TABLE', $wpdb->prefix . 'fr_soft_earnings');
define('FRS_RULES_TABLE', $wpdb->prefix . 'fr_split_rules');
define('FRS_TRANSACTIONS_TABLE', $wpdb->prefix . 'fr_transactions');
define('FRS_USER_ACCOUNTS_TABLE', $wpdb->prefix . 'fr_user_accounts');
/**
* 创建插件所需数据库表
*/
function frs_create_database_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 收益记录表
$earnings_table_sql = "CREATE TABLE " . FRS_EARNINGS_TABLE . " (
id bigint(20) NOT NULL AUTO_INCREMENT,
post_id bigint(20) NOT NULL,
user_id bigint(20) NOT NULL,
earnings_date date NOT NULL,
revenue_source varchar(100) NOT NULL,
total_amount decimal(10,2) NOT NULL,
user_share decimal(10,2) NOT NULL,
platform_share decimal(10,2) NOT NULL,
status varchar(20) DEFAULT 'pending',
calculated_at datetime DEFAULT CURRENT_TIMESTAMP,
paid_at datetime NULL,
PRIMARY KEY (id),
KEY post_id (post_id),
KEY user_id (user_id),
KEY earnings_date (earnings_date)
) $charset_collate;";
// 分成规则表
$rules_table_sql = "CREATE TABLE " . FRS_RULES_TABLE . " (
rule_id bigint(20) NOT NULL AUTO_INCREMENT,
rule_name varchar(200) NOT NULL,
rule_type varchar(50) NOT NULL,
target_user_role varchar(50) NOT NULL,
base_percentage decimal(5,2) NOT NULL,
conditions text,
min_amount decimal(10,2) DEFAULT 0,
max_amount decimal(10,2) DEFAULT NULL,
effective_from date NOT NULL,
effective_to date NULL,
is_active tinyint(1) DEFAULT 1,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (rule_id)
) $charset_collate;";
// 用户账户表
$accounts_table_sql = "CREATE TABLE " . FRS_USER_ACCOUNTS_TABLE . " (
account_id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
total_earnings decimal(10,2) DEFAULT 0,
pending_balance decimal(10,2) DEFAULT 0,
available_balance decimal(10,2) DEFAULT 0,
total_withdrawn decimal(10,2) DEFAULT 0,
last_calculated datetime NULL,
payment_method varchar(100),
payment_details text,
PRIMARY KEY (account_id),
UNIQUE KEY user_id (user_id)
) $charset_collate;";
// 交易记录表
$transactions_table_sql = "CREATE TABLE " . FRS_TRANSACTIONS_TABLE . " (
transaction_id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
transaction_type varchar(50) NOT NULL,
amount decimal(10,2) NOT NULL,
description text,
reference_id varchar(100),
status varchar(20) DEFAULT 'completed',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
processed_at datetime NULL,
PRIMARY KEY (transaction_id),
KEY user_id (user_id),
KEY transaction_type (transaction_type)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// 执行创建表
dbDelta($earnings_table_sql);
dbDelta($rules_table_sql);
dbDelta($accounts_table_sql);
dbDelta($transactions_table_sql);
}
?>
核心功能模块实现
1. 分成规则管理类
<?php
/**
* 收益分成规则管理类
*/
class FRS_Rule_Manager {
private $db;
public function __construct() {
global $wpdb;
$this->db = $wpdb;
}
/**
* 创建新的分成规则
* @param array $rule_data 规则数据
* @return int|false 规则ID或false
*/
public function create_rule($rule_data) {
$defaults = array(
'rule_name' => '',
'rule_type' => 'percentage',
'target_user_role' => 'author',
'base_percentage' => 50.00,
'conditions' => '',
'min_amount' => 0,
'max_amount' => null,
'effective_from' => current_time('mysql'),
'effective_to' => null,
'is_active' => 1
);
$rule_data = wp_parse_args($rule_data, $defaults);
// 验证数据
if (empty($rule_data['rule_name']) || $rule_data['base_percentage'] < 0) {
return false;
}
// 插入数据库
$result = $this->db->insert(
FRS_RULES_TABLE,
$rule_data,
array('%s', '%s', '%s', '%f', '%s', '%f', '%f', '%s', '%s', '%d')
);
return $result ? $this->db->insert_id : false;
}
/**
* 获取适用于特定用户和金额的规则
* @param int $user_id 用户ID
* @param float $amount 金额
* @param string $revenue_source 收入来源
* @return array 适用的规则数组
*/
public function get_applicable_rules($user_id, $amount, $revenue_source = '') {
$user = get_userdata($user_id);
if (!$user) {
return array();
}
$current_date = current_time('mysql');
$sql = $this->db->prepare(
"SELECT * FROM " . FRS_RULES_TABLE . "
WHERE target_user_role = %s
AND is_active = 1
AND effective_from <= %s
AND (effective_to IS NULL OR effective_to >= %s)
AND (min_amount <= %f OR min_amount = 0)
AND (max_amount IS NULL OR max_amount >= %f)
ORDER BY base_percentage DESC",
$user->roles[0],
$current_date,
$current_date,
$amount,
$amount
);
$rules = $this->db->get_results($sql, ARRAY_A);
// 应用条件过滤
$applicable_rules = array();
foreach ($rules as $rule) {
if ($this->check_rule_conditions($rule, $user_id, $amount, $revenue_source)) {
$applicable_rules[] = $rule;
}
}
return $applicable_rules;
}
/**
* 检查规则条件
* @param array $rule 规则数据
* @param int $user_id 用户ID
* @param float $amount 金额
* @param string $revenue_source 收入来源
* @return bool 是否满足条件
*/
private function check_rule_conditions($rule, $user_id, $amount, $revenue_source) {
if (empty($rule['conditions'])) {
return true;
}
$conditions = json_decode($rule['conditions'], true);
if (!is_array($conditions)) {
return true;
}
foreach ($conditions as $condition) {
if (!$this->evaluate_condition($condition, $user_id, $amount, $revenue_source)) {
return false;
}
}
return true;
}
/**
* 评估单个条件
*/
private function evaluate_condition($condition, $user_id, $amount, $revenue_source) {
// 这里可以实现复杂的条件判断逻辑
// 例如:用户等级、内容类型、收入来源等
return true; // 简化实现,实际开发中需要完整实现
}
}
?>
2. 收益计算引擎
<?php
/**
* 收益计算引擎
*/
class FRS_Earnings_Calculator {
private $rule_manager;
private $db;
public function __construct() {
global $wpdb;
$this->db = $wpdb;
$this->rule_manager = new FRS_Rule_Manager();
}
/**
* 计算单篇文章的收益分成
* @param int $post_id 文章ID
* @param float $total_revenue 总收益
* @param string $revenue_source 收益来源
* @return array 计算结果
*/
public function calculate_post_earnings($post_id, $total_revenue, $revenue_source = 'advertising') {
$post = get_post($post_id);
if (!$post) {
return array('error' => '文章不存在');
}
$author_id = $post->post_author;
$results = array(
'post_id' => $post_id,
'total_revenue' => $total_revenue,
'revenue_source' => $revenue_source,
'distribution' => array(),
'summary' => array()
);
// 获取适用的分成规则
$applicable_rules = $this->rule_manager->get_applicable_rules(
$author_id,
$total_revenue,
$revenue_source
);
// 如果没有适用规则,使用默认规则
if (empty($applicable_rules)) {
$applicable_rules = array(array(
'rule_id' => 0,
'rule_name' => '默认规则',
'base_percentage' => 50.00
));
}
// 应用第一个适用规则(优先级最高)
$primary_rule = $applicable_rules[0];
$author_percentage = $primary_rule['base_percentage'];
// 计算作者收益
$author_share = round($total_revenue * $author_percentage / 100, 2);
$platform_share = $total_revenue - $author_share;
// 保存计算结果
$this->save_earning_record(
$post_id,
$author_id,
$total_revenue,
$author_share,
$platform_share,
$revenue_source
);
// 更新用户账户
$this->update_user_account($author_id, $author_share);
// 构建返回结果
$results['distribution']['author'] = array(
'user_id' => $author_id,
'percentage' => $author_percentage,
'amount' => $author_share,
'rule_applied' => $primary_rule['rule_name']
);
$results['distribution']['platform'] = array(
'percentage' => 100 - $author_percentage,
'amount' => $platform_share
);
$results['summary'] = array(
'author_total' => $author_share,
'platform_total' => $platform_share,
'calculation_date' => current_time('mysql')
);
return $results;
}
/**
* 保存收益记录
*/
private function save_earning_record($post_id, $user_id, $total_amount, $user_share, $platform_share, $revenue_source) {
$data = array(
'post_id' => $post_id,
'user_id' => $user_id,
'earnings_date' => current_time('Y-m-d'),
'revenue_source' => $revenue_source,
'total_amount' => $total_amount,
'user_share' => $user_share,
'platform_share' => $platform_share,
'status' => 'calculated'
);
return $this->db->insert(FRS_EARNINGS_TABLE, $data);
}
/**
* 更新用户账户余额
*/
private function update_user_account($user_id, $amount) {
// 检查用户账户是否存在
$account_exists = $this->db->get_var(
$this->db->prepare(
"SELECT COUNT(*) FROM " . FRS_USER_ACCOUNTS_TABLE . " WHERE user_id = %d",
$user_id
)
);
if ($account_exists) {
// 更新现有账户
$this->db->query(
$this->db->prepare(
"UPDATE " . FRS_USER_ACCOUNTS_TABLE . "
SET pending_balance = pending_balance + %f,
total_earnings = total_earnings + %f,
last_calculated = %s
WHERE user_id = %d",
$amount,
$amount,
current_time('mysql'),
$user_id
)
);
} else {
// 创建新账户
$this->db->insert(
FRS_USER_ACCOUNTS_TABLE,
array(
'user_id' => $user_id,
'pending_balance' => $amount,
'total_earnings' => $amount,
'last_calculated' => current_time('mysql')
)
);
}
}
}
?>
3. 管理界面实现
<?php
/**
* 插件管理界面
*/
class FRS_Admin_Interface {
public function __construct() {
add_action('admin_menu', array($this, 'add_admin_menus'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* 添加管理菜单
*/
public function add_admin_menus() {
// 主菜单
add_menu_page(
'柔性收益分成',
'收益分成',
'manage_options',
'frs-dashboard',
array($this, 'render_dashboard'),
'dashicons-chart-pie',
30
);
// 子菜单
add_submenu_page(
'frs-dashboard',
'分成规则',
'分成规则',
'manage_options',
'frs-rules',
array($this, 'render_rules_page')
);
add_submenu_page(
'frs-dashboard',
'收益报表',
'收益报表',
'manage_options',
'frs-reports',
array($this, 'render_reports_page')
);
add_submenu_page(
'frs-dashboard',
'支付管理',
'支付管理',
'manage_options',
'frs-payments',
array($this, 'render_payments_page')
);
}
/**
* 渲染仪表盘页面
*/
public function render_dashboard() {
?>
<div class="wrap frs-admin-dashboard">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<div class="frs-dashboard-widgets">
<div class="frs-widget">
<h3>总收益概览</h3>
<div class="frs-widget-content">
<?php $this->display_earnings_overview(); ?>
</div>
</div>
<div class="frs-widget">
<h3>待处理支付</h3>
<div class="frs-widget-content">
<?php $this->display_pending_payments(); ?>
</div>
</div>
<div class="frs-widget">
<h3>最近收益记录</h3>
<div class="frs-widget-content">
<?php $this->display_recent_earnings(); ?>
</div>
</div>
</div>
</div>
<?php
}
/**
* 渲染规则管理页面
*/
public function render_rules_page() {
?>
<div class="wrap">
<h1>分成规则管理</h1>
<div class="frs-rules-actions">
<button class="button button-primary" id="frs-add-rule">
添加新规则
</button>
</div>
<div class="frs-rules-list">
<?php $this->display_rules_table(); ?>
</div>
<!-- 规则编辑模态框 -->
<div id="frs-rule-modal" class="frs-modal" style="display:none;">
<div class="frs-modal-content">
<span class="frs-close-modal">×</span>
<h2>编辑分成规则</h2>
<form id="frs-rule-form">
<!-- 规则表单字段 -->
<input type="hidden" id="rule_id" name="rule_id" value="0">
<table class="form-table">
<tr>
<th><label for="rule_name">规则名称</label></th>
<td><input type="text" id="rule_name" name="rule_name" class="regular-text" required></td>
</tr>
<tr>
<th><label for="target_user_role">目标用户角色</label></th>
<td>
<select id="target_user_role" name="target_user_role">
<?php
$editable_roles = get_editable_roles();
foreach ($editable_roles as $role => $details) {
echo '<option value="' . esc_attr($role) . '">' . esc_html($details['name']) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="base_percentage">基础分成比例 (%)</label></th>
<td><input type="number" id="base_percentage" name="base_percentage" min="0" max="100" step="0.01" required></td>
</tr>
<tr>
<th><label for="min_amount">最低金额限制</label></th>
<td><input type="number" id="min_amount" name="min_amount" min="0" step="0.01"></td>
</tr>
<tr>
<th><label for="max_amount">最高金额限制</label></th>
<td><input type="number" id="max_amount" name="max_amount" min="0" step="0.01"></td>
</tr>
<tr>
<th><label for="effective_from">生效日期</label></th>
<td><input type="date" id="effective_from" name="effective_from" required></td>
</tr>
<tr>
<th><label for="effective_to">失效日期</label></th>
<td><input type="date" id="effective_to" name="effective_to"></td>
</tr>
<tr>
<th><label for="is_active">是否激活</label></th>
<td><input type="checkbox" id="is_active" name="is_active" value="1" checked></td>
</tr>
</table>
<div class="frs-modal-footer">
<button type="submit" class="button button-primary">保存规则</button>
<button type="button" class="button frs-cancel-rule">取消</button>
</div>
</form>
</div>
</div>
</div>
<?php
}
/**
* 显示规则表格
*/
private function display_rules_table() {
global $wpdb;
$rules = $wpdb->get_results(
"SELECT * FROM " . FRS_RULES_TABLE . " ORDER BY created_at DESC",
ARRAY_A
);
if (empty($rules)) {
echo '<p>暂无分成规则,请添加第一条规则。</p>';
return;
}
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead>
<tr>
<th>规则名称</th>
<th>目标角色</th>
<th>分成比例</th>
<th>金额限制</th>
<th>有效期</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>';
echo '<tbody>';
foreach ($rules as $rule) {
echo '<tr>';
echo '<td>' . esc_html($rule['rule_name']) . '</td>';
echo '<td>' . esc_html($rule['target_user_role']) . '</td>';
echo '<td>' . esc_html($rule['base_percentage']) . '%</td>';
echo '<td>' . esc_html($rule['min_amount'] . ' - ' . ($rule['max_amount'] ?: '无限制')) . '</td>';
echo '<td>' . esc_html($rule['effective_from'] . ' 至 ' . ($rule['effective_to'] ?: '永久')) . '</td>';
echo '<td>' . ($rule['is_active'] ? '<span class="dashicons dashicons-yes" style="color:green;"></span> 激活' : '<span class="dashicons dashicons-no" style="color:red;"></span> 禁用') . '</td>';
echo '<td>
<button class="button button-small frs-edit-rule" data-rule-id="' . esc_attr($rule['rule_id']) . '">编辑</button>
<button class="button button-small frs-delete-rule" data-rule-id="' . esc_attr($rule['rule_id']) . '">删除</button>
</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
/**
* 加载管理脚本和样式
*/
public function enqueue_admin_scripts($hook) {
if (strpos($hook, 'frs-') === false) {
return;
}
wp_enqueue_style(
'frs-admin-style',
plugin_dir_url(__FILE__) . 'assets/css/admin-style.css',
array(),
'1.0.0'
);
wp_enqueue_script(
'frs-admin-script',
plugin_dir_url(__FILE__) . 'assets/js/admin-script.js',
array('jquery'),
'1.0.0',
true
);
wp_localize_script('frs-admin-script', 'frs_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('frs_admin_nonce')
));
}
}
?>
## 前端用户界面与短代码实现
<?php
/**
- 前端用户界面类
*/
class FRS_Frontend_Interface {
public function __construct() {
// 注册短代码
add_shortcode('frs_user_earnings', array($this, 'render_user_earnings_shortcode'));
add_shortcode('frs_withdrawal_form', array($this, 'render_withdrawal_form_shortcode'));
// 添加用户前端页面
add_action('init', array($this, 'add_user_endpoints'));
add_filter('query_vars', array($this, 'add_query_vars'));
add_action('template_redirect', array($this, 'handle_user_pages'));
}
/**
* 渲染用户收益短代码
*/
public function render_user_earnings_shortcode($atts) {
if (!is_user_logged_in()) {
return '<p>请先登录查看收益信息。</p>';
}
$user_id = get_current_user_id();
$atts = shortcode_atts(array(
'show_chart' => 'true',
'limit' => 10
), $atts);
ob_start();
?>
<div class="frs-user-earnings">
<h3>我的收益概览</h3>
<?php $this->display_user_earnings_summary($user_id); ?>
<?php if ($atts['show_chart'] === 'true') : ?>
<div class="frs-earnings-chart">
<canvas id="frsEarningsChart" width="400" height="200"></canvas>
</div>
<?php endif; ?>
<h4>最近收益记录</h4>
<?php $this->display_user_recent_earnings($user_id, $atts['limit']); ?>
</div>
<?php
return ob_get_clean();
}
/**
* 显示用户收益摘要
*/
private function display_user_earnings_summary($user_id) {
global $wpdb;
$account = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM " . FRS_USER_ACCOUNTS_TABLE . " WHERE user_id = %d",
$user_id
),
ARRAY_A
);
if (!$account) {
echo '<p>暂无收益数据。</p>';
return;
}
?>
<div class="frs-earnings-summary">
<div class="frs-summary-item">
<span class="frs-label">总收益:</span>
<span class="frs-value">¥<?php echo number_format($account['total_earnings'], 2); ?></span>
</div>
<div class="frs-summary-item">
<span class="frs-label">待结算:</span>
<span class="frs-value">¥<?php echo number_format($account['pending_balance'], 2); ?></span>
</div>
<div class="frs-summary-item">
<span class="frs-label">可提现:</span>
<span class="frs-value">¥<?php echo number_format($account['available_balance'], 2); ?></span>
</div>
<div class="frs-summary-item">
<span class="frs-label">已提现:</span>
<span class="frs-value">¥<?php echo number_format($account['total_withdrawn'], 2); ?></span>
</div>
</div>
<?php
}
/**
* 显示用户最近收益记录
*/
private function display_user_recent_earnings($user_id, $limit = 10) {
global $wpdb;
$earnings = $wpdb->get_results(
$wpdb->prepare(
"SELECT e.*, p.post_title
FROM " . FRS_EARNINGS_TABLE . " e
LEFT JOIN {$wpdb->posts} p ON e.post_id = p.ID
WHERE e.user_id = %d
ORDER BY e.calculated_at DESC
LIMIT %d",
$user_id,
$limit
),
ARRAY_A
);
if (empty($earnings)) {
echo '<p>暂无收益记录。</p>';
return;
}
echo '<table class="frs-earnings-table">';
echo '<thead>
<tr>
<th>日期</th>
<th>文章</th>
<th>收益来源</th>
<th>总金额</th>
<th>我的分成</th>
<th>状态</th>
</tr>
</thead>';
echo '<tbody>';
foreach ($earnings as $earning) {
echo '<tr>';
echo '<td>' . esc_html($earning['earnings_date']) . '</td>';
echo '<td>' . esc_html($earning['post_title'] ?: '未知文章') . '</td>';
echo '<td>' . esc_html($earning['revenue_source']) . '</td>';
echo '<td>¥' . number_format($earning['total_amount'], 2) . '</td>';
echo '<td>¥' . number_format($earning['user_share'], 2) . '</td>';
echo '<td>';
switch ($earning['status']) {
case 'paid':
echo '<span class="frs-status-paid">已支付</span>';
break;
case 'pending':
echo '<span class="frs-status-pending">待支付</span>';
break;
default:
echo esc_html($earning['status']);
}
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
/**
* 渲染提现表单短代码
*/
public function render_withdrawal_form_shortcode($atts) {
if (!is_user_logged_in()) {
return '<p>请先登录进行提现操作。</p>';
}
$user_id = get_current_user_id();
$account = $this->get_user_account($user_id);
if (!$account || $account['available_balance'] <= 0) {
return '<p>当前无可提现金额。</p>';
}
ob_start();
?>
<div class="frs-withdrawal-form">
<h3>收益提现</h3>
<div class="frs-available-balance">
可提现金额:<strong>¥<?php echo number_format($account['available_balance'], 2); ?></strong>
</div>
<form id="frsWithdrawalForm" method="post">
<?php wp_nonce_field('frs_withdrawal_request', 'frs_withdrawal_nonce'); ?>
<div class="frs-form-group">
<label for="withdrawal_amount">提现金额:</label>
<input type="number"
id="withdrawal_amount"
name="withdrawal_amount"
min="1"
max="<?php echo esc_attr($account['available_balance']); ?>"
step="0.01"
required>
<small>最小提现金额:¥1.00</small>
</div>
<div class="frs-form-group">
<label for="payment_method">收款方式:</label>
<select id="payment_method" name="payment_method" required>
<option value="">请选择收款方式</option>
<option value="alipay">支付宝</option>
<option value="wechat_pay">微信支付</option>
<option value="bank_transfer">银行转账</option>
</select>
</div>
<div class="frs-form-group" id="payment_details_section" style="display:none;">
<label for="payment_details">收款账号:</label>
<input type="text" id="payment_details" name="payment_details">
</div>
<div class="frs-form-group">
<button type="submit" class="button button-primary">提交提现申请</button>
</div>
</form>
<div id="frsWithdrawalResult" style="display:none;"></div>
</div>
<script>
jQuery(document).ready(function($) {
// 显示/隐藏收款账号输入框
$('#payment_method').change(function() {
if ($(this).val()) {
$('#payment_details_section').show();
} else {
$('#payment_details_section').hide();
}
});
// 提现表单提交
$('#frsWithdrawalForm').submit(function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: formData + '&action=frs_process_withdrawal',
beforeSend: function() {
$('#frsWithdrawalResult').html('<p>处理中...</p>').show();
},
success: function(response) {
if (response.success) {
$('#frsWithdrawalResult').html(
'<div class="notice notice-success"><p>' + response.data.message + '</p></div>'
);
$('#frsWithdrawalForm')[0].reset();
setTimeout(function() {
location.reload();
}, 2000);
} else {
$('#frsWithdrawalResult').html(
'<div class="notice notice-error"><p>' + response.data.message + '</p></div>'
);
}
}
});
});
});
</script>
<?php
return ob_get_clean();
}
/**
* 获取用户账户信息
*/
private function get_user_account($user_id) {
global $wpdb;
return $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM " . FRS_USER_ACCOUNTS_TABLE . " WHERE user_id = %d",
$user_id
),
ARRAY_A
);
}
}
?>
## 支付处理与Webhook集成
<?php
/**
- 支付处理类
*/
class FRS_Payment_Processor {
private $db;
public function __construct() {
global $wpdb;
$this->db = $wpdb;
// 注册AJAX处理
add_action('wp_ajax_frs_process_withdrawal', array($this, 'process_withdrawal_request'));
add_action('wp_ajax_nopriv_frs_webhook', array($this, 'handle_payment_webhook'));
}
/**
* 处理提现请求
*/
public function process_withdrawal_request() {
// 验证nonce
if (!isset($_POST['frs_withdrawal_nonce']) ||
!wp_verify_nonce($_POST['frs_withdrawal_nonce'], 'frs_withdrawal_request')) {
wp_send_json_error(array('message' => '安全验证失败'));
}
// 验证用户登录
if (!is_user_logged_in()) {
wp_send_json_error(array('message' => '请先登录'));
}
$user_id = get_current_user_id();
$amount = floatval($_POST['withdrawal_amount']);
$payment_method = sanitize_text_field($_POST['payment_method']);
$payment_details = sanitize_text_field($_POST['payment_details']);
// 验证金额
if ($amount <= 0) {
wp_send_json_error(array('message' => '提现金额必须大于0'));
}
// 获取用户账户
$account = $this->db->get_row(
$this->db->prepare(
"SELECT * FROM " . FRS_USER_ACCOUNTS_TABLE . " WHERE user_id = %d",
$user_id
),
ARRAY_A
);
if (!$account || $account['available_balance'] < $amount) {
wp_send_json_error(array('message' => '可提现金额不足'));
}
// 创建提现记录
$withdrawal_id = $this->create_withdrawal_record(
$user_id,
$amount,
$payment_method,
$payment_details
);
if ($withdrawal_id) {
// 更新用户账户
$this->update_account_for_withdrawal($user_id, $amount);
// 发送通知
$this->send_withdrawal_notification($user_id, $amount);
wp_send_json_success(array(
'
