예제 #1
0
 public function applyToOrder(CustomerOrder $order)
 {
     $amount = $order->getCurrency()->convertAmount(CustomerOrder::getApplication()->getDefaultCurrency(), $this->getDiscountAmount($order->totalAmount->get()));
     $orderDiscount = OrderDiscount::getNewInstance($order);
     $orderDiscount->amount->set($amount);
     $orderDiscount->description->set($this->parentCondition->getParam('name_lang'));
     $order->registerOrderDiscount($orderDiscount);
 }
예제 #2
0
파일: OrderHistory.php 프로젝트: saiber/www
 private function serializeToArray(CustomerOrder $order)
 {
     $array = array();
     $array['ID'] = $order->getID();
     $array['totalAmount'] = $order->totalAmount->get();
     $array['isCancelled'] = (int) $order->isCancelled->get();
     $array['status'] = (int) $order->status->get();
     $array['shipments'] = array();
     foreach ($order->getShipments() as $shipment) {
         $shippingServiceArray = null;
         if ($shipment->shippingService->get() && (int) $shipment->shippingService->get()->getID()) {
             $shippingServiceArray = $shipment->shippingService->get()->toArray();
         } else {
             $shippingService = unserialize($shipment->shippingServiceData->get());
             $shippingServiceArray = null;
             if (is_object($shippingService)) {
                 $shippingService->setApplication($order->getApplication());
                 $shippingServiceArray = $shippingService->toArray();
             }
         }
         $array['shipments'][$shipment->getID()] = array('ID' => $shipment->getID(), 'status' => (int) $shipment->status->get(), 'ShippingService' => $shippingServiceArray);
     }
     $array['items'] = array();
     foreach ($order->getOrderedItems() as $item) {
         $array['items'][$item->getID()] = array('ID' => $item->getID(), 'shipmentID' => $item->shipment->get() ? $item->shipment->get()->getID() : null, 'count' => (int) $item->count->get(), 'price' => $item->price->get(), 'Product' => array('ID' => (int) $item->getProduct()->getID(), 'sku' => $item->getProduct()->sku->get(), 'name' => $item->getProduct()->getName($order->getApplication()->getDefaultLanguageCode()), 'isDownloadable' => (int) $item->getProduct()->isDownloadable()), 'Shipment' => $item->shipment->get() && isset($array['shipments'][$item->shipment->get()->getID()]) ? $array['shipments'][$item->shipment->get()->getID()] : array('ID' => 0));
     }
     // @todo: dirty fix
     if ($order->shippingAddress->get()) {
         if ($order->shippingAddress->get()->state->get()) {
             $order->shippingAddress->get()->state->get()->load();
         }
         $array['ShippingAddress'] = $order->shippingAddress->get()->toArray();
     } else {
         $array['ShippingAddress'] = array();
     }
     if ($order->billingAddress->get()) {
         if ($order->billingAddress->get()->state->get()) {
             $order->billingAddress->get()->state->get()->load();
         }
         $array['BillingAddress'] = $order->billingAddress->get()->toArray();
     } else {
         $array['BillingAddress'] = array();
     }
     return $array;
 }
예제 #3
0
 private function getPaymentHandler()
 {
     $payment = new GoogleCheckout($this->details);
     $payment->setApplication(CustomerOrder::getApplication());
     $payment->setConfigValue('sandbox', '1');
     $payment->setConfigValue('merchant_id', '952173450848810');
     $payment->setConfigValue('merchant_key', '7fUUEkKof9OD-6u6cUNX8g');
     $payment->setOrder($this->createOrder());
     return $payment;
 }
예제 #4
0
 public function __construct(CustomerOrder $order, Currency $currency)
 {
     parent::__construct();
     $this->order = $order;
     $order->loadAll();
     // billing address
     if ($address = $order->billingAddress->get()) {
         $fields = array('firstName', 'lastName', 'companyName', 'phone', 'city', 'postalCode', 'countryID' => 'country');
         foreach ($fields as $key => $field) {
             $addressField = is_numeric($key) ? $field : $key;
             $this->{$field}->set($address->{$addressField}->get());
         }
         $this->state->set($this->getStateValue($address));
         $this->address->set($address->address1->get() . ' ' . $address->address2->get());
     }
     // shipping address
     $address = $order->shippingAddress->get();
     if (!$address) {
         $address = $order->billingAddress->get();
     }
     if ($address) {
         foreach ($fields as $key => $field) {
             $addressField = is_numeric($key) ? $field : $key;
             $field = 'shipping' . ucfirst($field);
             $this->{$field}->set($address->{$addressField}->get());
         }
         $this->shippingState->set($this->getStateValue($address));
         $this->shippingAddress->set($address->address1->get() . ' ' . $address->address2->get());
     }
     // amount
     $order->currency->set($currency);
     $this->amount->set(round($order->getDueAmount(), 2));
     $this->currency->set($currency->getID());
     // transaction identification
     $this->invoiceID->set($order->getID());
     if (isset($_SERVER['REMOTE_ADDR'])) {
         $this->ipAddress->set($_SERVER['REMOTE_ADDR']);
     }
     // customer identification
     if ($order->user->get()) {
         $order->user->get()->load();
         $this->shippingEmail->set($order->user->get()->email->get());
         $this->email->set($order->user->get()->email->get());
         $this->clientID->set($order->user->get()->getID());
     }
     // order details
     // load variation data
     $variations = new ProductSet();
     foreach ($order->getShoppingCartItems() as $item) {
         if ($item->product->get() && $item->product->get()->parent->get()) {
             $variations->unshift($item->product->get());
         }
     }
     if ($variations->size()) {
         $variations->loadVariations();
     }
     foreach ($order->getShoppingCartItems() as $item) {
         $product = $item->getProduct();
         $variations = array();
         foreach ($product->getRegisteredVariations() as $variation) {
             $variations[] = $variation->getValueByLang('name');
         }
         $ri = RecurringItem::getInstanceByOrderedItem($item);
         if ($ri && $ri->isExistingRecord()) {
             $ri->load();
         } else {
             $ri = null;
         }
         $this->addLineItem($product->getName() . ($variations ? ' (' . implode(' / ', $variations) . ')' : ''), $item->getPrice(false), $item->count->get(), $product->sku->get(), $ri);
     }
     if ($discount = $order->getFixedDiscountAmount()) {
         $this->addLineItem(CustomerOrder::getApplication()->translate('_discount'), $discount * -1, 1, 'discount');
     }
     foreach ($order->getShipments() as $shipment) {
         if ($rate = $shipment->getSelectedRate()) {
             $rate = $rate->toArray();
             $name = empty($rate['ShippingService']['name_lang']) ? $rate['serviceName'] : $rate['ShippingService']['name_lang'];
             $this->addLineItem($name, $shipment->getShippingTotalBeforeTax(), 1, 'shipping');
         }
     }
     if ($taxes = $order->getTaxBreakdown()) {
         foreach ($taxes as $id => $amount) {
             $tax = Tax::getInstanceById($id, true);
             $this->addLineItem($tax->getValueByLang('name', null), $amount, 1, 'tax');
         }
     }
 }
예제 #5
0
 public function getHandler($returnUrl = '', $cancelUrl = '')
 {
     $application = ActiveRecordModel::getApplication();
     $GLOBALS['merchant_id'] = $this->getConfigValue('merchant_id');
     if ($this->getConfigValue('sandbox') && !defined('PHPGCHECKOUT_USE_SANDBOX')) {
         define('PHPGCHECKOUT_USE_SANDBOX', true);
     }
     include_once dirname(dirname(__FILE__)) . '/library/google/config.php';
     if ($this->order) {
         $GLOBALS['GCheckout_currency'] = $this->getValidCurrency($this->order->getCurrency()->getID());
     }
     $handler = new gCart($this->getConfigValue('merchant_id'), $this->getConfigValue('merchant_key'));
     $handler->setMerchantCheckoutFlowSupport($returnUrl, $cancelUrl, $this->application->getConfig()->get('REQUIRE_PHONE'));
     // add cart items
     if ($this->order) {
         $items = array();
         foreach ($this->order->getOrderedItems() as $item) {
             if (!$item->isSavedForLater->get()) {
                 $gItem = new gItem(htmlspecialchars($item->product->get()->getValueByLang('name')), htmlspecialchars($item->product->get()->getValueByLang('shortDescription')), $item->count->get(), $item->price->get());
                 $gItem->setPrivateItemData('<item-id>' . $item->getID() . '</item-id><order-id>' . $this->order->getID() . '</order-id>');
                 $items[] = $gItem;
             }
             // add discounts
             if ($discounts = $this->order->getFixedDiscountAmount()) {
                 $items[] = new gItem($application->translate('_discount'), '', 1, $discounts * -1);
             }
             $handler->addItems($items);
         }
         // get shipping rates for all zones - silly, eh?
         if ($this->order->isShippingRequired()) {
             $shipment = $this->order->getShipments()->get(0);
             $zoneCountries = $zoneStates = $zoneZips = array();
             foreach (DeliveryZone::getAll() as $zone) {
                 $countries = $zone->getCountries()->extractField('countryCode');
                 $states = array();
                 foreach ($zone->getStates()->extractReferencedItemSet('state') as $state) {
                     if ($state->countryID == 'US') {
                         $states[] = $state->code->get();
                     } else {
                         $countries[] = $state->countryID->get();
                     }
                 }
                 $countries = array_intersect(array_unique($countries), $this->getSupportedCountries());
                 $zipMasks = $zone->getZipMasks()->extractField('mask');
                 foreach ($zone->getShippingRates($shipment)->toArray() as $rate) {
                     $name = $rate['serviceName'] ? $rate['serviceName'] : $rate['ShippingService']['name_lang'];
                     $gRate = new gShipping($name, round($rate['costAmount'], 2), 'merchant-calculated-shipping');
                     // @todo: remove this. chokes up on non-US postal codes
                     $zipMasks = array();
                     $gRate->addAllowedAreas($countries, $states, $zipMasks);
                     $shipping[$name] = $gRate;
                 }
                 $zoneCountries = array_merge($zoneCountries, $countries);
                 $zoneStates = array_merge($zoneStates, $states);
                 $zoneZips = array_merge($zoneZips, $zipMasks);
             }
             // default zone
             $enabledCountries = array_keys($application->getConfig()->get('ENABLED_COUNTRIES'));
             $defCountries = array_intersect($enabledCountries, $zoneCountries, $this->getSupportedCountries());
             foreach (DeliveryZone::getDefaultZoneInstance()->getShippingRates($shipment)->toArray() as $rate) {
                 $gRate = new gShipping($rate['serviceName'] ? $rate['serviceName'] : $rate['ShippingService']['name_lang'], round($rate['costAmount'], 2), 'merchant-calculated-shipping');
                 $gRate->addAllowedAreas($defCountries, array(), array());
                 $shipping[] = $gRate;
             }
             $handler->_setShipping($shipping);
         }
     }
     // set merchant calculations
     $router = CustomerOrder::getApplication()->getRouter();
     $calcUrl = $router->createFullUrl($router->createUrl(array('controller' => 'googleCheckout', 'action' => 'index')), !$this->getConfigValue('sandbox'));
     $handler->setMerchantCalculations(new gMerchantCalculations($calcUrl, $this->getConfigValue('coupons')));
     $handler->setDefaultTaxTable(new gTaxTable('Tax', array(new gTaxRule(0))));
     return $handler;
 }