AAAAklarnacheckout.xml 0000666 00000012607 15137370070 0010301 0 ustar 00
';
}
$payment_cost = '';
if ($methodSalesPrice) {
$payment_cost = $currency->priceDisplay($methodSalesPrice);
}
if ($selected == $method->virtuemart_paymentmethod_id) {
$checked = 'checked="checked"';
} else {
$checked = '';
}
if ($cart->virtuemart_paymentmethod_id == $method->virtuemart_paymentmethod_id) {
$showallform=false;
}
$html = $this->renderByLayout('display_payment', array(
'plugin' => $method,
'checked' => $checked,
'payment_logo' => $logo,
'payment_cost' => $payment_cost,
'showallform' => $showallform
));
$htmla[] = $html;
}
}
if ( $showallform) {
$js = '
jQuery(document).ready(function( $ ) {
$("#checkoutForm").show();
$(".billto-shipto").show();
$("#com-form-login").show();
});
';
$document = JFactory::getDocument();
$document->addScriptDeclaration ( $js);
}
if (!empty($htmla)) {
$htmlIn[] = $htmla;
}
return true;
}
function getCartItems ($cart) {
vmdebug('getProductItems', $cart->pricesUnformatted);
//self::includeKlarnaFiles();
$i = 0;
foreach ($cart->products as $pkey => $product) {
$items[$i]['reference'] = !empty($product->sku) ? $product->sku : $product->virtuemart_product_id;
$items[$i]['name'] = $product->product_name;
$items[$i]['quantity'] = (int)$product->quantity;
$price = !empty($product->prices['basePriceWithTax']) ? $product->prices['basePriceWithTax'] : $product->prices['basePriceVariant'];
$itemInPaymentCurrency = vmPSPlugin::getAmountInCurrency($price,$this->method->payment_currency);
$items[$i]['unit_price'] = round($itemInPaymentCurrency['value'] * 100, 0) ;
//$items[$i]['discount_rate'] = $discountRate;
// Bug indoc: discount is not supported
//$items[$i]['discount'] = abs($cart->pricesUnformatted[$pkey]['discountAmount']*100);
$tax_rate = round($this->getVatTaxProduct($cart->pricesUnformatted[$pkey]['VatTax']) );
$items[$i]['tax_rate'] = $tax_rate * 100;
//$this->debugLog($unitPriceCentsInPaymentCurrency, 'getCartItems', 'debug');
//$this->debugLog($cart->pricesUnformatted[$pkey], 'getCartItems Products', 'debug');
$this->debugLog($items[$i], 'getCartItems', 'debug');
$i++;
// ADD A DISCOUNT AS A NEGATIVE VALUE FOR THAT PRODUCT
if ($cart->pricesUnformatted[$pkey]['discountAmount'] != 0.0) {
$items[$i]['reference'] = $items[$i-1]['reference'];
$items[$i]['name'] = $items[$i-1]['name']. ' ('.JText::_('VMPAYMENT_KLARNACHECKOUT_PRODUCTDISCOUNT'). ')';
$items[$i]['quantity'] =(int)$product->quantity;
$discount_tax_percent=0.0;
$discountInPaymentCurrency = vmPSPlugin::getAmountInCurrency(abs($cart->pricesUnformatted[$pkey]['discountAmount']),$this->method->payment_currency);
$discountAmount=- abs( round($discountInPaymentCurrency['value'] * 100 , 0));
if ($cart->pricesUnformatted[$pkey]['discountAmount'] > 0.0) {
$items[$i]['tax_rate'] =$items[$i-1]['tax_rate'];
} else {
$items[$i]['tax_rate'] =0.0;
$tax_rate =0.0;
}
$items[$i]['unit_price'] = round($discountAmount * (1+ ($tax_rate*0.01)) , 0);
$this->debugLog($items[$i], 'getCartItems', 'debug');
$i++;
}
}
if ($cart->pricesUnformatted['salesPriceCoupon']) {
$items[$i]['reference'] = 'COUPON';
$items[$i]['name'] = 'Coupon discount';
$items[$i]['quantity'] = 1;
$couponInPaymentCurrency = vmPSPlugin::getAmountInCurrency($cart->pricesUnformatted['salesPriceCoupon'],$this->method->payment_currency);
$items[$i]['unit_price'] = round( $couponInPaymentCurrency['value'] *100, 0);
$items[$i]['tax_rate'] = 0;
$this->debugLog($cart->pricesUnformatted['salesPriceCoupon'], 'getCartItems Coupon', 'debug');
$this->debugLog($items[$i], 'getCartItems', 'debug');
$i++;
}
if ($cart->pricesUnformatted['salesPriceShipment']) {
$items[$i]['reference'] = 'SHIPPING';
$items[$i]['name'] = 'Shipping Fee';
$items[$i]['quantity'] = 1;
$shipmentInPaymentCurrency = vmPSPlugin::getAmountInCurrency($cart->pricesUnformatted['salesPriceShipment'],$this->method->payment_currency);
$items[$i]['unit_price'] = round( $shipmentInPaymentCurrency['value'] *100, 0);
$items[$i]['tax_rate'] = $this->getTaxShipment($cart->pricesUnformatted['shipment_calc_id']);
$this->debugLog($cart->pricesUnformatted['salesPriceShipment'], 'getCartItems Shipment', 'debug');
$this->debugLog($items[$i], 'getCartItems', 'debug');
}
$currency = CurrencyDisplay::getInstance($cart->paymentCurrency);
return $items;
}
function getTaxShipment ($shipment_calc_id) {
// TO DO add shipmentTaxRate in the cart
// assuming there is only one rule +%
$db = JFactory::getDBO();
$q = 'SELECT * FROM #__virtuemart_calcs WHERE `virtuemart_calc_id`="' . $shipment_calc_id . '" ';
$db->setQuery($q);
$taxrule = $db->loadObject();
if ($taxrule->calc_value_mathop != "+%") {
VmError('KlarnaCheckout getTaxShipment: expecting math operation to be +% but is ' . $taxrule->calc_value_mathop);
}
return $taxrule->calc_value * 100;
}
function getVatTaxProduct ($vatTax) {
$countRules = count($vatTax);
if ($countRules == 0) {
return 0;
}
if ($countRules > 1) {
VmError('KlarnaCheckout: More then one VATax for the product:' . $countRules);
}
$tax = current($vatTax);
if ($tax[2] != "+%") {
VmError('KlarnaCheckout: expecting math operation to be +% but is ' . $tax[2]);
}
return $tax[1];
}
function plgVmOnCheckoutAdvertise ($cart, &$payment_advertise) {
if ($this->getPluginMethods($cart->vendorId) === 0) {
return FALSE;
}
$virtuemart_paymentmethod_id = 0;
foreach ($this->methods as $method) {
if ($cart->virtuemart_paymentmethod_id == $method->virtuemart_paymentmethod_id) {
$virtuemart_paymentmethod_id = $method->virtuemart_paymentmethod_id;
}
}
if ($virtuemart_paymentmethod_id == 0 or empty($cart->products)) {
return;
}
if (!($this->method = $this->getVmPluginMethod($virtuemart_paymentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
// Check if it is the same payment_method_id as the previous one.
$session = JFactory::getSession();
$klarna_paymentmethod_id_active = $session->get('klarna_paymentmethod_id_active', '', 'vm');
if ($klarna_paymentmethod_id_active != $cart->virtuemart_paymentmethod_id) {
$session->clear('klarna_checkout', 'vm');
$session->clear('klarna_paymentmethod_id_active', 'vm');
}
$return = $this->initKlarnaParams($this->method);
if (!$return) {
return;
}
$message = '';
$snippet = '';
$hide_BTST=true;
if ($cart->virtuemart_shipmentmethod_id == 0) {
$message = JText::sprintf('VMPAYMENT_KLARNACHECKOUT_SELECT_SHIPMENT_FIRST', $this->method->payment_name);
} else {
$session = JFactory::getSession();
$cartIdInTable = $this->storeCartInTable($cart);
require_once 'klarnacheckout/library/Checkout.php';
Klarna_Checkout_Order::$baseUri = 'https://checkout.testdrive.klarna.com/checkout/orders';
Klarna_Checkout_Order::$contentType = "application/vnd.klarna.checkout.aggregated-order-v2+json";
//session_start();
$klarna_checkout = $session->get('klarna_checkout', '', 'vm');
$connector = Klarna_Checkout_Connector::create($this->sharedsecret);
$klarnaOrder = null;
//if (array_key_exists('klarna_checkout', $_SESSION)) {
if (!empty($klarna_checkout)) {
// Resume session
$klarnaOrder = new Klarna_Checkout_Order($connector, $klarna_checkout);
try {
$klarnaOrder->fetch();
// Reset cart
$update['cart']['items'] = array();
$update['cart']['items'] = $this->getCartItems($cart );
if (!empty( $cart->BT['email'])) {
$update['shipping_address']['email'] = $cart->BT['email'];
$hide_BTST=false;
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
if (isset($address['zip']) and !empty($address['zip'])) {
$update['shipping_address']['postal_code'] = $cart->BT['zip'];
}
}
$klarnaOrder->update($update);
$this->debugLog($update, 'plgVmOnCheckoutAdvertise update', 'debug');
} catch (Exception $e) {
// Reset session
$klarnaOrder = null;
//unset($_SESSION['klarna_checkout']);
$session->clear('klarna_checkout', 'vm');
$session->clear('klarna_paymentmethod_id_active', 'vm');
}
}
if ($klarnaOrder == null) {
// Start new session
$create['purchase_country'] = $this->country_code_2;
$create['purchase_currency'] = $this->currency_code_3;
$create['locale'] = $this->locale;
$create['merchant']['id'] = $this->merchantid;
$create['merchant']['terms_uri'] = $this->getTermsURI($cart->vendorId);
$create['merchant']['checkout_uri'] = JURI::root(). 'index.php?option=com_virtuemart&view=cart';
$create['merchant']['confirmation_uri'] = JURI::root().'index.php?option=com_virtuemart&view=pluginresponse&task=pluginresponsereceived&t&pm=' . $virtuemart_paymentmethod_id . '&cartId=' . $cartIdInTable . '&klarna_order={checkout.order.uri}';
// You can not receive push notification on non publicly available uri
$create['merchant']['push_uri'] = JURI::root().'index.php?option=com_virtuemart&view=pluginresponse&task=pluginnotification&tmpl=component&pm=' . $virtuemart_paymentmethod_id . '&cartId=' . $cartIdInTable . '&klarna_order={checkout.order.uri}';
if (!empty( $cart->BT['email'])) {
$create['shipping_address']['email'] = $cart->BT['email'];
$hide_BTST=false;
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
if (isset($address['zip']) and !empty($address['zip'])) {
$create['shipping_address']['postal_code'] = $cart->BT['zip'];
}
}
$create['cart']['items'] = $this->getCartItems($cart, $method);
try {
$klarnaOrder = new Klarna_Checkout_Order($connector);
$klarnaOrder->create($create);
$klarnaOrder->fetch();
$this->debugLog($create, 'plgVmOnCheckoutAdvertise create', 'debug');
} catch (Exception $e) {
$session->clear('klarna_checkout', 'vm');
$session->clear('klarna_paymentmethod_id_active', 'vm');
$admin_msg = $e->getMessage();
vmError($admin_msg, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name));
$this->debugLog($admin_msg, 'plgVmOnCheckoutAdvertise', 'error');
$this->debugLog($create, 'plgVmOnCheckoutAdvertise', 'error');
return NULL;
}
}
// Store location of checkout session
//$_SESSION['klarna_checkout'] = $sessionId = $order->getLocation();
$session->set('klarna_checkout', $klarnaOrder->getLocation(), 'vm');
$session->set('klarna_paymentmethod_id_active', $virtuemart_paymentmethod_id, 'vm');
// Display checkout
$snippet = $klarnaOrder['gui']['snippet'];
// DESKTOP: Width of containing block shall be at least 750px
// MOBILE: Width of containing block shall be 100% of browser window (No
// padding or margin)
}
$payment_advertise[] = $this->renderByLayout('cart_advertisement', array(
'snippet' => $snippet,
'message' => $message,
'hide_BTST' => $hide_BTST,
));
}
/**
* cf https://docs.klarna.com/en/rest-api#supported_locales
* @param $method
*/
function initKlarnaParams ($method) {
$return = true;
$db = JFactory::getDBO();
$q = 'SELECT ' . $db->getEscaped('country_2_code') . ' , ' . $db->getEscaped('country_3_code') . ' FROM `#__virtuemart_countries` WHERE virtuemart_country_id = ' . (int)$method->purchase_country;
$db->setQuery($q);
$country = $db->loadObject();
if (!$country) {
vmError('Klarna Checkout: No country has been found with country id=' . $method->purchase_country, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name));
$this->debugLog('No country has been found with country id=' . $method->purchase_country, 'initKlarnaParams', 'error');
$return = false;
}
$this->country_code_2 = $country->country_2_code;
$this->country_code_3 = $country->country_3_code;
$this->getPaymentCurrency($method);
$this->currency_code_3 = shopFunctions::getCurrencyByID($method->payment_currency, 'currency_code_3');
if (!$this->currency_code_3) {
vmError('Klarna Checkout: No currency has been found with currency id=' . $method->payment_currency, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name));
$this->debugLog('No currency has been found with currency id=' . $method->payment_currency, 'initKlarnaParams', 'error');
$return = false;
}
$this->currency_id = $method->payment_currency;
if (empty($method->sharedsecret) or empty($method->merchantid)) {
vmError('Klarna Checkout: Missing mandatory values merchant id=' . $method->merchantid . ' shared secret=' . $method->sharedsecret, JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $method->payment_name));
$this->debugLog('Missing mandatory values merchant id=' . $method->merchantid . ' shared secret=' . $method->sharedsecret, 'initKlarnaParams', 'error');
$return = false;
}
$this->locale = $method->locale;
$this->sharedsecret = $method->sharedsecret;
$this->merchantid = $method->merchantid;
if ($method->server == 'beta') {
$this->mode = Klarna::BETA;
} else {
$this->mode = Klarna::LIVE;
}
$this->ssl = KlarnaHandler::getKlarnaSSL($this->mode);
return $return;
}
function getTermsURI ($vendorId) {
return JURI::root() . 'index.php?option=com_virtuemart&view=vendor&layout=tos&virtuemart_vendor_id=' . $vendorId . '&lang='.JRequest::getCmd('lang','') ;;
}
/** Insert or Update the cart content in the table
* will be used by the push notification to retrieve the cart and save the order
* @param $cart
*/
function storeCartInTable ($cart, $cartId = 0, $dbValues = array()) {
if (empty($dbValues)) {
$dbValues['order_number'] = '';
$dbValues['payment_name'] = '';
$dbValues['virtuemart_paymentmethod_id'] = $cart->virtuemart_paymentmethod_id;
$dbValues['action'] = 'storeCart';
$dbValues['klarna_status'] = 'pre-purchase';
}
if (empty($cartId)) {
$session = JFactory::getSession();
$cartIdInTable = $session->get('cartId', 0, 'vm');
$dbValues['data'] = serialize($cart);
//$dbValues['data'] = ($cart);
$preload = false;
} else {
$cartIdInTable = $cartId;
//$dbValues['data'] = $this->getCartFromTable($cartId, true);
$dbValues['data'] = serialize($cart);
$preload = true;
}
$dbValues ['id'] = $cartIdInTable;
$this->debugLog($dbValues, 'storePSPluginInternalData storeCartInTable', 'debug');
//$values = $this->storePSPluginInternalData($dbValues, $this->_tablepkey, $preload);
$values = $this->storePluginInternalData($dbValues, $this->_tablepkey, 0, $preload);
/*
//$storedcart=unserialize($dbValues['data']);
$storedcart= ($dbValues['data']);
if ($storedcart !== $cart) {
$this->debugLog($cart, 'storeCartInTable cart', 'error');
$this->debugLog($dbValues, 'storeCartInTable dbValues', 'error');
}
*/
if (empty($cartId)) {
$session->set('cartId', $values ['id'], 'vm');
}
return $values ['id'];
}
/** get the cart saved in the cart table
* used by the push notification to retrieve the cart and save the order
* @param $cart
*/
function getCartFromTable ($cartId, $serialized = false) {
$db = JFactory::getDBO();
$q = 'SELECT * FROM `' . $this->_tablename . '` ' . 'WHERE `id` = ' . $cartId . ' AND `action` = "storeCart"';
$db->setQuery($q);
$result = $db->loadObject();
if ($serialized) {
$data = $result->data;
} else {
$data = unserialize($result->data);
//return ($result->data);
}
return $data;
}
/** get the cart saved in the cart table
* used by the push notification to retrieve the cart and save the order
* @param $cart
*/
function clearCartFromTable () {
//$session = JFactory::getSession();
//$session->clear('cartId', 'vm');
/*
$db = JFactory::getDBO();
$q = 'DELETE FROM ' . $db->quoteName($this->_cartTablename) . ' WHERE `session_id`=' . $db->quote($sessionId);
$db->setQuery($q);
$db->query();
*/
}
/*
* @param $method plugin
* @param $where from where tis function is called
*/
protected function renderPluginName ($method, $where = 'checkout') {
$payment_logo = "";
if (!empty($method->payment_logos)) {
$payment_logo = '
';
}
$payment_name = $method->payment_name;
$html = $this->renderByLayout('render_pluginname', array(
'where' => $where,
'logo' => $payment_logo,
'payment_name' => $payment_name,
'payment_description' => $method->payment_desc,
));
return $html;
}
/**
* This is for checking the input data of the payment method within the checkout
*
* @author Valerie Cartan Isaksen
*/
function plgVmOnCheckoutCheckDataPayment (VirtueMartCart $cart) {
if (!$this->selectedThisByMethodId($cart->virtuemart_paymentmethod_id)) {
return NULL; // Another method was selected, do nothing
}
return true;
}
/**
* @return bool|null
*/
/**
* @param $html
* @return bool|null|string
*/
function plgVmOnPaymentResponseReceived (&$html) {
if (!class_exists('VirtueMartCart')) {
require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
}
if (!class_exists('shopFunctionsF')) {
require(JPATH_VM_SITE . DS . 'helpers' . DS . 'shopfunctionsf.php');
}
if (!class_exists('VirtueMartModelOrders')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
}
require_once 'klarnacheckout/library/Checkout.php';
$virtuemart_paymentmethod_id = JRequest::getInt('pm', 0);
if (!($this->method = $this->getVmPluginMethod($virtuemart_paymentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
if (!$this->selectedThisElement($this->method->payment_element)) {
return NULL;
}
//session_start();
$session = JFactory::getSession();
Klarna_Checkout_Order::$contentType = "application/vnd.klarna.checkout.aggregated-order-v2+json";
$this->initKlarnaParams($this->method);
$connector = Klarna_Checkout_Connector::create($this->sharedsecret);
//$checkoutId = $_SESSION['klarna_checkout'];
$checkoutId = $session->get('klarna_checkout', 0, 'vm');
if (empty($checkoutId)) {
vmError('Missing klarna_checkout in session', 'Missing klarna_checkout in session', JText::sprintf('VMPAYMENT_KLARNACHECKOUT_ERROR_OCCURRED', $this->method->payment_name));
$this->debugLog('Missing klarna_checkout in session', 'plgVmOnPaymentResponseReceived', 'error');
return NULL;
}
$order = new Klarna_Checkout_Order($connector, $checkoutId);
$order->fetch();
if ($order['status'] == 'checkout_incomplete') {
$app = JFactory::getApplication();
$app->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart',false), JText::_('VMPAYMENT_KLARNACHECKOUT_INCOMPLETE'));
}
$snippet = $order['gui']['snippet'];
// DESKTOP: Width of containing block shall be at least 750px
// MOBILE: Width of containing block shall be 100% of browser window (No
// padding or margin)
//$html .= var_export($order->_data);
$html = $this->renderByLayout('response_received', array(
'snippet' => $snippet,
));
//unset($_SESSION['klarna_checkout']);
$session->clear('klarna_checkout', 'vm');
$session->clear('cartId', 'vm');
// let's do
//We delete the old stuff
// get the correct cart / session
$cart = VirtueMartCart::getCart();
$cart->emptyCart();
return TRUE;
}
/*
* plgVmOnPaymentNotification() - This event is fired by Offline Payment. It can be used to validate the payment data as entered by the user.
* Return:
* Parameters:
* None
* @author Valerie Isaksen
*/
/**
* @return bool|null
*/
function plgVmOnPaymentNotification () {
$virtuemart_paymentmethod_id = JRequest::getInt('pm', '');
$checkoutId = JRequest::getString('klarna_order', '');
$cartId = JRequest::getString('cartId', '');
if (empty($virtuemart_paymentmethod_id) or !$this->selectedThisByMethodId($virtuemart_paymentmethod_id) or empty($checkoutId) or empty($cartId)) {
return NULL;
}
if (!($this->method = $this->getVmPluginMethod($virtuemart_paymentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
if (!($cartDataFromTable = $this->getCartFromTable($cartId))) {
$this->debugLog('No cart with this Id=' . $cartId, 'plgVmOnPaymentNotification', 'error');
return NULL; // No cart with this Id
}
$this->debugLog('OK', 'plgVmOnPaymentNotification getCartFromTable', 'debug');
require_once 'klarnacheckout/library/Checkout.php';
Klarna_Checkout_Order::$contentType = "application/vnd.klarna.checkout.aggregated-order-v2+json";
$this->initKlarnaParams($this->method);
$connector = Klarna_Checkout_Connector::create($this->sharedsecret);
$klarna_order = new Klarna_Checkout_Order($connector, $checkoutId);
$klarna_order->fetch();
if ($klarna_order['status'] != "checkout_complete") {
$this->debugLog($klarna_order, 'plgVmOnPaymentNotification Klarna_Checkout_Order', 'error');
return NULL;
}
// At this point make sure the order is created in your system and send a
// confirmation email to the customer
$vmOrderNumber = $this->createVmOrder($klarna_order, $cartDataFromTable, (int)$cartId);
// update Order status
$update['status'] = 'created';
$update['merchant_reference'] = array(
'orderid1' => $vmOrderNumber
);
$klarna_order->update($update);
if (!class_exists('VirtueMartModelOrders')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
}
$values['virtuemart_order_id'] = VirtueMartModelOrders::getOrderIdByOrderNumber($vmOrderNumber);
$dbValues = array(
'virtuemart_order_id' => VirtueMartModelOrders::getOrderIdByOrderNumber($vmOrderNumber),
'order_number' => $vmOrderNumber,
'virtuemart_paymentmethod_id' => $this->method->virtuemart_paymentmethod_id,
'payment_name' => $this->renderPluginName($this->method, 'create_order'),
'action' => 'update',
'klarna_status' => $update['status'],
'data' => serialize($update)
);
$this->debugLog($dbValues, 'plgVmOnPaymentNotification update', 'debug');
//$this->storePSPluginInternalData($dbValues );
$return = $this->storePluginInternalData($dbValues, 0, 0, false);
$this->debugLog($return, 'plgVmOnPaymentNotification RETURN', 'debug');
}
/**
* Create the VM order with the saved cart, and the users infos from klarna
* @param $klarna_order return data from Klarna
* @param $cartData cart unserialized saved in the table
* @param $method
* @param $cartId pkey of the cart saved in the table
*
*/
function createVmOrder ($klarna_order, $cartData, $cartId) {
if (!class_exists('VirtueMartCart')) {
require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
}
if (!class_exists('shopFunctionsF')) {
require(JPATH_VM_SITE . DS . 'helpers' . DS . 'shopfunctionsf.php');
}
if (!class_exists('VirtueMartModelOrders')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
}
$cartData->_confirmDone = true;
$cartData->_dataValidated = true;
if (!class_exists('VirtueMartCart')) {
require(JPATH_VM_SITE . DS . 'helpers' . DS . 'cart.php');
}
$cart = VirtueMartCart::getCart(false, array(), serialize($cartData));
$this->updateBTSTAddressInCart($cart, $klarna_order);
$orderId = $cart->confirmedOrder();
$this->debugLog($orderId, 'createVmOrder', 'debug');
if (!class_exists('VirtueMartModelOrders')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'orders.php');
}
$modelOrder = VmModel::getModel('orders');
$order_number = VirtueMartModelOrders::getOrderNumber($orderId);
$history = array();
$history['customer_notified'] = 1;
$history['order_status'] = $this->method->status_checkout_complete;
$history['comments'] = JText::sprintf('VMPAYMENT_KLARNACHECKOUT_PAYMENT_STATUS_CHECKOUT_COMPLETE', $order_number);
$modelOrder->updateStatusForOneOrder($orderId, $history, TRUE);
$this->debugLog('', 'AFTER updateStatusForOneOrder', 'debug');
$klarna_data = $this->getKlarnaData($klarna_order);
//$this->debugLog('plgVmOnPaymentNotification KLARNA DATA ' . var_export($klarna_data, true), 'message');
$order_number = VirtueMartModelOrders::getOrderNumber($orderId);
$dbValues = array(
'virtuemart_order_id' => $orderId,
'order_number' => $order_number,
'virtuemart_paymentmethod_id' => $this->method->virtuemart_paymentmethod_id,
'payment_name' => $this->renderPluginName($this->method, 'create_order'),
'action' => 'createOrder',
'klarna_status' => $klarna_order['status'],
//'data' => ($klarna_data),
'data' => serialize($klarna_data),
);
$this->debugLog($dbValues, 'storePSPluginInternalData createVmOrder', 'debug');
$this->storePSPluginInternalData($dbValues);
$dbValues = array(
'virtuemart_order_id' => $orderId,
'order_number' => VirtueMartModelOrders::getOrderNumber($orderId),
'virtuemart_paymentmethod_id' => $this->method->virtuemart_paymentmethod_id,
'payment_name' => 'Klarna Checkout',
'action' => 'storeCart',
'klarna_status' => 'pre-purchase',
);
$this->storeCartInTable($cartData, $cartId, $dbValues);
return $order_number;
}
function updateBTSTAddressInCart ($cart, $klarna_order) {
$result = $this->updateAddressInCart($cart, $klarna_order['billing_address'], 'BT');
$result = $this->updateAddressInCart($cart, $klarna_order['shipping_address'], 'ST');
}
function updateAddressInCart ($cart, $klarna_address, $address_type) {
if ($address_type == 'BT') {
$prefix = '';
$vmAddress = $cart->BT;
} else {
$prefix = 'shipto_';
$vmAddress = $cart->ST;
}
// Update the Shipping Address to what is specified in the register.
$update_data = array(
$prefix . 'address_type_name' => 'klarnacheckout',
$prefix . 'company' => $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 . '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'] = $klarna_address['email'];
}
if (!empty($st)) {
$update_data = array_merge($vmAddress, $update_data);
}
$cart->saveAddressInCart($update_data, $update_data['address_type'], FALSE);
}
function getKlarnaData ($klarna_order) {
$push_params = $this->getKlarnaDisplayParams();
foreach ($push_params as $key => $value) {
$klarna_data[$key] = $klarna_order[$key];
}
return $klarna_data;
}
function getKlarnaDisplayParams () {
return array(
'id' => 'debug',
'purchase_country' => 'display',
'purchase_currency' => 'display',
'locale' => 'debug',
'status' => 'display',
'reference' => 'display',
'reservation' => 'display',
'started_at' => 'debug',
'completed_at' => 'debug',
'last_modified_at' => 'debug',
'expires_at' => 'debug',
'cart' => 'debug',
'customer' => 'debug',
'shipping_address' => 'debug',
'billing_address' => 'debug',
'options' => 'debug',
'merchant' => 'debug',
);
}
/**
* @param $virtuemart_paymentmethod_id
* @param $paymentCurrencyId
* @return bool|null
*/
function plgVmgetEmailCurrency ($virtuemart_paymentmethod_id, $virtuemart_order_id, &$emailCurrencyId) {
if (!($this->method = $this->getVmPluginMethod($virtuemart_paymentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
if (!$this->selectedThisElement($this->method->payment_element)) {
return FALSE;
}
if (!($payments = $this->getDatasByOrderId($virtuemart_order_id))) {
return '';
}
if (empty($payments[0]->email_currency)) {
$vendorId = 1; //VirtueMartModelVendor::getLoggedVendor();
$db = JFactory::getDBO();
$q = 'SELECT `vendor_currency` FROM `#__virtuemart_vendors` WHERE `virtuemart_vendor_id`=' . $vendorId;
$db->setQuery($q);
$emailCurrencyId = $db->loadResult();
} else {
$emailCurrencyId = $payments[0]->email_currency;
}
}
/**
* @param $virtuemart_paymentmethod_id
* @param $paymentCurrencyId
* @return bool|null
*/
function plgVmgetPaymentCurrency ($virtuemart_paymentmethod_id, &$paymentCurrencyId) {
if (!($this->method = $this->getVmPluginMethod($virtuemart_paymentmethod_id))) {
return NULL; // Another method was selected, do nothing
}
if (!$this->selectedThisElement($this->method->payment_element)) {
return FALSE;
}
$this->getPaymentCurrency($this->method);
$paymentCurrencyId = $this->method->payment_currency;
}
/**
* Display stored payment data for an order
* @param int $virtuemart_order_id
* @param int $payment_method_id
* @see components/com_virtuemart/helpers/vmPSPlugin::plgVmOnShowOrderBEPayment()
*/
function plgVmOnShowOrderBEPayment ($virtuemart_order_id, $payment_method_id) {
if (!$this->selectedThisByMethodId($payment_method_id)) {
return NULL; // Another method was selected, do nothing
}
if (!($this->method = $this->getVmPluginMethod($payment_method_id))) {
return NULL; // Another method was selected, do nothing
}
if (!($payments = $this->getDatasByOrderId($virtuemart_order_id))) {
// JError::raiseWarning(500, $db->getErrorMsg());
return '';
}
$html = '
| ' . JText::_('VMPAYMENT_KLARNACHECKOUT_ORDER_BE_WARNING') . ' | |
| ' . JText::_('VMPAYMENT_KLARNACHECKOUT_DATE') . ' | ' . $payment->created_on . ' |
" . var_export($klarna_order[$key][$order_key], true) . "
" . var_export($cart->products, true) . "
';
return $logo ;
}
}
} klarnacheckout/elements/vmcountry.php 0000666 00000003126 15137370070 0014146 0 ustar 00 setQuery($query);
$fields = $db->loadObjectList();
$class = '';
//$class = 'multiple="true" size="10" ';
return JHTML::_('select.genericlist', $fields, $control_name . '[' . $name . ']', $class, 'value', 'text', $value, $control_name . $name);
}
} klarnacheckout/elements/.htaccess 0000666 00000000177 15137370070 0013170 0 ustar 00