/** * @param $cData * @param $shipTo */ function __construct($cData, $shipTo) { $this->shipTo = $shipTo; $this->country = $cData['country_code']; $this->country_code_3 = $cData['country_code_3']; $this->currency = $cData['currency_code']; $this->virtuemart_currency_id = $cData['virtuemart_currency_id']; //$this->currency = $vendor_currency; // Get EID and Secret $this->eid = $cData['eid']; $this->secret = $cData['secret']; $this->lang = $cData['language_code']; // Is Invoice enabled? $this->enabled = TRUE; // Set modes $this->mode = $cData['mode']; $this->ssl = KlarnaHandler::getKlarnaSSL($this->mode); $this->web_root = JURI::base(); try { $this->klarna = new Klarna_virtuemart(); $this->klarna->config($this->eid, $this->secret, $this->country, $this->lang, $this->currency, $this->mode, VMKLARNA_PC_TYPE, KlarnaHandler::getKlarna_pc_type(), $this->ssl); } catch (Exception $e) { VmError('klarna_payments', $e); unset($this->klarna); } }
function __construct($method, $country_code_3, $currency_code_3) { parent::__construct($method, $country_code_3, $currency_code_3); if (!class_exists('KlarnaHandler')) { require JPATH_ROOT . DS . 'plugins' . DS . 'vmpayment' . DS . 'klarna' . DS . 'helpers' . DS . 'klarnahandler.php'; } if ($this->_currentMethod->server == 'beta') { $this->mode = Klarna::BETA; } else { $this->mode = Klarna::LIVE; } $this->ssl = KlarnaHandler::getKlarnaSSL($this->mode); }
/** * cf https://docs.klarna.com/en/rest-api#supported_locales * @param $method */ function initKlarnaParams() { $return = true; $db = JFactory::getDBO(); $q = 'SELECT country_2_code , country_3_code FROM `#__virtuemart_countries` WHERE virtuemart_country_id = ' . (int) $this->_currentMethod->purchase_country; $db->setQuery($q); $country = $db->loadObject(); if (!$country) { $this->KlarnacheckoutError('Klarna Checkout: No country has been found with country id=' . $this->_currentMethod->purchase_country, vmText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $this->_currentMethod->payment_name)); $this->debugLog('No country has been found with country id=' . $this->_currentMethod->purchase_country, 'initKlarnaParams', 'debug'); $return = false; } $this->country_code_2 = $country->country_2_code; $this->country_code_3 = $country->country_3_code; $this->getPaymentCurrency($this->_currentMethod); $this->currency_code_3 = shopFunctions::getCurrencyByID($this->_currentMethod->payment_currency, 'currency_code_3'); if (!$this->currency_code_3) { $this->KlarnacheckoutError('Klarna Checkout: No currency has been found with currency id=' . $this->_currentMethod->payment_currency, vmText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $this->_currentMethod->payment_name)); $this->debugLog('No currency has been found with currency id=' . $this->_currentMethod->payment_currency, 'initKlarnaParams', 'debug'); $return = false; } $this->currency_id = $this->_currentMethod->payment_currency; if (empty($this->_currentMethod->sharedsecret) or empty($this->_currentMethod->merchantid)) { $this->KlarnacheckoutError('Klarna Checkout: Missing mandatory values merchant id=' . $this->_currentMethod->merchantid . ' shared secret=' . $this->_currentMethod->sharedsecret, vmText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $this->_currentMethod->payment_name)); $this->debugLog('Missing mandatory values merchant id=' . $this->_currentMethod->merchantid . ' shared secret=' . $this->_currentMethod->sharedsecret, 'initKlarnaParams', 'debug'); $return = false; } $this->locale = $this->_currentMethod->locale; $this->sharedsecret = $this->_currentMethod->sharedsecret; $this->merchantid = $this->_currentMethod->merchantid; if ($this->_currentMethod->server == 'beta') { $this->mode = Klarna::BETA; } else { $this->mode = Klarna::LIVE; } $this->ssl = KlarnaHandler::getKlarnaSSL($this->mode); if (!isset($this->_currentMethod->email_currency)) { $this->_currentMethod->email_currency = 'vendor'; } if (!isset($this->_currentMethod->terms_uri)) { $this->_currentMethod->terms_uri = ''; } return $return; }
/** * @static * @param $method * @param $order * @param $klarna_pclass * @return array|bool * @throws Exception */ public static function addTransaction($method, $order, $klarna_pclass) { if (!class_exists('KlarnaAddr')) { require JPATH_VMKLARNAPLUGIN . DS . 'klarna' . DS . 'api' . DS . 'klarnaaddr.php'; } $session = JFactory::getSession(); $sessionKlarna = $session->get('Klarna', 0, 'vm'); $sessionKlarnaData = (object) json_decode($sessionKlarna, true); if (!isset($sessionKlarnaData)) { throw new Exception("No klarna Session data set"); } $klarnaData = $sessionKlarnaData->KLARNA_DATA; // let's put it back as an array $klarnaData = (array) $klarnaData; if (VMKLARNA_SHIPTO_SAME_AS_BILLTO) { $shipTo = $order['details']['BT']; } else { $shipTo = (!isset($order['details']['ST']) or empty($order['details']['ST']) or count($order['details']['ST']) == 0) ? $order['details']['BT'] : $order['details']['ST']; } $billTo = $order['details']['BT']; $country = shopFunctions::getCountrybyID($shipTo->virtuemart_country_id, 'country_3_code'); $cData = self::countryData($method, $country); //$total_price_excl_vat = self::convertPrice($order['details']['BT']->order_subtotal, $cData['currency_code']); //$total_price_incl_vat = self::convertPrice($order['details']['BT']->order_subtotal + $order['details']['BT']->order_tax, $cData['currency_code'], $order['details']['BT']->order_currency); $mode = KlarnaHandler::getKlarnaMode($method, $cData['country_code_3']); $ssl = KlarnaHandler::getKlarnaSSL($mode); // Instantiate klarna object. $klarna = new Klarna_virtuemart(); $klarna->config($cData['eid'], $cData['secret'], $cData['country_code'], $cData['language'], $cData['currency_code'], $mode, VMKLARNA_PC_TYPE, KlarnaHandler::getKlarna_pc_type(), $ssl); // Sets order id's from other systems for the upcoming transaction. $klarna->setEstoreInfo($order['details']['BT']->order_number); // Fill the good list the we send to Klarna foreach ($order['items'] as $item) { if ($item->product_basePriceWithTax != 0.0) { if ($item->product_basePriceWithTax != $item->product_final_price) { $price = $item->product_basePriceWithTax; } else { $price = $item->product_final_price; } } else { if ($item->product_priceWithoutTax != $item->product_item_price) { $price = $item->product_item_price; } else { $price = $item->product_discountedPriceWithoutTax; } } $item_price = self::convertPrice($price, $order['details']['BT']->order_currency, $cData['currency_code']); $item_price = (double) round($item_price, 2); $item_tax_percent = 0; foreach ($order['calc_rules'] as $calc_rule) { if ($calc_rule->virtuemart_order_item_id == $item->virtuemart_order_item_id and $calc_rule->calc_kind == 'VatTax') { $item_tax_percent = $calc_rule->calc_value; break; } } //$item_discount_percent = (double)(round (abs (($item->product_subtotal_discount / $item->product_quantity) * 100 / $price), 2)); $item_discount_percent = 0.0; $discount_tax_percent = 0.0; $klarna->addArticle($item->product_quantity, utf8_decode($item->order_item_sku), utf8_decode(strip_tags($item->order_item_name)), $item_price, (double) $item_tax_percent, $item_discount_percent, KlarnaFlags::INC_VAT); $discount_tax_percent = 0.0; $includeVat = KlarnaFlags::INC_VAT; if ($item->product_subtotal_discount != 0.0) { if ($item->product_subtotal_discount > 0.0) { $discount_tax_percent = $item_tax_percent; $includeVat = 0; } $name = utf8_decode(strip_tags($item->order_item_name)) . ' (' . vmText::_('VMPAYMENT_KLARNA_PRODUCTDISCOUNT') . ')'; $discount = self::convertPrice(abs($item->product_subtotal_discount), $order['details']['BT']->order_currency, $cData['currency_code']); $discount = (double) round(abs($discount), 2) * -1; $klarna->addArticle(1, utf8_decode($item->order_item_sku), $name, $discount, (double) $discount_tax_percent, $item_discount_percent, $includeVat); } } // this is not correct yet /* foreach($order['calc_rules'] as $rule){ if ($rule->calc_kind == 'DBTaxRulesBill' or $rule->calc_kind == 'taxRulesBill' or $rule->calc_kind == 'DATaxRulesBill') { $klarna->addArticle (1, "", $rule->calc_rule_name, $rule->calc_amount, 0.0, 0.0, 0); } } */ // Add shipping $shipment = self::convertPrice($order['details']['BT']->order_shipment + $order['details']['BT']->order_shipment_tax, $order['details']['BT']->order_currency, $cData['currency_code']); foreach ($order['calc_rules'] as $calc_rule) { if ($calc_rule->calc_kind == 'shipment') { $shipment_tax_percent = $calc_rule->calc_value; break; } } $klarna->addArticle(1, "shippingfee", vmText::_('VMPAYMENT_KLARNA_SHIPMENT'), (double) round($shipment, 2), round($shipment_tax_percent, 2), 0, KlarnaFlags::IS_SHIPMENT + KlarnaFlags::INC_VAT); // Add invoice fee if ($klarna_pclass == -1) { //Only for invoices! $payment_without_tax = self::convertPrice($order['details']['BT']->order_payment, $order['details']['BT']->order_currency, $cData['currency_code']); $payment_with_tax = self::convertPrice($order['details']['BT']->order_payment + $order['details']['BT']->order_payment_tax, $order['details']['BT']->order_currency, $cData['currency_code']); foreach ($order['calc_rules'] as $calc_rule) { if ($calc_rule->calc_kind == 'payment') { $payment_tax_percent = $calc_rule->calc_value; break; } } if ($payment_without_tax > 0) { //vmdebug('invoicefee', $payment, $payment_tax); $klarna->addArticle(1, "invoicefee", utf8_decode(vmText::_('VMPAYMENT_KLARNA_INVOICE_FEE_TITLE')), (double) round($payment_with_tax, 2), (double) round($payment_tax_percent, 2), 0, KlarnaFlags::IS_HANDLING + KlarnaFlags::INC_VAT); } } // Add coupon if there is any if (abs($order['details']['BT']->coupon_discount) > 0.0) { $coupon_discount = self::convertPrice(round($order['details']['BT']->coupon_discount), $order['details']['BT']->order_currency, $cData['currency_code']); $coupon_discount = (double) round(abs($coupon_discount), 2) * -1; //vmdebug('discount', $coupon_discount); $klarna->addArticle(1, 'discount', utf8_decode(vmText::_('VMPAYMENT_KLARNA_DISCOUNT')) . ' ' . utf8_decode($order['details']['BT']->coupon_code), $coupon_discount, 0, 0, KlarnaFlags::INC_VAT); } try { $klarna_shipping = new KlarnaAddr($order['details']['BT']->email, $shipTo->phone_1, isset($shipTo->phone_2) ? $shipTo->phone_2 : "", utf8_decode($shipTo->first_name), utf8_decode($shipTo->last_name), '', utf8_decode($shipTo->address_1), $shipTo->zip, utf8_decode($shipTo->city), utf8_decode($cData['country']), KlarnaHandler::setHouseNo(isset($shipTo->house_no) ? $shipTo->house_no : "", $cData['country_code_3']), KlarnaHandler::setAddress2($shipTo->address_2, $cData['country_code_3'])); } catch (Exception $e) { VmInfo($e->getMessage()); return FALSE; } $klarna_reference = ""; // what is that? if ($klarnaData['invoice_type'] == 'company') { $klarna_shipping->isCompany = TRUE; $klarna_shipping->setCompanyName($shipTo->company); $klarna_comment = $shipTo->first_name . ' ' . $shipTo->last_name; //$klarnaData['reference']; if ($klarna_shipping->getLastName() == "") { $klarna_shipping->setLastName("-"); } if ($klarna_shipping->getFirstName() == "") { $klarna_shipping->setFirstName("-"); } } else { $klarna_reference = ""; $klarna_comment = ""; } // Only allow billing and shipping to be the same for Germany and the Netherlands if (VMKLARNA_SHIPTO_SAME_AS_BILLTO) { $klarna_billing = $klarna_shipping; } else { $klarna_billing = self::getBilling($cData, $order); } $klarna_flags = KlarnaFlags::RETURN_OCR; // get ocr back from KO. $klarna->setComment($klarna_comment); $klarna->setReference($klarna_reference, ""); $pno = self::getPNOfromSession($sessionKlarnaData->KLARNA_DATA, $country); try { $klarna->setAddress(KlarnaFlags::IS_SHIPPING, $klarna_shipping); $klarna->setAddress(KlarnaFlags::IS_BILLING, $klarna_billing); if (isset($klarnaData['year_salary'])) { $klarna->setIncomeInfo("'yearly_salary'", $klarnaData['year_salary']); } $result = $klarna->addTransaction($pno, $klarna->getCountry() == KlarnaCountry::DE || $klarna->getCountry() == KlarnaCountry::NL ? $klarnaData['gender'] : NULL, $klarna_flags, $klarna_pclass); $result['eid'] = $cData['eid']; $result['status_code'] = $result[2]; $result['status_text'] = vmText::_('VMPAYMENT_KLARNA_ORDER_STATUS_TEXT_' . $result[2]); return $result; //return $result; } catch (Exception $e) { $result['status_code'] = KlarnaFlags::DENIED; $result['status_text'] = mb_convert_encoding($e->getMessage(), 'UTF-8', 'ISO-8859-1') . " (#" . $e->getCode() . ")"; return $result; //return $result; //self::redirectPaymentMethod('error', htmlentities($e->getMessage()) . " (#" . $e->getCode() . ")"); } }
/** * cf https://docs.klarna.com/en/rest-api#supported_locales * @param $method */ function initKlarnaParams($method) { $return = true; $db = JFactory::getDBO(); $q = 'SELECT ' . $db->getEscaped('country_2_code') . ' , ' . $db->getEscaped('country_3_code') . ' FROM `#__virtuemart_countries` WHERE virtuemart_country_id = ' . (int) $method->purchase_country; $db->setQuery($q); $country = $db->loadObject(); if (!$country) { vmError('Klarna Checkout: No country has been found with country id=' . $method->purchase_country, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name)); $this->writelog('No country has been found with country id=' . $method->purchase_country, 'initKlarnaParams', 'error'); $return = false; } $this->country_code_2 = $country->country_2_code; $this->country_code_3 = $country->country_3_code; $this->getPaymentCurrency($method); $this->currency_code_3 = shopFunctions::getCurrencyByID($method->payment_currency, 'currency_code_3'); if (!$this->currency_code_3) { vmError('Klarna Checkout: No currency has been found with currency id=' . $method->payment_currency, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name)); $this->writelog('No currency has been found with currency id=' . $method->payment_currency, 'initKlarnaParams', 'error'); $return = false; } $this->currency_id = $method->payment_currency; if (empty($method->sharedsecret) or empty($method->merchantid)) { vmError('Klarna Checkout: Missing mandatory values merchant id=' . $method->merchantid . ' shared secret=' . $method->sharedsecret, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name)); $this->writelog('Missing mandatory values merchant id=' . $method->merchantid . ' shared secret=' . $method->sharedsecret, 'initKlarnaParams', 'error'); $return = false; } $this->locale = $method->locale; $this->sharedsecret = $method->sharedsecret; $this->merchantid = $method->merchantid; if ($method->server == 'beta') { $this->mode = Klarna::BETA; } else { $this->mode = Klarna::LIVE; } $this->ssl = KlarnaHandler::getKlarnaSSL($this->mode); return $return; }
public static function addTransaction($method, $order, $klarna_pclass) { if (!class_exists('KlarnaAddr')) { require JPATH_VMKLARNAPLUGIN . DS . 'klarna' . DS . 'api' . DS . 'klarnaaddr.php'; } $session = JFactory::getSession(); $sessionKlarna = $session->get('Klarna', 0, 'vm'); $sessionKlarnaData = unserialize($sessionKlarna); if (!isset($sessionKlarnaData)) { throw new Exception("No klarna Session data set"); } $klarnaData = $sessionKlarnaData->KLARNA_DATA; // $shipTo = (!isset($order['details']['ST']) or empty($order['details']['ST']) or count($order['details']['ST']) == 0) ? $order['details']['BT'] : $order['details']['ST']; $billTo = $order['details']['BT']; $country = shopFunctions::getCountrybyID($shipTo->virtuemart_country_id, 'country_3_code'); $cData = self::countryData($method, $country); //$total_price_excl_vat = self::convertPrice($order['details']['BT']->order_subtotal, $cData['currency_code']); //$total_price_incl_vat = self::convertPrice($order['details']['BT']->order_subtotal + $order['details']['BT']->order_tax, $cData['currency_code'], $order['details']['BT']->order_currency); $mode = KlarnaHandler::getKlarnaMode($method); $ssl = KlarnaHandler::getKlarnaSSL($mode); // Instantiate klarna object. $klarna = new Klarna_virtuemart(); $klarna->config($cData['eid'], $cData['secret'], $cData['country_code'], NULL, $cData['currency_code'], $mode, VMKLARNA_PC_TYPE, KlarnaHandler::getKlarna_pc_type(), $ssl); // Fill the good list the we send to Klarna foreach ($order['items'] as $item) { $item_price = self::convertPrice($item->product_final_price, $cData['currency_code']); $item_price = (double) round($item_price, 2); $item_tax_percent = (double) round(self::getTaxPercent($item->product_item_price + $item->product_tax / $item->product_quantity, $item->product_item_price), 2); $item_discount = self::convertPrice($item->product_subtotal_discount / $item->product_quantity, $cData['currency_code']); $item_discount = (double) abs(round($item_discount, 2)); //vmdebug('addarticle', $item->order_item_sku, $item, $item_tax_percent); $klarna->addArticle($item->product_quantity, utf8_decode($item->order_item_sku), utf8_decode(strip_tags($item->order_item_name)), $item_price, (double) $item_tax_percent, $item_discount, KlarnaFlags::INC_VAT); } // Add shipping $shipment = self::convertPrice($order['details']['BT']->order_shipment + $order['details']['BT']->order_shipment_tax, $cData['currency_code']); $shipment_tax_percent = self::getTaxPercent($order['details']['BT']->order_shipment + $order['details']['BT']->order_shipment_tax, $order['details']['BT']->order_shipment); $klarna->addArticle(1, "shippingfee", JText::_('VMPAYMENT_KLARNA_SHIPMENT'), (double) round($shipment, 2), (double) $shipment_tax_percent, 0, KlarnaFlags::IS_SHIPMENT + KlarnaFlags::INC_VAT); // Add invoice fee if ($klarna_pclass == -1) { //Only for invoices! $payment = self::convertPrice($order['details']['BT']->order_payment + $order['details']['BT']->order_payment_tax, $cData['currency_code']); $payment_tax_percent = self::getTaxPercent($order['details']['BT']->order_payment + $order['details']['BT']->order_payment_tax, $order['details']['BT']->order_payment); if ($payment > 0) { //vmdebug('invoicefee', $payment, $payment_tax); $klarna->addArticle(1, "invoicefee", JText::_('VMPAYMENT_KLARNA_INVOICE_FEE_TITLE'), (double) round($payment, 2), (double) round($payment_tax_percent, 2), 0, KlarnaFlags::IS_HANDLING + KlarnaFlags::INC_VAT); } } // Add coupon if there is any if ($order['details']['BT']->coupon_discount > 0) { $coupon_discount = self::convertPrice(round($order['details']['BT']->coupon_discount, $cData['currency_code'])); //vmdebug('discount', $coupon_discount); $klarna->addArticle(1, 'discount', JText::_('VMPAYMENT_KLARNA_DISCOUNT') . ' ' . $order['details']['BT']->coupon_code, (int) (round($coupon_discount, 2) * -1), 0, 0, KlarnaFlags::INC_VAT); } try { $klarna_shipping = new KlarnaAddr($order['details']['BT']->email, $shipTo->phone_1, '', utf8_decode($shipTo->first_name), utf8_decode($shipTo->last_name), '', utf8_decode($shipTo->address_1), $shipTo->zip, utf8_decode($shipTo->city), utf8_decode($cData['country']), $shipTo->house_no, $shipTo->address_2); } catch (Exception $e) { VmInfo($e->getMessage()); return false; } if ($klarnaData['invoice_type'] == 'company') { $klarna_shipping->isCompany = true; $klarna_shipping->setCompanyName($klarna_company_name); $klarna_comment = $shipTo->first_name . ' ' . $shipTo->last_name; //$klarnaData['reference']; if ($klarna_shipping->getLastName() == "") { $klarna_shipping->setLastName("-"); } if ($klarna_shipping->getFirstName() == "") { $klarna_shipping->setFirstName("-"); } } else { $klarna_reference = ""; $klarna_comment = ""; } // Only allow billing and shipping to be the same for Germany and the Netherlands if (VMKLARNA_SHIPTO_SAME_AS_BILLTO) { $klarna_billing = $klarna_shipping; } else { $klarna_billing = self::getBilling($cData, $order); } $klarna_flags = KlarnaFlags::RETURN_OCR; // get ocr back from KO. $klarna->setComment($klarna_comment); $klarna->setReference($klarna_reference, ""); $pno = self::getPNOfromOrder($billTo, $country); try { $klarna->setAddress(KlarnaFlags::IS_SHIPPING, $klarna_shipping); $klarna->setAddress(KlarnaFlags::IS_BILLING, $klarna_billing); if (isset($klarnaData['year_salary'])) { $klarna->setIncomeInfo("'yearly_salary'", $klarnaData['year_salary']); } $result = $klarna->addTransaction($pno, $klarna->getCountry() == KlarnaCountry::DE || $klarna->getCountry() == KlarnaCountry::NL ? $klarnaData['gender'] : NULL, $klarna_flags, $klarna_pclass); $result['eid'] = $cData['eid']; $result['status_code'] = $result[2]; $result['status_text'] = JText::_('VMPAYMENT_KLARNA_ORDER_STATUS_TEXT_' . $result[2]); return $result; //return $result; } catch (Exception $e) { $result['status_code'] = KlarnaFlags::DENIED; $result['status_text'] = htmlentities($e->getMessage()) . " (#" . $e->getCode() . ")"; return $result; //return $result; //self::redirectPaymentMethod('error', htmlentities($e->getMessage()) . " (#" . $e->getCode() . ")"); } throw new Exception("Something went wrong!"); }