Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragipostolovski/9c53cdcd61f979dbe566c45566f5413b to your computer and use it in GitHub Desktop.
Save dragipostolovski/9c53cdcd61f979dbe566c45566f5413b to your computer and use it in GitHub Desktop.
<?php
/**
* Datatrans class for payments.
*
* https://docs.datatrans.ch/docs
* https://admin.sandbox.datatrans.com/
* https://testaccount.datatrans.com/testaccounts
*
* Class PE_Datatrans
*/
class PE_Datatrans {
public function __construct() {
$this->merchantId = 'use-your-merchantId'; // get_option('_pe_dt_merchantId');
$this->password = 'use-your-password'; // get_option('_pe_dt_password');
$this->refno = rand(5, 10);
}
// Use this endpoint, if you want ot use the Redirect and the Lightbox integration
public function initializeTransaction($amount) {
$url = 'https://api.sandbox.datatrans.com/v1/transactions';
$postFields = json_encode( array(
'amount' => $amount,
'currency' => "CHF",
'refno' => $this->refno,
'redirect' => [
'successUrl' => 'once-the-user-pays-and-it-is-a-success',
"cancelUrl" => "canceled-payment",
"errorUrl" => "transaction-error"
],
// if you have a registration form you can use these fields as well
'cusotmer' => [
'title' => 'Mr.' // string,
'firstName' => 'Projects' // string
'lastName' => 'Engine', // string etc.
],
) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_USERPWD, $this->merchantId . ':' . $this->password);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$data = json_decode(curl_exec($ch));
$result = $data->transactionId;
if (curl_errno($ch)) {
$result = 'Error: ' . curl_error($ch);
}
curl_close($ch);
return $result;
}
// If you are using the SecureFields integration, use this endpoint to get the transactionId
public function initializeTransactionWithSecureFields($amount) {
$url = 'https://api.sandbox.datatrans.com/v1/transactions/secureFields';
$postFields = json_encode( array(
'amount' => $amount,
'currency' => "CHF",
'returnUrl' => '/datatrans-payment/',
) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_USERPWD, $this->merchantId . ':' . $this->password);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$data = json_decode(curl_exec($ch));
$result = $data->transactionId;
if (curl_errno($ch)) {
$result = 'Error: ' . curl_error($ch);
}
curl_close($ch);
return $result;
}
// If you are using the SecureFields integrration you need to authorize the transaction
// Use this API endpoint to do that
// Use the same amount and refno
// The amount should be decimal number without the comma
// for example 60.00 should be 6000
public function authorizeAuthenticatedTransaction($amount, $transactionId) {
$url = 'https://api.sandbox.datatrans.com/v1/transactions/' . $transactionId . '/authorize';
$postFields = json_encode( array(
'refno' => $this->refno,
'amount' => $amount,
'autoSettle' => true
) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->merchantId . ':' . $this->password);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$data = json_decode(curl_exec($ch));
curl_close($ch);
return $data;
}
/**
* Check the status of the transaction.
*
* @param $transactionId
* @return mixed
*/
public function checkingTheStatusOfATransaction($transactionId) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.datatrans.com/v1/transactions/' . $transactionId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERPWD, $this->merchantId . ':' . $this->password);
$result = curl_exec($ch);
$data = json_decode( $result );
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $data;
}
// Remove the sandbox. once you are done with testing and your sure your code is good.
}
new PE_Datatrans;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment