/** * {@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()); $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); $request->setOrder($order); }
/** * {@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); }
/** * {@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} */ 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); }
/** * @return \Klarna_Checkout_ConnectorInterface */ protected function getConnector() { if ($this->connector) { return $this->connector; } \Klarna_Checkout_Order::$contentType = $this->config->contentType; \Klarna_Checkout_Order::$baseUri = $this->config->baseUri; return \Klarna_Checkout_Connector::create($this->config->secret); }
/** * Test that get and set work * * @return void */ public function testGetValues() { $key = "testKey1"; $value = "testValue1"; $this->order->parse( array( $key => $value ) ); $this->assertEquals($value, $this->order[$key]); }
/** * @return \Klarna_Checkout_ConnectorInterface */ protected function getConnector() { if ($this->connector) { return $this->connector; } \Klarna_Checkout_Order::$contentType = $this->config->contentType; \Klarna_Checkout_Order::$baseUri = $this->config->baseUri; if (property_exists('Klarna_Checkout_Order', 'accept')) { \Klarna_Checkout_Order::$accept = $this->config->acceptHeader; } return \Klarna_Checkout_Connector::create($this->config->secret); }
/** * {@inheritDoc} */ public function apply($method, \Klarna_Checkout_ResourceInterface $resource, array $options = null) { $previousContentType = \Klarna_Checkout_Order::$contentType; $options['url'] = isset($options['url']) ? $options['url'] : $this->baseUri; \Klarna_Checkout_Order::$contentType = $this->contentType; if ($this->baseUri == $options['url'] && false == isset($options['data']['merchant']['id']) && $this->merchantId) { $options['data']['merchant']['id'] = (string) $this->merchantId; } try { $this->internalConnector->apply($method, $resource, $options); \Klarna_Checkout_Order::$contentType = $previousContentType; } catch (\Exception $e) { \Klarna_Checkout_Order::$contentType = $previousContentType; throw $e; } }
/** * @test */ public function shouldSetBaseUrlAndContentTypeBackAfterInternalConnectorThrowsExceptionOnApply() { \Klarna_Checkout_Order::$baseUri = 'theBaseUri'; \Klarna_Checkout_Order::$contentType = 'theContentType'; $internalConnectorMock = $this->createConnectorMock(); $internalConnectorMock->expects($this->once())->method('apply')->will($this->throwException(new \Exception())); $connector = new GlobalStateSafeConnector($internalConnectorMock, 'aMerchantId', 'theOtherBaseUri', 'theOtherContentType'); $order = new \Klarna_Checkout_Order($connector); try { $connector->apply('GET', $order); } catch (\Exception $e) { $this->assertEquals('theBaseUri', \Klarna_Checkout_Order::$baseUri); $this->assertEquals('theContentType', \Klarna_Checkout_Order::$contentType); return; } $this->fail('Expect an exception to be thrown'); }
/** * 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(); }
/** * Create a new cURL handle * * @return Klarna_Checkout_HTTP_CURLHandleStub */ public function handle() { $stub = new Klarna_Checkout_HTTP_CURLHandleStub; $stub->info['http_code'] = 201; return $stub; } } $resource = new Klarna_Checkout_Order( array( '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);
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://developers.klarna.com/ */ require_once 'src/Klarna/Checkout.php'; session_start(); Klarna_Checkout_Order::$contentType = "application/vnd.klarna.checkout.aggregated-order-v2+json"; $sharedSecret = 'sharedSecret'; $checkoutId = $_SESSION['klarna_checkout']; $connector = Klarna_Checkout_Connector::create($sharedSecret); $order = new Klarna_Checkout_Order($connector, $checkoutId); $order->fetch(); if ($order['status'] == 'checkout_incomplete') { echo "Checkout not completed, redirect to checkout.php"; die; } $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>"; unset($_SESSION['klarna_checkout']);
'unit_price' => 12300, 'discount_rate' => 1000, 'tax_rate' => 2500 ), array( 'type' => 'shipping_fee', 'reference' => 'SHIPPING', 'name' => 'Shipping Fee', 'quantity' => 1, 'unit_price' => 4900, 'tax_rate' => 2500 ) ); $connector = Klarna_Checkout_Connector::create($sharedSecret); $order = new Klarna_Checkout_Order($connector); $create['purchase_country'] = 'SE'; $create['purchase_currency'] = 'SEK'; $create['locale'] = 'sv-se'; $create['merchant']['id'] = $eid; $create['merchant']['terms_uri'] = 'http://example.com/terms.php'; $create['merchant']['checkout_uri'] = 'https://example.com/checkout.php'; $create['merchant']['confirmation_uri'] = 'https://example.com/thankyou.php?sid=123&klarna_order={checkout.order.uri}'; $create['merchant']['push_uri'] = 'https://example.com/push.php?sid=123&klarna_order={checkout.order.uri}'; $create['cart'] = array(); foreach ($cart as $item) { $create['cart']['items'][] = $item;
* @author Klarna <*****@*****.**> * @copyright 2012 Klarna AB * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0 * @link http://developers.klarna.com/ */ require_once 'src/Klarna/Checkout.php'; session_start(); Klarna_Checkout_Order::$contentType = "application/vnd.klarna.checkout.aggregated-order-v2+json"; $sharedSecret = 'sharedSecret'; @$checkoutId = $_GET['klarna_order']; $connector = Klarna_Checkout_Connector::create($sharedSecret); $order = new Klarna_Checkout_Order($connector, $checkoutId); $order->fetch(); if ($order['status'] == "checkout_complete") { // At this point make sure the order is created in your system and send a // confirmation email to the customer $update['status'] = 'created'; $update['merchant_reference'] = array( 'orderid1' => uniqid() ); $order->update($update); }
'id' => $method->id, 'name' => $method->label, 'price' => round( ( $method->cost + array_sum( $method->taxes ) ) * 100 ), 'tax_amount' => round( array_sum( $method->taxes ) * 100 ), 'tax_rate' => $tax_rate, 'description' => '', 'preselected' => $preselected ); } } } } $create['shipping_options'] = $shipping_options; */ $klarna_order = new \Klarna\Rest\Checkout\Order($connector); } else { // Klarna_Checkout_Order::$baseUri = $this->klarna_server; // Klarna_Checkout_Order::$contentType = 'application/vnd.klarna.checkout.aggregated-order-v2+json'; $klarna_order = new Klarna_Checkout_Order($connector, $this->klarna_server); } try { $klarna_order->create(apply_filters('kco_create_order', $create)); $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 class="woocommerce-error">'; print_r($e->getMessage()); echo '</div>'; } }
$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>";
$cart = array( array( 'reference' => '123456789', 'name' => 'Klarna t-shirt', 'quantity' => 4, 'unit_price' => 12300, 'discount_rate' => 1000, 'tax_rate' => 2500 ), array( 'type' => 'shipping_fee', 'reference' => 'SHIPPING', 'name' => 'Shipping Fee', 'quantity' => 1, 'unit_price' => 4900, 'tax_rate' => 2500 ) ); $connector = Klarna_Checkout_Connector::create($sharedSecret); $order = new Klarna_Checkout_Order($connector, $orderUri); // Reset cart $update['cart']['items'] = array(); foreach ($cart as $item) { $update['cart']['items'][] = $item; } $order->update($update);
/** * 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)); } } } }
/** * @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'); }
} 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($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; }
/** * Test that entry point (Base URL) can be changed * * @return void */ public function testCreateAlternativeEntryPoint() { $data = array("foo" => "boo"); $baseUri = "https://checkout.klarna.com/beta/checkout/orders"; Klarna_Checkout_Order::$baseUri = $baseUri; $order = new Klarna_Checkout_Order($this->connector); $order->create($data); $this->assertEquals($baseUri, $this->connector->applied["options"]["url"]); }
/** * 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; }