/**
     * @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);
    }
Beispiel #2
0
 /**
  * @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);
 }
 /**
  * @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');
 }
    /**
     * Set up test
     *
     * @return void
     */
    public function setUp()
    {
        global $_SESSION;

        $_SESSION = array();

        $factory = new Klarna_Checkout_CurlFactoryStub();

        Klarna_Checkout_Order::$baseUri = 'test1';

        $factory->addHandle('test1', 201, array('Location: test2'));
        $factory->addHandle('test2', 200, array());

        $this->connector = new Klarna_Checkout_BasicConnector(
            new Klarna_Checkout_HTTP_CURLTransport($factory),
            new Klarna_Checkout_Digest,
            'sharedSecret'
        );
    }
    /**
     * 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"]);
    }
Beispiel #6
0
 *
 * PHP version 5.3.4
 *
 * @category  Payment
 * @package   Klarna_Checkout
 * @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::$baseUri
    = 'https://checkout.testdrive.klarna.com/checkout/orders';
Klarna_Checkout_Order::$contentType
    = "application/vnd.klarna.checkout.aggregated-order-v2+json";

$order = null;
$eid = '0';
$sharedSecret = 'sharedSecret';
$cart = array(
    array(
        'reference' => '123456789',
        'name' => 'Klarna t-shirt',
        'quantity' => 2,
        'unit_price' => 12300,
        'discount_rate' => 1000,
        'tax_rate' => 2500
    ),
 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));
 }