/** * {@inheritDoc} * * @param FetchOrder $request */ public function execute($request) { RequestNotSupportedException::assertSupports($this, $request); $model = ArrayObject::ensureArrayObject($request->getModel()); if (false == $model['location']) { throw new LogicException('Location has to be provided to fetch an order'); } $order = new \Klarna_Checkout_Order($this->getConnector(), $model['location']); $order->fetch(); $request->setOrder($order); }
/** * Test that fetch works as intended * * @return void */ public function testFetch() { $this->order->setLocation("http://klarna.com/foo/bar/15"); $url = $this->order->getLocation(); $this->order->fetch(); $this->assertEquals("GET", $this->connector->applied["method"]); $this->assertEquals($this->order, $this->connector->applied["resource"]); $this->assertArrayHasKey("url", $this->connector->applied["options"]); $this->assertEquals($url, $this->connector->applied["options"]["url"]); }
/** * {@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); $order->create($model->toUnsafeArray()); $order->fetch(); $request->setOrder($order); }
/** * {@inheritDoc} * * @param CreateOrder $request */ public function execute($request) { RequestNotSupportedException::assertSupports($this, $request); $model = ArrayObject::ensureArrayObject($request->getModel()); $this->addMerchantId($model); $order = new \Klarna_Checkout_Order($this->getConnector()); $order->create($model->toUnsafeArray()); $order->fetch(); $request->setOrder($order); }
/** * {@inheritDoc} */ public function execute($request) { /** @var $request FetchOrder */ if (false == $this->supports($request)) { throw RequestNotSupportedException::createActionNotSupported($this, $request); } $model = ArrayObject::ensureArrayObject($request->getModel()); if (false == $model['location']) { throw new LogicException('Location has to be provided to fetch an order'); } $order = new \Klarna_Checkout_Order($this->api, $model['location']); $order->fetch(); $request->setOrder($order); }
/** * {@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); $order->fetch(); $request->setOrder($order); }
$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'; $create['merchant']['confirmation_uri'] = 'http://example.com/confirmation.php' . '?sid=123&klarna_order={checkout.order.uri}'; // You can not receive push notification on non publicly available uri $create['merchant']['push_uri'] = 'http://example.com/push.php' . '?sid=123&klarna_order={checkout.order.uri}'; $create['cart'] = array(); foreach ($cart as $item) { $create['cart']['items'][] = $item; } $order = new Klarna_Checkout_Order($connector); $order->create($create); $order->fetch(); } // Store location of checkout session $_SESSION['klarna_checkout'] = $sessionId = $order->getLocation(); // Display checkout $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) echo "<div>{$snippet}</div>";
/** * @return bool|null */ function plgVmOnPaymentNotification() { $virtuemart_paymentmethod_id = vRequest::getInt('pm', ''); $checkoutId = vRequest::getString('klarna_order', ''); $cartId = vRequest::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($cartDataFromTable, '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'], 'format' => 'json', 'data' => json_encode($update)); $this->debugLog($dbValues, 'plgVmOnPaymentNotification update', 'debug'); //$this->storePSPluginInternalData($dbValues ); $return = $this->storePluginInternalData($dbValues, 0, 0, false); $this->debugLog($return, 'plgVmOnPaymentNotification RETURN', 'debug'); }
/** * Fetch KCO order. * * @since 2.0.0 * @access public */ public function retrieve_klarna_order() { if ($this->klarna_debug == 'yes') { $this->klarna_log->add('klarna', 'Klarna order - ' . $this->klarna_order_uri); } if (sanitize_key($_GET['klarna-api']) && 'rest' == sanitize_key($_GET['klarna-api'])) { $klarna_country = sanitize_key($_GET['scountry']); if ($this->klarna_test_mode == 'yes') { if ('gb' == $klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL; } elseif ('us' == $klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL; } } else { if ('gb' == $klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL; } elseif ('us' == $klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL; } } $connector = \Klarna\Rest\Transport\Connector::create($this->eid, $this->secret, $klarna_server_url); $klarna_order = new \Klarna\Rest\OrderManagement\Order($connector, $this->klarna_order_uri); } else { $connector = Klarna_Checkout_Connector::create($this->secret, $this->klarna_server); $checkoutId = $this->klarna_order_uri; $klarna_order = new Klarna_Checkout_Order($connector, $checkoutId); } $klarna_order->fetch(); return $klarna_order; }
} else { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL; } } $connector = \Klarna\Rest\Transport\Connector::create($merchantId, $sharedSecret, $klarna_server_url); $klarna_order = new Klarna\Rest\Checkout\Order($connector, $orderUri); } else { // Klarna_Checkout_Order::$contentType = 'application/vnd.klarna.checkout.aggregated-order-v2+json'; $connector = Klarna_Checkout_Connector::create($sharedSecret, $this->klarna_server); $klarna_order = new Klarna_Checkout_Order($connector, $orderUri); } try { $klarna_order->fetch(); } catch (Exception $e) { if (is_user_logged_in() && $this->debug) { // The purchase was denied or something went wrong, print the message: echo '<div>'; print_r($e->getMessage()); echo '</div>'; } } if ($klarna_order['status'] == 'checkout_incomplete') { wp_redirect($this->klarna_checkout_url); exit; } // Display Klarna iframe if ($this->is_rest()) { $snippet = '<div>' . $klarna_order['html_snippet'] . '</div>';
/** * Sample component test * * @group component * * @return void */ public function testShow() { // Start new session $banana = array( 'type' => 'physical', 'reference' => 'BANAN01', 'name' => 'Bananana', 'unit_price' => 450, 'discount_rate' => 0, 'tax_rate' => 2500 ); $shipping = array( 'type' => 'shipping_fee', 'reference' => 'SHIPPING', 'name' => 'Shipping Fee', 'unit_price' => 450, 'discount_rate' => 0, 'tax_rate' => 2500 ); $order = new Klarna_Checkout_Order($this->connector); $order->create( array( 'purchase_country' => 'SE', 'purchase_currency' => 'SEK', 'locale' => 'sv-se', 'merchant' => array( 'id' => 2, 'terms_uri' => 'http://localhost/terms.html', 'checkout_uri' => 'http://localhost/checkout.php', 'confirmation_uri' =>'http://localhost/thank-you.php', 'push_uri' => 'http://localhost/push.php' ), 'cart' => array( 'total_price_including_tax' => 9000, 'items' => array( $banana, $shipping ) ) ) ); $this->assertEquals($order->getLocation(), 'test2'); $order->fetch(); }
/** * Pushes Klarna order update in AJAX calls. * * @since 2.0 **/ function ajax_update_klarna_order() { global $woocommerce; // Check if Euro is selected, get correct country if ('EUR' == get_woocommerce_currency() && WC()->session->get('klarna_euro_country')) { $klarna_c = strtolower(WC()->session->get('klarna_euro_country')); $eid = $this->settings["eid_{$klarna_c}"]; $sharedSecret = $this->settings["secret_{$klarna_c}"]; } else { $eid = $this->klarna_eid; $sharedSecret = $this->klarna_secret; } if ($this->is_rest()) { if ($this->testmode == 'yes') { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL; } } else { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL; } } $connector = Klarna\Rest\Transport\Connector::create($eid, $sharedSecret, $klarna_server_url); $klarna_order = new \Klarna\Rest\Checkout\Order($connector, WC()->session->get('klarna_checkout')); } else { $connector = Klarna_Checkout_Connector::create($sharedSecret, $this->klarna_server); $klarna_order = new Klarna_Checkout_Order($connector, WC()->session->get('klarna_checkout')); $klarna_order->fetch(); } // Process cart contents and prepare them for Klarna include_once KLARNA_DIR . 'classes/class-wc-to-klarna.php'; $wc_to_klarna = new WC_Gateway_Klarna_WC2K($this->is_rest(), $this->klarna_country); $cart = $wc_to_klarna->process_cart_contents(); if (0 == count($cart)) { $klarna_order = null; } else { // Reset cart if ($this->is_rest()) { $update['order_lines'] = array(); $klarna_order_total = 0; $klarna_tax_total = 0; foreach ($cart as $item) { $update['order_lines'][] = $item; $klarna_order_total += $item['total_amount']; // Process sales_tax item differently if (array_key_exists('type', $item) && 'sales_tax' == $item['type']) { $klarna_tax_total += $item['total_amount']; } else { $klarna_tax_total += $item['total_tax_amount']; } } $update['order_amount'] = $klarna_order_total; $update['order_tax_amount'] = $klarna_tax_total; } else { $update['cart']['items'] = array(); foreach ($cart as $item) { $update['cart']['items'][] = $item; } } try { $klarna_order->update(apply_filters('kco_update_order', $update)); } catch (Exception $e) { if ($this->debug == 'yes') { $this->log->add('klarna', 'KCO ERROR: ' . var_export($e, true)); error_log('ERROR: ' . var_export($e, true)); } } } }
'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 ) );