/** * @test */ public function shouldCallKlarnaGetAddresses() { $first = new \KlarnaAddr(); $first->setCountry('SE'); $second = new \KlarnaAddr(); $second->setCountry('SE'); $klarnaMock = $this->createKlarnaMock(); $klarnaMock->expects($this->once())->method('getAddresses')->with('thePno')->will($this->returnValue(array($first, $second))); $action = new GetAddressesAction($klarnaMock); $action->setApi(new Config()); $action->execute($getAddresses = new GetAddresses('thePno')); $this->assertCount(2, $getAddresses->getAddresses()); $this->assertSame($first, $getAddresses->getFirstAddress()); }
/** * Outputs the html form hidden elements sent as POST data to the payment * gateway. * * @return string */ public function processButton() { global $order; $shipping = $_SESSION['shipping']; $invoiceType = $_POST["klarna_{$this->_option}_invoice_type"]; $reference = $_POST["klarna_{$this->_option}_reference"]; $process_button_string = $this->_utils->hiddenFieldString($this->_addrs, $invoiceType, $this->_paymentPlan, $order->customer['email_address'], $reference); if ($this->_addrs->isCompany) { $process_button_string .= xtc_draw_hidden_field('klarna_fname', $order->delivery['firstname']) . xtc_draw_hidden_field('klarna_lname', $order->delivery['lastname']); } else { $process_button_string .= xtc_draw_hidden_field('klarna_fname', $this->_addrs->getFirstName()) . xtc_draw_hidden_field('klarna_lname', $this->_addrs->getLastName()); } $_SESSION['klarna_ot'] = $this->_utils->getOrderTotal(); $process_button_string .= xtc_draw_hidden_field(xtc_session_name(), xtc_session_id()); return $process_button_string; }
/** * @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() . ")"); } }
public function setAddress(KlarnaAddr $addr) { if (!$addr instanceof KlarnaAddr) { throw new KlarnaApiException(__METHOD__ . ': must be passed a KlarnaAddr'); } $reference = @($addr->getFirstName() . ' ' . $addr->getLastName()); $cellno = $addr->getCellno(); $telno = $addr->getTelno(); $phone = strlen($cellno) > 0 ? $cellno : $telno; $values =& $this->aInputValues; $values['firstName'] = $addr->getFirstName(); $values['lastName'] = $addr->getLastName(); $values['phoneNumber'] = $phone; $values['zipcode'] = $addr->getZipCode(); $values['city'] = $addr->getCity(); $values['street'] = $addr->getStreet(); $values['homenumber'] = $addr->getHouseNumber(); $values['house_extension'] = $addr->getHouseExt(); $values['reference'] = $reference; }
/** * Get a unique key used to identify the given address * * The key is a hash of the lower bit ascii portion of company name, * first name, last name and street joined with pipes * * @param KlarnaAddr $addr address * * @return string key for this address */ public static function getAddressKey($addr) { return hash('crc32', preg_replace('/[^\\w]*/', '', $addr->getCompanyName() . '|' . $addr->getFirstName() . '|' . $addr->getLastName() . '|' . $addr->getStreet())); }
/** * Attempt to guess customer country to determine if things should be shown. * * @param string $option payment option * * @return string or null */ public static function deduceCountry($option) { $customer_country_id = isset($_SESSION['customer_country_id']) ? $_SESSION['customer_country_id'] : STORE_COUNTRY; $currency = $_SESSION['currency']; $addr = null; if ($customer_country_id !== null) { $addr = new KlarnaAddr(); $addr->setCountry(self::getCountryByID($customer_country_id)); } $lang = self::getLanguageCode(); self::configureKiTT($option); self::configureKlarna($option); return KiTT::locator()->locate($currency, $lang, $addr); }
/** * The invoice_address function is used to retrieve the address of a purchase. * * * * @link http://integration.klarna.com/en/api/other-functions/functions/invoiceaddress * @param string $invNo Invoice number. * @throws KlarnaException * @return KlarnaAddr */ public function invoiceAddress($invNo) { $this->checkInvNo($invNo, __METHOD__); $digestSecret = self::digest($this->colon($this->eid, $invNo, $this->secret)); $paramList = array($this->eid, $invNo, $digestSecret); self::printDebug('invoice_address', $paramList); $result = $this->xmlrpc_call('invoice_address', $paramList); $addr = new KlarnaAddr(); if (strlen($result[0]) > 0) { $addr->isCompany = false; $addr->setFirstName($result[0]); $addr->setLastName($result[1]); } else { $addr->isCompany = true; $addr->setCompanyName($result[1]); } $addr->setStreet($result[2]); $addr->setZipCode($result[3]); $addr->setCity($result[4]); $addr->setCountry($result[5]); return $addr; }
/** * Activates the reservation when the order or a part of it is send. * Saves the invoice on the webserver. * * @param Object $k Configured Klarna object. * @param String $ordernumber The ordernumber from the order the items are send from. * @param String $action The transaction id from the changed order. * @param Array $articles Array of articles that are send. * * @throws KlarnaException * * @return Array $myerror Configured Klarna object with error message. */ function piKlarnaActivateReservation($k, $ordernumber, $action, $articles) { $piKlarnaConfig = array(); $sql = "SELECT id FROM s_order WHERE ordernumber = ?"; $orderId = Shopware()->Db()->fetchOne($sql, array($ordernumber)); $sql = "SELECT * FROM s_order_billingaddress WHERE orderID = ?"; $myuser = Shopware()->Db()->fetchRow($sql, array((int)$orderId)); $sql = "SELECT countryiso FROM s_core_countries WHERE id = ?"; $piKlarnaCountryIso = Shopware()->Db()->fetchOne($sql, array((int)$myuser["countryID"])); $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config(); $sql = "SELECT * FROM Pi_klarna_payment_user_data WHERE ordernumber = ?"; $myKlarnaUser = Shopware()->Db()->fetchRow($sql, array($ordernumber)); if ($piKlarnaCountryIso == 'DE' || $piKlarnaCountryIso == 'NL') { $piKlarnaStreet = $myuser["street"]; } else { $piKlarnaStreet = $myuser["street"] . ' ' . $myuser["streetnumber"]; } $myerror = array(); $myerror['error'] = false; $myerror['errormessage'] = " "; $addr = new KlarnaAddr( $myKlarnaUser["mail"], '', $myKlarnaUser["cellphone"], utf8_decode($myKlarnaUser["firstname"]), utf8_decode($myKlarnaUser["lastname"]), '', utf8_decode($piKlarnaStreet), $myKlarnaUser["zip"], utf8_decode($myKlarnaUser["city"]), getCountryCode($piKlarnaCountryIso), $myKlarnaUser["housenr"], utf8_decode($myuser["text4"]) ); if($piKlarnaCountryIso=='DE' || $piKlarnaCountryIso=='NL'){ $addr->setHouseNumber($myuser["streetnumber"]); if($piKlarnaCountryIso=='NL' && $myuser["text4"]){ $addr->setHouseExt(utf8_decode($myuser["text4"])); } } elseif($myuser["company"]){ $addr->setCompanyName(utf8_decode($myuser["company"])); $addr->isCompany=true; } $k->setAddress(KlarnaFlags::IS_BILLING, $addr); $k->setAddress(KlarnaFlags::IS_SHIPPING, $addr); $k->setEstoreInfo($ordernumber); $sql = "SELECT transactionid FROM Pi_klarna_payment_order_data WHERE order_number = ?"; $rno = Shopware()->Db()->fetchOne($sql, array($ordernumber)); $sql = "SELECT payment_name FROM Pi_klarna_payment_order_data WHERE order_number = ?"; $PigmbhKlarnaPaymentName = Shopware()->Db()->fetchOne($sql, array($ordernumber)); if ($PigmbhKlarnaPaymentName == 'KlarnaInvoice') $PigmbhKlarnaPaymentFlag = KlarnaPClass::INVOICE; else { try { $pclasses = $k->getPClasses(null); $PigmbhKlarnaPaymentFlag = $pclasses[0]->getId(); } catch (Exception $e) { $myerror['error'] = true; $myerror['errormessage'] = $e->getMessage() . " (#" . $e->getCode() . ")"; return $myerror; } } if ($piKlarnaConfig->pi_klarna_Testmode == true) $piKlarnaTestmode = KlarnaFlags::TEST_MODE; else $piKlarnaTestmode = 0; if ($myuser["salutation"] == "mr") $mygender = KlarnaFlags::MALE; else $mygender = KlarnaFlags::FEMALE; $sql = "SELECT birthday FROM Pi_klarna_payment_user_data WHERE ordernumber = ?"; $myBirthday = Shopware()->Db()->fetchOne($sql, array($ordernumber)); try { $result = $k->activateReservation( $myBirthday , $rno, $mygender, '', $piKlarnaTestmode, $PigmbhKlarnaPaymentFlag ); $invno = $result[1]; $invNo = $invno; $k2 = piKlarnaCreateKlarnaInstance($ordernumber); if ($piKlarnaConfig->pi_klarna_liveserver == true) $testvar = 'true'; else $testvar = 'false'; try { $result = $k2->invoiceAmount($invNo); if ($action == 'last') $method = 'Letzte Rechnung'; elseif ($action == 'complete') $method = 'Komplette Rechnung'; else $method = 'Teilrechnung'; $sql = "INSERT INTO `Pi_klarna_payment_bills`(`method`, `order_number`, `invoice_amount`, `invoice_number`, `liveserver`) VALUES(?, ?, ?, ?, ?)"; Shopware()->Db()->query($sql, array($method, $ordernumber, $result, $invno, $testvar)); for ($i = 0; $i < sizeof($articles); $i++) { $myarticlename = $articles[$i]['name']; // $myarticlename = str_replace("'", "\'", $articles[$i]['name']); $sql = "INSERT INTO `Pi_klarna_payment_bills_articles` (`order_number`, `invoice_number`, `name`, `bestell_nr`, `anzahl`, `einzelpreis`) VALUES(?, ?, ?, ?, ? ,?)"; Shopware()->Db()->query($sql, array( $ordernumber, $invno, $myarticlename, $articles[$i]['bestell_nr'], (int)$articles[$i]['anzahl'], $articles[$i]['einzelpreis'] )); } $ch = ""; if ($piKlarnaConfig->pi_klarna_liveserver == true) { $ch = curl_init('https://online.klarna.com/invoices/' . $invno . '.pdf'); } else { $ch = curl_init('https://beta-test.klarna.com/invoices/' . $invno . '.pdf'); } $fp = fopen('files/documents/' . $invno . '.pdf', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); return $myerror; } catch (Exception $e) { $myerror['error'] = true; $myerror['errormessage'] = $e->getMessage() . " (#" . $e->getCode() . ")"; return $myerror; } } catch (Exception $e) { $myerror['error'] = true; $myerror['errormessage'] = $e->getMessage() . " (#" . $e->getCode() . ")"; return $myerror; } }
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!"); }
/** * Attempt to locate where a user is from based on currency, * language and address. * * @param string $currency iso-4217 currency code * @param string $language iso-639-1 language code * @param KlarnaAddr $address Customers KlarnaAddr object * @param string $ipAddress Customer IPAddress for eventual geoIP lookup * * @return Country iso-3166-alpha-2 code if a match is found, null * otherwise. */ public function locate($currency = null, $language = null, $address = null, $ipAddress = null) { // Polish input to be the expected uppercase and lowercase if ($currency !== null) { $currency = strtoupper($currency); } if ($language !== null) { $language = strtolower($language); } // If the currency is not in the list of supported currencies, exit if ($this->_lookup->getCountries($currency) === null) { return null; } // If we do have an address, check if it is valid to sell to. If it is, // return it, otherwise exit if ($address !== null) { $country = strtoupper(KlarnaCountry::getCode($address->getCountry())); return $this->_getValid($country, $currency); } // Get all possible valid candidates based on currency and language. $candidates = $this->_getCandidates($currency, $language); // If there is only one valid candidate, return it if (count($candidates) === 1) { return $candidates[0]; } // Check if the default store is a valid candidate, and if it is, // return it. $default = $this->_getDefaultCountry(); if ($default !== null && in_array($default, $candidates)) { return $default; } // No match has been found, exit. return null; }
/** * Extract values for input fields from address object * * @param KlarnaAddr $addr address to get data from * * @return void */ public function setAddress($addr) { $reference = @($addr->getFirstName() . ' ' . $addr->getLastName()); $cellno = $addr->getCellno(); $telno = $addr->getTelno(); $phone = strlen($cellno) > 0 ? $cellno : $telno; $this->company_name = utf8_encode($addr->getCompanyName()); $this->first_name = utf8_encode($addr->getFirstName()); $this->last_name = utf8_encode($addr->getLastName()); $this->phone_number = utf8_encode($phone); $this->zipcode = utf8_encode($addr->getZipCode()); $this->city = utf8_encode($addr->getCity()); $this->street = utf8_encode($addr->getStreet()); $this->house_number = utf8_encode($addr->getHouseNumber()); $this->house_extension = utf8_encode($addr->getHouseExt()); $this->reference = utf8_encode($reference); }
/** * Set shipping and billing address. * * @since 2.0 **/ function set_addresses($klarna_billing, $klarna_shipping, $ship_to_billing_address) { $order = $this->order; $klarna = $this->klarna; $klarna_billing_address = $klarna_billing['address']; $klarna_billing_house_number = $klarna_billing['house_number']; $klarna_billing_house_extension = $klarna_billing['house_extension']; $klarna_shipping_address = $klarna_shipping['address']; $klarna_shipping_house_number = $klarna_shipping['house_number']; $klarna_shipping_house_extension = $klarna_shipping['house_extension']; // Billing address $addr_billing = new KlarnaAddr($email = $order->billing_email, $telno = '', $cellno = $order->billing_phone, $fname = utf8_decode($order->billing_first_name), $lname = utf8_decode($order->billing_last_name), $careof = utf8_decode($order->billing_address_2), $street = utf8_decode($klarna_billing_address), $zip = utf8_decode($order->billing_postcode), $city = utf8_decode($order->billing_city), $country = utf8_decode($order->billing_country), $houseNo = utf8_decode($klarna_billing_house_number), $houseExt = utf8_decode($klarna_billing_house_extension)); // Add Company if one is set if ($order->billing_company) { $addr_billing->setCompanyName(utf8_decode($order->billing_company)); } // Shipping address if ($order->get_shipping_method() == '' || $ship_to_billing_address == 'yes') { // Use billing address if Shipping is disabled in Woocommerce $addr_shipping = new KlarnaAddr($email = $order->billing_email, $telno = '', $cellno = $order->billing_phone, $fname = utf8_decode($order->billing_first_name), $lname = utf8_decode($order->billing_last_name), $careof = utf8_decode($order->billing_address_2), $street = utf8_decode($klarna_billing_address), $zip = utf8_decode($order->billing_postcode), $city = utf8_decode($order->billing_city), $country = utf8_decode($order->billing_country), $houseNo = utf8_decode($klarna_billing_house_number), $houseExt = utf8_decode($klarna_billing_house_extension)); // Add Company if one is set if ($order->billing_company) { $addr_shipping->setCompanyName(utf8_decode($order->billing_company)); } } else { $addr_shipping = new KlarnaAddr($email = $order->billing_email, $telno = '', $cellno = $order->billing_phone, $fname = utf8_decode($order->shipping_first_name), $lname = utf8_decode($order->shipping_last_name), $careof = utf8_decode($order->shipping_address_2), $street = utf8_decode($klarna_shipping_address), $zip = utf8_decode($order->shipping_postcode), $city = utf8_decode($order->shipping_city), $country = utf8_decode($order->shipping_country), $houseNo = utf8_decode($klarna_shipping_house_number), $houseExt = utf8_decode($klarna_shipping_house_extension)); // Add Company if one is set if ($order->shipping_company) { $addr_shipping->setCompanyName(utf8_decode($order->shipping_company)); } } // Next we tell the Klarna instance to use the address in the next order. $klarna->setAddress(KlarnaFlags::IS_BILLING, $addr_billing); // Billing / invoice address $klarna->setAddress(KlarnaFlags::IS_SHIPPING, $addr_shipping); // Shipping / delivery address }
/** * Convert a given array to a KlarnaAddr object. * * @param array $array an array of customer data * @param string $country the customers country * * @return KlarnaAddr object */ public function buildKlarnaAddressFromArray($array, $country) { $address = new KlarnaAddr("", KiTT_String::encode($array["phone_number"]), KiTT_String::encode($array["phone_number"]), KiTT_String::encode($array["first_name"]), KiTT_String::encode($array["last_name"]), "", KiTT_String::encode($array["street"]), KiTT_String::encode($array["zipcode"]), KiTT_String::encode($array["city"]), $country, KiTT_String::encode($array["house_number"]), KiTT_String::encode($array["house_extension"])); if ($array["klarna_invoice_type"] == "company") { $address->isCompany = true; $address->setCompanyName(KiTT_String::encode($array["company_name"])); $name = explode(' ', KiTT_String::encode($array["reference"]), 2); if (strlen($name[0]) > 0) { $address->setFirstName($name[0]); } else { $address->setFirstName(" "); } if (count($name) > 1 && strlen($name[1]) > 0) { $address->setLastName($name[1]); } else { $address->setLastName(" "); } } return $address; }
/** * Adds address to Klarna object * * @param Object $k Klarna object * */ public function addKlarnaAddress($k) { $myuser = $this->getUser(); $piKlarnaCountryIso = getBillingCountry($myuser); if ($piKlarnaCountryIso == 'DE' || $piKlarnaCountryIso == 'NL') { $piKlarnaStreet = $myuser["billingaddress"]["street"]; } else { $piKlarnaStreet = $myuser["billingaddress"]["street"] . ' ' . $myuser["billingaddress"]["streetnumber"]; } $addr = new KlarnaAddr($myuser["additional"]["user"]["email"], '', $myuser["billingaddress"]["phone"], $this->_convertEncoding($myuser["billingaddress"]["firstname"]), $this->_convertEncoding($myuser["billingaddress"]["lastname"]), '', $this->_convertEncoding($piKlarnaStreet), $myuser["billingaddress"]["zipcode"], $this->_convertEncoding($myuser["billingaddress"]["city"]), getCountryCode($piKlarnaCountryIso) ); if ($piKlarnaCountryIso == 'DE' || $piKlarnaCountryIso == 'NL') { $addr->setHouseNumber($myuser["billingaddress"]["streetnumber"]); if ($piKlarnaCountryIso == 'NL') { $addr->setHouseExt($this->_convertEncoding($myuser["billingaddress"]["text4"])); } } elseif ($myuser["billingaddress"]["company"]) { $addr->setCompanyName($this->_convertEncoding($myuser["billingaddress"]["company"])); $addr->isCompany = true; } $k->setCountry($piKlarnaCountryIso); $k->setAddress(KlarnaFlags::IS_BILLING, $addr); $k->setAddress(KlarnaFlags::IS_SHIPPING, $addr); }