Description: This tutorial demonstrates how to integrate FraudLabs Pro fraud detection service into Checkout.com payment process. Below we show you the step-by-step instructions using the PHP language.
Using PHP
Create a new table to store the transaction value of FraudLabs Pro and Checkout.com payment processing. This table will be used during the settlement, void or refund process.
CREATE TABLE `fraudlabs_pro` ( `flp_transaction_id` CHAR(15) NOT NULL, `flp_status` VARCHAR(10) NOT NULL, `checkout_transaction_id` VARCHAR(30) NOT NULL, `checkout_amount` DECIMAL(12,2) NOT NULL, PRIMARY KEY (`flp_transaction_id`) ) COLLATE='utf8_general_ci' ENGINE=MyISAM;
Download FraudLabs Pro PHP class from https://github.com/fraudlabspro/fraudlabspro-php/releases
Integrate FraudLabs Pro fraud detection logic with your Checkout.com code. This code will perform a simple validation check of one credit card purchase and perform the appropriate action based on the fraud validation result.
// Include FraudLabs Pro library require_once 'PATH_TO_FRAUDLABSPRO/lib/FraudLabsPro.php'; // Include Checkout.com library include 'checkout-php-library/autoload.php'; use com\checkout; use com\checkout\ApiServices; $apiClient = new ApiClient('your_checkout.com_secret_key'); $charge = $apiClient->chargeService(); FraudLabsPro\Configuration::apiKey('your_fraudlabspro_api_key'); // Check this transaction for possible fraud. FraudLabs Pro support comprehensive validation check, // and for this example, we only perform the IP address, BIN and billing country validation. // For complete validation, please check our developer page at http://www.fraudlabspro.com/developer $orderDetails = [ 'order' => [ 'amount' => $_POST['amount'], ], 'billing' => [ 'country' => $_POST['country'], ], ]; // Sends the order details to FraudLabs Pro $fraudResult = FraudLabsPro\Order::validate($orderDetails); // This transaction is legitimate, let's submit to PayPal if ($fraudResult->fraudlabspro_status == 'APPROVE') { // Create an instance of CardTokenChargeCreate Model $cardTokenChargePayload = new Charges\RequestModels\CardTokenChargeCreate(); $cardTokenChargePayload->setAutoCapture('Y'); $cardTokenChargePayload->setValue($_POST['amount']); $cardTokenChargePayload->setCurrency('usd'); $cardTokenChargePayload->setTrackId('Demo-0001'); $cardTokenChargePayload->setCardToken($_POST['checkoutCardToken']); // Create a charge with cardToken try { $chargeResponse = $charge->chargeWithCardToken($cardTokenChargePayload); } catch (com\checkout\helpers\ApiHttpClientCustomException $e) { echo 'Caught exception Message: ', $e->getErrorMessage(), "\n"; echo 'Caught exception Error Code: ', $e->getErrorCode(), "\n"; echo 'Caught exception Event id: ', $e->getEventId(), "\n"; } } // Transaction has been rejected by FraudLabs Pro based on your custom validation rules. elseif ($fraudResult->fraudlabspro_status == 'REJECT') { /* Do something here, try contact the customer for verification */ } // Transaction is marked for a manual review by FraudLabs Pro based on your custom validation rules. elseif ($fraudResult->fraudlabspro_status == 'REVIEW') { // Create an instance of CardTokenChargeCreate Model $cardTokenChargePayload = new Charges\RequestModels\CardTokenChargeCreate(); $cardTokenChargePayload->setAutoCapture('N'); $cardTokenChargePayload->setAutoCaptime('0'); $cardTokenChargePayload->setValue($_POST['amount']); $cardTokenChargePayload->setCurrency('usd'); $cardTokenChargePayload->setTrackId('Demo-0001'); $cardTokenChargePayload->setCardToken($_POST['checkoutCardToken']); // Create a charge with cardToken try { $chargeResponse = $charge->chargeWithCardToken($cardTokenChargePayload); $checkoutTransId = $chargeResponse->getId(); try{ // Initial MySQL connection $db = new PDO('mysql:host=your_database_host;dbname=your_database_name;charset=utf8', 'your_database_user', 'your_database_password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Store the transaction information for decision making $st = $db->prepare('INSERT INTO `fraudlabs_pro` (flp_transaction_id, flp_status, checkout_transaction_id, checkout_amount) VALUES (:flpId, :flpStatus, :checkoutId, :checkoutAmount)'); $st->execute(array( ':flpId'=>$fraudResult->fraudlabspro_id, ':flpStatus'=>$fraudResult->fraudlabspro_status, ':checkoutId'=>$checkoutTransId, ':checkoutAmount'=>$_POST['amount'] )); } catch(PDOException $e){ // MySQL error die($e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage()); } } catch (com\checkout\helpers\ApiHttpClientCustomException $e) { echo 'Caught exception Message: ', $e->getErrorMessage(), "\n"; echo 'Caught exception Error Code: ', $e->getErrorCode(), "\n"; echo 'Caught exception Event id: ', $e->getEventId(), "\n"; } }
Now, we are going to create a callback page to receive the review action, APPROVE or REJECT, initiated by the merchant.
Note: You need to configure the callback URL at the FraudLabs Pro merchant area->settings page. It has to be pointed to the location where you hosted this “fraudlabspro-callback.php” file. Below is the sample code for fraudlabspro-callback.php
// Include Checkout.com library include 'checkout-php-library/autoload.php'; use com\checkout; use com\checkout\ApiServices; $id = (isset($_POST['id'])) ? $_POST['id'] : ''; $action = (isset($_POST['action'])) ? $_POST['action'] : ''; if($id && in_array($action, array('APPROVE', 'REJECT'))){ try{ // Initial MySQL connection $db = new PDO('mysql:host=your_database_host;dbname=your_database_name;charset=utf8', 'your_database_user', 'your_database_password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Get the Checkout.com Transaction ID $st = $db->prepare('SELECT * FROM `fraudlabs_pro` WHERE `flp_transaction_id`=:flpId AND `flp_status`=\'REVIEW\''); $st->execute(array( ':flpId'=>$id )); if($st->rowCount() == 1){ $row = $st->fetch(PDO::FETCH_ASSOC); $apiClient = new ApiClient('your_checkout.com_secret_key'); $charge = $apiClient->chargeService(); $chargeId = $row['checkout_transaction_id']; if($action == 'REJECT'){ // Merchant rejected the order. Void the transaction in Checkout.com $chargePayload = new Charges\RequestModels\ChargeVoid(); $chargePayload->setTrackId('TrackId'); try { $ChargeResponse = $charge->voidCharge($chargeId,$chargePayload); // Update database $st = $db->prepare('UPDATE `fraudlabs_pro` SET `flp_status`=:action WHERE `flp_transaction_id`=:flpId'); $st->execute(array( ':flpId'=>$id, ':action'=>$action )); } catch (com\checkout\helpers\ApiHttpClientCustomException $e) { echo 'Caught exception Message: ', $e->getErrorMessage(), "\n"; echo 'Caught exception Error Code: ', $e->getErrorCode(), "\n"; echo 'Caught exception Event id: ', $e->getEventId(), "\n"; } } else{ // Merchant approved the order. Submit for settlement $chargeCapturePayload = new Charges\RequestModels\ChargeCapture(); $chargeCapturePayload->setChargeId($chargeId); $chargeCapturePayload->setValue($row['checkout_amount']); try { $ChargeResponse = $charge->CaptureCardCharge($chargeCapturePayload); // Update database $st = $db->prepare('UPDATE `fraudlabs_pro` SET `flp_status`=:action WHERE `flp_transaction_id`=:flpId'); $st->execute(array( ':flpId'=>$id, ':action'=>$action )); } catch (com\checkout\helpers\ApiHttpClientCustomException $e) { echo 'Caught exception Message: ', $e->getErrorMessage(), "\n"; echo 'Caught exception Error Code: ', $e->getErrorCode(), "\n"; echo 'Caught exception Event id: ', $e->getEventId(), "\n"; } } } } catch(PDOException $e){ // MySQL error die($e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage()); } }
If there is a need to issue a refund of a settled transaction, below is the sample code of how to accomplish it.
// Include Checkout.com library include 'checkout-php-library/autoload.php'; use com\checkout; use com\checkout\ApiServices; try{ // Initial MySQL connection $db = new PDO('mysql:host=your_database_host;dbname=your_database_name;charset=utf8', 'your_database_user', 'your_database_password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Get the Checkout.com transaction ID based on the FraudLabs Pro ID $st = $db->prepare('SELECT * FROM `fraudlabs_pro` WHERE `flp_transaction_id`=:flpId'); $st->execute(array( ':flpId'=>$_POST['flpId'] )); if($st->rowCount() == 1){ $row = $st->fetch(PDO::FETCH_ASSOC); $apiClient = new ApiClient('your_checkout.com_secret_key'); $charge = $apiClient->chargeService(); $chargeCapturePayload = new Charges\RequestModels\ChargeRefund(); $chargeCapturePayload->setChargeId($row['checkout_transaction_id']); $chargeCapturePayload->setValue($row['checkout_amount']); try { $chargeResponse = $charge->refundCardChargeRequest($chargeCapturePayload); // Update database $st = $db->prepare('UPDATE `fraudlabs_pro` SET `flp_status`=\'REFUNDED\' WHERE `flp_transaction_id`=:flpId'); $st->execute(array( ':flpId'=>$_POST['flpId'] )); } catch (com\checkout\helpers\ApiHttpClientCustomException $e) { echo 'Caught exception Message: ', $e->getErrorMessage(), "\n"; echo 'Caught exception Error Code: ', $e->getErrorCode(), "\n"; echo 'Caught exception Event id: ', $e->getEventId(), "\n"; } } } catch(PDOException $e){ // MySQL error die($e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage()); }