/** * Set correct values on subscription address based on given subscription and order address * * @param Adyen_Subscription_Model_Subscription $subscription * @param Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address $address * @return $this */ public function initAddress(Adyen_Subscription_Model_Subscription $subscription, $address) { $this->setSubscriptionId($subscription->getId()); // Reset (possible) original values $this->setOrderAddressId(null)->setCustomerAddressId(null)->setQuoteAddressId(null); if ($address->getAddressType() == Mage_Sales_Model_Order_Address::TYPE_BILLING) { $this->setType(self::ADDRESS_TYPE_BILLING); } else { $this->setType(self::ADDRESS_TYPE_SHIPPING); } // Note: Only use customer address if 'save_in_address_book' or 'same_as_billing' // is also checked at the address, because it's not enough to rely solely on the // customer address ID, because an address can be changed when creating an order // in the backend, but this ID still remains when a quote is converted to an order if ($address->getCustomerAddressId() && $address->getData('save_in_address_book')) { // Create customer address $this->setSource(self::ADDRESS_SOURCE_CUSTOMER)->setCustomerAddressId($address->getCustomerAddressId()); } elseif ($address instanceof Mage_Sales_Model_Quote_Address) { // Create quote address $this->setSource(self::ADDRESS_SOURCE_QUOTE)->setQuoteAddressId($address->getId()); } else { // Create order address $this->setSource(self::ADDRESS_SOURCE_ORDER)->setOrderAddressId($address->getId()); } return $this; }
/** * @param Adyen_Subscription_Model_Subscription_Address $object * @param Adyen_Subscription_Model_Subscription $subscription * @param int $type * @return $this */ public function loadBySubscription(Adyen_Subscription_Model_Subscription_Address $object, Adyen_Subscription_Model_Subscription $subscription, $type) { $select = Mage::getResourceModel('adyen_subscription/subscription_address_collection')->addFieldToFilter('subscription_id', $subscription->getId())->addFieldToFilter('type', $type)->getSelect(); $select->reset($select::COLUMNS); $select->columns('item_id'); $addressId = $this->_getConnection('read')->fetchOne($select); $this->load($object, $addressId); return $this; }
public function getPaymentHistoryErrors(Adyen_Subscription_Model_Subscription $subscription) { $this->addFieldToFilter('status', array('in' => Adyen_Subscription_Model_Subscription::STATUS_PAYMENT_ERROR)); $subSelect = $this->getConnection()->select(); $subSelect->from(array('subscription_history' => $this->getTable('adyen_subscription/subscription_history')), 'MAX(date) as date'); $subSelect->where('subscription_id = ?', $subscription->getId()); $subSelect->where('status = ?', Adyen_Subscription_Model_Subscription::STATUS_ACTIVE); $this->getSelect()->where("subscription_id = ?", $subscription->getId())->where("date >= (?)", $subSelect)->order(array('date ASC')); return $this; }
/** * Retrieve current tax percent for customer based on subscription and product * * @param Adyen_Subscription_Model_Subscription $subscription * @param Mage_Catalog_Model_Product $product * @return int */ public function getCustomerTaxPercent(Adyen_Subscription_Model_Subscription $subscription, Mage_Catalog_Model_Product $product) { $percent = $product->getTaxPercent(); $includingPercent = null; $taxClassId = $product->getTaxClassId(); if (is_null($percent)) { if ($taxClassId) { $request = Mage::getSingleton('tax/calculation')->getRateRequest($subscription->getShippingAddress(), $subscription->getBillingAddress(), $subscription->getCustomer()->getTaxClassId(), $product->getStore()); $percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId)); } } return $percent ?: 0; }
protected function _prepareColumns() { $helper = Mage::helper('adyen_subscription'); $this->addColumn('entity_id', ['header' => $helper->__('ID'), 'align' => 'right', 'width' => 1, 'index' => 'entity_id', 'filter_index' => 'main_table.entity_id']); $this->addColumn('increment_id', ['header' => $helper->__('Increment ID'), 'align' => 'right', 'width' => 1, 'index' => 'increment_id', 'filter_index' => 'main_table.increment_id']); $this->addColumn('error_message', ['header' => $helper->__('Error Message'), 'index' => 'error_message']); if (!Mage::getStoreConfig(Mage_Customer_Model_Customer::XML_PATH_GENERATE_HUMAN_FRIENDLY_ID)) { $this->addColumn('customer_id', ['header' => $helper->__('Customer ID'), 'index' => 'customer_id']); } else { $this->addColumn('customer_increment_id', ['header' => $helper->__('Customer Inc.'), 'index' => 'customer_increment_id', 'filter_condition_callback' => array($this, '_customCustomerIncrementIdSort')]); } $this->addColumn('customer_email', ['header' => $helper->__('Customer Email'), 'index' => 'customer_email', 'filter_index' => 'ce.email']); $this->addColumn('customer_name', ['header' => $helper->__('Name'), 'index' => 'customer_name']); $this->addColumn('ba_method_code', ['type' => 'options', 'header' => $helper->__('Payment method'), 'index' => 'ba_method_code', 'options' => Mage::helper('payment')->getAllBillingAgreementMethods(), 'filter_index' => 'ba.method_code']); $this->addColumn('ba_reference_id', ['header' => $helper->__('Billing Agreement'), 'index' => 'ba_reference_id', 'filter_index' => 'ba.reference_id']); $this->addColumn('created_at', ['header' => $helper->__('Created at'), 'index' => 'created_at', 'filter_index' => 'main_table.created_at', 'type' => 'datetime']); // $this->addColumn('ends_at', [ // 'header' => $helper->__('Ends at'), // 'index' => 'ends_at', // 'type' => 'datetime' // ]); // // $this->addColumn('next_order_at', [ // 'header' => $helper->__('Next shipment'), // 'index' => 'next_order_at', // 'type' => 'datetime' // ]); $this->addColumn('status', ['header' => $helper->__('Status'), 'index' => 'status', 'type' => 'options', 'options' => Adyen_Subscription_Model_Subscription::getStatuses(), 'renderer' => 'Adyen_Subscription_Block_Adminhtml_Subscription_Renderer_Status', 'filter_index' => 'main_table.status']); $this->addColumn('action', ['header' => $helper->__('Actions'), 'width' => '1', 'type' => 'action', 'getter' => 'getId', 'actions' => [['caption' => $helper->__('View'), 'url' => ['base' => '*/subscription/view', 'params' => ['store' => $this->getRequest()->getParam('store')]], 'field' => 'id']], 'filter' => false, 'sortable' => false]); return parent::_prepareColumns(); }
/** * Set correct values on subscription address based on given subscription and order address * * @param Adyen_Subscription_Model_Subscription $subscription * @param Mage_Sales_Model_Order_Address|Mage_Sales_Model_Quote_Address $address * @return $this */ public function initAddress(Adyen_Subscription_Model_Subscription $subscription, $address) { $this->setSubscriptionId($subscription->getId()); // Reset (possible) original values $this->setOrderAddressId(null)->setCustomerAddressId(null)->setQuoteAddressId(null); if ($address->getAddressType() == Mage_Sales_Model_Order_Address::TYPE_BILLING) { $this->setType(self::ADDRESS_TYPE_BILLING); } else { $this->setType(self::ADDRESS_TYPE_SHIPPING); } if ($address instanceof Mage_Sales_Model_Quote_Address) { // Create quote address $this->setSource(self::ADDRESS_SOURCE_QUOTE)->setQuoteAddressId($address->getId()); } else { // Create order address $this->setSource(self::ADDRESS_SOURCE_ORDER)->setOrderAddressId($address->getId()); } return $this; }
protected function _prepareColumns() { $helper = Mage::helper('adyen_subscription'); $this->addColumn('entity_id', array('header' => $helper->__('History #'), 'index' => 'entity_id')); $this->addColumn('date', array('header' => $helper->__('Date'), 'index' => 'date', 'type' => 'datetime', 'width' => '100px')); $this->addColumn('username', array('header' => $helper->__('Admin mail'), 'index' => 'username', 'type' => 'integer')); $this->addColumn('email', array('header' => $helper->__('Customer mail'), 'index' => 'email')); $this->addColumn('status', ['header' => $helper->__('Status'), 'index' => 'status', 'type' => 'options', 'options' => Adyen_Subscription_Model_Subscription::getStatuses(), 'renderer' => 'Adyen_Subscription_Block_Adminhtml_Subscription_Renderer_Status', 'filter_index' => 'main_table.status']); $this->addColumn('code', array('header' => $helper->__('Code'), 'index' => 'code')); return parent::_prepareColumns(); }
/** * Set new increment id to object * * @param Adyen_Subscription_Model_Subscription $object * @return Mage_Eav_Model_Entity_Abstract */ public function setNewIncrementId(Adyen_Subscription_Model_Subscription $object) { if ($object->getIncrementId() && $object->getIncrementId() != $object->getId()) { return $this; } $incrementId = Mage::getSingleton('eav/config')->getEntityType('adyen_subscription')->fetchNewIncrementId($object->getStoreId()); if ($incrementId !== false) { $object->setIncrementId($incrementId); } return $this; }
/** * @return $this */ public function addPlaceOrderFilter() { $this->addFieldToFilter('status', array('in' => Adyen_Subscription_Model_Subscription::getPlaceOrderStatuses())); $this->getSelect()->joinLeft(['subscription_quote' => $this->getTable('adyen_subscription/subscription_quote')], 'main_table.entity_id = subscription_quote.subscription_id', ['quote_id', 'order_id']); $this->getSelect()->where("scheduled_at < ?", now())->where('subscription_quote.order_id IS NULL')->where('subscription_quote.quote_id IS NOT NULL'); return $this; }
/** * @param Adyen_Subscription_Model_Subscription $subscription * @return string */ public function getAgreementUrl($subscription) { $agreementId = $subscription->getBillingAgreementId(); return $this->getUrl('sales/billing_agreement/view', array('agreement' => $agreementId)); }
public function updateQuotePayment(Adyen_Subscription_Model_Subscription $subscription, Mage_Sales_Model_Quote $quote) { Mage::dispatchEvent('adyen_subscription_service_updatequotepayment_before', array('subscription' => $subscription, 'quote' => $quote)); // Set payment method $methodInstance = $subscription->getBillingAgreement()->getPaymentMethodInstance(); if (!method_exists($methodInstance, 'initBillingAgreementPaymentInfo')) { Mage::helper('adyen_subscription')->logQuoteCron(sprintf('Payment method %s does not support Adyen_Subscription', $methodInstance->getCode())); Adyen_Subscription_Exception::throwException(Mage::helper('adyen_subscription')->__('Payment method %s does not support Adyen_Subscription', $methodInstance->getCode())); } // Set billing agreement data /* @noinspection PhpUndefinedMethodInspection */ try { /* @noinspection PhpUndefinedMethodInspection */ $methodInstance->initBillingAgreementPaymentInfo($subscription->getBillingAgreement(), $quote->getPayment()); // importan $quote->save() will not update payment object so use this: $quote->getPayment()->save(); Mage::dispatchEvent('adyen_subscription_service_updatequotepayment_after', array('subscription' => $subscription, 'quote' => $quote)); } catch (Mage_Core_Exception $e) { Mage::helper('adyen_subscription')->logQuoteCron(sprintf('Failed to set billing agreement data %s', $e->getMessage())); $subscription->setErrorMessage($e->getMessage()); $subscription->setStatus($subscription::STATUS_QUOTE_ERROR); Mage::getModel('core/resource_transaction')->addObject($quote)->addObject($subscription)->save(); Mage::dispatchEvent('adyen_subscription_service_updatequotepayment_fail', array('subscription' => $subscription, 'status' => $subscription::STATUS_QUOTE_ERROR, 'error' => $e->getMessage())); } }
/** * Update subscription based on given quote * * @param Mage_Sales_Model_Quote $quote * @param Adyen_Subscription_Model_Subscription $subscription * @return Adyen_Subscription_Model_Subscription $subscription */ public function updateSubscription(Mage_Sales_Model_Quote $quote, Adyen_Subscription_Model_Subscription $subscription) { Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_before', array('subscription' => $subscription, 'quote' => $quote)); $term = $termType = $stockId = null; foreach ($quote->getItemsCollection() as $quoteItem) { /** @var Mage_Sales_Model_Quote_Item $quoteItem */ $productSubscription = $this->_getProductSubscription($quoteItem); if (!$productSubscription) { // No product subscription found, no subscription needs to be created continue; } if (is_null($stockId)) { $stockId = $quoteItem->getStockId(); } if (is_null($term)) { $term = $productSubscription->getTerm(); } if (is_null($termType)) { $termType = $productSubscription->getTermType(); } if ($term != $productSubscription->getTerm() || $termType != $productSubscription->getTermType()) { Adyen_Subscription_Exception::throwException('Adyen Subscription options of products in quote have different terms'); } } $billingAgreement = $this->getBillingAgreement($quote); $this->updateQuotePayment($quote, $billingAgreement); if (!$quote->getShippingAddress()->getShippingMethod()) { Adyen_Subscription_Exception::throwException('No shipping method selected'); } // Update subscription $subscription->setStatus(Adyen_Subscription_Model_Subscription::STATUS_ACTIVE)->setStockId($stockId)->setBillingAgreementId($billingAgreement->getId())->setTerm($term)->setTermType($termType)->setShippingMethod($quote->getShippingAddress()->getShippingMethod())->setUpdatedAt(now())->save(); // Create subscription addresses Mage::getModel('adyen_subscription/subscription_address')->getSubscriptionAddress($subscription, self::ADDRESS_TYPE_BILLING)->initAddress($subscription, $quote->getBillingAddress())->save(); Mage::getModel('adyen_subscription/subscription_address')->getSubscriptionAddress($subscription, self::ADDRESS_TYPE_SHIPPING)->initAddress($subscription, $quote->getShippingAddress())->save(); // Delete current subscription items foreach ($subscription->getItemCollection() as $subscriptionItem) { /** @var Adyen_Subscription_Model_Subscription_Item $subscriptionItem */ $subscriptionItem->delete(); } $i = 0; // Create new subscription items foreach ($quote->getItemsCollection() as $quoteItem) { /** @var Mage_Sales_Model_Quote_Item $quoteItem */ /** @var Adyen_Subscription_Model_Product_Subscription $productSubscription */ $productSubscription = $this->_getProductSubscription($quoteItem); if (!$productSubscription) { // No product subscription found, no subscription needs to be created continue; } $productOptions = array('info_buyRequest' => unserialize($quoteItem->getOptionByCode('info_buyRequest')->getValue()), 'additional_options' => unserialize($quoteItem->getOptionByCode('additional_options')->getValue())); /** @var Adyen_Subscription_Model_Subscription_Item $subscriptionItem */ $subscriptionItem = Mage::getModel('adyen_subscription/subscription_item')->setSubscriptionId($subscription->getId())->setStatus(Adyen_Subscription_Model_Subscription_Item::STATUS_ACTIVE)->setProductId($quoteItem->getProductId())->setProductOptions(serialize($productOptions))->setSku($quoteItem->getSku())->setName($quoteItem->getName())->setLabel($productSubscription->getLabel())->setPrice($quoteItem->getPrice())->setPriceInclTax($quoteItem->getPriceInclTax())->setQty($quoteItem->getQty())->setOnce(0)->setCreatedAt(now())->save(); Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_add_item', array('subscription' => $subscription, 'item' => $subscriptionItem)); $i++; } if ($i <= 0) { Adyen_Subscription_Exception::throwException('No subscription products in the subscription'); } Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_after', array('subscription' => $subscription, 'quote' => $quote)); return $subscription; }
/** * @param Adyen_Subscription_Model_Subscription $subscription * @return Adyen_Subscription_Model_Subscription_History */ public function setSubscription(Adyen_Subscription_Model_Subscription $subscription) { $this->setSubscriptionId($subscription->getId()); return $this; }
protected function _editSubscription(Adyen_Subscription_Model_Subscription $subscription, array $params = array()) { $quote = $subscription->getActiveQuote(); Mage::getSingleton('adminhtml/session_quote')->setCustomerId($quote->getCustomerId())->setStoreId($quote->getStoreId())->setQuoteId($quote->getId()); $params['subscription'] = $subscription->getId(); $this->_redirect('adminhtml/sales_order_create/index', $params); }
/** * Update subscription based on given quote * * @param Mage_Sales_Model_Quote $quote * @param Adyen_Subscription_Model_Subscription $subscription * @return Adyen_Subscription_Model_Subscription $subscription */ public function updateSubscription(Mage_Sales_Model_Quote $quote, Adyen_Subscription_Model_Subscription $subscription) { Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_before', array('subscription' => $subscription, 'quote' => $quote)); try { $subscription->getResource()->beginTransaction(); $term = $termType = $stockId = null; foreach ($quote->getItemsCollection() as $quoteItem) { /** @var Mage_Sales_Model_Quote_Item $quoteItem */ $productSubscription = $this->_getProductSubscription($quoteItem); if (!$productSubscription) { // No product subscription found, no subscription needs to be created continue; } if (is_null($stockId)) { $stockId = $quoteItem->getStockId(); } if (is_null($term)) { $term = $productSubscription->getTerm(); } if (is_null($termType)) { $termType = $productSubscription->getTermType(); } if ($term != $productSubscription->getTerm() || $termType != $productSubscription->getTermType()) { Adyen_Subscription_Exception::throwException('Adyen Subscription options of products in quote have different terms'); } } $billingAgreement = $this->getBillingAgreement($quote); $this->updateQuotePayment($quote, $billingAgreement, $subscription->getData('payment')); if (!$quote->getShippingAddress()->getShippingMethod()) { Adyen_Subscription_Exception::throwException('No shipping method selected'); } // Update subscription $subscription->setStatus(Adyen_Subscription_Model_Subscription::STATUS_ACTIVE)->setStockId($stockId)->setBillingAgreementId($billingAgreement->getId())->setTerm($term)->setTermType($termType)->setShippingMethod($quote->getShippingAddress()->getShippingMethod())->setUpdatedAt(now())->save(); // Create subscription addresses $billingAddress = $quote->getBillingAddress(); $billingAddress->setCustomerAddressId($subscription->getData('billing_customer_address_id'))->setSaveInAddressBook($subscription->getData('billing_address_save_in_address_book')); Mage::getModel('adyen_subscription/subscription_address')->getSubscriptionAddress($subscription, self::ADDRESS_TYPE_BILLING)->initAddress($subscription, $billingAddress)->save(); $shippingAddress = $quote->getShippingAddress(); $shippingAddress->setCustomerAddressId($subscription->getData('shipping_customer_address_id'))->setSaveInAddressBook($subscription->getData('shipping_address_save_in_address_book'))->setSameAsBilling($billingAddress->getSaveInAddressBook() && $subscription->getData('shipping_as_billing')); Mage::getModel('adyen_subscription/subscription_address')->getSubscriptionAddress($subscription, self::ADDRESS_TYPE_SHIPPING)->initAddress($subscription, $shippingAddress)->save(); // Save addresses at customer when 'Save in address book' is selected if ($billingAddress->getCustomerAddressId() && $billingAddress->getSaveInAddressBook()) { $customerBillingAddress = Mage::getModel('customer/address')->load($billingAddress->getCustomerAddressId()); Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_customer_address', $billingAddress, $customerBillingAddress); $customerBillingAddress->save(); } if (!$subscription->getBillingAddress() instanceof Mage_Customer_Model_Address) { $quote->getBillingAddress()->setCustomerAddressId(null)->save(); } if ($shippingAddress->getCustomerAddressId() && $shippingAddress->getSaveInAddressBook()) { $customerShippingAddress = Mage::getModel('customer/address')->load($shippingAddress->getCustomerAddressId()); Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_customer_address', $shippingAddress, $customerShippingAddress); $customerShippingAddress->save(); } if (!$subscription->getShippingAddress() instanceof Mage_Customer_Model_Address) { $quote->getShippingAddress()->setCustomerAddressId(null)->save(); } // Delete current subscription items foreach ($subscription->getItemCollection() as $subscriptionItem) { /** @var Adyen_Subscription_Model_Subscription_Item $subscriptionItem */ $subscriptionItem->delete(); } $i = 0; // Create new subscription items foreach ($quote->getItemsCollection() as $quoteItem) { /** @var Mage_Sales_Model_Quote_Item $quoteItem */ /** @var Adyen_Subscription_Model_Product_Subscription $productSubscription */ $productSubscription = $this->_getProductSubscription($quoteItem); if (!$productSubscription || $quoteItem->getParentItemId()) { // No product subscription found, no subscription needs to be created // or item is child of bundle/configurable continue; } $productOptions = array('info_buyRequest' => unserialize($quoteItem->getOptionByCode('info_buyRequest')->getValue()), 'additional_options' => unserialize($quoteItem->getOptionByCode('additional_options')->getValue())); /** @var Adyen_Subscription_Model_Subscription_Item $subscriptionItem */ $subscriptionItem = Mage::getModel('adyen_subscription/subscription_item')->setSubscriptionId($subscription->getId())->setStatus(Adyen_Subscription_Model_Subscription_Item::STATUS_ACTIVE)->setProductId($quoteItem->getProductId())->setProductOptions(serialize($productOptions))->setSku($quoteItem->getSku())->setName($quoteItem->getName())->setProductSubscriptionId($productSubscription->getId())->setLabel($productSubscription->getLabel())->setPrice($quoteItem->getPrice())->setPriceInclTax($quoteItem->getPriceInclTax())->setQty($quoteItem->getQty())->setOnce(0)->setCreatedAt(now())->save(); Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_add_item', array('subscription' => $subscription, 'item' => $subscriptionItem)); $i++; } if ($i <= 0) { Adyen_Subscription_Exception::throwException('No subscription products in the subscription'); } $subscription->getResource()->commit(); Mage::helper('adyen_subscription')->logOrderCron(sprintf("Successfully updated subscription (%s) from quote (%s)", $subscription->getId(), $quote->getId())); } catch (Exception $e) { // 1. rollback everything $subscription->getResource()->rollBack(); // 2. log the error to the debuglog Mage::helper('adyen_subscription')->logOrderCron(sprintf("Error while updating subscription (%s) from quote (%s) error is: %s", $subscription->getId(), $quote->getId(), $e->getMessage())); // 3. save the error on the subscription $subscription->setErrorMessage($e->getMessage()); $subscription->setStatus($subscription::STATUS_SUBSCRIPTION_ERROR); $subscription->save(); // 4. dispatch the failure event Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_fail', array('subscription' => $subscription, 'quote' => $quote, 'status' => $subscription->getStatus(), 'error' => $e->getMessage())); throw $e; } Mage::dispatchEvent('adyen_subscription_quote_updatesubscription_after', array('subscription' => $subscription, 'quote' => $quote)); return $subscription; }