diff --git a/gateways/gateway.php b/gateways/gateway.php index d55313e..6cdacfb 100644 --- a/gateways/gateway.php +++ b/gateways/gateway.php @@ -204,7 +204,7 @@ public function getCurrency() { */ public function checkFields(array $fields) { foreach ($fields as $k => $v) { - if (empty($v)) { + if (!isset($v) || $v == "") { return FALSE; } } diff --git a/gateways/luottokunta.php b/gateways/luottokunta.php index 6242c7e..131c2b2 100644 --- a/gateways/luottokunta.php +++ b/gateways/luottokunta.php @@ -1,99 +1,139 @@ name = "Luottokunta"; - $this->postUrl = 'https://dmp2.luottokunta.fi/dmp/html_payments'; - $this->hasPaymentAbility = true; - } - - /** - * getPaymentFields() - * @see fpiapi/gateways/FpiapiGateway::getPaymentFields() - */ - public function getPaymentFields() { - - // First fill in the field used to calculate mac - $fields = array( - 'Merchant_Number' => $this->configuration['publicKey'], - 'Order_ID' => $this->transaction->getUid(), - 'Amount' => round($this->transaction->getSum()*100), - 'Transaction_Type' => '1' - ); - - // Calculate mac - $mac = implode('', $fields) . $this->configuration['privateKey']; - $mac = strtolower(md5($mac)); - - // Convert currency to numeric code - switch($this->getCurrency()) { - case 'EUR': - $currency = 978; - break; + + /** + * Constructor + */ + public function __construct() { + parent::__construct(); + $this->name = "Luottokunta"; + $this->postUrl = 'https://dmp2.luottokunta.fi/dmp/html_payments'; + $this->hasPaymentAbility = true; + } + + /** + * getPaymentFields() + * @see fpiapi/gateways/FpiapiGateway::getPaymentFields() + */ + public function getPaymentFields() { + + $mac_fields = $this->getFieldArrayForRequest(); + + $mac_str = implode("&", $mac_fields); + $mac = hash('sha256', $mac_str); + + $fields = array( + 'Authentication_Mac' => $mac, + 'Success_Url' => $this->getReturnUrl(), + 'Failure_Url' => $this->getErrorUrl(), + 'Cancel_Url' => $this->getErrorUrl(), + 'Device_Category' => $this->getDeviceCategory(), + 'Card_Details_Transmit' => $this->getCardDetailsTransmit(), + 'Currency_Code' => $this->getCurrencyCode(), + 'Merchant_Number' => $this->configuration['publicKey'], + 'Order_ID' => $this->transaction->getUid(), + 'Amount' => $this->getFormattedSum(), + 'Transaction_Type' => $this->getTransactionType(), + ); + + return $fields; } - - $fields['Authentication_Mac'] = $mac; - $fields['Success_Url'] = $this->getReturnUrl(); - $fields['Failure_Url'] = - $fields['Cancel_Url'] = $this->getErrorUrl(); - $fields['Device_Category'] = '1'; - $fields['Card_Details_Transmit'] = '0'; // Ask card details at the other end - $fields['Currency_Code'] = $currency; - - - return $fields; - } - - - /** - * isPaymentCompleted() - * @see fpiapi/gateways/FpiapiGateway::isPaymentCompleted() - */ - public function isPaymentCompleted() { - - $params = &$_REQUEST; - - if (!isset($params['LKMAC'])) - return false; - - if (!isset($params['LKPRC'])) - return false; - - // First fill in the field used to calculate mac - $fields = array( - 'Merchant_Number' => $this->configuration['publicKey'], - 'Order_ID' => $this->transaction->getUid(), - 'Amount' => $this->transaction->getSum(), - 'Transaction_Type' => '1' - ); - - if (!$this->checkFields($fields)) { - return false; + + /** + * isPaymentCompleted() + * @see fpiapi/gateways/FpiapiGateway::isPaymentCompleted() + */ + public function isPaymentCompleted() { + + $params = &$_REQUEST; + + if (!isset($params['LKMAC'])) { + return false; + } + + $fields = $this->getFieldArrayForResponse(); + + if (!$this->checkFields($fields)) { + return false; + } + + $mac_str = implode("&", $fields); + $mac = hash('sha256', $mac_str); + + return strtolower($mac) == strtolower($params['LKMAC']); } - - // reverse array to calculate return mac - $fields = array_reverse($fields); - - // Calculate mac - $mac = implode('', $fields) . $this->configuration['privateKey']; - $mac = strtolower(md5($mac)); - - return $mac == $params['LKMAC']; - - } - - - -} + + protected function getFieldArrayForResponse() { + + $fields = array( + 'Private_key' => $this->configuration['privateKey'], + 'Transaction_Type' => $this->getTransactionType(), + 'Currency_Code' => $this->getCurrencyCode(), + 'Amount' => $this->getFormattedSum(), + 'Order_ID' => $this->transaction->getUid(), + 'Merchant_Number' => $this->configuration['publicKey'], + ); + + $LB_fields = array( + 'LKBINCOUNTRY', + 'LKIPCOUNTRY', + 'LKECI', + ); + + // Add LB-fields if they exist in reponse + foreach ($LB_fields as $LB_field) { + if (isset($_REQUEST[$LB_field])) { + $fields[$LB_field] = $_REQUEST[$LB_field]; + } + } + + return $fields; + } + + protected function getFieldArrayForRequest() { + + $fields = array( + 'Merchant_Number' => $this->configuration['publicKey'], + 'Order_ID' => $this->transaction->getUid(), + 'Amount' => $this->getFormattedSum(), + 'Currency_Code' => $this->getCurrencyCode(), + 'Transaction_Type' => $this->getTransactionType(), + 'Private_key' => $this->configuration['privateKey'], + ); + + return $fields; + } + + protected function getFilteredSum() { + return str_replace(",", ".", $this->transaction->getSum()); + } + + protected function getFormattedSum() { + return round($this->getFilteredSum() * 100); + } + + protected function getTransactionType() { + return '1'; + } + + protected function getCurrencyCode() { + return '978'; + } + + protected function getDeviceCategory() { + return '1'; + } + + protected function getCardDetailsTransmit() { + return '0'; + } + +} \ No newline at end of file