<?php namespace TPS\Birzha\Classes;

use TPS\Birzha\Models\Settings;

class Halkbank extends Payment
{
    protected function getApiCredentials()
    {
        return [
            'userName' => Settings::getValue('api_login'),
            'password' => Settings::getValue('api_password'),
        ];
        
    }

    protected function getApiUrl()
    {
        return 'https://mpi.gov.tm/payment/rest/';
    }

    protected function getRegistrationUri()
    {
        return 'register.do';
    }

    protected function getStatusUri()
    {
        return 'getOrderStatus.do';
    }

    protected function registerOrderParams($payment, $url)
    {
        return [
            'amount'      => $payment->amount * 100, //multiply by 100 to obtain tenge
            'currency' => 934,
            'description' => 'Balansa pul goşmak - Birža şahsy otag',
            'orderNumber'     => strtoupper(str_random(5)) . date('jn'),
            'failUrl'     => $url . '?status=failed',
            'returnUrl' => $url . '?status=success',
        ];
    }

    public function checkRegisterOrderErrorCode($registerOrderResponse)
    {
        return $registerOrderResponse['errorCode'] == 0;
    }

    public function checkReturnUrlParams($returnUrlParams, $payment)
    {
        if (!array_key_exists('status', $returnUrlParams)) {
            return false;
        }

        if($payment && $returnUrlParams['status'] === 'success' && $returnUrlParams['orderId'] === $payment->order_id) {
            
            return true;
        } else {

            return false;
        }
        
    }

    public function checkOrderStatusResponseCodes($orderStatusResponse)
    {
        return $orderStatusResponse['ErrorCode'] == 0 && $orderStatusResponse['OrderStatus'] == 2;
    }
}