/** * @return array */ protected function _prepareOrderData() { // magento 1.5 compat $shipping_method_c = $this->_order->getShippingMethod(true); $shipping_method = $this->_order->getData('shipping_method'); $shipping_method_code = $shipping_method_c ? $shipping_method_c->getData('carrier_code') : $shipping_method; $data = array_filter(array('currency_code' => $this->_order->getOrderCurrencyCode(), 'shipping_method_code' => $shipping_method_code, 'shipping_method_title' => $this->_order->getShippingDescription(), 'created_on' => $this->_order->getCreatedAt(), 'updated_on' => $this->_order->getUpdatedAt(), 'state' => $this->_order->getState(), 'status' => $this->_order->getStatus(), 'is_gift' => $this->_order->getGiftMessageId() != null, 'ref_quote_id' => $this->_order->getQuoteId(), 'order_subtotal_with_tax' => $this->_order->getSubtotalInclTax(), 'order_subtotal' => $this->_order->getSubtotal(), 'order_tax' => $this->_order->getTaxAmount(), 'order_hidden_tax' => $this->_order->getHiddenTaxAmount(), 'order_shipping_with_tax' => $this->_order->getShippingInclTax(), 'order_shipping' => $this->_order->getShippingAmount(), 'order_discount' => $this->_order->getDiscountAmount(), 'order_shipping_discount' => $this->_order->getShippingDiscountAmount(), 'order_total' => $this->_order->getGrandTotal(), 'order_total_items' => $this->_order->getTotalItemCount())); return $data; }
/** * Add PayEx Single Order Line * @param string $orderRef * @param Mage_Sales_Model_Order $order * @return bool */ public function addOrderLine($orderRef, $order) { // add Order Items $items = $order->getAllVisibleItems(); $i = 1; /** @var $item Mage_Sales_Model_Order_Item */ foreach ($items as $item) { // @todo Calculate prices using Discount Rules // @todo Get children products from bundle //if (!$item->getNoDiscount()) { // Mage::helper('payexautopay/tools')->addToDebug('Warning: The product has a discount. There might be problems.', $order->getIncrementId()); //} $itemQty = (int) $item->getQtyOrdered(); //$taxPrice = $item->getTaxAmount(); $taxPrice = $itemQty * $item->getPriceInclTax() - $itemQty * $item->getPrice(); $taxPercent = $item->getTaxPercent(); $priceWithTax = $itemQty * $item->getPriceInclTax(); // Calculate tax percent for Bundle products if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $taxPercent = $taxPrice > 0 ? round(100 / (($priceWithTax - $taxPrice) / $taxPrice)) : 0; } $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $item->getName(), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => $itemQty, 'amount' => (int) (100 * $priceWithTax), 'vatPrice' => (int) (100 * $taxPrice), 'vatPercent' => (int) (100 * $taxPercent)); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // add Shipping if (!$order->getIsVirtual()) { $shipping = $order->getShippingAmount(); $shippingIncTax = $order->getShippingInclTax(); $shippingTax = $order->getShippingTaxAmount(); $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $order->getShippingDescription(), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => (int) (100 * $shippingIncTax), 'vatPrice' => (int) (100 * $shippingTax), 'vatPercent' => $shipping != 0 ? round(100 * 100 * $shippingTax / $shipping) : 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // add Discount $discount = $order->getDiscountAmount() + $order->getShippingDiscountAmount(); if (abs($discount) > 0) { $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $order->getDiscountDescription() !== null ? Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription()) : Mage::helper('sales')->__('Discount'), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => (int) (100 * $discount), 'vatPrice' => 0, 'vatPercent' => 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // Add reward points if ((double) $order->getBaseRewardCurrencyAmount() > 0) { $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => Mage::helper('payexautopay')->__('Reward points'), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => -1 * (int) (100 * $order->getBaseRewardCurrencyAmount()), 'vatPrice' => 0, 'vatPercent' => 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } return true; }
/** * Initializes the payment. * @param Mage_Sales_Model_Order * @param Mage_Shipping_Model_Shipping * @return array */ public function orderCreateRequest(Mage_Sales_Model_Order $order, $allShippingRates) { $this->_order = $order; $orderCurrencyCode = $this->_order->getOrderCurrencyCode(); $orderCountryCode = $this->_order->getBillingAddress()->getCountry(); $shippingCostList = array(); if (empty($allShippingRates) || Mage::getSingleton('customer/session')->isLoggedIn()) { if ($order->getShippingInclTax() > 0) { $shippingCostList['shippingMethods'][] = array('name' => $order->getShippingDescription(), 'country' => $orderCountryCode, 'price' => $this->toAmount($order->getShippingInclTax())); } $grandTotal = $this->_order->getGrandTotal() - $order->getShippingInclTax(); } else { $firstPrice = 0; foreach ($allShippingRates as $key => $rate) { $gross = $this->toAmount($rate->getPrice()); if ($key == 0) { $firstPrice = $rate->getPrice(); } $shippingCostList['shippingMethods'][] = array('name' => $rate->getMethodTitle(), 'country' => $orderCountryCode, 'price' => $gross); } $grandTotal = $this->_order->getGrandTotal() - $firstPrice; } $shippingCost = array('countryCode' => $orderCountryCode, 'shipToOtherCountry' => 'true', 'shippingCostList' => $shippingCostList); $orderItems = $this->_order->getAllVisibleItems(); $items = array(); $productsTotal = 0; $is_discount = false; foreach ($orderItems as $key => $item) { $itemInfo = $item->getData(); if ($itemInfo['discount_amount'] > 0) { $itemInfo['price_incl_tax'] = $itemInfo['price_incl_tax'] - $itemInfo['discount_amount']; $is_discount = true; } else { if ($itemInfo['discount_percent'] > 0) { $itemInfo['price_incl_tax'] = $itemInfo['price_incl_tax'] * (100 - $itemInfo['discount_percent']) / 100; } } // Check if the item is countable one if ($this->toAmount($itemInfo['price_incl_tax']) > 0) { $items['products'][] = array('quantity' => (int) $itemInfo['qty_ordered'], 'name' => $itemInfo['name'], 'unitPrice' => $this->toAmount($itemInfo['price_incl_tax'])); $productsTotal += $itemInfo['price_incl_tax'] * $itemInfo['qty_ordered']; } } //if($this->_order->getShippingAmount () > 0 && !empty ( $shippingCostList['shippingMethods'][0] ) ){ // $items ['products'] ['products'] [] = array ( // 'quantity' => 1 ,'name' => Mage::helper ( 'payu_account' )->__('Shipping costs') . " - " . $shippingCostList['shippingMethods'][0]['name'] ,'unitPrice' => $this->toAmount ( $this->_order->getShippingAmount () )); //} // assigning the shopping cart $shoppingCart = array('grandTotal' => $this->toAmount($grandTotal), 'CurrencyCode' => $orderCurrencyCode, 'ShoppingCartItems' => $items); $orderInfo = array('merchantPosId' => OpenPayU_Configuration::getMerchantPosId(), 'orderUrl' => Mage::getBaseUrl() . 'sales/order/view/order_id/' . $this->_order->getId() . '/', 'description' => 'Order no ' . $this->_order->getRealOrderId(), 'validityTime' => $this->_config->getOrderValidityTime()); if ($is_discount) { $items['products'] = array(); $items['products'][] = array('quantity' => 1, 'name' => Mage::helper('payu_account')->__('Order # ') . $this->_order->getId(), 'unitPrice' => $this->toAmount($grandTotal)); } $OCReq = $orderInfo; $OCReq['products'] = $items['products']; $OCReq['customerIp'] = Mage::app()->getFrontController()->getRequest()->getClientIp(); $OCReq['notifyUrl'] = $this->_myUrl . 'orderNotifyRequest'; $OCReq['cancelUrl'] = $this->_myUrl . 'cancelPayment'; $OCReq['continueUrl'] = $this->_myUrl . 'continuePayment'; $OCReq['currencyCode'] = $orderCurrencyCode; $OCReq['totalAmount'] = $shoppingCart['grandTotal']; $OCReq['extOrderId'] = $this->_order->getId() . '-' . microtime(); if (!empty($shippingCostList)) { $OCReq['shippingMethods'] = $shippingCostList['shippingMethods']; } unset($OCReq['shoppingCart']); $customer_sheet = array(); $billingAddressId = $this->_order->getBillingAddressId(); if (!empty($billingAddressId)) { $billingAddress = $this->_order->getBillingAddress(); $customer_mail = $billingAddress->getEmail(); if (!empty($customer_mail)) { $customer_sheet = array('email' => $billingAddress->getEmail(), 'phone' => $billingAddress->getTelephone(), 'firstName' => $billingAddress->getFirstname(), 'lastName' => $billingAddress->getLastname()); $shippingAddressId = $this->_order->getShippingAddressId(); if (!empty($shippingAddressId)) { $shippingAddress = $this->_order->getShippingAddress(); } if (!$this->_order->getIsVirtual()) { $customer_sheet['delivery'] = array('street' => trim(implode(' ', $shippingAddress->getStreet())), 'postalCode' => $shippingAddress->getPostcode(), 'city' => $shippingAddress->getCity(), 'countryCode' => $shippingAddress->getCountry(), 'recipientName' => trim($shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname()), 'recipientPhone' => $shippingAddress->getTelephone(), 'recipientEmail' => $shippingAddress->getEmail()); } $OCReq['buyer'] = $customer_sheet; } } $result = OpenPayU_Order::create($OCReq); if ($result->getStatus() == 'SUCCESS') { // store session identifier in session info Mage::getSingleton('core/session')->setPayUSessionId($result->getResponse()->orderId); // assign current transaction id $this->_transactionId = $result->getResponse()->orderId; $order->getPayment()->setLastTransId($this->_transactionId); $locale = Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId()); $lang_code = explode('_', $locale, 2); $ret = array('redirectUri' => $result->getResponse()->redirectUri, 'url' => OpenPayu_Configuration::getSummaryUrl(), 'sessionId' => $result->getResponse()->orderId, 'lang' => strtolower($lang_code[1])); $customer = Mage::getModel('customer/customer'); if ($this->_order->getCustomerIsGuest()) { $email = $billingAddress->getEmail(); $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); $customer->loadByEmail($email); } else { $customer->load($this->_order->getCustomerId()); } if (!$customer->getId()) { $this->_order->setCustomerEmail($email); } $this->_order->sendNewOrderEmail(); $this->_order->save(); } else { Mage::throwException(Mage::helper('payu_account')->__('There was a problem with initializing the payment, please contact the store administrator. ' . $result->getError())); } return $ret; }
public function init(Mage_Sales_Model_Order $order) { $this->setLiczOd("BRT"); $this->setDataWystawienia(date('Y-m-d')); $this->setDataSprzedazy(date('Y-m-d')); $this->setFormatDatySprzedazy("DZN"); $this->setRodzajPodpisuOdbiorcy("BPO"); $this->setWidocznyNumerGios(true); $this->setZaplacono('0'); $this->setTerminPlatnosci(''); $items = $order->getAllVisibleItems(); $positions = array(); foreach ($items as $item) { /* @var $item Mage_Sales_Model_Order_Item */ $invoice_position = new PowerMedia_Ifirma_Model_InvoicePosition(); $tax = sprintf('%.2f', $item->getTaxPercent() / 100); $invoice_position->setStawkaVat((string) $tax); $invoice_position->setIlosc($item->getQtyOrdered()); $invoice_position->setNazwaPelna($item->getName()); $invoice_position->setJednostka("sztuk"); $invoice_position->setTypStawkiVat("PRC"); $invoice_position->setCenaJednostkowa($item->getPriceInclTax()); $invoice_position->setPKWiU(""); $positions[] = $invoice_position->getProperties(); } $shipping = $order->getShippingInclTax(); if ($shipping >= 0) { /* @var $item Mage_Sales_Model_Order_Item */ $invoice_position = new PowerMedia_Ifirma_Model_InvoicePosition(); $tax = sprintf('%.2f', $item->getTaxPercent() / 100); $invoice_position->setStawkaVat((string) $tax); $invoice_position->setIlosc(1); $invoice_position->setNazwaPelna('Koszty dostawy'); $invoice_position->setJednostka("sztuk"); $invoice_position->setTypStawkiVat("PRC"); $invoice_position->setCenaJednostkowa($shipping); $invoice_position->setPKWiU(""); $positions[] = $invoice_position->getProperties(); } $this->setPozycje($positions); $invoice_contractor = new PowerMedia_Ifirma_Model_InvoiceContractor(); $invoice_contractor->setNazwa($order->getBillingAddress()->getName()); $invoice_contractor->setUlica($order->getBillingAddress()->getStreetFull()); $invoice_contractor->setKodPocztowy($order->getBillingAddress()->getPostcode()); $invoice_contractor->setMiejscowosc($order->getBillingAddress()->getCity()); $invoice_contractor->setKraj($order->getBillingAddress()->getCountry()); $this->setKontrahent($invoice_contractor->getProperties()); }
/** * Gets the total discount from $order * inkl. and excl. tax * Data is returned as a Varien_Object with these data-keys set: * - discount_incl_tax * - discount_excl_tax * * @param Mage_Sales_Model_Order $order * * @return Varien_Object */ public function getOrderDiscountData(Mage_Sales_Model_Order $order) { // if catalog-prices includes tax $CatPriceIncl = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $order->getStore()); $discountIncl = 0; $discountExcl = 0; // find discount on the items foreach ($order->getItemsCollection() as $item) { /** @var Mage_Sales_Model_Quote_Item $item */ if (!$CatPriceIncl) { $discountExcl += $item->getDiscountAmount(); $discountIncl += $item->getDiscountAmount() * ($item->getTaxPercent() / 100 + 1); } else { $discountExcl += $item->getDiscountAmount() / ($item->getTaxPercent() / 100 + 1); $discountIncl += $item->getDiscountAmount(); } } // find out tax-rate for the shipping if ((double) $order->getShippingInclTax() && (double) $order->getShippingAmount()) { $shippingTaxRate = $order->getShippingInclTax() / $order->getShippingAmount(); } else { $shippingTaxRate = 1; } // get discount amount for shipping $shippingDiscount = (double) $order->getShippingDiscountAmount(); // apply/remove tax to shipping-discount if (!$CatPriceIncl) { $discountIncl += $shippingDiscount * $shippingTaxRate; $discountExcl += $shippingDiscount; } else { $discountIncl += $shippingDiscount; $discountExcl += $shippingDiscount / $shippingTaxRate; } $return = new Varien_Object(); return $return->setDiscountInclTax($discountIncl)->setDiscountExclTax($discountExcl); }
/** * Generate Invoice Print XML * (only used for Factoring & PartPayment) * @param Mage_Sales_Model_Order $order * @return mixed */ public function getInvoiceExtraPrintBlocksXML($order) { $dom = new DOMDocument('1.0', 'utf-8'); $OnlineInvoice = $dom->createElement('OnlineInvoice'); $dom->appendChild($OnlineInvoice); $OnlineInvoice->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $OnlineInvoice->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsd', 'http://www.w3.org/2001/XMLSchema'); $OrderLines = $dom->createElement('OrderLines'); $OnlineInvoice->appendChild($OrderLines); // Add Order Lines $items = $order->getAllVisibleItems(); /** @var $item Mage_Sales_Model_Order_Item */ foreach ($items as $item) { $itemQty = (int) $item->getQtyOrdered(); $priceWithTax = $item->getRowTotalInclTax(); $priceWithoutTax = $item->getRowTotal(); $taxPercent = ($priceWithTax / $priceWithoutTax - 1) * 100; // works for all types $taxPrice = $priceWithTax - $priceWithoutTax; mb_regex_encoding("utf-8"); $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', trim(mb_ereg_replace('[^a-zA-Z0-9_:!#=?\\[\\]@{}´ %-\\/À-ÖØ-öø-ú]', "-", $item->getName())))); $OrderLine->appendChild($dom->createElement('Qty', $itemQty)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $priceWithoutTax / $itemQty))); $OrderLine->appendChild($dom->createElement('VatRate', sprintf("%.2f", $taxPercent))); $OrderLine->appendChild($dom->createElement('VatAmount', sprintf("%.2f", $taxPrice))); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $priceWithTax))); $OrderLines->appendChild($OrderLine); } // Add Shipping Line if (!$order->getIsVirtual()) { $shippingExclTax = $order->getShippingAmount(); $shippingIncTax = $order->getShippingInclTax(); $shippingTax = $shippingIncTax - $shippingExclTax; // find out tax-rate for the shipping if ((double) $shippingIncTax && (double) $shippingExclTax) { $shippingTaxRate = ($shippingIncTax / $shippingExclTax - 1) * 100; } else { $shippingTaxRate = 0; } $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $order->getShippingDescription())); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $shippingExclTax))); $OrderLine->appendChild($dom->createElement('VatRate', sprintf("%.2f", $shippingTaxRate))); $OrderLine->appendChild($dom->createElement('VatAmount', sprintf("%.2f", $shippingTax))); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $shippingIncTax))); $OrderLines->appendChild($OrderLine); } // add Payment Fee $fee = $order->getPartpaymentPaymentFee(); if ($fee > 0) { $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $this->getHelper()->__('Payment fee'))); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $fee))); $OrderLine->appendChild($dom->createElement('VatRate', 0)); $OrderLine->appendChild($dom->createElement('VatAmount', 0)); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $fee))); $OrderLines->appendChild($OrderLine); } // add Discount /** @var AAIT_Shared_Helper_Discount $discountHelper */ $discountHelper = Mage::helper("payexshared/discount"); $discountData = $discountHelper->getOrderDiscountData($order); $discountInclTax = $discountData->getDiscountInclTax(); $discountExclTax = $discountData->getDiscountExclTax(); $discountVatAmount = $discountInclTax - $discountExclTax; $discountVatPercent = ($discountInclTax / $discountExclTax - 1) * 100; if (abs($discountInclTax) > 0) { $discount_description = $order->getDiscountDescription() !== null ? Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription()) : Mage::helper('sales')->__('Discount'); $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $discount_description)); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", -1 * $discountExclTax))); $OrderLine->appendChild($dom->createElement('VatRate', sprintf("%.2f", $discountVatPercent))); $OrderLine->appendChild($dom->createElement('VatAmount', sprintf("%.2f", -1 * $discountVatAmount))); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", -1 * $discountInclTax))); $OrderLines->appendChild($OrderLine); } // Add reward points if ((double) $order->getBaseRewardCurrencyAmount() > 0) { $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $this->getHelper()->__('Reward points'))); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', -1 * $order->getBaseRewardCurrencyAmount())); $OrderLine->appendChild($dom->createElement('VatRate', 0)); $OrderLine->appendChild($dom->createElement('VatAmount', 0)); $OrderLine->appendChild($dom->createElement('Amount', -1 * $order->getBaseRewardCurrencyAmount())); $OrderLines->appendChild($OrderLine); } return str_replace("\n", '', $dom->saveXML()); }