'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', ]; /** * Transaction Types * * @var array */ public $types = [ 'pay' => 'Auth', 'pre' => 'PreAuth', 'post' => 'PostAuth', ]; /** * Currencies * * @var array */ public $currencies = []; /** * Transaction Type * * @var string */ public $type; /** * API Account * * @var array */ protected $account = []; /** * Order Details * * @var array */ protected $order = []; /** * Credit Card * * @var object */ protected $card; /** * Request * * @var Request */ protected $request; /** * Response Raw Data * * @var object */ protected $data; /** * Processed Response Data * * @var mixed */ public $response; /** * Configuration * * @var array */ protected $config = []; /** * Response Raw Data * * @var object */ protected $mbrId = 5; /** * EstPos constructor. * * @param array $config * @param mixed $account * @param array $currencies */ public function __construct($config, $account, array $currencies) { $this->config = $config; $this->account = $account; $this->currencies = $currencies; $this->url = isset($this->config['urls'][$this->account->env]) ? $this->config['urls'][$this->account->env] : $this->config['urls']['production']; $this->gateway = isset($this->config['urls']['gateway'][$this->account->env]) ? $this->config['urls']['gateway'][$this->account->env] : $this->config['urls']['gateway']['production']; return $this; } /** * Get ProcReturnCode * * @return string|null */ protected function getRandomNumberBase16() { $n = 128; $characters = '0123456789ABCDEF'; $randomString = ''; for ($i = 0; $i < $n; $i++) { $index = rand(0, strlen($characters) - 1); $randomString .= $characters[$index]; } return strtoupper($randomString); } /** * Create Regular Payment XML * * @return string */ protected function createRegularPaymentXML() { $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, '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, ] ] ]; return $this->createXML($nodes, 'ISO-8859-9'); } /** * Create Regular Payment Post XML * * @return string */ 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, ] ]; return $this->createXML($nodes, 'ISO-8859-9'); } /** * Create 3D Payment XML * @return string */ 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' => '', ] ]; if ($this->order->name) { $nodes['BillTo'] = [ 'Name' => $this->order->name, ]; } return $this->createXML($nodes, 'ISO-8859-9'); } /** * Get ProcReturnCode * * @return string|null */ protected function getProcReturnCode() { return isset($this->data->ProcReturnCode) ? (string)$this->data->ProcReturnCode : null; } /** * Get Status Detail Text * * @return string|null */ protected function getStatusDetail() { $proc_return_code = $this->getProcReturnCode(); return $proc_return_code ? (isset($this->codes[$proc_return_code]) ? (string)$this->codes[$proc_return_code] : null) : null; } /** * Create 3D Hash * * @return string */ public function create3DHash() { $hashItems = []; $hashItems[] = mb_strtoupper($this->account->model); $hashItems[] = $this->txnCode; $hashItems[] = $this->account->merchant_safe_id; $hashItems[] = $this->account->terminal_safe_id; $hashItems[] = $this->order->id; $hashItems[] = mb_strtoupper($this->order->lang); $hashItems[] = $this->order->amount; //$hashItems[] = $this->order->ccbRewardAmount; //$hashItems[] = $this->order->pcbRewardAmount; //$hashItems[] = $this->order->xcbRewardAmount; $hashItems[] = $this->order->currency; $hashItems[] = $this->order->installment; $hashItems[] = $this->order->success_url; $hashItems[] = $this->order->fail_url; //$hashItems[] = $this->order->emailAddress; //$hashItems[] = $this->order->subMerchantId; $hashItems[] = $this->card->number; $hashItems[] = $this->getCardExpDate(); $hashItems[] = $this->card->cvv; $hashItems[] = $this->order->rand; $hashItems[] = $this->order->requestDateTime; //$hashItems[] = $this->order->b2bIdentityNumber; $hashItemsString = implode('', $hashItems); $hashItemsStringHashed = hash_hmac('sha512', $hashItemsString, $this->account->secret_key, true); return base64_encode($hashItemsStringHashed); } /** * Check 3D Hash * * @param array $data * @return bool */ public function check3DHash($data) { $return = false; $params = explode('+', $data['hashParams']); $builder = ''; foreach ($params as $param) { $builder .= $data[$param]; } $hashItemsStringHashed = hash_hmac('sha512', $builder, $this->account->secret_key, true); $hashItemsStringHashed = base64_encode($hashItemsStringHashed); if ($data['hash'] == $hashItemsStringHashed) { $return = true; } return $return; } /** * Regular Payment * * @return $this * @throws GuzzleException */ public function makeRegularPayment() { return false; } /** * Make 3D Payment * * @return $this * @throws GuzzleException */ public function make3DPayment() { return false; } /** * Make 3D Pay Payment * * @return $this */ public function make3DPayPayment() { $this->request = Request::createFromGlobals(); $status = 'declined'; if ($this->check3DHash($this->request->request->all()) && (string)$this->request->get('responseCode') == 'VPS-0000') { $status = 'approved'; } $transaction_security = 'MPI fallback'; if ($status == 'approved') { //if ($this->request->get('3DStatus') == '1') { $transaction_security = 'Full 3D Secure'; // } elseif (in_array($this->request->get('3DStatus'), [2, 3, 4])) { //$transaction_security = 'Half 3D Secure'; //} } $this->response = (object)[ 'id' => (string)$this->request->get('authCode'), 'order_id' => (string)$this->request->get('orderId'), 'trans_id' => (string)$this->request->get('rrn'), 'response' => (string)$this->request->get('responseCode'), 'merchantSafeId' => (string)$this->request->get('merchantSafeId'), 'terminalSafeId' => (string)$this->request->get('terminalSafeId'), 'transaction_type' => $this->type, 'transaction' => (string)$this->request->get('rrn'), 'transaction_security' => $transaction_security, 'auth_code' => (string)$this->request->get('authCode'), 'host_ref_num' => (string)$this->request->get('rrn'), 'proc_return_code' => (string)$this->request->get('hostResponseCode'), 'code' => (string)$this->request->get('responseCode'), 'status' => $status, 'status_detail' => $this->getStatusDetail(), 'error_code' => (string)$this->request->get('responseCode') != 'VPS-0000' ? (string)$this->request->get('responseCode') : null, 'error_message' => (string)$this->request->get('responseCode') != 'VPS-0000' ? (string)$this->request->get('responseMessage') : null, 'md_status' => (string)$this->request->get('md_status'), 'hash' => (string)$this->request->get('hash'), 'rand' => null, 'hash_params' => (string)$this->request->get('hashParams'), 'hash_params_val' => (string)$this->request->get('hash'), 'amount' => (string)$this->request->get('amount'), 'md_error_message' => (string)$this->request->get('responseCode') != 'VPS-0000' ? (string)$this->request->get('responseMessage') : null, 'all' => $this->request->request->all() ]; return $this; } /** * Get 3d Form Data * * @return array */ public function get3DFormData() { $data = []; if ($this->order) { $this->order->amount = number_format($this->order->amount, 2, '.', ''); $this->order->rand = $this->getRandomNumberBase16(); $this->order->requestDateTime = mb_substr(Carbon::now()->toISOString(), 0, 23); $this->order->installment = $this->order->installment == 0 ? 1 : $this->order->installment; $this->order->hash = $this->create3DHash(); $inputs = [ 'paymentModel' => mb_strtoupper($this->account->model), 'txnCode' => $this->txnCode, 'merchantSafeId' => $this->account->merchant_safe_id, 'terminalSafeId' => $this->account->terminal_safe_id, 'orderId' => $this->order->id, 'lang' => mb_strtoupper($this->order->lang), 'amount' => $this->order->amount, 'currencyCode' => $this->order->currency, 'installCount' => $this->order->installment, 'okUrl' => $this->order->success_url, 'failUrl' => $this->order->fail_url, 'creditCard' => $this->card->number, 'expiredDate' => $this->getCardExpDate(), 'cvv' => $this->card->cvv, 'randomNumber' => $this->order->rand, 'requestDateTime' => $this->order->requestDateTime, 'hash' => $this->order->hash, ]; $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, ]; //$formData = $data; //echo view('threeDSecureForm', compact('formData'));die(); } return $data; } /** * Send contents to WebService * * @param $contents * @return $this * @throws GuzzleException */ public function send($contents) { $client = new Client(); $response = $client->request('POST', $this->url, [ 'body' => $contents ]); $this->data = $this->XMLStringToObject($response->getBody()->getContents()); return $this; } /** * Prepare Order * * @param object $order * @param object null $card * @return mixed * @throws UnsupportedTransactionTypeException */ public function prepare($order, $card = null) { $this->type = $this->types['pay']; if (isset($order->transaction)) { if (array_key_exists($order->transaction, $this->types)) { $this->type = $this->types[$order->transaction]; } else { throw new UnsupportedTransactionTypeException('Unsupported transaction type!'); } } $this->order = $order; $this->card = $card; $this->txnCode = 3000; $this->order->installment = $this->order->installment == 0 ? 0 : $this->order->installment; } /** * Make Payment * * @param object $card * @return mixed * @throws UnsupportedPaymentModelException * @throws GuzzleException */ public function payment($card) { $this->card = $card; $model = 'regular'; if (isset($this->account->model) && $this->account->model) { $model = $this->account->model; } if ($model == 'regular') { $this->makeRegularPayment(); } elseif ($model == '3d') { $this->make3DPayment(); } elseif ($model == '3d_pay') { $this->make3DPayPayment(); } else { throw new UnsupportedPaymentModelException(); } return $this; } /** * Refund Order * * @param array $meta * @return $this * @throws GuzzleException */ public function refund(array $meta) { return false; } /** * Cancel Order * * @param array $meta * @return $this * @throws GuzzleException */ public function cancel(array $meta) { return false; } /** * Order Status * * @param array $meta * @return $this * @throws GuzzleException */ public function status(array $meta) { return false; } /** * Order History * * @param array $meta * @return $this * @throws GuzzleException */ public function history(array $meta) { return false; } /** * @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 = '0'; } elseif ($this->card->type == 'master') { $card_type = '1'; } elseif ($this->card->type == '1' || $this->card->type == '2') { $card_type = $this->card->type; } } return $card_type; } protected function getCardExpDate() { $year = (string)substr($this->card->year, 2, 2); $month = (string)substr($this->card->month, 0, 2); return (string)$month . $year; } }