/** * {@inheritDoc} */ public function calculate($request, ShoppingCart $shoppingCart) { $paymentType = $shoppingCart->getSelectedPaymentType(); // iterate over all conditions $output = array(); foreach ($this->container->get('settingsService')->get('plugins.paymentSurcharge.conditions', array()) as $condition) { if ($paymentType->getId() == $condition['code'] || null === $condition['code']) { // payment module match if (null != $condition['cvalue']) { $cvalueToken = explode(':', $condition['cvalue']); if (2 == count($cvalueToken)) { $cvalueType = $cvalueToken[0]; $cvalueName = $cvalueToken[1]; } else { $cvalueType = 'field'; $cvalueName = $cvalueToken[0]; } // evaluate the value to use with the regexp $cvalueNames = explode(';', $cvalueName); switch ($cvalueType) { case 'field': $cvalue = null; foreach ($cvalueNames as $name) { if (isset($payment->{$name})) { $cvalue = $payment->{$name}; } else { $cvalue = $request->getParameter($name, null); } if (null !== $cvalue) { break; } } break; default: $this->container->get('logger')->err('invalid condition value type: ' . $cvalueType); return null; } } // check eregexp if (null == $condition['cvalue'] && null == $condition['regexp'] || ereg($condition['regexp'], $cvalue)) { // match, so apply condition // evaluate the condition's value $amount = 0; if (is_numeric($condition['value'])) { $amount = (double) $condition['value']; } if (0 === strpos($condition['value'], '%:')) { $amount = trim(str_replace('%:', '', $condition['value'])); $amount = $shoppingCart->getSubtotal() * ($amount / 100); } $details = Beans::getBean('ZMOrderTotalLineDetails'); $details->setTitle($condition['title']); $details->setAmount($amount); $details->setDisplayValue($amount); $output[] = $details; } } } return $output; }
/** * Populate info. * * @param ShoppingCart shoppingCart The shopping cart. */ protected function populateInfo(ShoppingCart $shoppingCart) { // general stuff // TODO: where from/to?? $languageId = $this->container->get('settingsService')->get('storeDefaultLanguageId'); // TODO: move all cart/session values into ShoppingCart $currencyCode = $this->container->get('session')->get('currency'); $couponAmount = 0; $couponCode = null; if (null != ($couponCodeId = $this->container->get('session')->get('cc_id'))) { $coupon = $this->container->get('couponService')->getCouponForId($couponCodeId, $languageId); if (null != $coupon) { $couponCode = $coupon->getCode(); $couponAmount = $coupon->getAmount(); } } $shippingMethod = $shoppingCart->getSelectedShippingMethod(); $paymentType = $shoppingCart->getSelectedPaymentType(); $orderStatus = DEFAULT_ORDERS_STATUS_ID; if (null != $paymentType && null !== ($pos = $paymentType->getOrderStatus())) { $orderStatus = $pos; } $tax = 0; $taxGroups = array(); foreach ($shoppingCart->getItems() as $item) { $itemTotal = $item->getItemTotal(false) + $item->getOneTimeCharge(false); $itemTotalPlusTax = $item->getItemTotal(true) + $item->getOneTimeCharge(true); $itemTax = $itemTotalPlusTax - $itemTotal; $productTaxRate = $item->getTaxRate(); $description = $productTaxRate->getDescription(); if (!array_key_exists($description, $taxGroups)) { $taxGroups[$description] = 0; } $taxGroups[$description] += $itemTax; $tax += $itemTax; } $info = array('order_status' => $orderStatus, 'currency' => $currencyCode, 'currency_value' => $this->container->get('currencyService')->getCurrencyForCode($currencyCode)->getRate(), 'payment_method' => null != $paymentType ? $paymentType->getTitle() : '', 'payment_module_code' => null != $paymentType ? $paymentType->getId() : '', 'coupon_code' => $couponCode, 'shipping_method' => null != $shippingMethod ? $shippingMethod->getName() : '', 'shipping_module_code' => null != $shippingMethod ? $shippingMethod->getShippingId() : '', 'shipping_cost' => null != $shippingMethod ? $shippingMethod->getCost() : '', 'subtotal' => $shoppingCart->getSubTotal(), 'shipping_tax' => 0, 'tax' => $tax, 'total' => $shoppingCart->getTotal() + $couponAmount, 'tax_groups' => $taxGroups, 'comments' => $shoppingCart->getComments()); $this->info = array_merge($this->info, $info); if ($this->container->get('settingsService')->get('apps.store.assertZencart', false)) { $this->assertInfo(false); } }