/** * {@inheritDoc} * * @param CreateOrder $request */ public function execute($request) { RequestNotSupportedException::assertSupports($this, $request); $model = ArrayObject::ensureArrayObject($request->getModel()); $order = new \Klarna_Checkout_Order($this->getConnector(), $model['location']); $data = $model->toUnsafeArray(); unset($data['location']); $order->update($data); $request->setOrder($order); }
/** * {@inheritDoc} */ public function execute($request) { /** @var $request CreateOrder */ if (false == $this->supports($request)) { throw RequestNotSupportedException::createActionNotSupported($this, $request); } $model = ArrayObject::ensureArrayObject($request->getModel()); $order = new \Klarna_Checkout_Order($this->api, $model['location']); $data = $model->toUnsafeArray(); unset($data['location']); $order->update($data); $order->fetch(); $request->setOrder($order); }
/** * Test that update works as intended * * @return void */ public function testUpdate() { $this->order->setLocation("http://klarna.com/foo/bar/15"); $uri = $this->order->getLocation(); $this->order->update( array( 'foo' => 'boo' ) ); $this->assertEquals("POST", $this->connector->applied["method"]); $this->assertEquals($this->order, $this->connector->applied["resource"]); $this->assertArrayHasKey("url", $this->connector->applied["options"]); $this->assertEquals($uri, $this->connector->applied["options"]["url"]); }
if (array_key_exists('klarna_checkout', $_SESSION)) { // Resume session $order = new Klarna_Checkout_Order( $connector, $_SESSION['klarna_checkout'] ); try { $order->fetch(); // Reset cart $update['cart']['items'] = array(); foreach ($cart as $item) { $update['cart']['items'][] = $item; } $order->update($update); } catch (Exception $e) { // Reset session $order = null; unset($_SESSION['klarna_checkout']); } } if ($order == null) { // Start new session $create['purchase_country'] = 'SE'; $create['purchase_currency'] = 'SEK'; $create['locale'] = 'sv-se'; $create['merchant']['id'] = $eid; $create['merchant']['terms_uri'] = 'http://example.com/terms.html'; $create['merchant']['checkout_uri'] = 'http://example.com/checkout.php';
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 = vmText::sprintf('VMPAYMENT_KLARNACHECKOUT_SELECT_SHIPMENT_FIRST', $this->method->payment_name); } else { $session = JFactory::getSession(); require_once 'klarnacheckout/library/Checkout.php'; $cartIdInTable = $this->storeCartInTable($cart); if ($this->method->server == 'beta') { Klarna_Checkout_Order::$baseUri = 'https://checkout.testdrive.klarna.com/checkout/orders'; } else { Klarna_Checkout_Order::$baseUri = 'https://checkout.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, vmText::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)); }
/** * Pushes Klarna order update in AJAX calls. * * Used to capture customer address, recalculate tax and shipping for order and user session * * @since 2.0 **/ function kco_iframe_change_cb() { if (!wp_verify_nonce($_REQUEST['nonce'], 'klarna_checkout_nonce')) { exit('Nonce can not be verified.'); } global $woocommerce; $data = array(); // Check stock if (is_wp_error($woocommerce->cart->check_cart_item_stock())) { wp_send_json_error(); wp_die(); } // Capture email if (isset($_REQUEST['email']) && is_string($_REQUEST['email']) && !is_user_logged_in()) { $this->update_or_create_local_order($_REQUEST['email']); $orderid = $woocommerce->session->get('ongoing_klarna_order'); $data['orderid'] = $orderid; $connector = Klarna_Checkout_Connector::create($this->klarna_secret, $this->klarna_server); $klarna_order = new Klarna_Checkout_Order($connector, WC()->session->get('klarna_checkout')); $klarna_order->fetch(); $update['merchant']['push_uri'] = add_query_arg(array('sid' => $orderid), $klarna_order['merchant']['push_uri']); $update['merchant']['confirmation_uri'] = add_query_arg(array('sid' => $orderid, 'order-received' => $orderid), $klarna_order['merchant']['confirmation_uri']); $klarna_order->update($update); } // Capture postal code if (isset($_REQUEST['postal_code']) && is_string($_REQUEST['postal_code']) && WC_Validation::is_postcode($_REQUEST['postal_code'], $this->klarna_country)) { $woocommerce->customer->set_shipping_postcode($_REQUEST['postal_code']); if (!defined('WOOCOMMERCE_CART')) { define('WOOCOMMERCE_CART', true); } // Update user session $woocommerce->cart->calculate_shipping(); $woocommerce->cart->calculate_fees(); $woocommerce->cart->calculate_totals(); // Update ongoing WooCommerce order $this->update_or_create_local_order(); $data['widget_html'] = $this->klarna_checkout_get_kco_widget_html(); if (WC()->session->get('klarna_checkout')) { $this->ajax_update_klarna_order(); } } wp_send_json_success($data); wp_die(); }
'foo' => 'Foo', 'bar' => 'bar' ) ); $connector = new Klarna_Checkout_Connector( new Klarna_Checkout_HTTP_CURLTransport( new CURLStubFactory ), new Klarna_Checkout_Digest, 'sharedSecret' ); $count = 20000; $start = microtime(true); for ($i = 0; $i < $count; $i++) { $resource->create($connector); $resource->fetch($connector); $resource->update($connector); } $end = microtime(true); echo json_encode( array( 'start' => $start, 'end' => $end, 'duration' => $end - $start, 'count' => $count ) );