Ejemplo n.º 1
0
    /**
     *  Displays the Klarna banner
     *
     *  @param Enlight_Event_EventArgs $piKlarnaArgs
     */
    static function piKlarnaOnPostDispatch(Enlight_Event_EventArgs $piKlarnaArgs) {
        $piKlarnaRequest = $piKlarnaArgs->getSubject()->Request();
        $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config();
        $piKlarnaView = $piKlarnaArgs->getSubject()->View();
        $piKlarnaAction = $piKlarnaRequest->getActionName();
        // break if no template is set.
        if(!$piKlarnaView->hasTemplate()) {
            return;
        }
        $templates = Shopware()->Template()->getTemplateDir();
        $firstTemplate = array_shift($templates);
        $piKlarnaView->_isEmotion = true;
        
        $piKlarnaView->addTemplateDir(dirname(__FILE__) . '/Views/Frontend/');
        if ($piKlarnaRequest->getModuleName() == 'frontend' && $piKlarnaConfig->pi_klarna_active == true) {
            $klarnaShopLang = checkKlarnaCountryCurrencys();
            if ($klarnaShopLang == 'de') $klarnaShopLang = Shopware()->Locale()->getLanguage();
            if ($piKlarnaView->sUserData) $piKlarnaImgPathLocale = strtolower($piKlarnaView->sUserData['additional']['country']['countryiso']);
            else $piKlarnaImgPathLocale = Shopware()->Locale()->getLanguage();
            if ($piKlarnaImgPathLocale == "sv") $piKlarnaImgPathLocale = "se";
            if (!checkKlarnaCountrys($piKlarnaImgPathLocale)) $piKlarnaImgPathLocale = 'de';
            
            $piKlarnaView->piKlarnaImgDir = 'http://' . Shopware()->Config()->Basepath
                    . '/engine/Shopware/Plugins/Default/Frontend/PigmbhKlarnaPayment/img/'
                    . $piKlarnaImgPathLocale
                    . '/';
            $piKlarnaView->piKlarnaShopLang = Shopware()->Locale()->getLanguage();
            $piKlarnaView->piKlarnaShopCurrency = Shopware()->Currency()->getShortName();
            $piKlarnaView->sPaymentErrorMethod = Shopware()->Session()->sPaymentErrorMethod;
            $piKlarnaView->pi_Klarna_lang = piKlarnaGetLanguage(Shopware()->Locale()->getLanguage());
            $piKlarnaView->pi_klarna_shopid = $piKlarnaConfig->pi_klarna_Merchant_ID;
            $sql = "SELECT active FROM s_core_paymentmeans  WHERE name = ?";
            $piKlarnaView->pi_klarna_active = Shopware()->Db()->fetchOne($sql,array('KlarnaInvoice'));
            $piKlarnaView->pi_klarna_rate_active = Shopware()->Db()->fetchOne($sql,array('KlarnaPartPayment'));
            $paymentMeans = Shopware()->Modules()->Admin()->sGetPaymentMeans();
            $allowKlarnaPartPayment = false;
            foreach($paymentMeans as $payment) {
                if('KlarnaPartPayment' == $payment['name']) { 
                    $allowKlarnaPartPayment = true;
                    break;
                }
            }
            if(!$allowKlarnaPartPayment) {
                $piKlarnaView->pi_klarna_rate_active = null;
            }

            
            $piKlarnaView->extendsTemplate('index/header.tpl');
            $piKlarnaUserdata = $piKlarnaView->sUserData;

            if($piKlarnaAction == 'payment' && $piKlarnaRequest->getControllerName() == 'checkout'
                 && ($piKlarnaUserdata["additional"]["payment"]["id"] == piKlarnaGetInvoicePaymentId()
                     || $piKlarnaUserdata["additional"]["payment"]["id"] == piKlarnaGetRatePaymentId())
            ){
            }
            if (($piKlarnaRequest->getControllerName() == 'account' 
                    && ($piKlarnaAction == 'payment' || $piKlarnaAction == 'savePayment' 
                        || $piKlarnaAction == 'saveBilling' || $piKlarnaAction == 'orders' || $piKlarnaAction == 'stornoOrder'))
                    || ($piKlarnaRequest->getControllerName() == 'checkout' 
                            && ($piKlarnaAction == 'confirm' || $piKlarnaAction == 'savePayment' || $piKlarnaAction == 'saveBilling'))
                    || ($piKlarnaRequest->getControllerName() == 'register' 
                            && ($piKlarnaAction == 'confirm' || $piKlarnaAction == 'savePayment' || $piKlarnaAction == 'saveBilling'))
                    || $piKlarnaAction == 'cart'
            ) {
                $piKlarnaCountryIso = getBillingCountry($piKlarnaView->sUserData);
                if ($piKlarnaCountryIso) {
                    $piKlarnaView->piKlarnaCountryIso = $piKlarnaCountryIso;
                    if (!checkKlarnaCurrency(strtolower($piKlarnaCountryIso))) $piKlarnaView->klarnaWrongCurrency = 1;
                }
                $piKlarnaView->KlarnaJS = true;
                $piKlarnaView->extendsTemplate('register/payment_fieldset.tpl');
                if (($piKlarnaAction == 'cart' || $piKlarnaAction == 'confirm' || $piKlarnaAction == 'payment') && $piKlarnaUserdata && checkKlarnaCountrys($klarnaShopLang)) {
                    $k = piKlarnaCreateKlarnaInstance();
                    $k->setCountry($klarnaShopLang);
                    $piKlarnaAmount = Shopware()->Modules()->Basket()->sgetAmount();
                    $piKlarnaSurcharge = piKlarnaGetSurcharge();
                    $piKlarnaBasketAmount = $piKlarnaAmount["totalAmount"];
                    $piKlarnaBasketAmount-= $piKlarnaSurcharge;
                    if ($piKlarnaBasketAmount == null) $piKlarnaBasketAmount = 0;
                    if($piKlarnaBasketAmount){
                        $pClass = $k->getCheapestPClass($piKlarnaBasketAmount, KlarnaFlags::CHECKOUT_PAGE);
                        if ($pClass) {
                            $piKlarnaView->pi_klarna_rateAmount = number_format(KlarnaCalc::calc_monthly_cost(
                                            $piKlarnaBasketAmount, 
                                            $pClass, 
                                            KlarnaFlags::CHECKOUT_PAGE), 2, ',', '.');
                            $piKlarnaView->pi_klarna_sum = $piKlarnaBasketAmount;
                            if ($piKlarnaCountryIso == "NO") {
                                $piKlarnaView->NorwayTotalCost = number_format(KlarnaCalc::total_credit_purchase_cost($piKlarnaBasketAmount, $pClass, 1), 2, ',', '.');
                                $piKlarnaView->NorwayAprCost = number_format(KlarnaCalc::apr_annuity(intval($piKlarnaBasketAmount), intval($pClass->getMonths()), $pClass->getInvoiceFee(), $pClass->getInterestRate(), $pClass->getStartFee(), 1), 2, ',', '.');
                            }
                            if ($piKlarnaConfig->pi_klarna_rate_checkout == true && $piKlarnaAction == 'confirm') $piKlarnaView->extendsTemplate('checkout/confirm_footer.tpl');
                            elseif (($piKlarnaAction == 'cart' || $piKlarnaRequest->sTargetAction == 'cart') && $piKlarnaConfig->pi_klarna_rate_cart == true) $piKlarnaView->extendsTemplate('checkout/cart_footer.tpl');
                            $piKlarnaView->RateIsTrue = true;
                        }
                    }
                }

                if ($piKlarnaAction == 'confirm' || $piKlarnaAction == 'payment') {
                    
                    $piKlarnaSurcharge = piKlarnaGetInvoiceSurcharge($piKlarnaUserdata);
                    if ($piKlarnaUserdata["additional"]["payment"]["id"] == piKlarnaGetInvoicePaymentId()
                            || $piKlarnaUserdata["additional"]["payment"]["id"] == piKlarnaGetRatePaymentId()
                    ) {
                        $piKlarnaUserdata["additional"]["payment"]["embediframe"] = true;
                        $piKlarnaView->sUserData = $piKlarnaUserdata;
                    }
                    if ($piKlarnaAction == 'confirm') {
                         if(Shopware()->Session()->klarnaAgb == true){
                             if(Shopware()->Session()->klarnaAgbChecked){
                                 $piKlarnaView->agbChecked = true;
                             }else{
                                 $piKlarnaView->agbChecked = false;
                             }
                             $piKlarnaView->piKlarnaError = true;
                             Shopware()->Session()->klarnaAgb =false;
                         }    
                         if (Shopware()->Session()->klarnaStandardAgb == true) {
                             if(Shopware()->Session()->klarnaStandardAgbChecked){
                                 $piKlarnaView->standardAgbChecked = true;
                             }else{
                                 $piKlarnaView->standardAgbChecked = false;
                             }
                             $piKlarnaView->sError = true;
                             Shopware()->Session()->klarnaStandardAgb =false;
                         } 
                         Shopware()->Session()->klarnaAgbChecked = false;
                         Shopware()->Session()->klarnaStandardAgbChecked = false;
                    }
                    $piKlarnaView->pi_klarna_surcharge = $piKlarnaSurcharge;
                    $piKlarnaResult = piKlarnaCheckBillingEqalShipping($piKlarnaUserdata);
                    if (Shopware()->Session()->sPaymentError) {
                        $piKlarnaView->piKlarnaSesssionWarning = true;
                        $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText = Shopware()->Session()->sPaymentError;
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText = Shopware()->Session()->sPaymentError;
                    }
                    if (Shopware()->Session()->klarnaDenied) $piKlarnaView->klarnaDenied = true;
                    if (($piKlarnaUserdata["billingaddress"]["company"] || $piKlarnaUserdata["shippingaddress"]["company"]) && ($piKlarnaCountryIso == "DE" || $piKlarnaCountryIso == "NL")) {
                        $piKlarnaView->sCompanyError = true;
                        $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText = $piKlarnaView->pi_Klarna_lang['invoice']['companyerror'];
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText = $piKlarnaView->pi_Klarna_lang['rate']['companyerror'];
                        $piKlarnaView->sPaymentRegisterError = true;
                    }
                    if ($piKlarnaUserdata["billingaddress"]["birthday"] == "0000-00-00" && ($piKlarnaCountryIso != "DK" && $piKlarnaCountryIso != "NO" && $piKlarnaCountryIso != "FI" && $piKlarnaCountryIso != "SE")) {
                        $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText = $piKlarnaView->pi_Klarna_lang['invoice']['birthdayerror'];
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText = $piKlarnaView->pi_Klarna_lang['rate']['birthdayerror'];
                        $piKlarnaView->sPaymentRegisterError = true;
                    }
                    if (count($piKlarnaResult) && !$piKlarnaArgs->getSubject()->View()->sCompanyError) {
                        $piKlarnaView->sAddressError = true;
                        $piKlarnaInvoiceError = str_replace("{_COUNTRYNAME_}", $piKlarnaView->pi_Klarna_lang['countryName'][$piKlarnaCountryIso], $piKlarnaView->pi_Klarna_lang['invoice']['addresserror']);
                        $piKlarnaRatepayError = str_replace("{_COUNTRYNAME_}", $piKlarnaView->pi_Klarna_lang['countryName'][$piKlarnaCountryIso], $piKlarnaView->pi_Klarna_lang['rate']['addresserror']);
                        $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText = $piKlarnaInvoiceError;
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText = $piKlarnaRatepayError;
                        $piKlarnaView->sPaymentRegisterError = true;
                    }
                    elseif (!$piKlarnaUserdata["billingaddress"]["text4"] && !Shopware()->Session()->sPaymentError 
                            && ($piKlarnaCountryIso == "DK" || $piKlarnaCountryIso == "NO" || $piKlarnaCountryIso == "FI" || $piKlarnaCountryIso == "SE")) {
                        $piKlarnaInvoiceError = str_replace("{_COUNTRYNAME_}", $piKlarnaView->pi_Klarna_lang['countryName'][$piKlarnaCountryIso], $piKlarnaView->pi_Klarna_lang['invoice']['skanderror']);
                        $piKlarnaRatepayError = str_replace("{_COUNTRYNAME_}", $piKlarnaView->pi_Klarna_lang['countryName'][$piKlarnaCountryIso], $piKlarnaView->pi_Klarna_lang['rate']['skanderror']);
                        if ($piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText != '') $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText.='<br />';
                        if ($piKlarnaView->PigmbhKlarnaPaymentRateWarningText != '') $piKlarnaView->PigmbhKlarnaPaymentRateWarningText.='<br />';
                        $piKlarnaView->PigmbhKlarnaPaymentInvoiceWarningText.=$piKlarnaInvoiceError;
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText.=$piKlarnaRatepayError;
                        $piKlarnaView->sPaymentRegisterError = true;
                    }
                    if (!$piKlarnaView->RateIsTrue) {
                        $piKlarnaView->PigmbhKlarnaPaymentRateWarningText = $piKlarnaView->pi_Klarna_lang['rate']['noPclass'];
                    }
                    $piKlarnaView->pi_klarna_viewport = $piKlarnaArgs->getSubject()->Request()->sViewport;
                    $piKlarnaView->pi_klarna_actions = $piKlarnaArgs->getSubject()->Request()->getActionName();
                    if ($piKlarnaUserdata["additional"]["payment"]["name"] == "KlarnaInvoice"
                            || $piKlarnaUserdata["additional"]["payment"]["name"] == "KlarnaPartPayment"
                    ) {
                        $piKlarnaView->extendsTemplate('checkout/confirm.tpl');
                    }
                }
            }
           
            if (($piKlarnaAction == 'billing') && $piKlarnaConfig->pi_klarna_active && $piKlarnaRequest->sViewport == 'account') {
                $piKlarnaCountryIso = getBillingCountry($piKlarnaView->sUserData);
                $piKlarnaView->piKlarnaCountryIso = $piKlarnaCountryIso;
                $piKlarnaView->extendsTemplate('register/personal_fieldset.tpl');
            }
            if (($piKlarnaAction == 'orders' || $piKlarnaAction == 'stornoOrder') && $piKlarnaConfig->pi_klarna_active) {
                $piKlarnaView->klarnaStatusIds = piKlarnaGetAllStatusIds();
                piKlarnaCheckPendingOrders();
                $piKlarnaView->extendsTemplate(dirname(__FILE__) . '/Views/Frontend/account/order_item.tpl');
                $PigmbhKlarnaPaymentIds = piKlarnaGetPaymentIds();
                $piKlarnaArgs->getSubject()->View()->pi_klarna_invoice_ids = $PigmbhKlarnaPaymentIds;
            }
            $piKlarnaGetPost = $piKlarnaArgs->getSubject()->Request()->getPost();
            if (isset($piKlarnaGetPost['KlarnaSubmit']) || $piKlarnaAction == 'saveBilling') {
                $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config();
                $piKlarnaInvoiceId = piKlarnaGetInvoicePaymentId();
                $piKlarnaRateId = piKlarnaGetRatePaymentId();
                if ($piKlarnaConfig->pi_klarna_active == true && ($piKlarnaGetPost['KlarnaSubmit'] || $piKlarnaAction == 'saveBilling')) {
                    $piKlarnaResponse = array();
                    $textVar = false;
                    $piKlarnaUserdata = $piKlarnaArgs->getSubject()->View()->sUserData;
                    $piKlarnaResponse = $piKlarnaArgs->getSubject()->Response();
                    $piKlarnaRequest = $piKlarnaArgs->getSubject()->Request();
                    $piKlarnaResult = piKlarnaCheckBillingEqalShipping($piKlarnaUserdata);
                    if ($piKlarnaGetPost['klarnaRegister']['personal']['additional']) {
                        $textVar = "";
                        $invoiceFlag = boolean;
                        $textVar = $piKlarnaGetPost['klarnaRegister']['personal']['additional'];
                        $invoiceFlag = true;
                    }
                    elseif ($piKlarnaGetPost['klarnaRegister']['personal']['additionalRate']){
                        $textVar = $piKlarnaGetPost['klarnaRegister']['personal']['additionalRate'];
                    }
                    if ($textVar) {
                        $sql = "INSERT INTO s_user_billingaddress_attributes (billingID, text4) VALUES (?, ?) ON DUPLICATE KEY UPDATE text4=VALUES(text4)";
//                        $sql = "UPDATE s_user_billingaddress SET text4 = ? WHERE ID= ?";
                        Shopware()->Db()->query($sql, array($textVar, (int)$piKlarnaUserdata['billingaddress']['id']));
                    }
                    $piKlarnaArgs->getSubject()->View()->pi_klarna_selectedPayment = $piKlarnaUserdata['additional']['payment']['name'];
                    $piKlarnaBirthday = $piKlarnaGetPost['klarnaRegister']['personal']['birthyear'] . "-"
                            . $piKlarnaGetPost['klarnaRegister']['personal']['birthmonth'] . "-"
                            . $piKlarnaGetPost['klarnaRegister']['personal']['birthday'];
                    if ($piKlarnaBirthday == '--') {
                        $piKlarnaBirthday = $piKlarnaGetPost['klarnaRegister']['personal']['birthyearRate'] . "-"
                                . $piKlarnaGetPost['klarnaRegister']['personal']['birthmonthRate'] . "-"
                                . $piKlarnaGetPost['klarnaRegister']['personal']['birthdayRate'];
                    }
                    else $invoiceFlag = true;
                    if ($piKlarnaBirthday != '--') {
                        $sql = " UPDATE s_user_billingaddress SET birthday = ? WHERE ID = ?";
                        Shopware()->Db()->query($sql, array($piKlarnaBirthday, (int)$piKlarnaUserdata['billingaddress']['id']));
                    }
                    if ($invoiceFlag) $piKlarnaId = $piKlarnaInvoiceId;
                    else $piKlarnaId = $piKlarnaRateId;
                    if (($piKlarnaUserdata['additional']['country']['countryiso'] == 'NL' && $piKlarnaBirthday != '--' 
                        && !$piKlarnaUserdata["billingaddress"]["company"] && !$piKlarnaUserdata["shippingaddress"]["company"])
                        || ($piKlarnaUserdata['additional']['country']['countryiso'] == 'DE' && $piKlarnaBirthday != '--' 
                                && !$piKlarnaUserdata["billingaddress"]["company"] && !$piKlarnaUserdata["shippingaddress"]["company"])
                        && (count($piKlarnaResult)
                        && $piKlarnaAction != 'saveBilling')
                    ) {
                        $sql = "UPDATE s_user SET paymentID = ? WHERE id= ?";
                        Shopware()->Db()->query($sql, array((int)$piKlarnaId, (int)$piKlarnaUserdata['billingaddress']['id']));
                    }
                    elseif ($textVar && count($piKlarnaResult)) {
                        $sql = "UPDATE s_user SET paymentID = ? WHERE id= ?";
                        Shopware()->Db()->query($sql, array((int)$piKlarnaId, (int)$piKlarnaUserdata['billingaddress']['id']));
                    }
                }
            }
            $piKlarnaResponse = $piKlarnaArgs->getSubject()->Response();
            if ($piKlarnaConfig->piKlarnaShowBannerOnStartpage
                    && (!$piKlarnaRequest->isDispatched()
                    || $piKlarnaResponse->isException()
                    || $piKlarnaRequest->getModuleName() != 'frontend'
                    || $piKlarnaRequest->getControllerName() != 'index'
                    )) return;
            elseif (!$piKlarnaConfig->piKlarnaShowBannerOnStartpage
                    && (!$piKlarnaRequest->isDispatched()
                    || $piKlarnaResponse->isException()
                    || $piKlarnaRequest->getModuleName() != 'frontend'
                    )) return;
            $piKlarnaView->piKlarnaConfig = $piKlarnaConfig;
            $piKlarnaView->extendsTemplate('index/left.tpl');
            $piKlarnaView->extendsTemplate('home/right.tpl');
            $piKlarnaView->extendsTemplate('index/index.tpl');
        }
    }
Ejemplo n.º 2
0
 /**
  * reserves the order at Klarna
  *
  * @param Object $k             Klarna object
  * @param int    $pclassId      Current Pclass
  *
  * @return Array $result    Response
  */
 public function reserveAmount($k,$pclassId)
 {
     $myuser = $this->getUser();
     $piKlarnaCountryIso = getBillingCountry($myuser) ;
     $piKlarnaConfig = array();
     $myKlarnaPno = "";
     $piKlarnaConfig = Shopware()->Plugins()->Frontend()->PigmbhKlarnaPayment()->Config();
     if ($myuser["billingaddress"]["salutation"] == "mr") {
         $mygender = KlarnaFlags::MALE;
     } else {
         $mygender = KlarnaFlags::FEMALE;
     }
     if($piKlarnaCountryIso=="DE" || $piKlarnaCountryIso=="NL"){
         $mybirthday = explode("-", $myuser["billingaddress"]["birthday"]);
         $myKlarnaPno = $mybirthday[2] . $mybirthday[1] . $mybirthday[0];
     }
     else{
         $myKlarnaPno=$myuser["billingaddress"]["text4"];
     }
     if ($piKlarnaConfig->pi_klarna_Testmode == true) {
         $myTestmode = 2;
     } else {
         $myTestmode = 0;
     }
     $result=$k->reserveAmount($myKlarnaPno, $mygender, -1, $myTestmode, $pclassId);
     return $result;
 }