1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33:
34: 35: 36: 37: 38: 39: 40:
41: class Eabi_DpdEE_Model_Payment_Processor extends Mage_Payment_Model_Method_Abstract {
42:
43: protected $_code = Eabi_DpdEE_Model_Config::PAYMENT_METHOD_COD;
44: protected $_canAuthorize = true;
45: protected $_canCapture = true;
46: protected $_canVoid = true;
47: protected $_canUseInternal = true;
48: protected $_canUseCheckout = true;
49: protected $_canUseForMultishipping = true;
50: protected $_formBlockType = 'eabi_dpdee/invoice';
51: protected $_infoBlockType = 'eabi_dpdee/info_payment';
52: protected $_checkout;
53: protected $_quote;
54:
55: 56: 57: 58:
59: private static $_methodInstance;
60: 61: 62: 63:
64: private $_currency;
65:
66:
67: 68: 69: 70: 71: 72:
73: public function initialize($action, $stateObject) {
74: if(($status = $this->getConfigData('order_status'))) {
75: $stateObject->setStatus($status);
76: $state = $this->_getAssignedState($status);
77: $stateObject->setState($state);
78: $stateObject->setIsNotified(true);
79: }
80: return $this;
81: }
82:
83:
84:
85:
86: 87: 88: 89: 90: 91: 92: 93: 94: 95:
96: public function isAvailable($quote = null) {
97:
98: $ret = parent::isAvailable($quote);
99: if (!$ret) {
100: self::$_methodInstance = null;
101: return false;
102: }
103: if ($quote === null || !$quote->getShippingAddress() || !$quote->getShippingAddress()->getShippingMethod()) {
104: self::$_methodInstance = null;
105: return false;
106: }
107: $method = $quote->getShippingAddress()->getShippingMethod();
108:
109:
110: $shippingMethodModel = Mage::getModel('shipping/shipping')->getCarrierByCode(substr($method, 0, strpos($method, '_')));
111: if ($shippingMethodModel && ($shippingMethodModel instanceof Eabi_DpdEE_Model_Post) && $shippingMethodModel->isCodEnabled($quote->getShippingAddress())) {
112:
113: if ($this->getConfigData('checkstore')) {
114: if ($quote->getAllItems()) {
115: foreach ($quote->getAllItems() as $item) {
116: if (!$item->getProduct()->isVirtual()) {
117: if ($item->getProduct()->getStockItem()->getQty() < 1) {
118: self::$_methodInstance = null;
119: return false;
120: }
121: }
122: }
123: }
124: }
125: self::$_methodInstance = $shippingMethodModel;
126: } else {
127: self::$_methodInstance = null;
128: return false;
129: }
130: return true;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function authorize(Varien_Object $payment, $amount) {
140:
141: parent::authorize($payment, $amount);
142: return $this;
143: }
144:
145: 146: 147: 148: 149: 150:
151: public function capture(Varien_Object $payment, $amount) {
152:
153: return $this;
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function void(Varien_Object $payment) {
163: return $this;
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function canUseForCurrency($currencyCode) {
173: $this->_currency = $currencyCode;
174: return parent::canUseForCurrency($currencyCode);
175: }
176:
177: 178: 179: 180:
181: public function getTitle() {
182:
183: if (self::$_methodInstance && $this->getQuote()->getShippingAddress() && self::$_methodInstance->getCodFee($this->getQuote()->getShippingAddress()) > 0) {
184: $rawFee = (float) self::$_methodInstance->getCodFee($this->getQuote()->getShippingAddress());
185: if (!$this->_currency) {
186: $this->_currency = Mage::app()->getStore()->getCurrentCurrencyCode();
187: }
188: $baseCurrency = Mage::app()->getStore()->getBaseCurrencyCode();
189:
190: $price = $this->getShippingPrice(Mage::helper('directory')->currencyConvert($rawFee, $baseCurrency, $this->_currency), null);
191:
192: $descriptionText = $this->_getDpdHelper()->__('Will be added to shipping fee');
193: if (self::$_methodInstance->getData('cod_fee_added')) {
194: $descriptionText = $this->_getDpdHelper()->__('Already added to shipping fee');
195: }
196: return parent::getTitle() . ' (' . Mage::helper('core')->currency($price, true, false) . ' ' . $descriptionText . ')';
197: } else {
198: return parent::getTitle();
199: }
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function getShippingPrice($price, $flag) {
209:
210: return Mage::helper('tax')->getShippingPrice($price, $flag, $this->getQuote()->getShippingAddress());
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function assignData($data) {
220: $details = array();
221: if ($data instanceof Varien_Object) {
222: $data = $data->getData();
223: }
224: if ($this->getInfoText()) {
225: $details['info_text'] = $this->getInfoText();
226: }
227: if (count($details)) {
228: $this->getInfoInstance()->setAdditionalData(serialize($details));
229: foreach ($details as $k => $v) {
230: $this->getInfoInstance()->setAdditionalInformation($k, $v);
231: }
232: }
233:
234: return $this;
235: }
236:
237:
238: 239: 240: 241: 242:
243: protected function _getAssignedState($status) {
244: $item = Mage::getResourceModel('sales/order_status_collection')
245: ->joinStates()
246: ->addFieldToFilter('main_table.status', $status)
247: ->getFirstItem();
248:
249: return $item->getState();
250: }
251:
252:
253: 254: 255: 256:
257: protected function _getQuoteItemModel() {
258: return Mage::getModel('sales/quote_item');
259: }
260:
261:
262: 263: 264: 265:
266: protected function _getProductModel() {
267: return Mage::getModel('catalog/product');
268: }
269:
270: 271: 272: 273:
274: public function getInfoText() {
275: return $this->getConfigData('info_text');
276: }
277:
278: 279: 280: 281:
282: protected function _getDpdHelper() {
283: return Mage::helper('eabi_dpdee');
284: }
285:
286: 287: 288: 289: 290:
291: public function getCheckout() {
292: if (empty($this->_checkout)) {
293: if (Mage::app()->getStore()->isAdmin()) {
294: $this->_checkout = Mage::getSingleton('adminhtml/session_quote');
295: } else {
296: $this->_checkout = Mage::getSingleton('checkout/session');
297: }
298: }
299: return $this->_checkout;
300: }
301:
302: 303: 304: 305: 306:
307: public function getQuote() {
308: if (empty($this->_quote)) {
309: $this->_quote = $this->getCheckout()->getQuote();
310: }
311: return $this->_quote;
312: }
313:
314: }
315: