Exemplo n.º 1
0
 /**
  * This event is fired after the payment method has been selected. It can be used to store
  * additional payment info in the cart.
  * @author Valérie isaksen
  *
  * @param VirtueMartCart $cart: the actual cart
  * @return null if the payment was not selected, true if the data is valid, error message if the data is not vlaid
  *
  */
 public function plgVmOnSelectCheckPayment(VirtueMartCart $cart)
 {
     if (!$this->selectedThisByMethodId($cart->virtuemart_paymentmethod_id)) {
         return NULL;
         // Another method was selected, do nothing
     }
     if (!($method = $this->getVmPluginMethod($cart->virtuemart_paymentmethod_id))) {
         return NULL;
         // Another method was selected, do nothing
     }
     if (!class_exists('KlarnaAddr')) {
         require JPATH_VMKLARNAPLUGIN . DS . 'klarna' . DS . 'api' . DS . 'klarnaaddr.php';
     }
     $session = JFactory::getSession();
     $sessionKlarna = new stdClass();
     //$post = JRequest::get('post');
     $klarnaData_paymentmethod = JRequest::getVar('klarna_paymentmethod', '');
     if ($klarnaData_paymentmethod == 'klarna_invoice') {
         $klarnaData_payment = "klarna_invoice";
         $sessionKlarna->klarna_option = 'invoice';
     } elseif ($klarnaData_paymentmethod == 'klarna_partPayment') {
         $klarnaData_payment = "klarna_partpay";
         $sessionKlarna->klarna_option = 'part';
     } elseif ($klarnaData_paymentmethod == 'klarna_speccamp') {
         $klarnaData_payment = "klarna_speccamp";
         $sessionKlarna->klarna_option = 'spec';
     } else {
         return NULL;
     }
     // Store payment_method_id so we can activate the
     // right payment in case something goes wrong.
     $sessionKlarna->virtuemart_payment_method_id = $cart->virtuemart_paymentmethod_id;
     $sessionKlarna->klarna_paymentmethod = $klarnaData_paymentmethod;
     $this->_getCartAddressCountryCode($cart, $country3, $countryId, 'country_3_code');
     // $country2=  strtolower($country2);
     if (empty($country3)) {
         $country3 = "SWE";
         $countryId = ShopFunctions::getCountryIDByName($country3);
     }
     $cData = KlarnaHandler::countryData($method, strtoupper($country3));
     $klarnaData = KlarnaHandler::getDataFromEditPayment();
     $klarnaData['country'] = $cData['country_code'];
     //$country = $cData['country_code']; //KlarnaHandler::convertCountry($method, $country2);
     //$lang = $cData['language_code']; //KlarnaHandler::getLanguageForCountry($method, $country);
     // Get the correct data
     //Removes spaces, tabs, and other delimiters.
     // If it is a swedish customer we use the information from getAddress
     if (strtolower($cData['country_code']) == "se") {
         if (empty($klarnaData['socialNumber'])) {
             VmInfo('VMPAYMENT_KLARNA_MUST_VALID_PNO');
             return false;
         }
         $swedish_addresses = klarnaHandler::getAddresses($klarnaData['socialNumber'], $cData, $method);
         if (empty($swedish_addresses)) {
             VmInfo('VMPAYMENT_KLARNA_NO_GETADDRESS');
             return false;
         }
         //This example only works for GA_GIVEN.
         foreach ($swedish_addresses as $address) {
             if ($address->isCompany) {
                 $klarnaData['company_name'] = $address->getCompanyName();
             } else {
                 $klarnaData['first_name'] = $address->getFirstName();
                 $klarnaData['last_name'] = $address->getLastName();
             }
             $klarnaData['street'] = $address->getStreet();
             $klarnaData['zip'] = $address->getZipCode();
             $klarnaData['city'] = $address->getCity();
             $klarnaData['country'] = $address->getCountryCode();
             $countryId = $klarnaData['virtuemart_country_id'] = shopFunctions::getCountryIDByName($klarnaData['country']);
         }
         foreach ($klarnaData as $key => $value) {
             $klarnaData[$key] = mb_convert_encoding($klarnaData[$key], 'UTF-8', 'ISO-8859-1');
         }
     } elseif (!KlarnaHandler::checkDataFromEditPayment($klarnaData)) {
         //VmInfo('VMPAYMENT_KLARNA_MISSING_DATA');
         //return false;
     }
     $st = $this->getCartAddress($cart, $address_type, true);
     if ($address_type == 'BT') {
         $prefix = '';
     } else {
         $prefix = 'shipto_';
     }
     // Update the Shipping Address to what is specified in the register.
     $update_data = array($prefix . 'address_type_name' => 'Klarna', $prefix . 'company' => $klarnaData['company_name'], $prefix . 'first_name' => $klarnaData['first_name'], $prefix . 'middle_name' => $st['middle_name'], $prefix . 'last_name' => $klarnaData['last_name'], $prefix . 'address_1' => $klarnaData['street'], $prefix . 'address_2' => $klarnaData['house_ext'], $prefix . 'house_no' => $klarnaData['house_no'], $prefix . 'zip' => html_entity_decode($klarnaData['zip']), $prefix . 'city' => $klarnaData['city'], $prefix . 'virtuemart_country_id' => $countryId, $prefix . 'state' => '', $prefix . 'phone_1' => $klarnaData['phone'], $prefix . 'phone_2' => $st['phone_2'], $prefix . 'fax' => $st['fax'], $prefix . 'birthday' => empty($klarnaData['birthday']) ? $st['birthday'] : $klarnaData['birthday'], $prefix . 'social_number' => empty($klarnaData['pno']) ? $klarnaData['socialNumber'] : $klarnaData['pno'], 'address_type' => $address_type);
     if ($address_type == 'BT') {
         $update_data['email'] = $klarnaData['email'];
     }
     // save address in cart if different
     // 	if (false) {
     $cart->saveAddressInCart($update_data, $update_data['address_type'], true);
     //vmdebug('plgVmOnSelectCheckPayment $cart',$cart);
     //vmInfo(JText::_('VMPAYMENT_KLARNA_ADDRESS_UPDATED_NOTICE'));
     // 	}
     //}
     // Store the Klarna data in a session variable so
     // we can retrevie it later when we need it
     //$klarnaData['pclass'] = ($klarnaData_paymentmethod == 'klarna_invoice' ? -1 : intval(JRequest::getVar($kIndex . "paymentPlan")));
     $klarnaData['pclass'] = $klarnaData_paymentmethod == 'klarna_invoice' ? -1 : intval(JRequest::getVar("klarna_paymentPlan"));
     $sessionKlarna->KLARNA_DATA = $klarnaData;
     // 2 letters small
     //$settings = KlarnaHandler::getCountryData($method, $cart_country2);
     try {
         $address = new KlarnaAddr($klarnaData['email'], $klarnaData['phone'], "", $klarnaData['first_name'], $klarnaData['last_name'], '', $klarnaData['street'], $klarnaData['zip'], $klarnaData['city'], $klarnaData['country'], $klarnaData['house_no'], $klarnaData['house_ext']);
     } catch (Exception $e) {
         VmInfo($e->getMessage());
         return false;
         //KlarnaHandler::redirectPaymentMethod('message', $e->getMessage());
     }
     if (isset($errors) && count($errors) > 0) {
         $msg = JText::_('VMPAYMENT_KLARNA_ERROR_TITLE_1');
         foreach ($errors as $error) {
             $msg .= "<li> -" . $error . "</li>";
         }
         $msg .= JText::_('VMPAYMENT_KLARNA_ERROR_TITLE_2');
         unset($errors);
         VmError($msg);
         return false;
         //KlarnaHandler::redirectPaymentMethod('error', $msg);
     }
     $session->set('Klarna', serialize($sessionKlarna), 'vm');
     return true;
 }
 function updateAddressInCart($cart, $klarna_address, $address_type)
 {
     if ($address_type == 'BT') {
         $prefix = '';
         $vmAddress = $cart->BT;
     } else {
         $prefix = 'shipto_';
         $vmAddress = $cart->ST;
     }
     $this->debugLog($klarna_address, 'updateAddressInCart', 'debug');
     // Update the Shipping Address to what is specified in the register.
     $update_data = array($prefix . 'address_type_name' => 'klarnacheckout', $prefix . 'company' => isset($klarna_address['company_name']) ? $klarna_address['company_name'] : '', $prefix . 'first_name' => $klarna_address['given_name'], $prefix . 'last_name' => $klarna_address['family_name'], $prefix . 'address_1' => $klarna_address['street_address'], $prefix . 'address_2' => isset($klarna_address['street_address2']) ? $klarna_address['street_address2'] : '', $prefix . 'zip' => $klarna_address['postal_code'], $prefix . 'city' => $klarna_address['city'], $prefix . 'virtuemart_country_id' => shopFunctions::getCountryIDByName($klarna_address['country']), $prefix . 'state' => '', $prefix . 'phone_1' => $klarna_address['phone'], 'address_type' => $address_type);
     if ($address_type == 'BT') {
         $update_data['email'] = strtolower($klarna_address['email']);
     }
     $update_data['tos'] = 1;
     if (!empty($st)) {
         $update_data = array_merge($vmAddress, $update_data);
     }
     $cart->saveAddressInCart($update_data, $update_data['address_type'], TRUE, $prefix);
 }
Exemplo n.º 3
0
	private function getUserInfoFromAmazon ($amazonAddress, $prefix = '', $all = true, $getEmail = false) {

		if ($amazonAddress->isSetName()) {
			$userInfoData[$prefix . 'last_name'] = $amazonAddress->getName();
			$userInfoData[$prefix . 'first_name'] = '';
		}

		if ($getEmail AND $amazonAddress->isSetEmail()) {
			$userInfoData['email'] = $amazonAddress->getEmail();
		}
		if ($amazonAddress->isSetPhone()) {
			$userInfoData['phone_1'] = $amazonAddress->getPhone();

		}
		if ($all) {
			if ($amazonAddress->isSetAddressLine1()) {
				$userInfoData[$prefix . 'address_1'] = $amazonAddress->getAddressLine1();
				if ($amazonAddress->isSetAddressLine2()) {
					$userInfoData[$prefix . 'address_2'] = $amazonAddress->getAddressLine2();
				}
				if ($amazonAddress->isSetAddressLine3()) {
					$userInfoData[$prefix . 'address_2'] .= ", " . $amazonAddress->getAddressLine3();
				}
			} else {
				if ($amazonAddress->isSetAddressLine2()) {
					$userInfoData[$prefix . 'address_1'] = $amazonAddress->getAddressLine2();
				}
				if ($amazonAddress->isSetAddressLine3()) {
					$userInfoData[$prefix . 'address_2'] = $amazonAddress->getAddressLine3();
				}
			}

			if ($amazonAddress->isSetCity()) {
				$userInfoData[$prefix . 'city'] = $amazonAddress->getCity();
			}
			if ($amazonAddress->isSetCounty()) {
				//$userInfoData['county'] = $amazonAddress->getCounty();
			}
			if ($amazonAddress->isSetDistrict()) {
				//$userInfoData['district'] = $amazonAddress->GetDistrict();
			}
			if ($amazonAddress->isSetStateOrRegion()) {
				$stateId = shopFunctions::getStateIDByName($amazonAddress->GetStateOrRegion());
				if ($stateId) {
					$userInfoData[$prefix . 'virtuemart_state_id'] = $stateId;
				} else {
					$userInfoData[$prefix . 'virtuemart_state_id'] = 0;
				}
			}
			if ($amazonAddress->isSetPostalCode()) {
				$userInfoData[$prefix . 'zip'] = $amazonAddress->GetPostalCode();
			}
			if ($amazonAddress->isSetCountryCode()) {
				$userInfoData[$prefix . 'virtuemart_country_id'] = shopFunctions::getCountryIDByName($amazonAddress->GetCountryCode());
			}
		}


		return $userInfoData;
	}