Skip to main content

WooCommerce Integration

Integrate Torque's multi-product checkout with your WooCommerce store to provide seamless checkout experiences for your customers.

๐Ÿš€ Quick Startโ€‹

1. Install Torque Pluginโ€‹

  1. Download Plugin: Get the Torque Checkout plugin
  2. Upload to WordPress: Install via WordPress admin or FTP
  3. Activate Plugin: Enable in WordPress plugins section
  4. Configure Settings: Enter your Torque API credentials

2. Configure Settingsโ€‹

  1. API Credentials: Enter your Torque Business ID and API Key
  2. Checkout Options: Configure checkout appearance and behavior
  3. Webhook Setup: Enable real-time order updates
  4. Test Mode: Verify integration in sandbox environment

3. Go Liveโ€‹

  1. Production Mode: Switch from test to live mode
  2. Monitor Orders: Track checkout performance
  3. Customer Support: Provide seamless customer experience

๐Ÿ”ง Manual Integrationโ€‹

Plugin Developmentโ€‹

If you prefer to build a custom integration:

Step 1: Create Torque Business Accountโ€‹

  1. Sign Up: Visit torque.fi/business
  2. Complete Profile: Fill out business information
  3. Get Credentials: Note your Business ID and API Key

Step 2: Install Torque Packageโ€‹

npm install torque-checkout
# or
composer require torque/checkout

Step 3: Create WordPress Pluginโ€‹

Create the plugin structure:

torque-checkout/
โ”œโ”€โ”€ torque-checkout.php
โ”œโ”€โ”€ includes/
โ”‚ โ”œโ”€โ”€ class-torque-checkout.php
โ”‚ โ”œโ”€โ”€ class-torque-api.php
โ”‚ โ””โ”€โ”€ class-torque-webhooks.php
โ”œโ”€โ”€ admin/
โ”‚ โ”œโ”€โ”€ class-torque-admin.php
โ”‚ โ””โ”€โ”€ js/
โ”œโ”€โ”€ public/
โ”‚ โ”œโ”€โ”€ class-torque-public.php
โ”‚ โ””โ”€โ”€ js/
โ””โ”€โ”€ languages/

Main Plugin Fileโ€‹

<?php
/**
* Plugin Name: Torque Checkout
* Description: Multi-product checkout integration with Torque
* Version: 1.0.0
* Author: Your Name
* License: GPL v2 or later
*/

if (!defined('ABSPATH')) {
exit;
}

define('TORQUE_CHECKOUT_VERSION', '1.0.0');
define('TORQUE_CHECKOUT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('TORQUE_CHECKOUT_PLUGIN_URL', plugin_dir_url(__FILE__));

require_once TORQUE_CHECKOUT_PLUGIN_DIR . 'includes/class-torque-checkout.php';

function run_torque_checkout() {
$plugin = new Torque_Checkout();
$plugin->run();
}

run_torque_checkout();

Main Classโ€‹

<?php
// includes/class-torque-checkout.php
class Torque_Checkout {

public function __construct() {
add_action('init', array($this, 'init'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('woocommerce_after_cart', array($this, 'add_torque_checkout_button'));
add_action('wp_ajax_generate_torque_checkout', array($this, 'generate_checkout_link'));
add_action('wp_ajax_nopriv_generate_torque_checkout', array($this, 'generate_checkout_link'));
}

public function init() {
// Initialize plugin
}

public function enqueue_scripts() {
wp_enqueue_script(
'torque-checkout',
TORQUE_CHECKOUT_PLUGIN_URL . 'public/js/torque-checkout.js',
array('jquery'),
TORQUE_CHECKOUT_VERSION,
true
);

wp_localize_script('torque-checkout', 'torque_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('torque_checkout_nonce')
));
}

public function add_torque_checkout_button() {
if (WC()->cart->is_empty()) {
return;
}

echo '<div class="torque-checkout-section">';
echo '<button id="torque-checkout-btn" class="button alt">Checkout with Torque</button>';
echo '</div>';
}

public function generate_checkout_link() {
check_ajax_referer('torque_checkout_nonce', 'nonce');

try {
$cart_data = $this->get_cart_data();
$customer_data = $this->get_customer_data();

$api = new Torque_API();
$checkout_url = $api->generate_checkout_link($cart_data, $customer_data);

if ($checkout_url) {
wp_send_json_success(array('checkout_url' => $checkout_url));
} else {
wp_send_json_error('Failed to generate checkout link');
}
} catch (Exception $e) {
wp_send_json_error($e->getMessage());
}
}

private function get_cart_data() {
$cart = WC()->cart;
$items = array();

foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$items[] = array(
'productId' => $product->get_id(),
'quantity' => $cart_item['quantity'],
'price' => $product->get_price(),
'variant' => $this->get_variant_name($cart_item),
'metadata' => array(
'sku' => $product->get_sku(),
'name' => $product->get_name()
)
);
}

return array(
'items' => $items,
'currency' => get_woocommerce_currency()
);
}

private function get_customer_data() {
$customer = array();

if (is_user_logged_in()) {
$user = wp_get_current_user();
$customer['email'] = $user->user_email;
$customer['firstName'] = $user->first_name;
$customer['lastName'] = $user->last_name;
}

return $customer;
}

private function get_variant_name($cart_item) {
if (isset($cart_item['variation'])) {
$variation = $cart_item['variation'];
$variation_names = array();

foreach ($variation as $key => $value) {
$taxonomy = str_replace('attribute_', '', $key);
$term = get_term_by('slug', $value, $taxonomy);
if ($term) {
$variation_names[] = $term->name;
}
}

return implode(' - ', $variation_names);
}

return null;
}
}

API Integrationโ€‹

<?php
// includes/class-torque-api.php
class Torque_API {

private $api_key;
private $business_id;


public function __construct() {
$this->api_key = get_option('torque_checkout_api_key');
$this->business_id = get_option('torque_checkout_business_id');
}

public function generate_checkout_link($cart_data, $customer_data) {
$url = $this->base_url . '/checkout/generate-link';

$response = wp_remote_post($url, array(
'headers' => array(
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json'
),
'body' => json_encode(array(
'businessId' => $this->business_id,
'cart' => $cart_data,
'customerData' => $customer_data,
'options' => array(
'expiresIn' => 3600,
'redirectUrl' => home_url('/thank-you'),
'webhookUrl' => home_url('/wp-json/torque/v1/webhook')
)
))
));

if (is_wp_error($response)) {
throw new Exception('API request failed: ' . $response->get_error_message());
}

$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

if (isset($data['checkoutUrl'])) {
return $data['checkoutUrl'];
}

throw new Exception('Invalid API response: ' . $body);
}
}

๐ŸŽจ Frontend Integrationโ€‹

Add Checkout Buttonโ€‹

JavaScript Implementationโ€‹

// public/js/torque-checkout.js
jQuery(document).ready(function($) {
$('#torque-checkout-btn').on('click', function(e) {
e.preventDefault();

var $button = $(this);
$button.prop('disabled', true);
$button.text('Generating Checkout...');

$.ajax({
url: torque_ajax.ajax_url,
type: 'POST',
data: {
action: 'generate_torque_checkout',
nonce: torque_ajax.nonce
},
success: function(response) {
if (response.success && response.data.checkout_url) {
window.location.href = response.data.checkout_url;
} else {
alert('Failed to generate checkout link: ' + (response.data || 'Unknown error'));
$button.prop('disabled', false);
$button.text('Checkout with Torque');
}
},
error: function() {
alert('Failed to generate checkout link. Please try again.');
$button.prop('disabled', false);
$button.text('Checkout with Torque');
}
});
});
});

CSS Stylingโ€‹

/* public/css/torque-checkout.css */
.torque-checkout-section {
margin: 20px 0;
text-align: center;
}

#torque-checkout-btn {
background-color: #3B82F6;
color: white;
padding: 15px 30px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}

#torque-checkout-btn:hover {
background-color: #2563EB;
}

#torque-checkout-btn:disabled {
background-color: #9CA3AF;
cursor: not-allowed;
}

๐Ÿ”— Webhook Integrationโ€‹

Handle Order Updatesโ€‹

<?php
// includes/class-torque-webhooks.php
class Torque_Webhooks {

public function __construct() {
add_action('rest_api_init', array($this, 'register_routes'));
}

public function register_routes() {
register_rest_route('torque/v1', '/webhook', array(
'methods' => 'POST',
'callback' => array($this, 'handle_webhook'),
'permission_callback' => array($this, 'verify_webhook')
));
}

public function handle_webhook($request) {
$body = $request->get_body();
$data = json_decode($body, true);

if (!$data) {
return new WP_Error('invalid_data', 'Invalid webhook data', array('status' => 400));
}

$event = $data['event'] ?? '';
$event_data = $data['data'] ?? array();

switch ($event) {
case 'order.paid':
$this->handle_order_paid($event_data);
break;
case 'order.completed':
$this->handle_order_completed($event_data);
break;
case 'order.cancelled':
$this->handle_order_cancelled($event_data);
break;
}

return new WP_REST_Response(array('success' => true), 200);
}

public function verify_webhook($request) {
$signature = $request->get_header('X-Torque-Signature');
$webhook_secret = get_option('torque_checkout_webhook_secret');

if (!$signature || !$webhook_secret) {
return false;
}

$body = $request->get_body();
$expected_signature = hash_hmac('sha256', $body, $webhook_secret);

return hash_equals($expected_signature, $signature);
}

private function handle_order_paid($data) {
$order_id = $this->get_woocommerce_order_id($data['orderId']);
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$order->update_status('processing', 'Payment confirmed via Torque');
}
}
}

private function handle_order_completed($data) {
$order_id = $this->get_woocommerce_order_id($data['orderId']);
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$order->update_status('completed', 'Order completed via Torque');
}
}
}

private function handle_order_cancelled($data) {
$order_id = $this->get_woocommerce_order_id($data['orderId']);
if ($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$order->update_status('cancelled', 'Order cancelled via Torque');
}
}
}

private function get_woocommerce_order_id($torque_order_id) {
// Implement logic to find WooCommerce order by Torque order ID
// This would typically involve a custom meta field
return null;
}
}

๐Ÿงช Testingโ€‹

Test Modeโ€‹

  1. Enable Test Mode: Use test API keys and sandbox environment
  2. Create Test Orders: Generate test checkout links
  3. Verify Webhooks: Test webhook delivery and processing
  4. Check Integration: Ensure data flows correctly between systems

Test Scenariosโ€‹

  • Cart Conversion: Verify WooCommerce cart converts to Torque format
  • Checkout Flow: Test complete checkout process
  • Order Sync: Confirm orders sync between platforms
  • Webhook Delivery: Test real-time updates
  • Error Handling: Test various error scenarios

๐Ÿšจ Troubleshootingโ€‹

Common Issuesโ€‹

Problem: API returns error when generating checkout link

Solutions:

  • Verify API credentials are correct
  • Check cart data format
  • Ensure all required fields are present
  • Review API error logs

Orders Not Syncingโ€‹

Problem: Orders created in Torque don't appear in WooCommerce

Solutions:

  • Verify webhook endpoints are accessible
  • Check webhook signature verification
  • Review webhook processing logs
  • Ensure proper error handling

Cart Data Mismatchโ€‹

Problem: Cart items don't match between platforms

Solutions:

  • Verify cart conversion logic
  • Check product ID mapping
  • Ensure price and quantity accuracy
  • Test with simple cart items first

Debug Toolsโ€‹

Loggingโ€‹

// Enable detailed logging
if (WP_DEBUG === true) {
error_log('Torque API call: ' . json_encode($post_data));
}

Webhook Testingโ€‹

# Test webhook endpoint
curl -X POST https://yourdomain.com/wp-json/torque/v1/webhook \
-H "Content-Type: application/json" \
-d '{
"event": "order.created",
"data": {"orderId": "test_123"}
}'

๐Ÿ”— Next Stepsโ€‹


Need help with WooCommerce integration? Contact our support team at hello@torque.fi or check our integration troubleshooting guide.