示例#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, &$msg) {

		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');
		$errors = array();
		$klarnaData_paymentmethod = JRequest::getVar ('klarna_paymentmethod', '');
		if ($klarnaData_paymentmethod == 'klarna_invoice') {
			$sessionKlarna->klarna_option = 'invoice';
		} elseif ($klarnaData_paymentmethod == 'klarna_partPayment') {
			$sessionKlarna->klarna_option = 'part';
		} elseif ($klarnaData_paymentmethod == '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;
		$country3 = NULL;
		$countryId = 0;
		$this->_getCountryCode ($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 ();

		if ($msg = KlarnaHandler::checkDataFromEditPayment ($klarnaData, $cData['country_code_3'])) {
			//vmInfo($msg); // meanwhile the red baloon works
			$session->set ('Klarna', serialize ($sessionKlarna), 'vm');
			return FALSE;
		}

		$klarnaData['country'] = $cData['country_code'];
		$klarnaData['country3'] = $cData['country_code_3'];

		//$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") {
			$swedish_addresses = KlarnaHandler::getAddresses ($klarnaData['socialNumber'], $cData, $method);
			if (empty($swedish_addresses)) {
				$msg = JText::_ ('VMPAYMENT_KLARNA_ERROR_TITLE_2');
				$msg .= JText::_ ('VMPAYMENT_KLARNA_NO_GETADDRESS');
				$session->set ('Klarna', serialize ($sessionKlarna), 'vm');
				return FALSE;
			}
			//This example only works for GA_GIVEN.
			foreach ($swedish_addresses as $address) {
				if ($address->isCompany) {
					$klarnaData['company_name'] = $address->getCompanyName ();
					$klarnaData['first_name'] = "-";
					$klarnaData['last_name'] = "-";
				} 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');
			}
		}
		$address_type = NULL;
		$st = $this->getCartAddress ($cart, $address_type, TRUE);
		vmDebug ('getCartAddress', $st);
		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 . 'title'                 => $klarnaData['title'],
			$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, //$klarnaData['virtuemart_country_id'],
			$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 . 'socialNumber'          => empty($klarnaData['pno']) ? $klarnaData['socialNumber'] : $klarnaData['pno'],
			'address_type'                    => $address_type
		);
		if ($address_type == 'BT') {
			$update_data ['email'] = $klarnaData['email'];
		}

		if (!empty($st)) {
			$update_data = array_merge ($st, $update_data);
		}
		// 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 ("part_klarna_paymentPlan")));

		$sessionKlarna->KLARNA_DATA = $klarnaData;

		// 2 letters small
		//$settings = KlarnaHandler::getCountryData($method, $cart_country2);

		try {
			$address = new KlarnaAddr(
				$klarnaData['email'],
				$klarnaData['phone'],
				"", //mobile
				$klarnaData['first_name'],
				$klarnaData['last_name'], '',
				$klarnaData['street'],
				$klarnaData['zip'],
				$klarnaData['city'],
				$klarnaData['country'], // $settings['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;
	}