How to Integrate Fluidpay into CS Cart Without Creating an Addon

cs-cart fluidpay integration

I was tasked with building a multi-vendor marketplace using CS-Cart for a company in a high-risk industry. This company uses Fluidpay for payment processing and CS-Cart does not offer an option to use Fluidpay with their platform.

Since the option to use Fluidpay on CS-Cart did not exist, I had to create it. I started out creating an addon, but during my research, I found out that I could edit the authorizenet_aim.php file to use Fluidpay.

So, I programmed an AI assistant, on the Metrotechs Brand Builder, with all of the necessary documentation from both CS-Cart and Fluidpay to rebuild the authorizenet_aim.php file to allow me to use Fluidpay to process payments.

You may have to do some editing to the code, but if you understand PHP, this is a great template to work with.

Integrating Fluidpay with CS-Cart for Secure Payment Processing

If you’re looking to integrate Fluid into your CS-Cart to handle online payments, you’ve come to the right place. In this guide, I’ll walk you through updating the authorizenet_aim.php file to facilitate Fluidpay payments. This integration ensures secure, reliable, and seamless online transactions directly from your CS-Cart environment.

Step-by-Step Tutorial

We’ll be modifying the authorizenet_aim.php file located at /app/payments/authorizenet_aim.php.

Step 1: Understanding the Existing Code

Before jumping into the updates, it’s crucial to understand the structure of the existing payment processing script. The original code handles the Authorize.Net AIM payment gateway, mapping transaction types, and preparing payment details.

Step 2: Define Transaction Types and Environment Variables

We mapped the existing Authorize.Net transaction types to Fluidpay and added a method to fetch environment variables securely:

use Tygh\Http;
use Tygh\Registry;

if (!defined('BOOTSTRAP')) { 
    die('Access denied'); 
}

// Define transaction types mapping for Fluidpay
$transaction_types = array(
    "P" => "sale",
    "A" => "authorize",
    "C" => "capture",
    "R" => "credit",
    "I" => "auth_capture"
);

// Environment check function
function get_env_var($var_name, $default = '') {
    $value = getenv($var_name);
    return $value !== false ? $value : $default;
}

// Fixed settings for Fluidpay integration
$api_key = get_env_var('FLUIDPAY_API_KEY');  // Ensure to set this in your server environment
$mode = $processor_data['processor_params']['mode'] == 'test' ? "test" : "live";
$currency = $processor_data['processor_params']['currency'];  // Assuming the currency is defined in the processor parameters
$order_prefix = $processor_data['processor_params']['order_prefix'];
$trans_type = $processor_data['processor_params']['transaction_type'];

Step 3: Validate and Sanitize Order Total

It’s essential to validate and sanitize the order total to prevent any invalid transaction amounts:

$order_total = floatval($order_info['total']);
if ($order_total <= 0) {
    die('Invalid order total.');
}

Step 4: Prepare Payment Details

Next, we prepare the payment details required by Fluidpay’s API. This includes payment method, billing address, and other necessary information:

// Prepare payment details
$post = array(
    'type' => $transaction_types[$trans_type],
    'amount' => intval($order_total * 100), // Convert amount to cents
    'currency' => $currency,
    'order_id' => $order_prefix . $order_id,
    'ip_address' => $_SERVER['REMOTE_ADDR'],
    'email_address' => $order_info['email'],
    'description' => 'Order ' . $order_id,
    'payment_method' => array(
        'card' => array(
            'entry_type' => 'keyed',
            'number' => $order_info['payment_info']['card_number'],
            'expiration_date' => $order_info['payment_info']['expiry_month'] . '/' . $order_info['payment_info']['expiry_year'],
            'cvc' => $order_info['payment_info']['cvv2']
        )
    ),
    'billing_address' => array(
        'first_name' => $order_info['b_firstname'],
        'last_name' => $order_info['b_lastname'],
        'company' => $order_info['company'],
        'address_line_1' => $order_info['b_address'],
        'city' => $order_info['b_city'],
        'state' => $order_info['b_state'],
        'postal_code' => $order_info['b_zipcode'],
        'country' => $order_info['b_country'],
        'email' => $order_info['email'],
        'phone' => $order_info['phone']
    )
);

Step 5: Determine Fluidpay URL

Depending on whether you’re operating in test mode or live mode, set the appropriate endpoint for Fluidpay:

// Determine Fluidpay URL
$payment_url = ($mode == 'test') ? "https://sandbox.fluidpay.com/api/transaction" : "https://app.fluidpay.com/api/transaction";

// Prepare headers for the API request
$headers = array(
    'Content-Type: application/json',
    'Authorization: ' . $api_key
);

Step 6: Send Request to Fluidpay

Send the request to Fluidpay and decode the response to handle both successful transactions and errors:

// Send request to Fluidpay
$response = Http::post($payment_url, json_encode($post), array('headers' => $headers));

// Decode response
$response_data = json_decode($response, true);

// Prepare the payment processor response
$pp_response = array();

if (isset($response_data['status']) && $response_data['status'] == 'success' && $response_data['data']['response_body']['card']['response'] != 'declined') {
    $pp_response['order_status'] = 'P';
    $pp_response['transaction_id'] = $response_data['data']['id'];
} else {
    $pp_response['order_status'] = 'F';
    $pp_response['reason_text'] = implode(' | ', array_filter(array(
        $response_data['data']['response_body']['card']['response'],
        $response_data['data']['response_body']['card']['processor_response_text'],
        $response_data['data']['response_body']['card']['card_type'],
        strval($response_data['amount_authorized'])
    )));
}

Step 7: Handle Additional Response Information

Lastly, include any additional information like AVS, CVV, and processor response codes in the response:

// Additional response information
if (!empty($response_data['data']['avs_response_code'])) {
    $pp_response['descr_avs'] = $response_data['data']['avs_response_code'];
}

if (!empty($response_data['data']['cvv_response_code'])) {
    $pp_response['descr_cvv'] = $response_data['data']['cvv_response_code'];
}

if (!empty($response_data['data']['processor_response_code'])) {
    $pp_response['descr_cavv'] = $response_data['data']['processor_response_code'];
}

return $pp_response;
?>

Conclusion

By following these steps, you’ve successfully integrated the Fluidpay payment gateway into CS-Cart. This integration provides a smooth and secure way to handle transactions directly within your e-commerce platform. Always test thoroughly in both test and live environments to ensure seamless operation.

Next Steps

  1. Environment Setup: Make sure to set up the FLUIDPAY_API_KEY in your server environment:
    bash export FLUIDPAY_API_KEY='your_actual_api_key'
  2. Thorough Testing: Test the integration in both sandbox and live environments to ensure correctness.
  3. Error Logging: Optionally, consider adding error logging to track any issues that arise during the transaction process.

I hope this guide helps you integrate Fluidpay effectively with CS-Cart. For any questions, feel free to leave a comment below. Happy coding!