/** * @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() . ")"); } }
/** * @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 = unserialize($sessionKlarna); if (!isset($sessionKlarnaData)) { throw new Exception("No klarna Session data set"); } $klarnaData = $sessionKlarnaData->KLARNA_DATA; 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'], NULL, $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) { $item_price = self::convertPrice($item->product_basePriceWithTax, $order['details']['BT']->order_currency, $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_percent = (double) round(abs($item->product_subtotal_discount / $item->product_quantity * 100 / $item->product_basePriceWithTax), 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_percent, KlarnaFlags::INC_VAT); } // Add shipping $shipment = self::convertPrice($order['details']['BT']->order_shipment + $order['details']['BT']->order_shipment_tax, $order['details']['BT']->order_currency, $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), 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']); $payment_tax_percent = self::getTaxPercent($order['details']['BT']->order_payment + $order['details']['BT']->order_payment_tax, $order['details']['BT']->order_payment); if ($payment_without_tax > 0) { //vmdebug('invoicefee', $payment, $payment_tax); $klarna->addArticle(1, "invoicefee", JText::_('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 ($order['details']['BT']->coupon_discount > 0) { $coupon_discount = self::convertPrice(round($order['details']['BT']->coupon_discount), $order['details']['BT']->order_currency, $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); } /* $test= mb_detect_encoding(utf8_decode ($shipTo->address_1), 'ISO-8859-1',true); $test2= mb_detect_encoding ($shipTo->address_1, 'ISO-8859-1',true); $test3=utf8_decode ($shipTo->address_1); $test5= mb_detect_encoding ($test3, 'ISO-8859-1',true); $test6 = mb_convert_encoding($shipTo->address_1, 'ISO-8859-1' , 'UTF-8'); $test7= mb_detect_encoding ($test6, 'ISO-8859-1',true); vmDebug('mb_detect_encoding',$shipTo->address_1,$test,$test2,$test5, $test7);*/ 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($shipTo->house_no, $cData['country_code_3']), KlarnaHandler::setAddress2($shipTo->address_2, $cData['country_code_3'])); } catch (Exception $e) { VmInfo($e->getMessage()); return FALSE; } if ($klarnaData['invoice_type'] == 'company') { $klarna_shipping->isCompany = TRUE; $klarna_shipping->setCompanyName($shipTo->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() . ")"); } }