/** * Get redirect URL or inline iFrame code * * @param bool $inline * @return bool * @throws Exception|Mage_Core_Exception */ public function getRedirectUrl($inline = false) { // Is current currency supported? if (!$this->canUseForCurrency($this->_config->getCurrentCurrency())) { return false; } // Create Rakuten Checkout Insert Cart XML request $xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?><tradoria_insert_cart />"); $merchantAuth = $xml->addChild('merchant_authentication'); $merchantAuth->addChild('project_id', $this->_projectId); $merchantAuth->addChild('api_key', $this->_apiKey); $xml->addChild('language', $this->_language); $xml->addChild('currency', $this->_currency); $merchantCart = $xml->addChild('merchant_carts')->addChild('merchant_cart'); // $merchantCart->addAttribute('merchant_id', $this->_merchantId); $quoteId = $this->getQuoteId(); $storeId = $this->getStoreId(); $merchantCart->addChild('custom_1', $quoteId); $merchantCart->addChild('custom_2', $storeId); $merchantCart->addChild('custom_3'); $merchantCart->addChild('custom_4'); $merchantCartItems = $merchantCart->addChild('items'); // $items = $this->getQuote()->getAllItems(); $items = $this->getQuote()->getAllVisibleItems(); /** @var $item Mage_Sales_Model_Quote_Item */ foreach ($items as $item) { $merchantCartItemsItem = $merchantCartItems->addChild('item'); $merchantCartItemsItemName = $merchantCartItemsItem->addChild('name'); $this->_addCDATA($merchantCartItemsItemName, $item->getName()); $merchantCartItemsItem->addChild('sku', $this->_escapeStr($item->getSku())); // THIS ONE IS SHOWN $merchantCartItemsItem->addChild('external_product_id'); // this one is not shown (optional) $merchantCartItemsItem->addChild('qty', $item->getQty()); // positive integers /** @var $checkoutHelper Mage_Checkout_Helper_Data */ $checkoutHelper = Mage::helper('checkout'); $merchantCartItemsItem->addChild('unit_price', $checkoutHelper->getPriceInclTax($item)); $merchantCartItemsItem->addChild('tax_class', $this->getRakutenTaxClass($item->getTaxPercent())); $merchantCartItemsItem->addChild('image_url', $this->_escapeStr($item->getProduct()->getThumbnailUrl())); // depracated method used for better backwards compatibility $merchantCartItemsItem->addChild('product_url', $this->_escapeStr($item->getProduct()->getProductUrl())); /** @var $helper Mage_Catalog_Helper_Product_Configuration */ // It'll work starting Magento CE 1.5.x only //$helper = Mage::helper('catalog/product_configuration'); //$options = $helper->getOptions($item); // So for backwards compatibility duplicating methods from the helper above $options = $this->_getOptions($item); if (!empty($options)) { $custom = serialize($options); $comment = array(); foreach ($options as $option) { $comment[] = "{$option['label']}: {$option['value']}"; } $comment = implode('; ', $comment); } else { $custom = ''; $comment = ''; } $merchantCartItemsItemComment = $merchantCartItemsItem->addChild('comment'); $this->_addCDATA($merchantCartItemsItemComment, $comment); $merchantCartItemsItemCustom = $merchantCartItemsItem->addChild('custom'); $this->_addCDATA($merchantCartItemsItemCustom, $custom); } $merchantCartShippingRates = $merchantCart->addChild('shipping_rates'); $shippingRates = $this->_config->getShippingRates(); foreach ($shippingRates as $shippingRate) { $merchantCartShippingRate = $merchantCartShippingRates->addChild('shipping_rate'); $merchantCartShippingRate->addChild('country', $shippingRate['country']); $merchantCartShippingRate->addChild('price', $shippingRate['price']); } $billingAddressRestrictions = $xml->addChild('billing_address_restrictions'); // restrict invoice address to require private / commercial and by country $billingAddressRestrictions->addChild('customer_type')->addAttribute('allow', $this->_customerType); // 1=all 2=business 3=private if ($this->_countries) { $billingAddressRestrictions->addChild('countries')->addAttribute('allow', $this->_countries); } $callbackUrl = $this->_config->getCallbackUrl(); $pipeUrl = $this->_config->getPipeUrl(); $xml->addChild('callback_url', $this->_escapeStr($callbackUrl)); $xml->addChild('pipe_url', $this->_escapeStr($pipeUrl)); $request = $xml->asXML(); $response = $this->sendRequest($request); $redirectUrl = false; $inlineUrl = false; $inlineCode = false; try { $response = new SimpleXMLElement($response); if ($response->success != 'true') { throw new Mage_Core_Exception(Mage::helper('rakuten')->__('Error #%s: %s', $response->code, $response->message)); } else { $redirectUrl = $response->redirect_url; $inlineUrl = $response->inline_url; $inlineCode = $response->inline_code; } } catch (Mage_Core_Exception $e) { $this->getCheckout()->addError($e->getMessage()); } catch (Exception $e) { $this->getCheckout()->addError(Mage::helper('rakuten')->__('Unable to redirect to Rakuten Checkout.')); Mage::logException($e); } if ($inline) { return $inlineCode; } else { return $redirectUrl; } }