How to integrate FraudLabs Pro fraud detection with Paysafe payment

Description: This tutorial demonstrates how to integrate FraudLabs Pro fraud detection service into Paysafe payment process. We show you the step-by-step instructions using the PHP language in the below section.

Using PHP

Create a new table to store the transaction value of FraudLabs Pro and Paysafe 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, 
	`paysafe_transaction_id` VARCHAR(30) NOT NULL,
	`paysafe_amount` DECIMAL(12,2) NOT NULL,
	`paysafe_settlement_id` VARCHAR(30) 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 Paysafe 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 Paysafe library
require_once('../source/paysafe.php');
use Paysafe\PaysafeApiClient;
use Paysafe\Environment;
use Paysafe\CardPayments\Authorization;

$paysafeApiKeyId = 'your_paysafe_api_key_id';
$paysafeApiKeySecret = 'your_paysafe_api_key_secret';
$paysafeAccountNumber = 'your_paysafe_api_account_number';

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'], 
		'paymentMethod'	=> FraudLabsPro\Order::CREDIT_CARD,
	],
	'card'		=> [
		'number'	=> $_POST['number'],
	],
	'billing'	=> [
		'country'	=> $_POST['country'],
	],
];

// Sends the order details to FraudLabs Pro
$fraudResult = FraudLabsPro\Order::validate($orderDetails);

// This transaction is legitimate, let's submit to Paysafe
if ($fraudResult->fraudlabspro_status == 'APPROVE') {
	$client = new PaysafeApiClient($paysafeApiKeyId, $paysafeApiKeySecret, Environment::TEST, $paysafeAccountNumber);

	$auth = $client->cardPaymentService()->authorize(new Authorization(array(
		'merchantRefNum' => $_POST['merchant_ref_num'],
		'amount' => $_POST['amount'],
		'settleWithAuth' => false,
		'card' => array(
				'cardNum' => $_POST['number']
			)
	)));
}

// 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') {
	$client = new PaysafeApiClient($paysafeApiKeyId, $paysafeApiKeySecret, Environment::TEST, $paysafeAccountNumber);

	try {
		$auth = $client->cardPaymentService()->authorize(new Authorization(array(
			'merchantRefNum' => $_POST['merchant_ref_num'],
			'amount' => $_POST['amount'],
			'settleWithAuth' => true,
			'card' => array(
				'cardNum' => $_POST['number']
			)
		)));

		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, paysafe_transaction_id, paysafe_amount) VALUES (:flpId, :flpStatus, :paysafeId, :paysafeAmount)');
			$st->execute(array(
				':flpId'=>$fraudResult->fraudlabspro_id,
				':flpStatus'=>$fraudResult->fraudlabspro_status,
				':paysafeId'=>$auth->id,
				':paysafeAmount'=>$_POST['amount']
			));
		}
		catch(PDOException $e){
			// MySQL error
			die($e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage());
		}

	} catch (Paysafe\PaysafeException $e) {
		var_dump($e->getMessage());
		if ($e->fieldErrors) {
			var_dump($e->fieldErrors);
		}
		if ($e->links) {
			var_dump($e->links);
		}
	}
}

Now, we are going to create a callback page to receive the review action, APPROVE or REJECT, performed 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 Paysafe library
require_once('../source/paysafe.php');
use Paysafe\PaysafeApiClient;
use Paysafe\Environment;
use Paysafe\CardPayments\Authorization;

$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 Paysafe 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);

			$paysafeApiKeyId = 'your_paysafe_api_key_id';
			$paysafeApiKeySecret = 'your_paysafe_api_key_secret';
			$paysafeAccountNumber = 'your_paysafe_api_account_number';
			$client = new PaysafeApiClient($paysafeApiKeyId, $paysafeApiKeySecret, Environment::TEST, $paysafeAccountNumber);

			$authorizationId = $row['paysafe_transaction_id'];

			if($action == 'REJECT'){
				// Merchant rejected the order. Void the transaction in Paysafe
				try {
					$authReversal = new AuthorizationReversal(array(
						'merchantRefNum' => $_POST['merchant_ref_num'],
						'amount' => $row['paysafe_amount'],
						'authorizationID' => $authorizationId
					));

					$client->cardPaymentService()->reverseAuth($authReversal);

					// 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 (Paysafe\PaysafeException $e) {
					// This will print the detailed information on the exception.
					echo $e->getMessage();
				}
			}
			else{
				// Merchant approved the order. Submit for settlement
				try {
					$response = $client->cardPaymentService()->settlement(new Settlement(array(
						'merchantRefNum' => $_POST['merchant_ref_num'],
						'authorizationID' => $authorizationId
					)));

					$settlementID = $response->id;

					// Update database
					$st = $db->prepare('UPDATE `fraudlabs_pro` SET `flp_status`=:action, `paysafe_settlement_id`=:settlementId WHERE `flp_transaction_id`=:flpId');
					$st->execute(array(
						':flpId'=>$id,
						':settlementId'=>$settlementID,
						':action'=>$action
					));
				} catch (Paysafe\PaysafeException $e) {
					// This will print the detailed information on the exception.
					echo $e->getMessage();
				}
			}
		}
	}
	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 Paysafe library
require_once('../source/paysafe.php');
use Paysafe\PaysafeApiClient;
use Paysafe\Environment;
use Paysafe\CardPayments\Authorization;

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 Paysafe 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);

		$paysafeApiKeyId = 'your_paysafe_api_key_id';
		$paysafeApiKeySecret = 'your_paysafe_api_key_secret';
		$paysafeAccountNumber = 'your_paysafe_api_account_number';
		$client = new PaysafeApiClient($paysafeApiKeyId, $paysafeApiKeySecret, Environment::TEST, $paysafeAccountNumber);

		$settlementId = $row['paysafe_settlement_id'];

		try {
			// Process a refund
			$response = $client->cardPaymentService()->refund(new Refund(array(
				'merchantRefNum' => $_POST['merchant_ref_num'],
				'settlementID' => $settlementId
			)));

			// Update database
			$st = $db->prepare('UPDATE `fraudlabs_pro` SET `flp_status`=\'REFUNDED\' WHERE `flp_transaction_id`=:flpId');
			$st->execute(array(
				':flpId'=>$_POST['flpId']
			));
		} catch (Paysafe\PaysafeException $e) {
			// This will print the detailed information on the exception.
			echo $e->getMessage();
		}
	}
}
catch(PDOException $e){
	// MySQL error
	die($e->getFile() . ':' . $e->getLine() . ' ' . $e->getMessage());
}

Was this article helpful?

Related Articles