WeFund WordPress Plugin API

Complete developer guide for integrating with WeFund's trading platform

Developer Integration Guide v1.0

Overview

What This Guide Covers:
  • JWT token generation and management
  • Universal order processing for any WordPress plugin
  • Security best practices and error handling
  • Complete code examples for WooCommerce and other plugins

Authentication System

Important Security Feature:

Each JWT token can only be used ONCE for order processing. This prevents replay attacks and ensures maximum security.

Step 1: Generate JWT Token

Use your API key (provided by WeFund) to generate JWT tokens:

PHP Code Example
<?php
function generate_wefund_jwt_token() {
    $api_key = get_option('wefund_api_key'); // Store securely in WordPress
    $api_url = 'https://api.we-fund.com/plugin/generate-token/';
    
    $data = array(
        'plugin_name' => 'Your Plugin Name',
        'plugin_key' => $api_key,
        'plugin_version' => '1.0.0',
        'site_url' => get_site_url(),
        'token_duration_hours' => 24
    );
    
    $response = wp_remote_post($api_url, array(
        'headers' => array('Content-Type' => 'application/json'),
        'body' => json_encode($data),
        'timeout' => 60
    ));
    
    if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
        $result = json_decode(wp_remote_retrieve_body($response), true);
        
        // Store tokens securely
        update_option('wefund_jwt_token', $result['jwt_token']);
        update_option('wefund_refresh_token', $result['refresh_token']);
        
        return $result['jwt_token'];
    }
    
    return false;
}
?>
Step 2: Process Orders

Send order data to WeFund API with JWT token in payload:

PHP Code Example
<?php
function process_wefund_order($order_data) {
    // Generate fresh JWT token for each order (required for security)
    $jwt_token = generate_wefund_jwt_token();
    
    if (!$jwt_token) {
        error_log('Failed to generate JWT token for WeFund order processing');
        return false;
    }
    
    $api_url = 'https://your-wefund-domain.com/api/plugin/order/process/';
    
    $payload = array(
        'jwt_token' => $jwt_token,  // JWT in payload (not header)
        'plugin_name' => 'Your Plugin Name',
        'order_data' => $order_data,
        'processing_options' => array(
            'create_mt5_account' => true,
            'send_email' => true,
            'create_user' => true
        )
    );
    
    $response = wp_remote_post($api_url, array(
        'headers' => array('Content-Type' => 'application/json'),
        'body' => json_encode($payload),
        'timeout' => 120  // Longer timeout for processing
    ));
    
    $status_code = wp_remote_retrieve_response_code($response);
    
    if (!is_wp_error($response) && ($status_code === 200 || $status_code === 201)) {
        $result = json_decode(wp_remote_retrieve_body($response), true);
        error_log('WeFund order processed successfully: ' . $result['order_id']);
        return $result;
    }
    
    // Handle errors
    if ($status_code === 401) {
        error_log('WeFund JWT token expired or invalid - will refresh on next request');
    } else {
        error_log('WeFund order processing failed: ' . wp_remote_retrieve_body($response));
    }
    
    return false;
}
?>

Plugin Integrations

WooCommerce Integration
PHP Code Example
<?php
// Hook into WooCommerce order completion
add_action('woocommerce_order_status_completed', 'process_woo_order_for_wefund');

function process_woo_order_for_wefund($order_id) {
    $order = wc_get_order($order_id);
    
    if (!$order) {
        return;
    }
    
    // Prepare order data in WeFund format
    $order_data = array(
        'order_id' => $order->get_id(),
        'total' => $order->get_total(),
        'currency' => $order->get_currency(),
        'status' => $order->get_status(),
        'customer' => array(
            'email' => $order->get_billing_email(),
            'first_name' => $order->get_billing_first_name(),
            'last_name' => $order->get_billing_last_name(),
            'phone' => $order->get_billing_phone(),
            'country' => $order->get_billing_country()
        ),
        'products' => array()
    );
    
    // Add product information
    foreach ($order->get_items() as $item) {
        $product = $item->get_product();
        $order_data['products'][] = array(
            'name' => $item->get_name(),
            'quantity' => $item->get_quantity(),
            'price' => $item->get_total(),
            'account_size' => $product->get_meta('account_size', true) ?: '10000',
            'challenge_type' => $product->get_meta('challenge_type', true) ?: 'standard'
        );
    }
    
    // Process order with WeFund API
    $result = process_wefund_order($order_data);
    
    if ($result) {
        // Store WeFund order ID for reference
        $order->update_meta_data('wefund_order_id', $result['order_id']);
        $order->update_meta_data('wefund_webhook_id', $result['webhook_id']);
        $order->save();
        
        $order->add_order_note('WeFund order processed successfully. Order ID: ' . $result['order_id']);
    } else {
        $order->add_order_note('WeFund order processing failed. Please check logs.');
    }
}
?>
Easy Digital Downloads Integration
PHP Code Example
<?php
// Hook into EDD purchase completion
add_action('edd_complete_purchase', 'process_edd_order_for_wefund');

function process_edd_order_for_wefund($payment_id) {
    $payment = new EDD_Payment($payment_id);
    
    $order_data = array(
        'order_id' => $payment->ID,
        'total' => $payment->total,
        'currency' => $payment->currency,
        'customer' => array(
            'email' => $payment->email,
            'first_name' => $payment->first_name,
            'last_name' => $payment->last_name
        ),
        'products' => array()
    );
    
    // Add download information
    foreach ($payment->downloads as $download) {
        $download_id = $download['id'];
        $download_obj = new EDD_Download($download_id);
        
        $order_data['products'][] = array(
            'name' => $download_obj->post_title,
            'price' => $download['price'],
            'account_size' => get_post_meta($download_id, 'account_size', true) ?: '10000'
        );
    }
    
    $result = process_wefund_order($order_data);
    
    if ($result) {
        $payment->update_meta('wefund_order_id', $result['order_id']);
        $payment->add_note('WeFund order processed successfully.');
    }
}
?>

Security Best Practices

Security Checklist:
  • Generate fresh JWT tokens for each order (required by single-use system)
  • Store API keys securely in WordPress options, never in code
  • Use HTTPS for all API communications
  • Implement proper error logging without exposing sensitive data
  • Validate all data before sending to API
  • Handle token refresh automatically on 401 errors

API Base Endpoint

Base URL: https://api.we-fund.com/

API Endpoints

Endpoint Method Purpose Authentication
plugin/generate-token/ POST Generate JWT tokens API Key
/plugin/refresh-token/ POST Refresh expired tokens Refresh Token
/plugin/order/process/ POST Process orders JWT Token
/plugin/health/ GET API health check None
/plugin/docs/ GET Interactive documentation None
/plugin/postman-collection/ GET Download Postman collection None

Testing Your Integration

Test Order Data Format
JSON Example
{
    "order_id": 12345,
    "status": "completed",
    "total": "99.00",
    "currency": "USD",
    "customer": {
        "email": "test@example.com",
        "first_name": "John",
        "last_name": "Doe",
        "phone": "+1234567890",
        "country": "US"
    },
    "products": [
        {
            "name": "Challenge Account - $10,000",
            "account_size": "10000",
            "challenge_type": "standard",
            "price": "99.00"
        }
    ]
}

Error Handling

Error Handling Example
PHP Code Example
<?php
function handle_wefund_api_error($response, $context = 'API Call') {
    if (is_wp_error($response)) {
        error_log("WeFund API Error ({$context}): " . $response->get_error_message());
        return false;
    }
    
    $status_code = wp_remote_retrieve_response_code($response);
    
    switch ($status_code) {
        case 200:
        case 201:
            return true;
        case 401:
            error_log("WeFund API: Token expired, will refresh on next request");
            // Trigger token refresh
            delete_option('wefund_jwt_token');
            return false;
        case 429:
            error_log("WeFund API: Rate limited, please retry later");
            return false;
        default:
            error_log("WeFund API Error ({$context}): HTTP {$status_code}");
            return false;
    }
}
?>

Support & Resources

Available Resources:
  • Interactive API Documentation: /api/plugin/docs/
  • Postman Collection: /api/plugin/postman-collection/
  • API Health Check: /api/plugin/health/
  • Test Endpoint: /api/plugin/test/

Quick Start Summary

  1. Get API Key: Request from WeFund team
  2. Generate JWT Token: Use generate_wefund_jwt_token()
  3. Process Orders: Call process_wefund_order($order_data)
  4. Handle Responses: Check for success and log results
  5. Test Integration: Use provided test data
Remember:

Each JWT token can only be used once. Generate a fresh token for every order processing request!