feat(affiliate): add core module, controllers, and route registration
This commit is contained in:
364
includes/Api/Controllers/AffiliateAdminController.php
Normal file
364
includes/Api/Controllers/AffiliateAdminController.php
Normal file
@@ -0,0 +1,364 @@
|
||||
<?php
|
||||
|
||||
namespace WooNooW\Api\Controllers;
|
||||
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Server;
|
||||
use WooNooW\Core\ModuleRegistry;
|
||||
|
||||
class AffiliateAdminController
|
||||
{
|
||||
private $namespace = 'woonoow/v1';
|
||||
|
||||
public function register_routes()
|
||||
{
|
||||
// List Affiliates
|
||||
register_rest_route($this->namespace, '/admin/affiliates', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_affiliates'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// Get Affiliate Balance (payable amount)
|
||||
register_rest_route($this->namespace, '/admin/affiliates/(?P<id>\d+)/balance', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_affiliate_balance'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// Approve Affiliate
|
||||
register_rest_route($this->namespace, '/admin/affiliates/(?P<id>\d+)/approve', [
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'approve_affiliate'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// Update Affiliate (commission rate)
|
||||
register_rest_route($this->namespace, '/admin/affiliates/(?P<id>\d+)/update', [
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'update_affiliate'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// List Referrals
|
||||
register_rest_route($this->namespace, '/admin/affiliates/referrals', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_referrals'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// List Payouts (for all affiliates)
|
||||
register_rest_route($this->namespace, '/admin/affiliates/payouts', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_payouts'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// Create Payout
|
||||
register_rest_route($this->namespace, '/admin/affiliates/payouts', [
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'create_payout'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function check_permission()
|
||||
{
|
||||
return current_user_can('manage_woocommerce');
|
||||
}
|
||||
|
||||
public function get_affiliates(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
$affiliates = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC", ARRAY_A);
|
||||
|
||||
// Add payable_balance to each affiliate
|
||||
foreach ($affiliates as &$affiliate) {
|
||||
$affiliate['payable_balance'] = (float) ($affiliate['total_earnings'] ?? 0) - (float) ($affiliate['paid_earnings'] ?? 0);
|
||||
// Get user info
|
||||
$user = get_userdata($affiliate['user_id']);
|
||||
if ($user) {
|
||||
$affiliate['user_email'] = $user->user_email;
|
||||
$affiliate['user_name'] = $user->display_name;
|
||||
}
|
||||
}
|
||||
|
||||
return rest_ensure_response($affiliates);
|
||||
}
|
||||
|
||||
public function get_affiliate_balance(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$id = $request->get_param('id');
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT id, user_id, referral_code, total_earnings, paid_earnings, total_referrals
|
||||
FROM $table WHERE id = %d",
|
||||
$id
|
||||
));
|
||||
|
||||
if (!$affiliate) {
|
||||
return new WP_REST_Response(['error' => 'Affiliate not found'], 404);
|
||||
}
|
||||
|
||||
$user = get_userdata($affiliate->user_id);
|
||||
|
||||
return rest_ensure_response([
|
||||
'id' => (int) $affiliate->id,
|
||||
'user_id' => (int) $affiliate->user_id,
|
||||
'user_name' => $user ? $user->display_name : 'Unknown',
|
||||
'user_email' => $user ? $user->user_email : '',
|
||||
'referral_code' => $affiliate->referral_code,
|
||||
'total_earnings' => (float) $affiliate->total_earnings,
|
||||
'paid_earnings' => (float) $affiliate->paid_earnings,
|
||||
'payable_balance' => (float) $affiliate->total_earnings - (float) $affiliate->paid_earnings,
|
||||
'total_referrals' => (int) $affiliate->total_referrals,
|
||||
'approved_referrals' => $this->get_approved_referral_count($affiliate->id),
|
||||
'pending_referrals' => $this->get_pending_referral_count($affiliate->id),
|
||||
]);
|
||||
}
|
||||
|
||||
private function get_approved_referral_count($affiliate_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$wpdb->prefix}woonoow_referrals WHERE affiliate_id = %d AND status = 'approved'",
|
||||
$affiliate_id
|
||||
));
|
||||
}
|
||||
|
||||
private function get_pending_referral_count($affiliate_id)
|
||||
{
|
||||
global $wpdb;
|
||||
return (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$wpdb->prefix}woonoow_referrals WHERE affiliate_id = %d AND status = 'pending'",
|
||||
$affiliate_id
|
||||
));
|
||||
}
|
||||
|
||||
public function approve_affiliate(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$id = $request->get_param('id');
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$wpdb->update(
|
||||
$table,
|
||||
['status' => 'active'],
|
||||
['id' => $id]
|
||||
);
|
||||
|
||||
// Trigger email notification for approval
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE id = %d", $id));
|
||||
if ($affiliate) {
|
||||
$user = get_userdata($affiliate->user_id);
|
||||
if ($user) {
|
||||
do_action('woonoow/email/trigger', 'affiliate_application_approved', $user->user_email, [
|
||||
'affiliate_name' => $user->display_name,
|
||||
'referral_code' => $affiliate->referral_code
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return rest_ensure_response(['success' => true]);
|
||||
}
|
||||
|
||||
public function update_affiliate(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$id = $request->get_param('id');
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$custom_rate = $request->get_param('custom_commission_rate');
|
||||
|
||||
// If rate is empty string or not provided, clear custom rate
|
||||
if ($custom_rate === '' || $custom_rate === null || $custom_rate === false) {
|
||||
$data = ['custom_commission_rate' => null];
|
||||
} else {
|
||||
$custom_rate = floatval($custom_rate);
|
||||
if ($custom_rate < 0 || $custom_rate > 100) {
|
||||
return new WP_REST_Response(['error' => 'Commission rate must be between 0 and 100'], 400);
|
||||
}
|
||||
$data = ['custom_commission_rate' => $custom_rate];
|
||||
}
|
||||
|
||||
$result = $wpdb->update(
|
||||
$table,
|
||||
$data,
|
||||
['id' => $id]
|
||||
);
|
||||
|
||||
if ($result === false) {
|
||||
return new WP_REST_Response(['error' => 'Failed to update affiliate'], 500);
|
||||
}
|
||||
|
||||
return rest_ensure_response(['success' => true, 'custom_commission_rate' => $data['custom_commission_rate']]);
|
||||
}
|
||||
|
||||
public function get_referrals(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'woonoow_referrals';
|
||||
|
||||
// Add filter support
|
||||
$where = "1=1";
|
||||
|
||||
$affiliate_id = $request->get_param('affiliate_id');
|
||||
if ($affiliate_id) {
|
||||
$where .= $wpdb->prepare(" AND affiliate_id = %d", $affiliate_id);
|
||||
}
|
||||
|
||||
$status = $request->get_param('status');
|
||||
if ($status) {
|
||||
$where .= $wpdb->prepare(" AND status = %s", $status);
|
||||
}
|
||||
|
||||
$date_start = $request->get_param('date_start');
|
||||
if ($date_start) {
|
||||
$where .= $wpdb->prepare(" AND created_at >= %s", $date_start . ' 00:00:00');
|
||||
}
|
||||
|
||||
$date_end = $request->get_param('date_end');
|
||||
if ($date_end) {
|
||||
$where .= $wpdb->prepare(" AND created_at <= %s", $date_end . ' 23:59:59');
|
||||
}
|
||||
|
||||
$order_id = $request->get_param('order_id');
|
||||
if ($order_id) {
|
||||
$where .= $wpdb->prepare(" AND order_id = %d", $order_id);
|
||||
}
|
||||
|
||||
$referrals = $wpdb->get_results("SELECT * FROM $table WHERE $where ORDER BY created_at DESC", ARRAY_A);
|
||||
|
||||
// Enrich with affiliate info
|
||||
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
foreach ($referrals as &$referral) {
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT a.*, u.display_name as affiliate_name, u.user_email as affiliate_email
|
||||
FROM $affiliates_table a
|
||||
LEFT JOIN $wpdb->users u ON a.user_id = u.ID
|
||||
WHERE a.id = %d",
|
||||
$referral['affiliate_id']
|
||||
));
|
||||
if ($affiliate) {
|
||||
$referral['affiliate_name'] = $affiliate->affiliate_name;
|
||||
$referral['affiliate_email'] = $affiliate->affiliate_email;
|
||||
}
|
||||
}
|
||||
|
||||
return rest_ensure_response($referrals);
|
||||
}
|
||||
|
||||
public function get_payouts(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'woonoow_affiliate_payouts';
|
||||
|
||||
$affiliate_id = $request->get_param('affiliate_id');
|
||||
$where = $affiliate_id ? $wpdb->prepare("WHERE affiliate_id = %d", $affiliate_id) : "";
|
||||
|
||||
$payouts = $wpdb->get_results("SELECT * FROM $table $where ORDER BY created_at DESC", ARRAY_A);
|
||||
|
||||
// Enrich with affiliate info
|
||||
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
foreach ($payouts as &$payout) {
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT a.*, u.display_name as affiliate_name, u.user_email as affiliate_email
|
||||
FROM $affiliates_table a
|
||||
LEFT JOIN $wpdb->users u ON a.user_id = u.ID
|
||||
WHERE a.id = %d",
|
||||
$payout['affiliate_id']
|
||||
));
|
||||
if ($affiliate) {
|
||||
$payout['affiliate_name'] = $affiliate->affiliate_name;
|
||||
$payout['affiliate_email'] = $affiliate->affiliate_email;
|
||||
}
|
||||
}
|
||||
|
||||
return rest_ensure_response($payouts);
|
||||
}
|
||||
|
||||
public function create_payout(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$payouts_table = $wpdb->prefix . 'woonoow_affiliate_payouts';
|
||||
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$affiliate_id = absint($request->get_param('affiliate_id'));
|
||||
$amount = floatval($request->get_param('amount'));
|
||||
$method = sanitize_text_field($request->get_param('method') ?: 'bank_transfer');
|
||||
$notes = '';
|
||||
|
||||
// Validate affiliate exists and get balance
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM $affiliates_table WHERE id = %d",
|
||||
$affiliate_id
|
||||
));
|
||||
|
||||
if (!$affiliate) {
|
||||
return new WP_REST_Response(['error' => 'Affiliate not found'], 404);
|
||||
}
|
||||
|
||||
$payable_balance = (float) $affiliate->total_earnings - (float) $affiliate->paid_earnings;
|
||||
|
||||
if ($amount <= 0) {
|
||||
return new WP_REST_Response(['error' => 'Amount must be greater than 0'], 400);
|
||||
}
|
||||
|
||||
if ($amount > $payable_balance) {
|
||||
return new WP_REST_Response([
|
||||
'error' => 'Amount exceeds payable balance',
|
||||
'payable_balance' => $payable_balance
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Generate coupon for store_credit method
|
||||
if ($method === 'store_credit') {
|
||||
$user = get_userdata($affiliate->user_id);
|
||||
if ($user) {
|
||||
$coupon_code = 'CREDIT-' . strtoupper(wp_generate_password(8, false));
|
||||
$coupon = new \WC_Coupon();
|
||||
$coupon->set_code($coupon_code);
|
||||
$coupon->set_discount_type('fixed_cart');
|
||||
$coupon->set_amount($amount);
|
||||
$coupon->set_email_restrictions([$user->user_email]);
|
||||
$coupon->set_usage_limit(1);
|
||||
$coupon->set_description('Store Credit for Affiliate Payout');
|
||||
$coupon->save();
|
||||
|
||||
$notes = 'Generated Store Credit Coupon: ' . $coupon_code;
|
||||
}
|
||||
}
|
||||
|
||||
// Create payout record
|
||||
$wpdb->insert($payouts_table, [
|
||||
'affiliate_id' => $affiliate_id,
|
||||
'amount' => $amount,
|
||||
'currency' => get_woocommerce_currency(),
|
||||
'method' => $method,
|
||||
'status' => 'completed',
|
||||
'notes' => $notes,
|
||||
'completed_at' => current_time('mysql')
|
||||
]);
|
||||
|
||||
$payout_id = $wpdb->insert_id;
|
||||
|
||||
// Update affiliate's paid_earnings
|
||||
$wpdb->query($wpdb->prepare(
|
||||
"UPDATE $affiliates_table SET paid_earnings = paid_earnings + %f WHERE id = %d",
|
||||
$amount,
|
||||
$affiliate_id
|
||||
));
|
||||
|
||||
return rest_ensure_response([
|
||||
'success' => true,
|
||||
'id' => $payout_id,
|
||||
'new_paid_earnings' => (float) $affiliate->paid_earnings + $amount,
|
||||
'new_payable_balance' => $payable_balance - $amount,
|
||||
'coupon_code' => $method === 'store_credit' ? $coupon_code : null
|
||||
]);
|
||||
}
|
||||
}
|
||||
273
includes/Api/Controllers/AffiliateCustomerController.php
Normal file
273
includes/Api/Controllers/AffiliateCustomerController.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace WooNooW\Api\Controllers;
|
||||
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Server;
|
||||
|
||||
class AffiliateCustomerController
|
||||
{
|
||||
private $namespace = 'woonoow/v1';
|
||||
|
||||
public function register_routes()
|
||||
{
|
||||
register_rest_route($this->namespace, '/account/affiliate', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_dashboard'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
register_rest_route($this->namespace, '/account/affiliate/apply', [
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'apply_affiliate'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
register_rest_route($this->namespace, '/account/affiliate/referrals', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_referrals'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
register_rest_route($this->namespace, '/account/affiliate/payouts', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_payouts'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
register_rest_route($this->namespace, '/account/affiliate/payment-details', [
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => [$this, 'get_payment_details'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
register_rest_route($this->namespace, '/account/affiliate/payment-details', [
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'update_payment_details'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function check_permission()
|
||||
{
|
||||
return is_user_logged_in();
|
||||
}
|
||||
|
||||
public function get_dashboard(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table WHERE user_id = %d", $user_id), ARRAY_A);
|
||||
|
||||
if (!$affiliate) {
|
||||
return new \WP_Error('not_found', 'Affiliate profile not found', ['status' => 404]);
|
||||
}
|
||||
|
||||
// Get global default rate
|
||||
$global_rate = (float) get_option('woonoow_affiliate_default_rate', 10);
|
||||
|
||||
// Use custom rate if set, otherwise global
|
||||
$effective_rate = !empty($affiliate['custom_commission_rate'])
|
||||
? (float) $affiliate['custom_commission_rate']
|
||||
: $global_rate;
|
||||
|
||||
$affiliate['global_commission_rate'] = $global_rate;
|
||||
$affiliate['commission_rate'] = $effective_rate;
|
||||
|
||||
return rest_ensure_response($affiliate);
|
||||
}
|
||||
|
||||
public function apply_affiliate(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
// Check if already applied
|
||||
$exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM $table WHERE user_id = %d", $user_id));
|
||||
if ($exists) {
|
||||
return new \WP_Error('exists', 'You have already applied.', ['status' => 400]);
|
||||
}
|
||||
|
||||
// Generate simple code
|
||||
$user = wp_get_current_user();
|
||||
$referral_code = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $user->user_login)) . wp_generate_password(4, false);
|
||||
|
||||
$auto_approve = get_option('woonoow_affiliate_auto_approve', false);
|
||||
$status = $auto_approve ? 'active' : 'pending';
|
||||
|
||||
$wpdb->insert($table, [
|
||||
'user_id' => $user_id,
|
||||
'referral_code' => $referral_code,
|
||||
'commission_rate' => get_option('woonoow_affiliate_default_rate', 10), // 10% default
|
||||
'status' => $status
|
||||
]);
|
||||
|
||||
// Trigger email notification for admin
|
||||
$admin_email = get_option('admin_email');
|
||||
do_action('woonoow/email/trigger', 'affiliate_application_received', $admin_email, [
|
||||
'affiliate_name' => $user->display_name,
|
||||
'customer_email' => $user->user_email
|
||||
]);
|
||||
|
||||
if ($auto_approve) {
|
||||
do_action('woonoow/email/trigger', 'affiliate_application_approved', $user->user_email, [
|
||||
'affiliate_name' => $user->display_name,
|
||||
'customer_email' => $user->user_email,
|
||||
'referral_code' => $referral_code
|
||||
]);
|
||||
}
|
||||
|
||||
return rest_ensure_response(['success' => true, 'status' => $status, 'referral_code' => $referral_code]);
|
||||
}
|
||||
|
||||
public function get_referrals(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
$referrals_table = $wpdb->prefix . 'woonoow_referrals';
|
||||
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id FROM $affiliates_table WHERE user_id = %d", $user_id));
|
||||
if (!$affiliate) {
|
||||
return rest_ensure_response([]);
|
||||
}
|
||||
|
||||
$referrals = $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT r.*,
|
||||
COALESCE(NULLIF(r.cancelled_reason, ''), NULL) as cancelled_reason,
|
||||
COALESCE(r.approved_at, r.created_at) as approved_at
|
||||
FROM $referrals_table r
|
||||
WHERE r.affiliate_id = %d
|
||||
ORDER BY r.created_at DESC",
|
||||
$affiliate->id
|
||||
), ARRAY_A);
|
||||
return rest_ensure_response($referrals);
|
||||
}
|
||||
|
||||
public function get_payouts(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
$payouts_table = $wpdb->prefix . 'woonoow_affiliate_payouts';
|
||||
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id FROM $affiliates_table WHERE user_id = %d", $user_id));
|
||||
if (!$affiliate) {
|
||||
return rest_ensure_response([]);
|
||||
}
|
||||
|
||||
$payouts = $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT id, amount, currency, method, status, notes, created_at, completed_at
|
||||
FROM $payouts_table
|
||||
WHERE affiliate_id = %d
|
||||
ORDER BY created_at DESC",
|
||||
$affiliate->id
|
||||
), ARRAY_A);
|
||||
|
||||
return rest_ensure_response($payouts);
|
||||
}
|
||||
|
||||
public function get_payment_details(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
$affiliate = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT payment_method, payment_details FROM $table WHERE user_id = %d",
|
||||
$user_id
|
||||
));
|
||||
|
||||
if (!$affiliate) {
|
||||
return new \WP_Error('not_found', 'Affiliate not found', ['status' => 404]);
|
||||
}
|
||||
|
||||
$payment_details = $affiliate->payment_details ? json_decode($affiliate->payment_details, true) : [];
|
||||
|
||||
return rest_ensure_response([
|
||||
'payment_method' => $affiliate->payment_method ?: '',
|
||||
'payment_details' => $payment_details ?: new \stdClass()
|
||||
]);
|
||||
}
|
||||
|
||||
public function update_payment_details(WP_REST_Request $request)
|
||||
{
|
||||
global $wpdb;
|
||||
$user_id = get_current_user_id();
|
||||
$table = $wpdb->prefix . 'woonoow_affiliates';
|
||||
|
||||
// Get allowed payment methods from settings
|
||||
$settings = get_option('woonoow_module_affiliate_settings', []);
|
||||
$allowed_methods = $settings['woonoow_affiliate_payment_methods'] ?? ['bank_transfer'];
|
||||
|
||||
$payment_method = sanitize_text_field($request->get_param('payment_method') ?: '');
|
||||
$payment_details_raw = $request->get_param('payment_details') ?: [];
|
||||
|
||||
// Validate payment method is allowed
|
||||
if (!in_array($payment_method, $allowed_methods)) {
|
||||
return new \WP_Error(
|
||||
'invalid_payment_method',
|
||||
'This payment method is not available. Please contact admin.',
|
||||
['status' => 400]
|
||||
);
|
||||
}
|
||||
|
||||
// Sanitize payment details based on method
|
||||
$sanitized_details = self::sanitize_payment_details($payment_method, $payment_details_raw);
|
||||
|
||||
$result = $wpdb->update(
|
||||
$table,
|
||||
[
|
||||
'payment_method' => $payment_method,
|
||||
'payment_details' => json_encode($sanitized_details)
|
||||
],
|
||||
['user_id' => $user_id]
|
||||
);
|
||||
|
||||
if ($result === false) {
|
||||
return new \WP_Error('update_failed', 'Failed to update payment details', ['status' => 500]);
|
||||
}
|
||||
|
||||
return rest_ensure_response([
|
||||
'success' => true,
|
||||
'payment_method' => $payment_method,
|
||||
'payment_details' => $sanitized_details
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize payment details based on payment method
|
||||
*/
|
||||
private static function sanitize_payment_details($method, $details)
|
||||
{
|
||||
$sanitized = [];
|
||||
|
||||
switch ($method) {
|
||||
case 'bank_transfer':
|
||||
$sanitized['bank_name'] = sanitize_text_field($details['bank_name'] ?? '');
|
||||
$sanitized['account_number'] = sanitize_text_field($details['account_number'] ?? '');
|
||||
$sanitized['account_holder'] = sanitize_text_field($details['account_holder'] ?? '');
|
||||
$sanitized['swift_code'] = sanitize_text_field($details['swift_code'] ?? '');
|
||||
$sanitized['bank_address'] = sanitize_text_field($details['bank_address'] ?? '');
|
||||
break;
|
||||
|
||||
case 'paypal':
|
||||
case 'wise':
|
||||
case 'skrill':
|
||||
case 'payoneer':
|
||||
$sanitized['email'] = sanitize_email($details['email'] ?? '');
|
||||
$sanitized['name'] = sanitize_text_field($details['name'] ?? '');
|
||||
break;
|
||||
|
||||
case 'custom':
|
||||
$sanitized['notes'] = sanitize_textarea_field($details['notes'] ?? '');
|
||||
break;
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user