09-29-2022, 05:12 AM
Datatrans Payments API for Secure Payment
<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/09/datatrans-payments-api-for-secure-payment.jpg" width="550" height="323" title="" alt="" /></div><div><div class="modified-on" readability="7.1666666666667"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on September 28th, 2022.</div>
<p>Datatrans is one of the popular payment gateway systems in Europe for e-commerce websites. It provides frictionless payment solutions.</p>
<p>The Datatrans Payment gateway is widely popular in European countries. One of my clients from Germany required this payment gateway integrated into their online shop.</p>
<p>In this tutorial, I share the knowledge of integrating Datatrans in a PHP application. It will save the developers time to do research with the long-form documentation.</p>
<p>There are many integration options provided by this payment gateway.</p>
<ol>
<li>Redirect and lightbox</li>
<li>Secure fields</li>
<li>Mobile SDK</li>
<li>API endpoints</li>
</ol>
<p>In this tutorial, we will see the first integration option to set up the Datatrans payment gateway. We have seen many <a href="https://phppot.com/php/stripe-payment-gateway-integration-using-php/">payment gateway integration examples in PHP</a> in the earlier articles.</p>
<p>A working example follows helps to understand things easily. Before seeing the example, get the Datatrans merchant id and password. It will be useful for authentication purposes while accessing this payment API.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-19571" src="https://phppot.com/wp-content/uploads/2022/09/datatrans-api-payment-550x323.jpg" alt="datatrans api payment" width="550" height="323" srcset="https://phppot.com/wp-content/uploads/2022/09/datatrans-api-payment-550x323.jpg 550w, https://phppot.com/wp-content/uploads/20...00x176.jpg 300w, https://phppot.com/wp-content/uploads/20...68x452.jpg 768w, https://phppot.com/wp-content/uploads/20...ayment.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Get Datatrans merchant id and password</h2>
<p>The following steps lead to getting the merchant id and password of your Datatrans account.</p>
<ol>
<li>
<ol>
<li>Open a <a href="https://www.datatrans.ch/en/" target="_blank" rel="noopener">Datatrans sandbox account</a> and verify it via email.</li>
<li>Set up your account by selecting the username, group name(Login) and password.</li>
<li>After Set up it will show the following details
<ul>
<li>Login credentials.</li>
<li>Sandbox URL of the admin dashboard.</li>
<li>Datatrans Merchant Id for Web, Mobile SDK.</li>
</ul>
</li>
<li>Visit the sandbox URL and log in using the credential got in step 3.</li>
<li>Click <em>Change Merchant</em> and Choose merchant to see the dashboard.</li>
<li>Go to UPP Administration -> Security to copy the (<strong>Marchant Id</strong>)Username and <strong>Password</strong> for API access.</li>
</ol>
</li>
</ol>
<p>When we see <a href="https://phppot.com/php/ccavenue-payment-gateway-integration-in-php-website-with-step-by-step-example/">CCAvenue payment integration</a>, it also listed a few steps to get the merchant id from the dashboard.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-19567" src="https://phppot.com/wp-content/uploads/2022/09/datatrans-merchant-account-credentials-550x323.jpg" alt="datatrans merchant account credentials" width="550" height="323" srcset="https://phppot.com/wp-content/uploads/2022/09/datatrans-merchant-account-credentials-550x323.jpg 550w, https://phppot.com/wp-content/uploads/20...00x176.jpg 300w, https://phppot.com/wp-content/uploads/20...68x452.jpg 768w, https://phppot.com/wp-content/uploads/20...ntials.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Application configuration</h2>
<p>This config file is created for this example to have the API keys used across the code.</p>
<p>It configures the URL that has to be called by the DataTrans server after the payment.</p>
<p class="code-heading">Config.php (Configure Datatrans authentication details)</p>
<pre class="prettyprint"><code class="language-php"><?php
class Config { const WEB_ROOT = 'http://localhost/pp-article-code/data-trans/'; const MERCHANT_ID = 'YOUR_MERCHANT_ID'; const PASSWORD = 'YOUR_MERCHANT_DASHBOARD_PASSWORD'; const SUCCESS_URL = Config::WEB_ROOT . 'return.php'; const CANCEL_URL = Config::WEB_ROOT . 'return.php?status=cancelled'; const ERROR_URL = Config::WEB_ROOT . 'return.php?status=error';
}
?>
</code></pre>
<h2>Show the “Pay now” option</h2>
<p>First, a landing page shows a product tile with a payment option. It will show a “Pay via DataTrans” button in the browser.</p>
<p>On clicking this button, it will call the AJAX script to get the transaction id.</p>
<p>This page includes the Datatrans JavaScript library to start payment with the transaction id.</p>
<p class="code-heading">index.php (Product tile with pay button)</p>
<pre class="prettyprint"><code class="language-html"><HTML>
<HEAD>
<TITLE>Datatrans Payments API for Secure Payment</TITLE>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> </HEAD>
<BODY> <div class="container"> <h1>Datatrans Payments API for Secure Payment</h1> <div class="outer-container"> <img src="image/camera.jpg" alt="camera image"> <div class="inner-container"> <p class="text-style">A6900 MirrorLess Camera</p> <p class="price-color-align"> $289.61<input type="hidden" name="amount" id="amount" value="289.61" /> </p> <input type="button" id="pay-now" class="pay-button" value="Pay via DataTrans" onClick="initiate()" /> </div> </div> </div> <script src="https://pay.sandbox.datatrans.com/upp/payment/js/datatrans-2.0.0.js"></script>
</BODY>
</HTML>
</code></pre>
<h2>Get transaction id and proceed with payment via API</h2>
<p>The <em>initiate()</em> function posts the amount to the PHP file to initiate Datatrans payment.</p>
<p>As the result, the PHP will return the transaction id to process the payment further.</p>
<p>The <em>proceedPayment()</em> function calls the Datatrans JavaScript API to start the payment. It will show a Datatrans overlay with card options to choose payment methods.</p>
<p class="code-heading">index.php (AJAX script to call Datatrans initiation)</p>
<pre class="prettyprint"><code class="language-javascript">function initiate() { $.ajax({ method: "POST", url: "initialize-datatrans-ajax.php", dataType: "JSON", data: { "amount": $("#amount").val() } }) .done(function(response) { if (response.responseType == 'success') { proceedPayment(response.transactionId); } else { alert(response.responseType + ": " + response.message); } }); }; function proceedPayment(transactionId) { Datatrans.startPayment({ transactionId: transactionId, 'opened': function() { console.log('payment-form opened'); }, 'loaded': function() { console.log('payment-form loaded'); }, 'closed': function() { console.log('payment-page closed'); }, 'error': function() { console.log('error'); } });
}
</code></pre>
<h2>Initiate payment transaction to get the Datatrans payment transaction id</h2>
<p>This file is the PHP endpoint called via AJAX script to get the Datatrans transaction id.</p>
<p>It invokes the <em>DatatransPaymentService</em> to post the cURL request to the Datatrans API. It requested to initiate the payment and receives the transaction id from the Datatrans server.</p>
<p>This output will be read in the AJAX success callback to start the payment via the JavaScript library.</p>
<p class="code-heading">initialize-datatrans-ajax.php</p>
<pre class="prettyprint"><code class="language-php"><?php
require_once 'DatatransPaymentService.php';
$dataTransPaymentService = new DatatransPaymentService();
$amount = $_POST["amount"];
$orderId = rand();
$result = $dataTransPaymentService->initializeTransaction($amount, $orderId);
print $result;
?>
</code></pre>
<h2>Datatrans payment transaction service to call API via PHP cURL</h2>
<p>This service call contains the <em>initializeTransaction()</em> method which prepares the cURL request in PHP to the Datatrans API.</p>
<p>It passed the amount, currency and more details to the API endpoint to request the transaction id.</p>
<p class="code-heading">DatatransPaymentService.php</p>
<pre class="prettyprint"><code class="language-php"><?php class DatatransPaymentService
{ public function initializeTransaction($amount, $orderId) { $url = 'https://api.sandbox.datatrans.com/v1/transactions'; require_once __DIR__ . '/Config.php'; $amount = $amount * 100; $postFields = json_encode(array( 'amount' => $amount, 'currency' => "USD", 'refno' => $orderId, 'redirect' => [ 'successUrl' => Config::SUCCESS_URL, "cancelUrl" => Config::CANCEL_URL, "errorUrl" => Config::ERROR_URL ] )); $key = Config::MERCHANT_ID . ':' . Config::PASSWORD; $keyBase64 = base64_encode($key); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $postFields, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_HTTPHEADER => array( "Authorization: Basic " . $keyBase64, "Content-Type: application/json" ) )); $curlResponse = curl_exec($ch); $curlJSONObject = json_decode($curlResponse); if (empty($curlResponse)) { $curlError = curl_error($ch); } else if (! empty($curlJSONObject->error)) { $curlError = $curlJSONObject->error->code . ": " . $curlJSONObject->error->message; } curl_close($ch); if (empty($curlJSONObject->transactionId)) { $result = array( 'responseType' => "Error", 'message' => $curlError ); } else { $result = array( 'responseType' => "success", 'transactionId' => $curlJSONObject->transactionId ); } $result = json_encode($result); return $result; }
}
?>
</code></pre>
<h2>Call application URL after payment</h2>
<p class="code-heading">return.php</p>
<pre class="prettyprint"><code class="language-php"><HTML>
<HEAD>
<TITLE>Datatrans payment status notice</TITLE>
</HEAD>
<BODY> <div class="text-center">
<?php
if (! empty($_GET["status"])) { ?> <h1>Something wrong with the payment process!</h1> <p>Kindly contact admin with the reference of your transaction id <?php echo $_GET["datatransTrxId"]; ?></p>
<?php
} else { ?> <h1>Your order has been placed</h1> <p>We will contact you shortly.</p>
<?php
}
?>
</div>
</BODY>
</HTML>
</code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/datatrans-api-payment.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/datatrans-api-payment/#top" class="top">↑ Back to Top</a> </p>
</div>
https://www.sickgaming.net/blog/2022/09/...e-payment/
<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/09/datatrans-payments-api-for-secure-payment.jpg" width="550" height="323" title="" alt="" /></div><div><div class="modified-on" readability="7.1666666666667"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on September 28th, 2022.</div>
<p>Datatrans is one of the popular payment gateway systems in Europe for e-commerce websites. It provides frictionless payment solutions.</p>
<p>The Datatrans Payment gateway is widely popular in European countries. One of my clients from Germany required this payment gateway integrated into their online shop.</p>
<p>In this tutorial, I share the knowledge of integrating Datatrans in a PHP application. It will save the developers time to do research with the long-form documentation.</p>
<p>There are many integration options provided by this payment gateway.</p>
<ol>
<li>Redirect and lightbox</li>
<li>Secure fields</li>
<li>Mobile SDK</li>
<li>API endpoints</li>
</ol>
<p>In this tutorial, we will see the first integration option to set up the Datatrans payment gateway. We have seen many <a href="https://phppot.com/php/stripe-payment-gateway-integration-using-php/">payment gateway integration examples in PHP</a> in the earlier articles.</p>
<p>A working example follows helps to understand things easily. Before seeing the example, get the Datatrans merchant id and password. It will be useful for authentication purposes while accessing this payment API.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-19571" src="https://phppot.com/wp-content/uploads/2022/09/datatrans-api-payment-550x323.jpg" alt="datatrans api payment" width="550" height="323" srcset="https://phppot.com/wp-content/uploads/2022/09/datatrans-api-payment-550x323.jpg 550w, https://phppot.com/wp-content/uploads/20...00x176.jpg 300w, https://phppot.com/wp-content/uploads/20...68x452.jpg 768w, https://phppot.com/wp-content/uploads/20...ayment.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Get Datatrans merchant id and password</h2>
<p>The following steps lead to getting the merchant id and password of your Datatrans account.</p>
<ol>
<li>
<ol>
<li>Open a <a href="https://www.datatrans.ch/en/" target="_blank" rel="noopener">Datatrans sandbox account</a> and verify it via email.</li>
<li>Set up your account by selecting the username, group name(Login) and password.</li>
<li>After Set up it will show the following details
<ul>
<li>Login credentials.</li>
<li>Sandbox URL of the admin dashboard.</li>
<li>Datatrans Merchant Id for Web, Mobile SDK.</li>
</ul>
</li>
<li>Visit the sandbox URL and log in using the credential got in step 3.</li>
<li>Click <em>Change Merchant</em> and Choose merchant to see the dashboard.</li>
<li>Go to UPP Administration -> Security to copy the (<strong>Marchant Id</strong>)Username and <strong>Password</strong> for API access.</li>
</ol>
</li>
</ol>
<p>When we see <a href="https://phppot.com/php/ccavenue-payment-gateway-integration-in-php-website-with-step-by-step-example/">CCAvenue payment integration</a>, it also listed a few steps to get the merchant id from the dashboard.</p>
<p><img loading="lazy" class="alignnone size-large wp-image-19567" src="https://phppot.com/wp-content/uploads/2022/09/datatrans-merchant-account-credentials-550x323.jpg" alt="datatrans merchant account credentials" width="550" height="323" srcset="https://phppot.com/wp-content/uploads/2022/09/datatrans-merchant-account-credentials-550x323.jpg 550w, https://phppot.com/wp-content/uploads/20...00x176.jpg 300w, https://phppot.com/wp-content/uploads/20...68x452.jpg 768w, https://phppot.com/wp-content/uploads/20...ntials.jpg 1000w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2>Application configuration</h2>
<p>This config file is created for this example to have the API keys used across the code.</p>
<p>It configures the URL that has to be called by the DataTrans server after the payment.</p>
<p class="code-heading">Config.php (Configure Datatrans authentication details)</p>
<pre class="prettyprint"><code class="language-php"><?php
class Config { const WEB_ROOT = 'http://localhost/pp-article-code/data-trans/'; const MERCHANT_ID = 'YOUR_MERCHANT_ID'; const PASSWORD = 'YOUR_MERCHANT_DASHBOARD_PASSWORD'; const SUCCESS_URL = Config::WEB_ROOT . 'return.php'; const CANCEL_URL = Config::WEB_ROOT . 'return.php?status=cancelled'; const ERROR_URL = Config::WEB_ROOT . 'return.php?status=error';
}
?>
</code></pre>
<h2>Show the “Pay now” option</h2>
<p>First, a landing page shows a product tile with a payment option. It will show a “Pay via DataTrans” button in the browser.</p>
<p>On clicking this button, it will call the AJAX script to get the transaction id.</p>
<p>This page includes the Datatrans JavaScript library to start payment with the transaction id.</p>
<p class="code-heading">index.php (Product tile with pay button)</p>
<pre class="prettyprint"><code class="language-html"><HTML>
<HEAD>
<TITLE>Datatrans Payments API for Secure Payment</TITLE>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> </HEAD>
<BODY> <div class="container"> <h1>Datatrans Payments API for Secure Payment</h1> <div class="outer-container"> <img src="image/camera.jpg" alt="camera image"> <div class="inner-container"> <p class="text-style">A6900 MirrorLess Camera</p> <p class="price-color-align"> $289.61<input type="hidden" name="amount" id="amount" value="289.61" /> </p> <input type="button" id="pay-now" class="pay-button" value="Pay via DataTrans" onClick="initiate()" /> </div> </div> </div> <script src="https://pay.sandbox.datatrans.com/upp/payment/js/datatrans-2.0.0.js"></script>
</BODY>
</HTML>
</code></pre>
<h2>Get transaction id and proceed with payment via API</h2>
<p>The <em>initiate()</em> function posts the amount to the PHP file to initiate Datatrans payment.</p>
<p>As the result, the PHP will return the transaction id to process the payment further.</p>
<p>The <em>proceedPayment()</em> function calls the Datatrans JavaScript API to start the payment. It will show a Datatrans overlay with card options to choose payment methods.</p>
<p class="code-heading">index.php (AJAX script to call Datatrans initiation)</p>
<pre class="prettyprint"><code class="language-javascript">function initiate() { $.ajax({ method: "POST", url: "initialize-datatrans-ajax.php", dataType: "JSON", data: { "amount": $("#amount").val() } }) .done(function(response) { if (response.responseType == 'success') { proceedPayment(response.transactionId); } else { alert(response.responseType + ": " + response.message); } }); }; function proceedPayment(transactionId) { Datatrans.startPayment({ transactionId: transactionId, 'opened': function() { console.log('payment-form opened'); }, 'loaded': function() { console.log('payment-form loaded'); }, 'closed': function() { console.log('payment-page closed'); }, 'error': function() { console.log('error'); } });
}
</code></pre>
<h2>Initiate payment transaction to get the Datatrans payment transaction id</h2>
<p>This file is the PHP endpoint called via AJAX script to get the Datatrans transaction id.</p>
<p>It invokes the <em>DatatransPaymentService</em> to post the cURL request to the Datatrans API. It requested to initiate the payment and receives the transaction id from the Datatrans server.</p>
<p>This output will be read in the AJAX success callback to start the payment via the JavaScript library.</p>
<p class="code-heading">initialize-datatrans-ajax.php</p>
<pre class="prettyprint"><code class="language-php"><?php
require_once 'DatatransPaymentService.php';
$dataTransPaymentService = new DatatransPaymentService();
$amount = $_POST["amount"];
$orderId = rand();
$result = $dataTransPaymentService->initializeTransaction($amount, $orderId);
print $result;
?>
</code></pre>
<h2>Datatrans payment transaction service to call API via PHP cURL</h2>
<p>This service call contains the <em>initializeTransaction()</em> method which prepares the cURL request in PHP to the Datatrans API.</p>
<p>It passed the amount, currency and more details to the API endpoint to request the transaction id.</p>
<p class="code-heading">DatatransPaymentService.php</p>
<pre class="prettyprint"><code class="language-php"><?php class DatatransPaymentService
{ public function initializeTransaction($amount, $orderId) { $url = 'https://api.sandbox.datatrans.com/v1/transactions'; require_once __DIR__ . '/Config.php'; $amount = $amount * 100; $postFields = json_encode(array( 'amount' => $amount, 'currency' => "USD", 'refno' => $orderId, 'redirect' => [ 'successUrl' => Config::SUCCESS_URL, "cancelUrl" => Config::CANCEL_URL, "errorUrl" => Config::ERROR_URL ] )); $key = Config::MERCHANT_ID . ':' . Config::PASSWORD; $keyBase64 = base64_encode($key); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $postFields, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_HTTPHEADER => array( "Authorization: Basic " . $keyBase64, "Content-Type: application/json" ) )); $curlResponse = curl_exec($ch); $curlJSONObject = json_decode($curlResponse); if (empty($curlResponse)) { $curlError = curl_error($ch); } else if (! empty($curlJSONObject->error)) { $curlError = $curlJSONObject->error->code . ": " . $curlJSONObject->error->message; } curl_close($ch); if (empty($curlJSONObject->transactionId)) { $result = array( 'responseType' => "Error", 'message' => $curlError ); } else { $result = array( 'responseType' => "success", 'transactionId' => $curlJSONObject->transactionId ); } $result = json_encode($result); return $result; }
}
?>
</code></pre>
<h2>Call application URL after payment</h2>
<p class="code-heading">return.php</p>
<pre class="prettyprint"><code class="language-php"><HTML>
<HEAD>
<TITLE>Datatrans payment status notice</TITLE>
</HEAD>
<BODY> <div class="text-center">
<?php
if (! empty($_GET["status"])) { ?> <h1>Something wrong with the payment process!</h1> <p>Kindly contact admin with the reference of your transaction id <?php echo $_GET["datatransTrxId"]; ?></p>
<?php
} else { ?> <h1>Your order has been placed</h1> <p>We will contact you shortly.</p>
<?php
}
?>
</div>
</BODY>
</HTML>
</code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/datatrans-api-payment.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/datatrans-api-payment/#top" class="top">↑ Back to Top</a> </p>
</div>
https://www.sickgaming.net/blog/2022/09/...e-payment/