Last active
April 9, 2021 23:45
-
-
Save dragipostolovski/9c53cdcd61f979dbe566c45566f5413b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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