/** * Checks if the country code is allowed for the service provider. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return boolean True if payment provider can be used, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $basket) { $addresses = $basket->getAddresses(); $paymentType = MShop_Order_Item_Base_Address_Abstract::TYPE_PAYMENT; $deliveryType = MShop_Order_Item_Base_Address_Abstract::TYPE_DELIVERY; if (isset($addresses[$deliveryType])) { $code = strtoupper($addresses[$deliveryType]->getCountryId()); if ($this->_checkCountryCode($code, 'country.delivery-include') === false || $this->_checkCountryCode($code, 'country.delivery-exclude') === true) { return false; } } else { if (isset($addresses[$paymentType])) { $code = strtoupper($addresses[$paymentType]->getCountryId()); if ($this->_checkCountryCode($code, 'country.delivery-include') === false || $this->_checkCountryCode($code, 'country.delivery-exclude') === true) { return false; } } } if (isset($addresses[$paymentType])) { $code = strtoupper($addresses[$paymentType]->getCountryId()); if ($this->_checkCountryCode($code, 'country.billing-include') === false || $this->_checkCountryCode($code, 'country.billing-exclude') === true) { return false; } } return $this->_getProvider()->isAvailable($basket); }
/** * Checks if payment provider can be used based on the basket content. * Checks for country, currency, address, scoring, etc. should be implemented in separate decorators * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return boolean True if payment provider can be used, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $basket) { if ($basket->getLocale()->getLanguageId() === 'en') { return $this->_getProvider()->isAvailable($basket); } return false; }
/** * Checks if the the basket weight is ok for the service provider. * * @param MShop_Order_Item_Base_Interface $basket Basket object * * @return boolean True if payment provider can be used, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $basket) { $context = $this->_getContext(); $basketWeight = 0; $basketItems = $basket->getProducts(); foreach ($basketItems as $basketItem) { $prodId = $basketItem->getProductId(); if (!isset($prodMap[$prodId])) { // basket can contain a product several times in different basket items $prodMap[$prodId] = 0.0; } $prodMap[$prodId] += $basketItem->getQuantity(); } $propertyManager = MShop_Factory::createManager($context, 'product/property'); $search = $propertyManager->createSearch(true); $expr = array($search->compare('==', 'product.property.productid', array_keys($prodMap)), $search->compare('==', 'product.property.type.code', 'package-weight'), $search->getConditions()); $search->setConditions($search->combine('&&', $expr)); $search->setSlice(0, 0x7fffffff); // if more than 100 products are in the basket foreach ($propertyManager->searchItems($search) as $property) { $basketWeight += (double) $property->getValue() * $prodMap[$property->getParentId()]; } if ($this->_checkWeightScale($basketWeight) === false) { return false; } return $this->_getProvider()->isAvailable($basket); }
/** * Returns the price when using the provider. * Usually, this is the lowest price that is available in the service item but can also be a calculated based on * the basket content, e.g. 2% of the value as transaction cost. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return MShop_Price_Item_Interface Price item containing the price, shipping, rebate */ public function calcPrice(MShop_Order_Item_Base_Interface $basket) { $config = $this->getServiceItem()->getConfig(); if (!isset($config['costs.percent'])) { throw new MShop_Service_Provider_Exception(sprintf('Missing configuration "%1$s"', 'costs.percent')); } $value = $basket->getPrice()->getValue() * $config['costs.percent'] / 100; $price = $this->_getProvider()->calcPrice($basket); $price->setCosts($price->getCosts() + $value); return $price; }
/** * Checks for requirements. * * @param MShop_Order_Item_Base_Interface $base Basic order of the customer * @return boolean True if the requirements are met, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $base) { if (($prodcode = $this->_getConfigValue('required.productcode')) !== null) { foreach ($base->getProducts() as $product) { if ($product->getProductCode() == $prodcode) { return parent::isAvailable($base); } } return false; } return true; }
/** * Checks for the min/max order value. * * @param MShop_Order_Item_Base_Interface $base Basic order of the customer * @return boolean True if the basket matches the constraints, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $base) { $price = $base->getPrice(); $currency = $price->getCurrencyId(); $value = $price->getValue() + $price->getRebate(); $minvalue = $this->_getConfigValue('basketvalues.total-value-min', array()); if (isset($minvalue[$currency]) && $minvalue[$currency] > $value) { return false; } $maxvalue = $this->_getConfigValue('basketvalues.total-value-max', array()); if (isset($maxvalue[$currency]) && $maxvalue[$currency] < $value) { return false; } return parent::isAvailable($base); }
/** * Returns the configuration attribute definitions of the provider to generate a list of available fields and * rules for the value of each field in the frontend. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return array List of attribute definitions implementing MW_Common_Critera_Attribute_Interface */ public function getConfigFE(MShop_Order_Item_Base_Interface $basket) { $list = array(); $feconfig = $this->_feConfig; try { $address = $basket->getAddress(); if (($fn = $address->getFirstname()) !== '' && ($ln = $address->getLastname()) !== '') { $feconfig['directdebit.accountowner']['default'] = $fn . ' ' . $ln; } } catch (MShop_Order_Exception $e) { } // If address isn't available foreach ($feconfig as $key => $config) { $list[$key] = new MW_Common_Criteria_Attribute_Default($config); } return $list; }
/** * Checks for the product limits when the configuration contains limits per currency. * * @param MShop_Order_Item_Base_Interface $order Basket object * @param MShop_Order_Item_Base_Product_Interface $value Order product item * @throws MShop_Plugin_Provider_Exception If one limit is exceeded */ protected function _checkWithCurrency(MShop_Order_Item_Base_Interface $order, MShop_Order_Item_Base_Product_Interface $value) { $config = $this->_getItem()->getConfig(); $currencyId = $value->getPrice()->getCurrencyId(); if (isset($config['single-value-max'][$currencyId]) && $value->getPrice()->getValue() * $value->getQuantity() > (double) $config['single-value-max'][$currencyId]) { $msg = sprintf('The maximum product value is %1$s', $config['single-value-max'][$currencyId]); throw new MShop_Plugin_Provider_Exception($msg); } if (isset($config['total-value-max'][$currencyId])) { $price = clone $value->getPrice(); $price->setValue($price->getValue() * $value->getQuantity()); foreach ($order->getProducts() as $product) { $price->addItem($product->getPrice(), $product->getQuantity()); } if ((double) $price->getValue() > (double) $config['total-value-max'][$currencyId]) { $msg = sprintf('The maximum value of all products is %1$s', $config['total-value-max'][$currencyId]); throw new MShop_Plugin_Provider_Exception($msg); } } }
/** * Saves the services of the order to the storage. * * @param MShop_Order_Item_Base_Interface $basket Basket containing service items */ protected function _storeServices(MShop_Order_Item_Base_Interface $basket) { $manager = $this->getSubManager('service'); $attrManager = $manager->getSubManager('attribute'); foreach ($basket->getServices() as $type => $item) { $item->setBaseId($basket->getId()); $item->setType($type); $manager->saveItem($item); foreach ($item->getAttributes() as $attribute) { $attribute->setServiceId($item->getId()); $attrManager->saveItem($attribute); } } }
/** * Returns an list of order data required by PayPal. * * @param MShop_Order_Item_Base_Interface $orderBase Order base item * @return array Associative list of key/value pairs with order data required by PayPal */ protected function _getOrderDetails(MShop_Order_Item_Base_Interface $orderBase) { $values = $this->_getAuthParameter(); try { $orderAddressDelivery = $orderBase->getAddress(MShop_Order_Item_Base_Address_Abstract::TYPE_DELIVERY); /* setting up the shipping address details (ReviewOrder) */ $values['ADDROVERRIDE'] = 1; $values['PAYMENTREQUEST_0_SHIPTONAME'] = $orderAddressDelivery->getFirstName() . ' ' . $orderAddressDelivery->getLastName(); $values['PAYMENTREQUEST_0_SHIPTOSTREET'] = $orderAddressDelivery->getAddress1() . ' ' . $orderAddressDelivery->getAddress2() . ' ' . $orderAddressDelivery->getAddress3(); $values['PAYMENTREQUEST_0_SHIPTOCITY'] = $orderAddressDelivery->getCity(); $values['PAYMENTREQUEST_0_SHIPTOSTATE'] = $orderAddressDelivery->getState(); $values['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $orderAddressDelivery->getCountryId(); $values['PAYMENTREQUEST_0_SHIPTOZIP'] = $orderAddressDelivery->getPostal(); } catch (Exception $e) { } // If no address is available $lastPos = 0; foreach ($orderBase->getProducts() as $product) { $lastPos = $product->getPosition() - 1; $values['L_PAYMENTREQUEST_0_NUMBER' . $lastPos] = $product->getId(); $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $product->getName(); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = $product->getQuantity(); $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $product->getPrice()->getValue(); } foreach ($orderBase->getServices() as $service) { if (($val = $service->getPrice()->getValue()) > '0.00') { $lastPos++; $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $service->getName(); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1'; $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $val; } } $paymentItem = $orderBase->getService('payment'); if (($paymentCosts = $paymentItem->getPrice()->getCosts()) > '0.00') { $lastPos++; $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $this->_getContext()->getI18n()->dt('mshop', 'Payment costs'); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1'; $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $paymentCosts; } $price = $orderBase->getPrice(); $amount = $price->getValue() + $price->getCosts(); $values['MAXAMT'] = $amount + 0.01; // @todo rounding error? $values['PAYMENTREQUEST_0_AMT'] = number_format($amount, 2, '.', ''); $values['PAYMENTREQUEST_0_ITEMAMT'] = (string) ($price->getValue() + $paymentCosts); $values['PAYMENTREQUEST_0_SHIPPINGAMT'] = (string) ($price->getCosts() - $paymentCosts); $values['PAYMENTREQUEST_0_INSURANCEAMT'] = '0.00'; $values['PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED'] = 'false'; $values['PAYMENTREQUEST_0_SHIPDISCAMT'] = '0.00'; $values['PAYMENTREQUEST_0_TAXAMT'] = $price->getTaxRate(); $values['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderBase->getPrice()->getCurrencyId(); $values['PAYMENTREQUEST_0_PAYMENTACTION'] = $this->_getConfigValue(array('paypalexpress.PaymentAction'), 'sale'); try { $orderServiceDeliveryItem = $orderBase->getService('delivery'); $values['L_SHIPPINGOPTIONAMOUNT0'] = (string) ($price->getCosts() - $paymentCosts); $values['L_SHIPPINGOPTIONLABEL0'] = $orderServiceDeliveryItem->getName(); $values['L_SHIPPINGOPTIONNAME0'] = $orderServiceDeliveryItem->getCode(); $values['L_SHIPPINGOPTIONISDEFAULT0'] = 'true'; } catch (Exception $e) { } // If no delivery service is available return $values; }
protected function loadDownloadAttrList(MShop_Order_Item_Base_Interface $order, $context) { $downloads = array(); $downloadManager = MShop_Factory::createManager($context, 'download'); foreach ($order->getProducts() as $product) { $search = $downloadManager->createSearch(true); $expr = array($search->compare('==', 'download.prodid', $product->getId()), $search->compare('==', 'download.orderid', $order->getId()), $search->getConditions()); $search->setConditions($search->combine('&&', $expr)); $dlList = $downloadManager->searchItems($search); if (count($dlList) > 0) { $download = reset($dlList); $downloads[$product->getId()] = $download->getFileId(); } } return $downloads; }
/** * Returns a list of tax rates and their price items for the given basket. * * @param MShop_Order_Item_Base_Interface $basket Basket containing the products, services, etc. * @return array Associative list of tax rates as key and corresponding price items as value */ protected function _getPriceByTaxRate(MShop_Order_Item_Base_Interface $basket) { $taxrates = array(); foreach ($basket->getProducts() as $product) { $price = $product->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } try { $price = $basket->getService('delivery')->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } catch (Exception $e) { } // if delivery service isn't available try { $price = $basket->getService('payment')->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } catch (Exception $e) { } // if payment service isn't available return $taxrates; }
/** * Adds the "additional" section to the XML object * * @param MShop_Order_Item_Base_Interface $base Order base object * @param DOMDocument $dom DOM document object with contains the XML structure * @param DOMElement $orderitem DOM element which will be the parent of the new child * @throws DOMException If an error occures */ protected function _buildXMLAdditional(MShop_Order_Item_Base_Interface $base, DOMDocument $dom, DOMElement $orderitem) { $additional = $dom->createElement('additional'); $this->_appendChildCDATA('comment', '', $dom, $additional); $couponItem = $dom->createElement('discount'); foreach ($base->getCoupons() as $code => $products) { $this->_appendChildCDATA('code', $code, $dom, $couponItem); } $additional->appendChild($couponItem); $orderitem->appendChild($additional); }
/** * Returns a list of tax rates and their price items for the given basket. * * @param MShop_Order_Item_Base_Interface $basket Basket containing the products, services, etc. * @return array Associative list of tax rates as key and corresponding price items as value */ protected function _getPriceByTaxRate(MShop_Order_Item_Base_Interface $basket) { $taxrates = array(); $manager = MShop_Factory::createManager($this->_getContext(), 'price'); foreach ($basket->getProducts() as $product) { $price = $product->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price, $product->getQuantity()); } try { $price = $basket->getService('delivery')->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price); } catch (Exception $e) { } // if delivery service isn't available try { $price = $basket->getService('payment')->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price); } catch (Exception $e) { } // if payment service isn't available return $taxrates; }
/** * Adds the default services to the basket if they are not available. * * @param MShop_Order_Item_Base_Interface $order Basket object */ protected function _setServicesDefault(MShop_Order_Item_Base_Interface $order) { $services = $order->getServices(); $type = MShop_Order_Item_Base_Service_Abstract::TYPE_DELIVERY; if (!isset($services[$type]) && $this->_getConfigValue('autofill.delivery', false) == true && (($item = $this->_getServiceItem($order, $type, $this->_getConfigValue('autofill.deliverycode'))) !== null || ($item = $this->_getServiceItem($order, $type)) !== null)) { $order->setService($item, $type); } $type = MShop_Order_Item_Base_Service_Abstract::TYPE_PAYMENT; if (!isset($services[$type]) && $this->_getConfigValue('autofill.payment', false) == true && (($item = $this->_getServiceItem($order, $type, $this->_getConfigValue('autofill.paymentcode'))) !== null || ($item = $this->_getServiceItem($order, $type)) !== null)) { $order->setService($item, $type); } }
/** * Migrates the services from the old basket to the current one. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @param array $errors Associative list of previous errors * @return array Associative list of errors occured */ private function _copyServices(MShop_Order_Item_Base_Interface $basket, array $errors) { foreach ($basket->getServices() as $type => $item) { try { $attributes = array(); foreach ($item->getAttributes() as $attrItem) { $attributes[$attrItem->getCode()] = $attrItem->getValue(); } $this->setService($type, $item->getServiceId(), $attributes); $basket->deleteService($type); } catch (Exception $e) { } // Don't notify the user as appropriate services can be added automatically } return $errors; }
/** * Returns the IDs of the products in the current basket. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return string[] List of product IDs */ protected function _getProductIdsFromBasket(MShop_Order_Item_Base_Interface $basket) { $list = array(); foreach ($basket->getProducts() as $orderProduct) { $list[$orderProduct->getProductId()] = true; foreach ($orderProduct->getProducts() as $subProduct) { $list[$subProduct->getProductId()] = true; } } return array_keys($list); }