WorldPay API integration with WooCommerce

70 Views Asked by At

I was hoping to use a plugin to integrate WorldPay with WooCommerce however the client seems to have a version of WorldPay that only works with API.

I've been using AI help but it's not getting me anywhere - does anyone have experience of creating a plugin to integrate WorldPay's API with WooCommerce?

The function and plugin code I have so far are below. I'm not a developer!

// Add Worldpay as a payment gateway
// Define the Worldpay payment gateway class if it's not already defined
if (!class_exists('WC_Gateway_Worldpay')) {
    class WC_Gateway_Worldpay extends WC_Payment_Gateway {
        public function __construct() {
            // Set up gateway settings
            $this->id = 'worldpay';
            $this->title = 'Worldpay';
            // ... other settings ...

            // Define gateway hooks
            add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));

            // Hook into the checkout process
            add_action('woocommerce_checkout_process', array($this, 'custom_worldpay_checkout_process'));
        }

        // Process the payment
        public function process_payment($order_id) {
            // Implement payment processing logic here
            // Call the functions to authorize payment using the Worldpay API
            $this->authorize_payment();
            // ... other processing ...

            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url($order),
            );
        }

        // Custom function for handling the checkout process
        public function custom_worldpay_checkout_process() {
            // Your custom logic for handling the checkout process goes here
        }

        // Function to authorize a payment
        private function authorize_payment() {
            // Implementation of the authorize_payment function
            // ...
        }
    }
}
<?php
/*
Plugin Name: Worldpay Integration
Description: Custom integration with Worldpay API.
Version: 1.0
Author:
*/

// Define the Worldpay payment gateway class
class WC_Gateway_Worldpay extends WC_Payment_Gateway {
    public function __construct() {
        // Set up gateway settings
        $this->id = 'worldpay';
        $this->title = 'Worldpay';
        // ... other settings ...

        // Define gateway hooks
        add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));

        // Hook into the checkout process
        add_action('woocommerce_checkout_process', array($this, 'custom_worldpay_checkout_process'));
    }

    // Process the payment
    public function process_payment($order_id) {
        $order = wc_get_order($order_id);

        // Implement payment processing logic here
        // Call the functions to authorize payment using the Worldpay API
        $this->authorize_payment($order);

        // Redirect to Worldpay
        return array(
            'result' => 'success',
            'redirect' => $this->get_return_url($order),
        );
    }

    // Custom function for handling the checkout process
    public function custom_worldpay_checkout_process() {
        // Retrieve and process checkout data here
        global $woocommerce;

        // Example: Get the order total
        $order_total = $woocommerce->cart->total;

        // Example: Get billing details
        $billing_address = $woocommerce->checkout->get_value('billing_address_1');
        $billing_city = $woocommerce->checkout->get_value('billing_city');

        // Example: Get selected payment method
        $payment_method = $woocommerce->session->get('chosen_payment_method');

        // Create the $data array for Worldpay API
        $data = array(
            'amount' => $order_total * 100, // Convert to cents
            'currencyCode' => get_woocommerce_currency(),
            'paymentMethod' => array(
                'type' => $payment_method,
                // Add other payment method-specific details here
            ),
            // Add other necessary data
        );

        // Call the function to authorize payment with Worldpay
        $this->authorize_payment($data);

        // Display the Worldpay API links on the checkout page
        echo do_shortcode('[worldpay_api_links]');
    }

    // Function to authorize a payment
    private function authorize_payment($data) {
        $api_url = 'https://try.access.worldpay.com/payments/authorize';

        // Customize the request data as needed
        $username = 'username';
        $password = 'password';

        $credentials = base64_encode($username . ':' . $password);

        $ch = curl_init($api_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Authorization: Basic ' . $credentials,
            'Content-Type: application/vnd.worldpay.payments-v6+json',
            'Accept: application/vnd.worldpay.payments-v6.hal+json',
        ));

        $response = curl_exec($ch);

        // Output the raw API response
        echo '<h2>Raw API Response:</h2>';
        echo '<pre>' . htmlspecialchars($response) . '</pre>';

        // Process the API response as needed
        $result = json_decode($response, true);

        // Example: Output the result
        echo '<h2>Authorize Payment Result:</h2>';
        echo '<pre>' . print_r($result, true) . '</pre>';

        curl_close($ch);
    }
}

// Add Worldpay as a payment gateway
add_filter('woocommerce_payment_gateways', 'add_worldpay_gateway');
function add_worldpay_gateway($gateways) {
    $gateways[] = 'WC_Gateway_Worldpay';
    return $gateways;
}

// Function to display Worldpay API links
function display_worldpay_api_links() {
    // For testing purposes, echo a simple message
    echo '<h2>Worldpay API Links Placeholder</h2>';
}

// Add shortcode
add_shortcode('worldpay_api_links', 'display_worldpay_api_links');

The above code shows a payment option of WorldPay, but doesn't perform a redirect to them to make payment.

0

There are 0 best solutions below