<?php

require_once dirname(dirname(__FILE__)) . '/Klarna.php';
// Dependencies from http://phpxmlrpc.sourceforge.net/
require_once dirname(dirname(__FILE__)) . '/transport/xmlrpc-3.0.0.beta/lib/xmlrpc.inc';
require_once dirname(dirname(__FILE__)) . '/transport/xmlrpc-3.0.0.beta/lib/xmlrpc_wrappers.inc';
$k = new Klarna();
$k->config(0, 'sharedSecret', KlarnaCountry::SE, KlarnaLanguage::SV, KlarnaCurrency::SEK, Klarna::BETA, 'json', './pclasses.json');
try {
    $k->fetchPClasses();
    $pclasses = $k->getAllPClasses();
    // $pclasses is now a list of KlarnaPClass instances.
    echo "OK\n";
} catch (Exception $e) {
    echo "{$e->getMessage()} (#{$e->getCode()})\n";
}
Beispiel #2
0
/**
 * Fetches PClass
 *
 * @param   String $piKlarnaEid     ShopId
 * @param   String $piKlarnaSecret  ShopSecret
 *
 * @return  String                  Result message
 */
function piKlarnaFetchKlarnaPClass($piKlarnaEid, $piKlarnaSecret)
{
    if ($piKlarnaEid && $piKlarnaSecret) {
        require_once dirname(__FILE__) . '/../api/Klarna.php';
        require_once dirname(__FILE__) . '/../api/transport/xmlrpc-3.0.0.beta/lib/xmlrpc.inc';
        require_once dirname(__FILE__) . '/../api/transport/xmlrpc-3.0.0.beta/lib/xmlrpc_wrappers.inc';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
        $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config();
        
//        $sql = "SELECT shopID FROM s_core_plugin_configs WHERE name LIKE 'pi_klarna_Merchant_ID' AND value like ?";
//        $piKlarnaShopId = Shopware()->Db()->fetchOne($sql, array("%" . $piKlarnaEid . "%"));
//        if($piKlarnaShopId){
//        $sql                  = "SELECT value FROM s_core_plugin_configs WHERE name LIKE 'pi_klarna_liveserver' AND shopID = ?";
//        $piKlarnaLivecheck    = $piKlarnaConfig->pi_klarna_liveserver;
//        $piKlarnaLivecheckSub = substr($piKlarnaLivecheck, -3, -2);

        
        if ($piKlarnaConfig->pi_klarna_liveserver) {
            $mymode = Klarna::LIVE;
        } else {
            $mymode = Klarna::BETA;
        }

        $countrys=Array(81,59,73,154,164,209);
        $currencys=Array(2,3,2,2,1,0);
        $languages=Array(28,37,97,101,27,138);
        $countryNames=Array('Deutschland','D&auml;nemark','Finnland','Niederlande','Norwegen','Schweden');
        $counter=0;
        $errortext="";
        $piKlarnaErrorText="";
        for($i=0;$i<count($countrys);$i++){
            $k = new Klarna();
            $k->config(
                $piKlarnaEid,
                $piKlarnaSecret,
                KlarnaCountry::DE,
                KlarnaLanguage::DE,
                KlarnaCurrency::EUR,
                $mymode,
                'json',
                dirname(__FILE__) . '/../classes/Pclass_' . $piKlarnaEid . '.php',
                true,
                true
            );
            try{
                if($i==0)$piKlarnaErrorText='Ratenzahlungsmodalit&auml;ten f&uuml;r folgende Länder gespeichert: <br />';
                $k->fetchPClasses($countrys[$i],$languages[$i],$currencys[$i]);
                $counter++;
                $piKlarnaErrorText .= "<b>" . $countryNames[$i] . "</b>";
                $piKlarnaErrorText .=", ";
            }
            catch(Exception $e){
                $errortext= $e->getMessage(). ' (#' . $e->getCode() . ')';
            }
        }
        $rest = substr($piKlarnaErrorText, 0, -2);
        if($counter==0)
            return $errortext;
        else
            return $rest;
    }

}
 /**
  * @brief Validate Method
  *
  * @return update the module depending on klarna webservices
  */
 private function _postValidation()
 {
     $klarna = new Klarna();
     if ($_POST['klarna_mod'] == 'live') {
         Configuration::updateValue('KLARNA_MOD', Klarna::LIVE);
     } else {
         Configuration::updateValue('KLARNA_MOD', Klarna::BETA);
     }
     if (isset($_POST['klarna_active_invoice']) && $_POST['klarna_active_invoice']) {
         Configuration::updateValue('KLARNA_ACTIVE_INVOICE', true);
     } else {
         Configuration::deleteByName('KLARNA_ACTIVE_INVOICE');
     }
     if (isset($_POST['klarna_active_partpayment']) && $_POST['klarna_active_partpayment']) {
         Configuration::updateValue('KLARNA_ACTIVE_PARTPAYMENT', true);
     } else {
         Configuration::deleteByName('KLARNA_ACTIVE_PARTPAYMENT');
     }
     if (isset($_POST['klarna_email']) && $_POST['klarna_email']) {
         Configuration::updateValue('KLARNA_EMAIL', true);
     } else {
         Configuration::deleteByName('KLARNA_EMAIL');
     }
     foreach ($this->countries as $country) {
         Db::getInstance()->delete(_DB_PREFIX_ . 'klarna_payment_pclasses', 'country = "' . (int) $country['code'] . '"');
         Configuration::updateValue('KLARNA_STORE_ID_' . $country['name'], null);
         Configuration::updateValue('KLARNA_SECRET_' . $country['name'], null);
     }
     foreach ($this->countries as $key => $country) {
         if (isset($_POST['activate' . $country['name']])) {
             $storeId = (int) Tools::getValue('klarnaStoreId' . $country['name']);
             $secret = pSQL(Tools::getValue('klarnaSecret' . $country['name']));
             if ($storeId > 0 && $secret == '' || $storeId <= 0 && $secret != '') {
                 $this->_postErrors[] = $this->l('your credentials are incorrect and can\'t be used in ') . $country['name'];
             } elseif ($storeId >= 0 && $secret != '') {
                 $error = false;
                 try {
                     $klarna->config($storeId, Tools::safeOutput($secret), $country['code'], $country['langue'], $country['currency'], Configuration::get('KLARNA_MOD'), 'mysql', $this->_getDb());
                     $PClasses = $klarna->fetchPClasses($country['code']);
                 } catch (Exception $e) {
                     $error = true;
                     $this->_postErrors[] = (int) $e->getCode() . ': ' . Tools::safeOutput($e->getMessage());
                 }
                 if (!$error) {
                     Configuration::updateValue('KLARNA_STORE_ID_' . $country['name'], $storeId);
                     Configuration::updateValue('KLARNA_SECRET_' . $country['name'], $secret);
                     Configuration::updateValue('KLARNA_INVOICE_FEE_' . $country['name'], (double) Tools::getValue('klarnaInvoiceFee' . $country['name']));
                     Configuration::updateValue('KLARNA_MINIMUM_VALUE_' . $country['name'], (double) Tools::getValue('klarnaMinimumValue' . $country['name']));
                     Configuration::updateValue('KLARNA_MAXIMUM_VALUE_' . $country['name'], $_POST['klarnaMaximumValue' . $country['name']] != 0 ? (double) Tools::getValue('klarnaMaximumValue' . $country['name']) : 99999);
                     $id_product = Db::getInstance()->getValue('SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product_lang` WHERE `name` = \'invoiceFee' . $country['name'] . '\'');
                     $taxeRules = TaxRulesGroup::getAssociatedTaxRatesByIdCountry(Country::getByIso($key));
                     $maxiPrice = 0;
                     $idTaxe = 0;
                     foreach ($taxeRules as $key => $val) {
                         if ((int) $val > $maxiPrice) {
                             $maxiPrice = (int) $val;
                             $idTaxe = $key;
                         }
                     }
                     if ($id_product != null) {
                         $productInvoicefee = new Product((int) $id_product);
                         $productInvoicefee->price = (double) Tools::getValue('klarnaInvoiceFee' . $country['name']);
                         if (_PS_VERSION_ >= 1.5) {
                             StockAvailable::setProductOutOfStock((int) $productInvoicefee->id, true, null, 0);
                         }
                         if ($idTaxe != 0) {
                             $productInvoicefee->id_tax_rules_group = (int) $idTaxe;
                         }
                         $productInvoicefee->update();
                     } else {
                         $productInvoicefee = new Product();
                         $productInvoicefee->out_of_stock = 1;
                         $productInvoicefee->available_for_order = true;
                         $productInvoicefee->id_category_default = 2;
                         if ($idTaxe != 0) {
                             $productInvoicefee->id_tax_rules_group = (int) $idTaxe;
                         }
                         $languages = Language::getLanguages(false);
                         foreach ($languages as $language) {
                             $productInvoicefee->name[$language['id_lang']] = 'invoiceFee' . $country['name'];
                             $productInvoicefee->link_rewrite[$language['id_lang']] = 'invoiceFee' . $country['name'];
                         }
                         $productInvoicefee->price = (double) Tools::getValue('klarnaInvoiceFee' . $country['name']);
                         if (_PS_VERSION_ >= 1.5) {
                             $productInvoicefee->active = false;
                         }
                         $productInvoicefee->add();
                         if (_PS_VERSION_ >= 1.5) {
                             StockAvailable::setProductOutOfStock((int) $productInvoicefee->id, true, null, 0);
                         }
                     }
                     Configuration::updateValue('KLARNA_INV_FEE_ID_' . $country['name'], $productInvoicefee->id);
                     $this->_postValidations[] = $this->l('Your account has been updated to be used in ') . $country['name'];
                 }
                 $error = false;
             }
         }
     }
 }