Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Datatrans Payments API for Secure Payment

#1
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 -&gt; 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">&lt;?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';
}
?&gt;
</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">&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Datatrans Payments API for Secure Payment&lt;/TITLE&gt;
&lt;link rel="stylesheet" type="text/css" href="css/style.css"&gt;
&lt;script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"&gt;&lt;/script&gt; &lt;/HEAD&gt;
&lt;BODY&gt; &lt;div class="container"&gt; &lt;h1&gt;Datatrans Payments API for Secure Payment&lt;/h1&gt; &lt;div class="outer-container"&gt; &lt;img src="image/camera.jpg" alt="camera image"&gt; &lt;div class="inner-container"&gt; &lt;p class="text-style"&gt;A6900 MirrorLess Camera&lt;/p&gt; &lt;p class="price-color-align"&gt; $289.61&lt;input type="hidden" name="amount" id="amount" value="289.61" /&gt; &lt;/p&gt; &lt;input type="button" id="pay-now" class="pay-button" value="Pay via DataTrans" onClick="initiate()" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script src="https://pay.sandbox.datatrans.com/upp/payment/js/datatrans-2.0.0.js"&gt;&lt;/script&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</code></pre>
<h2>Get transaction id and proceed with payment via API</h2>
<p>The&nbsp;<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&nbsp;<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">&lt;?php
require_once 'DatatransPaymentService.php';
$dataTransPaymentService = new DatatransPaymentService();
$amount = $_POST["amount"];
$orderId = rand();
$result = $dataTransPaymentService-&gt;initializeTransaction($amount, $orderId);
print $result;
?&gt;
</code></pre>
<h2>Datatrans payment transaction service to call API via PHP cURL</h2>
<p>This service call contains the&nbsp;<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">&lt;?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' =&gt; $amount, 'currency' =&gt; "USD", 'refno' =&gt; $orderId, 'redirect' =&gt; [ 'successUrl' =&gt; Config::SUCCESS_URL, "cancelUrl" =&gt; Config::CANCEL_URL, "errorUrl" =&gt; Config::ERROR_URL ] )); $key = Config::MERCHANT_ID . ':' . Config::PASSWORD; $keyBase64 = base64_encode($key); $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL =&gt; $url, CURLOPT_RETURNTRANSFER =&gt; true, CURLOPT_CUSTOMREQUEST =&gt; "POST", CURLOPT_POSTFIELDS =&gt; $postFields, CURLOPT_HTTPAUTH =&gt; CURLAUTH_BASIC, CURLOPT_HTTPHEADER =&gt; 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-&gt;error)) { $curlError = $curlJSONObject-&gt;error-&gt;code . ": " . $curlJSONObject-&gt;error-&gt;message; } curl_close($ch); if (empty($curlJSONObject-&gt;transactionId)) { $result = array( 'responseType' =&gt; "Error", 'message' =&gt; $curlError ); } else { $result = array( 'responseType' =&gt; "success", 'transactionId' =&gt; $curlJSONObject-&gt;transactionId ); } $result = json_encode($result); return $result; }
}
?&gt;
</code></pre>
<h2>Call application URL after payment</h2>
<p class="code-heading">return.php</p>
<pre class="prettyprint"><code class="language-php">&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Datatrans payment status notice&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt; &lt;div class="text-center"&gt;
&lt;?php
if (! empty($_GET["status"])) { ?&gt; &lt;h1&gt;Something wrong with the payment process!&lt;/h1&gt; &lt;p&gt;Kindly contact admin with the reference of your transaction id &lt;?php echo $_GET["datatransTrxId"]; ?&gt;&lt;/p&gt;
&lt;?php
} else { ?&gt; &lt;h1&gt;Your order has been placed&lt;/h1&gt; &lt;p&gt;We will contact you shortly.&lt;/p&gt;
&lt;?php
}
?&gt;
&lt;/div&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</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/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016