diff --git a/composer.json b/composer.json index f48065d2..50a51b25 100644 --- a/composer.json +++ b/composer.json @@ -14,15 +14,18 @@ "php": "^7.1.3", "ext-dom": "*", "ext-json": "*", - "ext-mcrypt": "*", + "ext-openssl": "*", "ext-SimpleXML": "*", "guzzlehttp/guzzle": "6.3.3", "symfony/http-foundation": "^4.1", - "symfony/serializer": "^4.1" + "symfony/serializer": "^5.0" }, "autoload": { "psr-4": { "Mews\\Pos\\": "src/" } - } + }, + "require-dev": { + "phpunit/phpunit": "^9.0" + } } diff --git a/config/pos.php b/config/pos.php index abf43cdc..ce0de468 100644 --- a/config/pos.php +++ b/config/pos.php @@ -62,6 +62,18 @@ ], ] ], + 'teb' => [ + 'name' => 'TEB', + 'class' => Mews\Pos\EstPos::class, + 'urls' => [ + 'production' => 'https://sanalpos.teb.com.tr/fim/api', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/api', + 'gateway' => [ + 'production' => 'https://sanalpos.teb.com.tr/fim/est3Dgate', + 'test' => 'https://entegrasyon.asseco-see.com.tr/fim/est3Dgate', + ], + ] + ], 'isbank' => [ 'name' => 'İşbank', 'class' => Mews\Pos\EstPos::class, @@ -90,13 +102,20 @@ 'name' => 'Yapıkredi', 'class' => Mews\Pos\PosNet::class, 'urls' => [ - 'production' => 'https://posnet.yapikredi.com.tr/PosnetWebService/XML', + 'production' => 'https://www.posnet.ykb.com/PosnetWebService/XML', 'test' => 'https://setmpos.ykb.com/PosnetWebService/XML', 'gateway' => [ - 'production' => 'https://posnet.ykb.com/3DSWebService/YKBPaymentService', + 'production' => 'https://www.posnet.ykb.com/3DSWebService/YKBPaymentService', 'test' => 'https://setmpos.ykb.com/3DSWebService/YKBPaymentService', ], - ] + ], + 'order' => [ + 'id_total_length' => 24, + 'id_length' => 20, + 'id_3d_prefix' => 'TDSC', + 'id_3d_pay_prefix' => '', //? + 'id_regular_prefix' => '' //? + ] ], 'garanti' => [ 'name' => 'Garanti', @@ -109,7 +128,7 @@ 'test' => 'https://sanalposprovtest.garanti.com.tr/servlet/gt3dengine', ], ] - ], + ] ], ]; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..6a1bed91 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,25 @@ + + + + + tests + + + + + src + + vendor + + + + diff --git a/src/EstPos.php b/src/EstPos.php index f57e31aa..db80c0f3 100644 --- a/src/EstPos.php +++ b/src/EstPos.php @@ -6,7 +6,6 @@ use GuzzleHttp\Exception\GuzzleException; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; -use SimpleXMLElement; use Symfony\Component\HttpFoundation\Request; /** @@ -42,19 +41,19 @@ class EstPos implements PosInterface * @var array */ public $codes = [ - '00' => 'approved', - '01' => 'bank_call', - '02' => 'bank_call', - '05' => 'reject', - '09' => 'try_again', - '12' => 'invalid_transaction', - '28' => 'reject', - '51' => 'insufficient_balance', - '54' => 'expired_card', - '57' => 'does_not_allow_card_holder', - '62' => 'restricted_card', - '77' => 'request_rejected', - '99' => 'general_error', + '00' => 'approved', + '01' => 'bank_call', + '02' => 'bank_call', + '05' => 'reject', + '09' => 'try_again', + '12' => 'invalid_transaction', + '28' => 'reject', + '51' => 'insufficient_balance', + '54' => 'expired_card', + '57' => 'does_not_allow_card_holder', + '62' => 'restricted_card', + '77' => 'request_rejected', + '99' => 'general_error', ]; /** @@ -63,9 +62,9 @@ class EstPos implements PosInterface * @var array */ public $types = [ - 'pay' => 'Auth', - 'pre' => 'PreAuth', - 'post' => 'PostAuth', + 'pay' => 'Auth', + 'pre' => 'PreAuth', + 'post' => 'PostAuth', ]; /** @@ -135,7 +134,7 @@ class EstPos implements PosInterface * EstPos constructor. * * @param array $config - * @param array $account + * @param mixed $account * @param array $currencies */ public function __construct($config, $account, array $currencies) @@ -163,27 +162,27 @@ public function __construct($config, $account, array $currencies) protected function createRegularPaymentXML() { $nodes = [ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->type, + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'Type' => $this->type, 'IPAddress' => $this->order->ip, - 'Email' => $this->order->email, - 'OrderId' => $this->order->id, - 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, - 'Total' => $this->order->amount, - 'Currency' => $this->order->currency, - 'Taksit' => $this->order->installment, - 'CardType' => isset($this->card->type) ? $this->card->type : null, - 'Number' => $this->card->number, - 'Expires' => $this->card->month . '/' . $this->card->year, - 'Cvv2Val' => $this->card->cvv, - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', - 'BillTo' => [ - 'Name' => $this->order->name ? $this->order->name : null, + 'Email' => $this->order->email, + 'OrderId' => $this->order->id, + 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, + 'Total' => $this->order->amount, + 'Currency' => $this->order->currency, + 'Taksit' => $this->order->installment, + 'CardType' => isset($this->card->type) ? $this->card->type : null, + 'Number' => $this->card->number, + 'Expires' => $this->card->month . '/' . $this->card->year, + 'Cvv2Val' => $this->card->cvv, + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', + 'BillTo' => [ + 'Name' => $this->order->name ? $this->order->name : null, ] ] ]; @@ -199,12 +198,12 @@ protected function createRegularPaymentXML() protected function createRegularPostXML() { $nodes = [ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->types[$this->order->transaction], - 'OrderId' => $this->order->id, + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'Type' => $this->types[$this->order->transaction], + 'OrderId' => $this->order->id, ] ]; @@ -218,34 +217,34 @@ protected function createRegularPostXML() protected function create3DPaymentXML() { $nodes = [ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'Type' => $this->type, - 'IPAddress' => $this->order->ip, - 'Email' => $this->order->email, - 'OrderId' => $this->order->id, - 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, - 'Total' => $this->order->amount, - 'Currency' => $this->order->currency, - 'Taksit' => $this->order->installment, - 'Number' => $this->request->get('md'), - 'Expires' => '', - 'Cvv2Val' => '', - 'PayerTxnId' => $this->request->get('xid'), - 'PayerSecurityLevel' => $this->request->get('eci'), - 'PayerAuthenticationCode' => $this->request->get('cavv'), - 'CardholderPresentCode' => '13', - 'Mode' => 'P', - 'GroupId' => '', - 'TransId' => '', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'Type' => $this->type, + 'IPAddress' => $this->order->ip, + 'Email' => $this->order->email, + 'OrderId' => $this->order->id, + 'UserId' => isset($this->order->user_id) ? $this->order->user_id : null, + 'Total' => $this->order->amount, + 'Currency' => $this->order->currency, + 'Taksit' => $this->order->installment, + 'Number' => $this->request->get('md'), + 'Expires' => '', + 'Cvv2Val' => '', + 'PayerTxnId' => $this->request->get('xid'), + 'PayerSecurityLevel' => $this->request->get('eci'), + 'PayerAuthenticationCode' => $this->request->get('cavv'), + 'CardholderPresentCode' => '13', + 'Mode' => 'P', + 'GroupId' => '', + 'TransId' => '', ] ]; if ($this->order->name) { $nodes['BillTo'] = [ - 'Name' => $this->order->name, + 'Name' => $this->order->name, ]; } @@ -259,7 +258,7 @@ protected function create3DPaymentXML() */ protected function getProcReturnCode() { - return isset($this->data->ProcReturnCode) ? (string) $this->data->ProcReturnCode : null; + return isset($this->data->ProcReturnCode) ? (string)$this->data->ProcReturnCode : null; } /** @@ -269,9 +268,9 @@ protected function getProcReturnCode() */ protected function getStatusDetail() { - $proc_return_code = $this->getProcReturnCode(); + $proc_return_code = $this->getProcReturnCode(); - return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string) $this->codes[$proc_return_code] : null) : null; + return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string)$this->codes[$proc_return_code] : null) : null; } /** @@ -289,35 +288,31 @@ public function create3DHash() $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->type . $this->order->installment . $this->order->rand . $this->account->store_key; } - return base64_encode(pack('H*', sha1($hash_str))); + return base64_encode(sha1($hash_str, true)); } /** * Check 3D Hash * + * @param array $data * @return bool */ - public function check3DHash() + public function check3DHash($data) { - $hash_params = $this->request->get('HASHPARAMS'); - $hash_params_val = $this->request->get('HASHPARAMSVAL'); - $hash_param = $this->request->get('HASH'); + $hash_params = $data['HASHPARAMS']; + $hash_params_val = $data['HASHPARAMSVAL']; + $hash_param = $data['HASH']; $params_val = ''; - $index1 = 0; - - while ($index1 < strlen($hash_params)) { - $index2 = strpos($hash_params, ':', $index1); - $value = $this->request->get(substr($hash_params, $index1, $index2 - $index1)); - - if ($value == null) $value = ''; - $params_val = $params_val . $value; - - $index1 = $index2 + 1; - } + $hashparams_arr = explode(':', $hash_params); + foreach ($hashparams_arr as $value) { + if(!empty($value) && isset($data[$value])){ + $params_val = $params_val . $data[$value]; + } + } $hash_val = $params_val . $this->account->store_key; - $hash = base64_encode(pack('H*', sha1($hash_val))); + $hash = base64_encode(sha1($hash_val, true)); $return = false; if ($hash_params && !($params_val != $hash_params_val || $hash_param != $hash)) { @@ -349,26 +344,26 @@ public function makeRegularPayment() $status = 'approved'; } - $this->response = (object) [ - 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, - 'campaign_url' => null, - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - 'original' => $this->data, + $this->response = (object)[ + 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, + 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, + 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, + 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, + 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, + 'transaction_type' => $this->type, + 'transaction' => $this->order->transaction, + 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, + 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, + 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, + 'campaign_url' => null, + 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, + 'all' => $this->data, + 'original' => $this->data, ]; return $this; @@ -385,7 +380,7 @@ public function make3DPayment() $this->request = Request::createFromGlobals(); $status = 'declined'; - if ($this->check3DHash()) { + if ($this->check3DHash($this->request->request->all())) { $contents = $this->create3DPaymentXML(); $this->send($contents); } @@ -401,44 +396,44 @@ public function make3DPayment() $status = 'approved'; } - $this->response = (object) [ - 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, - 'md_status' => $this->request->get('mdStatus'), - 'hash' => (string) $this->request->get('HASH'), - 'rand' => (string) $this->request->get('rnd'), - 'hash_params' => (string) $this->request->get('HASHPARAMS'), - 'hash_params_val' => (string) $this->request->get('HASHPARAMSVAL'), - 'masked_number' => (string) $this->request->get('maskedCreditCard'), - 'month' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Month'), - 'year' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Year'), - 'amount' => (string) $this->request->get('amount'), - 'currency' => (string) $this->request->get('currency'), - 'tx_status' => (string) $this->request->get('txstatus'), - 'eci' => (string) $this->request->get('eci'), - 'cavv' => (string) $this->request->get('cavv'), - 'xid' => (string) $this->request->get('xid'), - 'md_error_message' => (string) $this->request->get('mdErrorMsg'), - 'name' => (string) $this->request->get('firmaadi'), - 'campaign_url' => null, - 'email' => (string) $this->request->get('Email'), - 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, - 'all' => $this->data, - '3d_all' => $this->request->request->all(), + $this->response = (object)[ + 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, + 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, + 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, + 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, + 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, + 'transaction_type' => $this->type, + 'transaction' => $this->order->transaction, + 'transaction_security' => $transaction_security, + 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, + 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, + 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, + 'md_status' => $this->request->get('mdStatus'), + 'hash' => (string)$this->request->get('HASH'), + 'rand' => (string)$this->request->get('rnd'), + 'hash_params' => (string)$this->request->get('HASHPARAMS'), + 'hash_params_val' => (string)$this->request->get('HASHPARAMSVAL'), + 'masked_number' => (string)$this->request->get('maskedCreditCard'), + 'month' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Month'), + 'year' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Year'), + 'amount' => (string)$this->request->get('amount'), + 'currency' => (string)$this->request->get('currency'), + 'tx_status' => (string)$this->request->get('txstatus'), + 'eci' => (string)$this->request->get('eci'), + 'cavv' => (string)$this->request->get('cavv'), + 'xid' => (string)$this->request->get('xid'), + 'md_error_message' => (string)$this->request->get('mdErrorMsg'), + 'name' => (string)$this->request->get('firmaadi'), + 'campaign_url' => null, + 'email' => (string)$this->request->get('Email'), + 'extra' => isset($this->data->Extra) ? $this->data->Extra : null, + 'all' => $this->data, + '3d_all' => $this->request->request->all(), ]; return $this; @@ -455,7 +450,7 @@ public function make3DPayPayment() $status = 'declined'; - if ($this->check3DHash() && (string) $this->request->get('ProcReturnCode') == '00') { + if ($this->check3DHash($this->request->request->all()) && (string)$this->request->get('ProcReturnCode') == '00') { if (in_array($this->request->get('mdStatus'), [1, 2, 3, 4])) { $status = 'approved'; } @@ -470,41 +465,41 @@ public function make3DPayPayment() } } - $this->response = (object) [ - 'id' => (string) $this->request->get('AuthCode'), - 'trans_id' => (string) $this->request->get('TransId'), - 'auth_code' => (string) $this->request->get('AuthCode'), - 'host_ref_num' => (string) $this->request->get('HostRefNum'), - 'response' => (string) $this->request->get('Response'), - 'order_id' => (string) $this->request->get('oid'), - 'transaction_type' => $this->type, - 'transaction' => $this->order->transaction, - 'transaction_security' => $transaction_security, - 'code' => (string) $this->request->get('ProcReturnCode'), - 'md_status' => $this->request->get('mdStatus'), - 'status' => $status, - 'status_detail' => isset($this->codes[$this->request->get('ProcReturnCode')]) ? (string) $this->request->get('ProcReturnCode') : null, - 'hash' => (string) $this->request->get('HASH'), - 'rand' => (string) $this->request->get('rnd'), - 'hash_params' => (string) $this->request->get('HASHPARAMS'), - 'hash_params_val' => (string) $this->request->get('HASHPARAMSVAL'), - 'masked_number' => (string) $this->request->get('maskedCreditCard'), - 'month' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Month'), - 'year' => (string) $this->request->get('Ecom_Payment_Card_ExpDate_Year'), - 'amount' => (string) $this->request->get('amount'), - 'currency' => (string) $this->request->get('currency'), - 'tx_status' => (string) $this->request->get('txstatus'), - 'eci' => (string) $this->request->get('eci'), - 'cavv' => (string) $this->request->get('cavv'), - 'xid' => (string) $this->request->get('xid'), - 'error_code' => (string) $this->request->get('ErrCode'), - 'error_message' => (string) $this->request->get('ErrMsg'), - 'md_error_message' => (string) $this->request->get('mdErrorMsg'), - 'name' => (string) $this->request->get('firmaadi'), - 'email' => (string) $this->request->get('Email'), - 'campaign_url' => null, - 'extra' => $this->request->get('Extra'), - 'all' => $this->request->request->all(), + $this->response = (object)[ + 'id' => (string)$this->request->get('AuthCode'), + 'trans_id' => (string)$this->request->get('TransId'), + 'auth_code' => (string)$this->request->get('AuthCode'), + 'host_ref_num' => (string)$this->request->get('HostRefNum'), + 'response' => (string)$this->request->get('Response'), + 'order_id' => (string)$this->request->get('oid'), + 'transaction_type' => $this->type, + 'transaction' => $this->order->transaction, + 'transaction_security' => $transaction_security, + 'code' => (string)$this->request->get('ProcReturnCode'), + 'md_status' => $this->request->get('mdStatus'), + 'status' => $status, + 'status_detail' => isset($this->codes[$this->request->get('ProcReturnCode')]) ? (string)$this->request->get('ProcReturnCode') : null, + 'hash' => (string)$this->request->get('HASH'), + 'rand' => (string)$this->request->get('rnd'), + 'hash_params' => (string)$this->request->get('HASHPARAMS'), + 'hash_params_val' => (string)$this->request->get('HASHPARAMSVAL'), + 'masked_number' => (string)$this->request->get('maskedCreditCard'), + 'month' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Month'), + 'year' => (string)$this->request->get('Ecom_Payment_Card_ExpDate_Year'), + 'amount' => (string)$this->request->get('amount'), + 'currency' => (string)$this->request->get('currency'), + 'tx_status' => (string)$this->request->get('txstatus'), + 'eci' => (string)$this->request->get('eci'), + 'cavv' => (string)$this->request->get('cavv'), + 'xid' => (string)$this->request->get('xid'), + 'error_code' => (string)$this->request->get('ErrCode'), + 'error_message' => (string)$this->request->get('ErrMsg'), + 'md_error_message' => (string)$this->request->get('mdErrorMsg'), + 'name' => (string)$this->request->get('firmaadi'), + 'email' => (string)$this->request->get('Email'), + 'campaign_url' => null, + 'extra' => $this->request->get('Extra'), + 'all' => $this->request->request->all(), ]; return $this; @@ -522,49 +517,40 @@ public function get3DFormData() if ($this->order) { $this->order->hash = $this->create3DHash(); - $card_type = null; - if (isset($this->card->type)) { - if ($this->card->type == 'visa') { - $card_type = '1'; - } elseif ($this->card->type == 'master') { - $card_type = '2'; - } - } - $inputs = [ - 'clientid' => $this->account->client_id, - 'storetype' => $this->account->model, - 'hash' => $this->order->hash, - 'cardType' => $card_type, - 'pan' => $this->card->number, - 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, - 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, - 'cv2' => $this->card->cvv, - 'firmaadi' => $this->order->name, - 'Email' => $this->order->email, - 'amount' => $this->order->amount, - 'oid' => $this->order->id, - 'okUrl' => $this->order->success_url, - 'failUrl' => $this->order->fail_url, - 'rnd' => $this->order->rand, - 'lang' => $this->order->lang, - 'currency' => $this->order->currency, + 'clientid' => $this->account->client_id, + 'storetype' => $this->account->model, + 'hash' => $this->order->hash, + 'cardType' => $this->getCardCode(), + 'pan' => $this->card->number, + 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, + 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, + 'cv2' => $this->card->cvv, + 'firmaadi' => $this->order->name, + 'Email' => $this->order->email, + 'amount' => $this->order->amount, + 'oid' => $this->order->id, + 'okUrl' => $this->order->success_url, + 'failUrl' => $this->order->fail_url, + 'rnd' => $this->order->rand, + 'lang' => $this->order->lang, + 'currency' => $this->order->currency, ]; if ($this->account->model == '3d_pay') { $inputs = array_merge($inputs, [ 'islemtipi' => $this->type, - 'taksit' => $this->order->installment, + 'taksit' => $this->order->installment, ]); } $data = [ - 'gateway' => $this->gateway, - 'success_url' => $this->order->success_url, - 'fail_url' => $this->order->fail_url, - 'rand' => $this->order->rand, - 'hash' => $this->order->hash, - 'inputs' => $inputs, + 'gateway' => $this->gateway, + 'success_url' => $this->order->success_url, + 'fail_url' => $this->order->fail_url, + 'rand' => $this->order->rand, + 'hash' => $this->order->hash, + 'inputs' => $inputs, ]; } @@ -583,12 +569,10 @@ public function send($contents) $client = new Client(); $response = $client->request('POST', $this->url, [ - 'body' => $contents + 'body' => $contents ]); - $xml = new SimpleXMLElement($response->getBody()); - - $this->data = (object) json_decode(json_encode($xml)); + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); return $this; } @@ -656,12 +640,12 @@ public function payment($card) public function refund(array $meta) { $nodes = [ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Type' => 'Credit', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Type' => 'Credit', ] ]; @@ -675,19 +659,19 @@ public function refund(array $meta) $status = 'approved'; } - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, + $this->response = (object)[ + 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, + 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, + 'response' => isset($this->data->Response) ? $this->data->Response : null, + 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, + 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, + 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, + 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, + 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $this->data, ]; return $this; @@ -703,12 +687,12 @@ public function refund(array $meta) public function cancel(array $meta) { $xml = $this->createXML([ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Type' => 'Void', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Type' => 'Void', ] ], 'ISO-8859-9'); @@ -719,19 +703,19 @@ public function cancel(array $meta) $status = 'approved'; } - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, - 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, - 'response' => isset($this->data->Response) ? $this->data->Response : null, - 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, - 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, + $this->response = (object)[ + 'order_id' => isset($this->data->OrderId) ? $this->data->OrderId : null, + 'group_id' => isset($this->data->GroupId) ? $this->data->GroupId : null, + 'response' => isset($this->data->Response) ? $this->data->Response : null, + 'auth_code' => isset($this->data->AuthCode) ? $this->data->AuthCode : null, + 'host_ref_num' => isset($this->data->HostRefNum) ? $this->data->HostRefNum : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->data->ProcReturnCode : null, + 'trans_id' => isset($this->data->TransId) ? $this->data->TransId : null, + 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->data->Extra->ERRORCODE : null, + 'error_message' => isset($this->data->ErrMsg) ? $this->data->ErrMsg : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $this->data, ]; return $this; @@ -747,13 +731,13 @@ public function cancel(array $meta) public function status(array $meta) { $xml = $this->createXML([ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Extra' => [ - 'ORDERSTATUS' => 'QUERY', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Extra' => [ + 'ORDERSTATUS' => 'QUERY', ], ] ], 'ISO-8859-9'); @@ -769,24 +753,24 @@ public function status(array $meta) $capture_amount = isset($this->data->Extra->CAPTURE_AMT) ? $this->printData($this->data->Extra->CAPTURE_AMT) : null; $capture = $first_amount == $capture_amount ? true : false; - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, - 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->printData($this->data->Extra->HOST_REF_NUM) : null, - 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->printData($this->data->Extra->ORDERSTATUS) : null, - 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->printData($this->data->Extra->CHARGE_TYPE_CD) : null, - 'pan' => isset($this->data->Extra->PAN) ? $this->printData($this->data->Extra->PAN) : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, - 'first_amount' => $first_amount, - 'capture_amount' => $capture_amount, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'capture' => $capture, - 'all' => $this->data, - 'xml' => $xml, + $this->response = (object)[ + 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, + 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, + 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, + 'host_ref_num' => isset($this->data->Extra->HOST_REF_NUM) ? $this->printData($this->data->Extra->HOST_REF_NUM) : null, + 'order_status' => isset($this->data->Extra->ORDERSTATUS) ? $this->printData($this->data->Extra->ORDERSTATUS) : null, + 'process_type' => isset($this->data->Extra->CHARGE_TYPE_CD) ? $this->printData($this->data->Extra->CHARGE_TYPE_CD) : null, + 'pan' => isset($this->data->Extra->PAN) ? $this->printData($this->data->Extra->PAN) : null, + 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, + 'first_amount' => $first_amount, + 'capture_amount' => $capture_amount, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'capture' => $capture, + 'all' => $this->data, + 'xml' => $xml, ]; return $this; @@ -802,13 +786,13 @@ public function status(array $meta) public function history(array $meta) { $xml = $this->createXML([ - 'CC5Request' => [ - 'Name' => $this->account->username, - 'Password' => $this->account->password, - 'ClientId' => $this->account->client_id, - 'OrderId' => $meta['order_id'], - 'Extra' => [ - 'ORDERHISTORY' => 'QUERY', + 'CC5Request' => [ + 'Name' => $this->account->username, + 'Password' => $this->account->password, + 'ClientId' => $this->account->client_id, + 'OrderId' => $meta['order_id'], + 'Extra' => [ + 'ORDERHISTORY' => 'QUERY', ], ] ], 'ISO-8859-9'); @@ -820,19 +804,77 @@ public function history(array $meta) $status = 'approved'; } - $this->response = (object) [ - 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, - 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, - 'trans_count' => isset($this->data->Extra->TRXCOUNT) ? $this->printData($this->data->Extra->TRXCOUNT) : null, - 'status' => $status, - 'status_detail' => $this->getStatusDetail(), - 'all' => $this->data, - 'xml' => $xml, + $this->response = (object)[ + 'order_id' => isset($this->data->OrderId) ? $this->printData($this->data->OrderId) : null, + 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, + 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'error_message' => isset($this->data->ErrMsg) ? $this->printData($this->data->ErrMsg) : null, + 'num_code' => isset($this->data->Extra->NUMCODE) ? $this->printData($this->data->Extra->NUMCODE) : null, + 'trans_count' => isset($this->data->Extra->TRXCOUNT) ? $this->printData($this->data->Extra->TRXCOUNT) : null, + 'status' => $status, + 'status_detail' => $this->getStatusDetail(), + 'all' => $this->data, + 'xml' => $xml, ]; return $this; } + + /** + * @return array + */ + public function getConfig() + { + return $this->config; + } + + /** + * @return mixed + */ + public function getAccount() + { + return $this->account; + } + + /** + * @return array + */ + public function getCurrencies() + { + return $this->currencies; + } + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } + + /** + * @return mixed + */ + public function getCard() + { + return $this->card; + } + + /** + * @return string|null + */ + public function getCardCode() + { + $card_type = null; + if (isset($this->card->type)) { + if ($this->card->type == 'visa') { + $card_type = '1'; + } elseif ($this->card->type == 'master') { + $card_type = '2'; + }elseif($this->card->type == '1' || $this->card->type == '2'){ + $card_type = $this->card->type; + } + } + return $card_type; + } } diff --git a/src/GarantiPos.php b/src/GarantiPos.php index 660cfc23..d98f7b4d 100644 --- a/src/GarantiPos.php +++ b/src/GarantiPos.php @@ -6,7 +6,6 @@ use GuzzleHttp\Exception\GuzzleException; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; -use SimpleXMLElement; use Symfony\Component\HttpFoundation\Request; /** @@ -257,7 +256,7 @@ protected function make3dRequestHashData($security_data) * @param double $amount * @return int */ - protected function amountFormat($amount) + public function amountFormat($amount) { return (int) str_replace('.', '', number_format($amount, 2, '.', '')); } @@ -471,7 +470,7 @@ public function create3DHash() $hash_str = $this->account->client_id . $this->order->id . $this->order->amount . $this->order->success_url . $this->order->fail_url . $this->order->transaction_type . $this->order->installment . $this->order->rand . $this->account->store_key; } - return base64_encode(pack('H*', sha1($hash_str))); + return base64_encode(sha1($hash_str, true)); } /** @@ -719,9 +718,7 @@ public function send($contents) 'body' => $contents ]); - $xml = new SimpleXMLElement($response->getBody()); - - $this->data = (object) json_decode(json_encode($xml)); + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); return $this; } @@ -1011,4 +1008,44 @@ public function history(array $meta) { return $this->statusOrHistory($meta, 'orderhistoryinq'); } + + /** + * @return array + */ + public function getConfig() + { + return $this->config; + } + + /** + * @return mixed + */ + public function getAccount() + { + return $this->account; + } + + /** + * @return array + */ + public function getCurrencies() + { + return $this->currencies; + } + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } + + /** + * @return mixed + */ + public function getCard() + { + return $this->card; + } } diff --git a/src/Pos.php b/src/Pos.php index 82614060..f5ed872b 100644 --- a/src/Pos.php +++ b/src/Pos.php @@ -156,6 +156,41 @@ public function getGatewayUrl() return isset($this->bank->gateway) ? $this->bank->gateway : 'null'; } + /** + * @return array + */ + public function getConfig(){ + return $this->bank->getConfig(); + } + + /** + * @return mixed + */ + public function getAccount(){ + return $this->bank->getAccount(); + } + + /** + * @return array + */ + public function getCurrencies(){ + return $this->bank->getCurrencies(); + } + + /** + * @return mixed + */ + public function getOrder(){ + return $this->bank->getOrder(); + } + + /** + * @return mixed + */ + public function getCard(){ + return $this->bank->getCard(); + } + /** * Get 3d Form Data * diff --git a/src/PosHelpersTrait.php b/src/PosHelpersTrait.php index d1f46d9c..27a78ae1 100644 --- a/src/PosHelpersTrait.php +++ b/src/PosHelpersTrait.php @@ -3,6 +3,7 @@ namespace Mews\Pos; use Symfony\Component\Serializer\Encoder\XmlEncoder; +use SimpleXMLElement; /** * Trait PosHelpersTrait @@ -34,12 +35,12 @@ trait PosHelpersTrait public function createXML(array $nodes, $encoding = 'UTF-8') { $rootNodeName = array_keys($nodes)[0]; - $encoder = new XmlEncoder($rootNodeName); + $encoder = new XmlEncoder(); $xml = $encoder->encode($nodes[$rootNodeName], 'xml', [ - 'xml_encoding' => $encoding + XmlEncoder::ROOT_NODE_NAME => $rootNodeName, + XmlEncoder::ENCODING => $encoding ]); - return $xml; } @@ -51,11 +52,11 @@ public function createXML(array $nodes, $encoding = 'UTF-8') */ public function printData($data) { - if ((is_object($data) || is_array($data)) && !count((array) $data)) { + if ((is_object($data) || is_array($data)) && !count((array)$data)) { $data = null; } - return (string) $data; + return (string)$data; } /** @@ -82,4 +83,17 @@ public function isError() { return !$this->isSuccess(); } + + /** + * Converts XML string to object + * + * @param string data + * @return object + */ + public function XMLStringToObject($data) + { + $encoder = new XmlEncoder(); + $xml = $encoder->decode($data, 'xml'); + return (object)json_decode(json_encode($xml)); + } } diff --git a/src/PosNet.php b/src/PosNet.php index 912c6d66..083e1068 100644 --- a/src/PosNet.php +++ b/src/PosNet.php @@ -6,7 +6,6 @@ use GuzzleHttp\Exception\GuzzleException; use Mews\Pos\Exceptions\UnsupportedPaymentModelException; use Mews\Pos\Exceptions\UnsupportedTransactionTypeException; -use SimpleXMLElement; use Symfony\Component\HttpFoundation\Request; /** @@ -170,9 +169,7 @@ public function __construct($config, $account, array $currencies) $request = Request::createFromGlobals(); $this->request = $request->request; - $this->crypt = function_exists('mcrypt_encrypt') ? - new PosNetCrypt : - null; + $this->crypt = new PosNetCrypt(); $this->config = $config; $this->account = $account; @@ -215,15 +212,33 @@ protected function getAmount() } /** + * Get PrefixedOrderId + * To check the status of an order or cancel/refund order Yapikredi + * - requires the order length to be 24 + * - and order id prefix which is "TDSC" for 3D payments + * @return string + */ + protected function getPrefixedOrderId() + { + if($this->account->model == '3d'){ + return $this->config['order']['id_3d_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_3d_prefix'])); + }elseif($this->account->model == '3d_pay') { + return $this->config['order']['id_3d_pay_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_3d_pay_prefix'])); + } + return $this->config['order']['id_regular_prefix'] . $this->getOrderId($this->config['order']['id_total_length'] - strlen($this->config['order']['id_regular_prefix'])); + } + + /** * Get orderId * * @param int $pad_length * @return string */ - protected function getOrderId(int $pad_length = 24) + protected function getOrderId(int $pad_length = null) { - return (string) str_pad($this->order->id, $pad_length, '0', STR_PAD_LEFT); - } + if($pad_length === null) $pad_length = $this->config['order']['id_length']; + return (string) str_pad($this->order->id, $pad_length, '0', STR_PAD_LEFT); + } /** * Get Installment @@ -307,6 +322,7 @@ protected function create3DPaymentXML() 'bankData' => $this->request->get('BankPacket'), 'merchantData' => $this->request->get('MerchantPacket'), 'sign' => $this->request->get('Sign'), + 'mac' => $this->create3DHash() ], ] ]; @@ -374,7 +390,7 @@ public function getOosTransactionData() 'amount' => $this->getAmount(), 'currencyCode' => $this->getCurrency(), 'installment' => $this->getInstallment(), - 'XID' => $this->getOrderId(20), + 'XID' => $this->getOrderId(), 'cardHolderName' => $name, 'tranType' => $this->type, ] @@ -407,8 +423,7 @@ public function makeRegularPayment() $code = '1'; $proc_return_code = '01'; $obj = isset($this->data) ? $this->data : null; - $error_code = isset($obj->respCode) ? $obj->respCode : null; - $error_message = isset($obj->respText) ? $obj->respText : null; + $error_code = !empty($obj->respCode) ? $obj->respCode : null; if ($this->getProcReturnCode() == '00' && $this->getStatusDetail() == 'approved' && $obj && !$error_code) { $status = 'approved'; @@ -420,7 +435,7 @@ public function makeRegularPayment() 'id' => isset($obj->authCode) ? $this->printData($obj->authCode) : null, 'order_id' => $this->order->id, 'fixed_order_id' => $this->getOrderId(), - 'group_id' => isset($obj->groupID) ? $this->printData($obj->Order->groupID) : null, + 'group_id' => isset($obj->groupID) ? $this->printData($obj->groupID) : null, 'trans_id' => isset($obj->authCode) ? $this->printData($obj->authCode) : null, 'response' => $this->getStatusDetail(), 'transaction_type' => $this->type, @@ -433,7 +448,7 @@ public function makeRegularPayment() 'status' => $status, 'status_detail' => $this->getStatusDetail(), 'error_code' => $error_code, - 'error_message' => $error_message, + 'error_message' => !empty($obj->respText) ? $this->printData($obj->respText) : null, 'campaign_url' => null, 'extra' => null, 'all' => $this->data, @@ -443,19 +458,6 @@ public function makeRegularPayment() return $this; } - /** - * Get host name - * - * @param $url - * @return string - */ - public function getHostName($url) - { - $parse = parse_url($url); - - return $parse['host']; - } - /** * Check 3D Hash * @@ -467,7 +469,6 @@ protected function check3DHash() if ($this->crypt instanceof PosNetCrypt) { $decrypted_data = $this->crypt->decrypt($this->request->get('MerchantPacket'), $this->account->store_key); - $this->crypt->deInit(); $decrypted_data_array = explode(';', $decrypted_data); @@ -476,8 +477,7 @@ protected function check3DHash() $this->account->terminal_id, $this->getAmount(), $this->getInstallment(), - $this->getOrderId(20), - $this->getHostName($this->url), + $this->getOrderId() ]); $decrypted_data_list = array_map('strval', [ @@ -485,8 +485,7 @@ protected function check3DHash() $decrypted_data_array[1], $decrypted_data_array[2], $decrypted_data_array[3], - $decrypted_data_array[4], - $this->getHostName($decrypted_data_array[7]), + $decrypted_data_array[4] ]); if ($original_data == $decrypted_data_list) { @@ -513,6 +512,16 @@ public function make3DPayment() if ($this->check3DHash()) { $contents = $this->create3DPaymentXML(); $this->send($contents); + }else{ + goto end; + } + + if($this->getProcReturnCode() != '00'){ + goto end; + } + + if(!$this->verifyResponseMAC($this->data->oosResolveMerchantDataResponse)) { + goto end; } if ($this->getProcReturnCode() == '00' && $this->getStatusDetail() == 'approved') { @@ -532,7 +541,8 @@ public function make3DPayment() 'bankData' => $this->request->get('BankPacket'), 'merchantData' => $this->request->get('MerchantPacket'), 'sign' => $this->request->get('Sign'), - 'wpAmount' => $this->data->oosResolveMerchantDataResponse->amount, + 'wpAmount' => 0, + 'mac' => $this->create3DHash() ], ] ]; @@ -547,23 +557,26 @@ public function make3DPayment() $status = 'declined'; } + end: $this->response = (object) [ - 'id' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, + 'id' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, 'order_id' => isset($this->order->id) ? $this->printData($this->order->id) : null, - 'group_id' => isset($this->data->GroupId) ? $this->printData($this->data->GroupId) : null, - 'trans_id' => isset($this->data->TransId) ? $this->printData($this->data->TransId) : null, - 'response' => isset($this->data->Response) ? $this->printData($this->data->Response) : null, + 'fixed_order_id' => $this->getOrderId(), + 'group_id' => isset($this->data->groupID) ? $this->printData($this->data->groupID) : null, + 'trans_id' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, + 'response' => $this->getStatusDetail(), 'transaction_type' => $this->type, 'transaction' => $this->order->transaction, 'transaction_security' => $transaction_security, - 'auth_code' => isset($this->data->AuthCode) ? $this->printData($this->data->AuthCode) : null, - 'host_ref_num' => isset($this->data->HostRefNum) ? $this->printData($this->data->HostRefNum) : null, - 'proc_return_code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, - 'code' => isset($this->data->ProcReturnCode) ? $this->printData($this->data->ProcReturnCode) : null, + 'auth_code' => isset($this->data->authCode) ? $this->printData($this->data->authCode) : null, + 'host_ref_num' => isset($this->data->hostlogkey) ? $this->printData($this->data->hostlogkey) : null, + 'ret_ref_num' => isset($this->data->transaction->hostlogkey) ? $this->printData($this->data->transaction->hostlogkey) : null, + 'proc_return_code' => $this->getProcReturnCode(), + 'code' => $this->getProcReturnCode(), 'status' => $status, 'status_detail' => $this->getStatusDetail(), - 'error_code' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->Extra->ERRORCODE) : null, - 'error_message' => isset($this->data->Extra->ERRORCODE) ? $this->printData($this->data->ErrMsg) : null, + 'error_code' => !empty($this->data->respCode) ? $this->printData($this->data->respCode) : null, + 'error_message' => !empty($this->data->respText) ? $this->printData($this->data->respText) : null, 'md_status' => isset($this->data->oosResolveMerchantDataResponse->mdStatus) ? $this->printData($this->data->oosResolveMerchantDataResponse->mdStatus) : null, 'hash' => [ 'merchant_packet' => $this->request->get('MerchantPacket'), @@ -586,8 +599,7 @@ public function make3DPayment() */ public function make3DPayPayment() { - $this->make3DPayPayment(); - + //TODO return $this; } @@ -648,9 +660,7 @@ public function send($contents) 'body' => "xmldata=" . $contents, ]); - $xml = new SimpleXMLElement($response->getBody()); - - $this->data = (object) json_decode(json_encode($xml)); + $this->data = $this->XMLStringToObject($response->getBody()->getContents()); return $this; } @@ -736,7 +746,7 @@ protected function refundOrCancel(array $meta, $type = 'cancel') $return = [ 'amount' => $this->getAmount(), 'currencyCode' => $this->getCurrency(), - 'orderID' => $this->getOrderId(), + 'orderID' => $this->getPrefixedOrderId(), ]; if ($this->order->host_ref_num) { @@ -750,7 +760,7 @@ protected function refundOrCancel(array $meta, $type = 'cancel') } else { $reverse = [ 'transaction' => 'pointUsage', - 'orderID' => $this->getOrderId(), + 'orderID' => $this->getPrefixedOrderId(), 'authCode' => $this->order->auth_code, ]; @@ -779,8 +789,7 @@ protected function refundOrCancel(array $meta, $type = 'cancel') $code = '1'; $proc_return_code = '01'; $obj = isset($this->data) ? $this->data : null; - $error_code = isset($obj->respCode) ? $obj->respCode : null; - $error_message = null; + $error_code = !empty($obj->respCode) ? $obj->respCode : null; if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { $status = 'approved'; @@ -788,8 +797,6 @@ protected function refundOrCancel(array $meta, $type = 'cancel') $proc_return_code = $this->getProcReturnCode(); } - $error_message = isset($obj->respText) ? $obj->respText : null; - $transaction = null; $transaction_type = null; $state = isset($obj->state) ? $obj->state : null; @@ -812,8 +819,8 @@ protected function refundOrCancel(array $meta, $type = 'cancel') 'trans_id' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, 'response' => $this->getStatusDetail(), 'auth_code' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, - 'host_ref_num' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, - 'ret_ref_num' => isset($obj->transaction->authCode) ? $this->printData($obj->transaction->authCode) : null, + 'host_ref_num' => isset($obj->transaction->hostlogkey) ? $this->printData($obj->transaction->hostlogkey) : null, + 'ret_ref_num' => isset($obj->transaction->hostlogkey) ? $this->printData($obj->transaction->hostlogkey) : null, 'transaction' => $transaction, 'transaction_type' => $transaction_type, 'state' => $state, @@ -823,7 +830,7 @@ protected function refundOrCancel(array $meta, $type = 'cancel') 'status' => $status, 'status_detail' => $this->getStatusDetail(), 'error_code' => $error_code, - 'error_message' => $error_message, + 'error_message' => !empty($obj->respText) ? $this->printData($obj->respText) : null, 'extra' => null, 'all' => $this->data, 'original' => $this->data, @@ -877,7 +884,7 @@ public function status(array $meta, $history = false) 'mid' => $this->account->client_id, 'tid' => $this->account->terminal_id, 'agreement' => [ - 'orderID' => $this->getOrderId(), + 'orderID' => $this->getPrefixedOrderId(), ], ] ]); @@ -888,8 +895,7 @@ public function status(array $meta, $history = false) $code = '1'; $proc_return_code = '01'; $obj = isset($this->data->transactions) ? $this->data->transactions : null; - $error_code = isset($this->data->respCode) ? $this->data->respCode : null; - $error_message = null; + $error_code = !empty($this->data->respCode) ? $this->data->respCode : null; if ($this->getProcReturnCode() == '00' && $obj && !$error_code) { $status = 'approved'; @@ -897,8 +903,6 @@ public function status(array $meta, $history = false) $proc_return_code = $this->getProcReturnCode(); } - $error_message = isset($this->data->respText) ? $this->data->respText : null; - $transaction = null; $transaction_type = null; @@ -962,7 +966,7 @@ public function status(array $meta, $history = false) 'trans_id' => $auth_code, 'response' => $this->getStatusDetail(), 'auth_code' => $auth_code, - 'host_ref_num' => null, + 'host_ref_num' => isset($obj->transaction->hostLogKey) ? $this->printData($obj->transaction->hostLogKey) : null, 'ret_ref_num' => null, 'transaction' => $transaction, 'transaction_type' => $transaction_type, @@ -974,7 +978,7 @@ public function status(array $meta, $history = false) 'status' => $status, 'status_detail' => $this->getStatusDetail(), 'error_code' => $error_code, - 'error_message' => $error_message, + 'error_message' => !empty($this->data->respText) ? $this->printData($this->data->respText) : null, 'extra' => null, 'all' => $this->data, 'original' => $this->data, @@ -1000,4 +1004,86 @@ public function history(array $meta) { return $this->status($meta, true); } + + /** + * @return array + */ + public function getConfig(){ + return $this->config; + } + + /** + * @return mixed + */ + public function getAccount(){ + return $this->account; + } + + /** + * @return array + */ + public function getCurrencies(){ + return $this->currencies; + } + + /** + * @return mixed + */ + public function getOrder(){ + return $this->order; + } + + /** + * @return mixed + */ + public function getCard(){ + return $this->card; + } + + /** + * Hash string + * + * @return string + */ + public function hashString(string $str) + { + return base64_encode(hash('sha256',$str,true)); + } + + /** + * Create 3D Hash (MAC) + * + * @return string + */ + public function create3DHash() + { + $hash_str = ''; + + $firstHash = $this->hashString($this->account->store_key . ";" . $this->account->terminal_id); + + if ($this->account->model == '3d' || $this->account->model == '3d_pay') { + $hash_str = $this->hashString($this->getOrderId() . ";" . $this->getAmount() . ";" . $this->getCurrency() . ";" . $this->account->client_id . ";" . $firstHash); + } + + return $hash_str; + } + + /** + * verifies the if request came from bank + * + * @param mixed $data oosResolveMerchantDataResponse + * @return boolean + */ + public function verifyResponseMAC($data) + { + $hash_str = ''; + + $firstHash = $this->hashString($this->account->store_key . ";" . $this->account->terminal_id); + + if ($this->account->model == '3d' || $this->account->model == '3d_pay') { + $hash_str = $this->hashString($data->mdStatus . ";" . $this->getOrderId() . ";" . $this->getAmount() . ";" . $this->getCurrency() . ";" . $this->account->client_id . ";" . $firstHash); + } + + return $hash_str == $data->mac; + } } diff --git a/src/PosNetCrypt.php b/src/PosNetCrypt.php index df7ddbef..649b3a10 100644 --- a/src/PosNetCrypt.php +++ b/src/PosNetCrypt.php @@ -9,26 +9,25 @@ class PosNetCrypt { /** - * @var resource + * @var string */ - public $td; + private $algo; /** * @var int */ - public $ks; + private $ks; /** * @var int */ - public $block; + private $block; /** - * Error message for http connection * * @access private */ - public $error; + private $error; /** * PosNetCrypt constructor. @@ -36,9 +35,9 @@ class PosNetCrypt public function __construct () { srand((double) microtime() * 10000000); - $this->block = @mcrypt_get_block_size(MCRYPT_TripleDES, MCRYPT_MODE_CBC); - $this->td = @mcrypt_module_open(MCRYPT_TripleDES, '', MCRYPT_MODE_CBC, ''); - $this->ks = @mcrypt_enc_get_key_size($this->td); + $this->algo = 'des-ede3-cbc'; + $this->block = 8; + $this->ks = 24; $this->error = ''; } @@ -75,14 +74,8 @@ public function encrypt($data, $key) // Create IV $iv = $this->createIV(); - // PKCS Padding - $data = $this->doPadding($data); - - // Initialize - @mcrypt_generic_init($this->td, $this->detKey($key), $iv); - // Encrypt Data - $encrypted_data = @mcrypt_generic($this->td, $data); + $encrypted_data = openssl_encrypt($data, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); // Add IV and Convert to HEX $hex_encrypted_data = strtoupper(bin2hex($iv)).strtoupper(bin2hex($encrypted_data)); @@ -100,31 +93,26 @@ public function encrypt($data, $key) */ public function decrypt($data, $key) { - if (strlen($data) < 16 + 8) return false; + $parsed_data = $this->parseEncryptedData($data); - // Get IV - $iv = pack("H*", substr($data, 0, 16)); - - // Get Encrypted Data - $encrypted_data = pack("H*", substr($data, 16, strlen($data)-16-8)); - - // Get CRC - $crc = substr($data, -8); + if (!$parsed_data) return false; // Check CRC - if (!$this->checkCrc(substr($data, 0, strlen($data)-8), $crc)) { - $this->error = "CRC is not valid! ($crc)"; - return ''; + if (!$this->checkCrc($parsed_data['crc_data'], $parsed_data['crc'])) { + $this->error = "CRC is not valid! (" . $parsed_data['crc'] . ")"; + return FALSE; } - // Initialize - @mcrypt_generic_init($this->td, $this->detKey($key), $iv); + // Get IV + $iv = pack("H*", $parsed_data['iv']); + + // Get Encrypted Data + $encrypted_data = pack("H*", $parsed_data['payload']); // Decrypt Data - $decrypted_data = @mdecrypt_generic($this->td, $encrypted_data); + $decrypted_data = openssl_decrypt($encrypted_data, $this->algo, $this->detKey($key), OPENSSL_RAW_DATA, $iv); - // Remove Padded Data - return $this->removePaddedData($decrypted_data); + return $decrypted_data; } /** @@ -137,39 +125,6 @@ public function detKey($key) return $deskey; } - /** - * @param $data - * @return string - */ - public function doPadding($data) - { - $len = strlen($data); - $padding = $this->block - ($len % $this->block); - $data .= str_repeat(chr($padding), $padding); - - return $data; - } - - /** - * @param $data - * @return bool|string - */ - public function removePaddedData($data) - { - $packing = ord($data { strlen($data) - 1 }); - - if ($packing and ($packing < $this->block)) { - for($P = strlen($data) - 1; $P >= strlen($data) - $packing; $P--) { - if (ord($data { $P } ) != $packing) { - $packing = 0; - } - } - } - - $data = substr($data, 0, strlen($data) - $packing); - return $data; - } - /** * @param $data * @return string @@ -198,11 +153,18 @@ public function checkCrc($data, $crc) } /** - * + * @param string $data + * @return array|bool */ - public function deInit() - { - @mcrypt_generic_deinit($this->td); - @mcrypt_module_close($this->td); + private function parseEncryptedData(string $data){ + + if (strlen($data) < 16 + 8) return false; + + return [ + 'crc' => substr($data, -8), + 'crc_data' => substr($data, 0, strlen($data)-8), + 'iv' => substr($data, 0, 16), + 'payload' => substr($data, 16, strlen($data)-16-8) + ]; } } diff --git a/tests/EstPostTest.php b/tests/EstPostTest.php new file mode 100644 index 00000000..b468a4dc --- /dev/null +++ b/tests/EstPostTest.php @@ -0,0 +1,149 @@ +config = require __DIR__ . '/../config/pos.php'; + + $this->account = (object)[ + 'bank' => 'akbank', + 'model' => '3d', + 'client_id' => 'XXXXXXX', + 'username' => 'XXXXXXX', + 'password' => 'XXXXXXX', + 'store_key' => 'VnM5WZ3sGrPusmWP', + 'env' => 'test', + ]; + + $this->card = (object)[ + 'number' => '5555444433332222', + 'year' => '21', + 'month' => '12', + 'cvv' => '122', + 'name' => 'ahmet', + 'type' => 'visa' + ]; + + $this->order = (object)[ + 'id' => 'order222', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '100.25', + 'installment' => 0, + 'currency' => 'TRY', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime() + ]; + + $this->estpos = new EstPos( + $this->config['banks'][$this->account->bank], + $this->account, + $this->config['currencies']); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->bank], $this->estpos->getConfig()); + $this->assertEquals($this->account, $this->estpos->getAccount()); + $this->assertEquals($this->config['currencies'], $this->estpos->getCurrencies()); + } + + public function testPrepare() + { + + $this->estpos->prepare($this->order, $this->card); + $this->assertEquals($this->card, $this->estpos->getCard()); + $this->assertEquals($this->order, $this->estpos->getOrder()); + } + + public function testGetCardCode() + { + $card = $this->card; + + $card->type = '1'; + $this->estpos->prepare($this->order, $card); + $this->assertEquals($card->type, $this->estpos->getCardCode()); + + $card->type = 'visa'; + $this->estpos->prepare($this->order, $card); + $this->assertNotNull($this->estpos->getCardCode()); + + $card->type = 'master'; + $this->estpos->prepare($this->order, $card); + $this->assertNotNull($this->estpos->getCardCode()); + } + + public function testGet3DFormData() + { + $this->estpos->prepare($this->order, $this->card); + + $form = [ + 'gateway' => $this->config['banks'][$this->account->bank]['urls']['gateway'][$this->account->env], + 'success_url' => $this->order->success_url, + 'fail_url' => $this->order->fail_url, + 'rand' => $this->order->rand, + 'hash' => $this->estpos->create3DHash(), + 'inputs' => [ + 'clientid' => $this->account->client_id, + 'storetype' => $this->account->model, + 'hash' => $this->estpos->create3DHash(), + 'cardType' => $this->estpos->getCardCode(), + 'pan' => $this->card->number, + 'Ecom_Payment_Card_ExpDate_Month' => $this->card->month, + 'Ecom_Payment_Card_ExpDate_Year' => $this->card->year, + 'cv2' => $this->card->cvv, + 'firmaadi' => $this->order->name, + 'Email' => $this->order->email, + 'amount' => $this->order->amount, + 'oid' => $this->order->id, + 'okUrl' => $this->order->success_url, + 'failUrl' => $this->order->fail_url, + 'rnd' => $this->order->rand, + 'lang' => $this->order->lang, + 'currency' => $this->order->currency, + ] + ]; + $this->assertEquals($form, $this->estpos->get3DFormData()); + } + + public function testCheck3DHash() + { + $data = [ + "md" => "478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568", + "cavv" => "BwAQAhIYRwEAABWGABhHEE6v5IU=", + "AuthCode" => "", + "oid" => "880", + "mdStatus" => "4", + "eci" => "06", + "clientid" => "400902568", + "rnd" => "hDx50d0cq7u1vbpWQMae", + "ProcReturnCode" => "N7", + "Response" => "Declined", + "HASH" => "D+B5fFWXEWFqVSkwotyuTPUW800=", + "HASHPARAMS" => "clientid:oid:AuthCode:ProcReturnCode:Response:mdStatus:cavv:eci:md:rnd:", + "HASHPARAMSVAL" => "400902568880N7Declined4BwAQAhIYRwEAABWGABhHEE6v5IU=06478719:0373D10CFD8BDED34FA0546D27D5BE76F8BA4A947D1EC499102AE97B880EB1B9:4242:##400902568hDx50d0cq7u1vbpWQMae" + ]; + + $this->assertTrue($this->estpos->check3DHash($data)); + + $data['mdStatus'] = ''; + $this->assertFalse($this->estpos->check3DHash($data)); + } +} diff --git a/tests/GarantiPosTest.php b/tests/GarantiPosTest.php new file mode 100644 index 00000000..b6281242 --- /dev/null +++ b/tests/GarantiPosTest.php @@ -0,0 +1,98 @@ +config = require __DIR__ . '/../config/pos.php'; + + $this->account = (object)[ + 'bank' => 'akbank', + 'model' => '3d', + 'client_id' => 'XXXXXXX', + 'terminal_id' => '13456', + 'username' => 'XXXXXXX', + 'password' => 'XXXXXXX', + 'store_key' => 'XXXXXXX', + 'env' => 'test', + ]; + + $this->card = (object)[ + 'number' => '5555444433332222', + 'year' => '21', + 'month' => '12', + 'cvv' => '122', + 'name' => 'ahmet', + 'type' => 'visa' + ]; + + $this->order = (object)[ + 'id' => 'order222', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '100.25', + 'installment' => 0, + 'currency' => 'TRY', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime(), + 'ip' => '156.155.154.153' + ]; + + $this->garantiPos = new GarantiPos( + $this->config['banks'][$this->account->bank], + $this->account, + $this->config['currencies']); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->bank], $this->garantiPos->getConfig()); + $this->assertEquals($this->account, $this->garantiPos->getAccount()); + $this->assertEquals($this->config['currencies'], $this->garantiPos->getCurrencies()); + } + + public function testPrepare() + { + + $this->garantiPos->prepare($this->order, $this->card); + $this->assertEquals($this->card, $this->garantiPos->getCard()); + $this->assertEquals($this->order, $this->garantiPos->getOrder()); + } + + public function testGet3DFormData() + { + $this->garantiPos->prepare($this->order, $this->card); + + $form = [ + 'gateway' => $this->config['banks'][$this->account->bank]['urls']['gateway'][$this->account->env], + 'success_url' => $this->order->success_url, + 'fail_url' => $this->order->fail_url, + 'rand' => $this->order->rand + ]; + $actualForm = $this->garantiPos->get3DFormData(); + $this->assertNotEmpty($actualForm['inputs']); + $this->assertNotEmpty($actualForm['hash']); + + unset($actualForm['inputs']); + unset($actualForm['hash']); + $this->assertEquals($form, $actualForm); + } + + +} diff --git a/tests/PosNetCryptTest.php b/tests/PosNetCryptTest.php new file mode 100644 index 00000000..6ef38893 --- /dev/null +++ b/tests/PosNetCryptTest.php @@ -0,0 +1,35 @@ +crypt = new PosNetCrypt(); + } + + public function testDecrypt(){ + $data = '9ACF38C842B3522415364850EAD1909BD43FD590BE3CBD539AD5FF6C7465973ABD61E8371E03282605ED06C994DF394244B7E7DAD54A046510484FAA724330C4C95A527D7891151E7C195D4136CBD70A87D1BD1F75473CF6B45A3F2FA8231DD71FFB4150E0BF4B133ECAA5ACC82CFD74903E21BC6EECB4B33AF39B8AF0C183A64002CFC125A55685C69A13192F3A9A4FDAC860E90C3FB6D125285E9E687BEFBE05707E131FC7ABE25FE35AB114FAE8A247B8C0F3DBA8AA74396D10564B7A0617EED913ED'; + + $key = '10,10,10,10,10,10,10,10'; + $expected_output = '6706598320;67005551;100;00;YKB_TST_090519001330;0;0;https://setmpos.ykb.com/PosnetWebService/YKBTransactionService;posnettest.ykb.com;2225;N;0;Not authenticated;1557398383820;TL'; + + $dc = $this->crypt->decrypt($data, $key); + $this->assertEquals($expected_output, $dc); + + $data = '1974BC4B9984FF173F9DF305090E44241243CCC9648349C0D607AECACCB55C1F1ED47452B3AD90785F9BCC6AC7E65450D4E72F31B9FC8F9F55A7D109C2BE966C6DD3F3DE12B3457FF0C6FA8BCDBB4B8E5341C1C3DA327992C28354EB3B5C62472C06B8FB6DF34E351206D2CCD5E323FA26EC3EFF2C25656C74C836954F193E634AF391761F88B5D53DBE1FC61A89DB713DFC983D9605D080ACA857196DD3E7B3C52BFCC914CE47961D76B590ECB34B28113A6E4FAA572958D836B09546A9A62D24F829FE18628DF84504FB02'; + $expected_output = '6706598320;67825768;100;00;00000000000000000892;0;0;https://setmpos.ykb.com/PosnetWebService/YKBTransactionService;posnettest.ykb.com;2225;N;9;None 3D - Secure Transaction;1586175747626;TL'; + + $dc = $this->crypt->decrypt($data, $key); + $this->assertEquals($expected_output, $dc); + } + +} diff --git a/tests/PosNetTest.php b/tests/PosNetTest.php new file mode 100644 index 00000000..e8d9c2c6 --- /dev/null +++ b/tests/PosNetTest.php @@ -0,0 +1,110 @@ +account = (object)[ + 'bank' => 'yapikredi', + 'model' => 'regular', + 'client_id' => '6706598320', + 'terminal_id' => '67005551', + 'posnet_id' => '27426', + 'env' => 'test', + 'store_key' => '10,10,10,10,10,10,10,10', + 'model' => '3d' + ]; + + $this->card = (object)[ + 'number' => '5555444433332222', + 'year' => '21', + 'month' => '12', + 'cvv' => '122', + 'name' => 'ahmet', + 'type' => 'visa' + ]; + + $this->order = (object)[ + 'id' => 'YKB_TST_190620093100_024', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '1.75', + 'installment' => 0, + 'currency' => 'TL', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime() + ]; + + + $this->config = require __DIR__ . '/../config/pos.php'; + $this->posnet = new PosNet( + $this->config['banks'][$this->account->bank], + $this->account, + $this->config['currencies']); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account->bank], $this->posnet->getConfig()); + $this->assertEquals($this->account, $this->posnet->getAccount()); + $this->assertEquals($this->config['currencies'], $this->posnet->getCurrencies()); + } + + public function testPrepare() + { + $this->posnet->prepare($this->order, $this->card); + $this->assertEquals($this->card, $this->posnet->getCard()); + $this->assertEquals($this->order, $this->posnet->getOrder()); + } + + public function testCreate3DHash(){ + + $this->posnet->prepare($this->order, $this->card); + $this->assertEquals('J/7/Xprj7F/KDf98luVfIGyUPRQzUCqGwpmvz3KT7oQ=', $this->posnet->create3DHash()); + } + + public function testVerifyResponseMAC(){ + + $order = $this->order; + $order->id = '895'; + $order->amount = 1; + $order->currency = 'TL'; + + $account = $this->account; + $account->client_id = '6706598320'; + $account->terminal_id = '67825768'; + $account->store_key = '10,10,10,10,10,10,10,10'; + + $this->posnet->prepare($order, $account); + $data = (object)[ + 'mdStatus' => '9', + 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=' + ]; + $this->assertTrue($this->posnet->verifyResponseMAC($data)); + + $order->id = '800'; + $this->posnet->prepare($order, $account); + $data = (object)[ + 'mdStatus' => '9', + 'mac' => 'U2kU/JWjclCvKZjILq8xBJUXhyB4DswKvN+pKfxl0u0=' + ]; + $this->assertFalse($this->posnet->verifyResponseMAC($data)); + } +} diff --git a/tests/PosTest.php b/tests/PosTest.php new file mode 100644 index 00000000..f31f9030 --- /dev/null +++ b/tests/PosTest.php @@ -0,0 +1,94 @@ +config = require __DIR__ . '/../config/pos.php'; + $this->account = [ + 'bank' => 'yapikredi', + 'model' => 'regular', + 'client_id' => '6706598320', + 'terminal_id' => '67322946', + 'posnet_id' => '27426', + 'env' => 'test', + 'store_key' => '10,10,10,10,10,10,10,10' + ]; + + $this->card = [ + 'number' => '5555444433332222', + 'year' => '21', + 'month' => '12', + 'cvv' => '122', + 'name' => 'ahmet', + 'type' => 'visa' + ]; + + $this->order = [ + 'id' => 'order222', + 'name' => 'siparis veren', + 'email' => 'test@test.com', + 'amount' => '100.25', + 'installment' => 0, + 'currency' => 'TRY', + 'success_url' => 'https://domain.com/success', + 'fail_url' => 'https://domain.com/fail_url', + 'lang' => 'tr', + 'rand' => microtime() + ]; + + $this->pos = new Pos($this->account); + } + + public function testInit() + { + $this->assertEquals($this->config['banks'][$this->account['bank']], $this->pos->getConfig()); + $this->assertEquals((object)$this->account, $this->pos->getAccount()); + $this->assertEquals($this->config['currencies'], $this->pos->getCurrencies()); + $this->assertInstanceOf(PosNet::class, $this->pos->bank); + } + + public function testCreateXML() + { + $xml_str = $this->createXML($this->order); + $this->assertIsString($xml_str); + } + + public function testXMLStringToObject() + { + $xml_str = $this->createXML(['order' => $this->order]); + $this->assertEquals((object)$this->order, $this->XMLStringToObject($xml_str)); + } + + public function testPrepare() + { + + $this->pos->prepare($this->order, $this->card); + $this->assertEquals((object)$this->card, $this->pos->getCard()); + //$this->assertEquals((object)$order, $this->pos->getOrder()); + } + + public function testGetGatewayUrl() + { + $this->assertEquals($this->config['banks'][$this->account['bank']]['urls']['gateway'][$this->account['env']], $this->pos->getGatewayUrl()); + } +}