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: class Mage_Payment_Helper_Data extends Mage_Core_Helper_Abstract
33: {
34: const XML_PATH_PAYMENT_METHODS = 'payment';
35: const XML_PATH_PAYMENT_GROUPS = 'global/payment/groups';
36:
37: 38: 39: 40: 41: 42:
43: public function getMethodInstance($code)
44: {
45: $key = self::XML_PATH_PAYMENT_METHODS.'/'.$code.'/model';
46: $class = Mage::getStoreConfig($key);
47: return Mage::getModel($class);
48: }
49:
50: 51: 52: 53: 54: 55: 56: 57: 58: 59:
60: public function getStoreMethods($store = null, $quote = null)
61: {
62: $res = array();
63: foreach ($this->getPaymentMethods($store) as $code => $methodConfig) {
64: $prefix = self::XML_PATH_PAYMENT_METHODS . '/' . $code . '/';
65: if (!$model = Mage::getStoreConfig($prefix . 'model', $store)) {
66: continue;
67: }
68: $methodInstance = Mage::getModel($model);
69: if (!$methodInstance) {
70: continue;
71: }
72: $methodInstance->setStore($store);
73: if (!$methodInstance->isAvailable($quote)) {
74:
75: continue;
76: }
77: $sortOrder = (int)$methodInstance->getConfigData('sort_order', $store);
78: $methodInstance->setSortOrder($sortOrder);
79: $res[] = $methodInstance;
80: }
81:
82: usort($res, array($this, '_sortMethods'));
83: return $res;
84: }
85:
86: protected function _sortMethods($a, $b)
87: {
88: if (is_object($a)) {
89: return (int)$a->sort_order < (int)$b->sort_order ? -1 : ((int)$a->sort_order > (int)$b->sort_order ? 1 : 0);
90: }
91: return 0;
92: }
93:
94: 95: 96: 97: 98: 99:
100: public function getMethodFormBlock(Mage_Payment_Model_Method_Abstract $method)
101: {
102: $block = false;
103: $blockType = $method->getFormBlockType();
104: if ($this->getLayout()) {
105: $block = $this->getLayout()->createBlock($blockType);
106: $block->setMethod($method);
107: }
108: return $block;
109: }
110:
111: 112: 113: 114: 115: 116:
117: public function getInfoBlock(Mage_Payment_Model_Info $info)
118: {
119: $blockType = $info->getMethodInstance()->getInfoBlockType();
120: if ($this->getLayout()) {
121: $block = $this->getLayout()->createBlock($blockType);
122: }
123: else {
124: $className = Mage::getConfig()->getBlockClassName($blockType);
125: $block = new $className;
126: }
127: $block->setInfo($info);
128: return $block;
129: }
130:
131: 132: 133: 134: 135: 136: 137:
138: public function getBillingAgreementMethods($store = null, $quote = null)
139: {
140: $result = array();
141: foreach ($this->getStoreMethods($store, $quote) as $method) {
142: if ($method->canManageBillingAgreements()) {
143: $result[] = $method;
144: }
145: }
146: return $result;
147: }
148:
149: 150: 151: 152: 153: 154:
155: public function getRecurringProfileMethods($store = null)
156: {
157: $result = array();
158: foreach ($this->getPaymentMethods($store) as $code => $data) {
159: $method = $this->getMethodInstance($code);
160: if ($method && $method->canManageRecurringProfiles()) {
161: $result[] = $method;
162: }
163: }
164: return $result;
165: }
166:
167: 168: 169: 170: 171: 172:
173: public function getPaymentMethods($store = null)
174: {
175: return Mage::getStoreConfig(self::XML_PATH_PAYMENT_METHODS, $store);
176: }
177:
178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
199: public function getPaymentMethodList($sorted = true, $asLabelValue = false, $withGroups = false, $store = null)
200: {
201: $methods = array();
202: $groups = array();
203: $groupRelations = array();
204:
205: foreach ($this->getPaymentMethods($store) as $code => $data) {
206: if ((isset($data['title']))) {
207: $methods[$code] = $data['title'];
208: } else {
209: if ($this->getMethodInstance($code)) {
210: $methods[$code] = $this->getMethodInstance($code)->getConfigData('title', $store);
211: }
212: }
213: if ($asLabelValue && $withGroups && isset($data['group'])) {
214: $groupRelations[$code] = $data['group'];
215: }
216: }
217: if ($asLabelValue && $withGroups) {
218: $groups = Mage::app()->getConfig()->getNode(self::XML_PATH_PAYMENT_GROUPS)->asCanonicalArray();
219: foreach ($groups as $code => $title) {
220: $methods[$code] = $title;
221: }
222: }
223: if ($sorted) {
224: asort($methods);
225: }
226: if ($asLabelValue) {
227: $labelValues = array();
228: foreach ($methods as $code => $title) {
229: $labelValues[$code] = array();
230: }
231: foreach ($methods as $code => $title) {
232: if (isset($groups[$code])) {
233: $labelValues[$code]['label'] = $title;
234: } elseif (isset($groupRelations[$code])) {
235: unset($labelValues[$code]);
236: $labelValues[$groupRelations[$code]]['value'][$code] = array('value' => $code, 'label' => $title);
237: } else {
238: $labelValues[$code] = array('value' => $code, 'label' => $title);
239: }
240: }
241: return $labelValues;
242: }
243:
244: return $methods;
245: }
246:
247: 248: 249: 250: 251:
252: public function getAllBillingAgreementMethods()
253: {
254: $result = array();
255: $interface = 'Mage_Payment_Model_Billing_Agreement_MethodInterface';
256: foreach ($this->getPaymentMethods() as $code => $data) {
257: if (!isset($data['model'])) {
258: continue;
259: }
260: $method = Mage::app()->getConfig()->getModelClassName($data['model']);
261: if (in_array($interface, class_implements($method))) {
262: $result[$code] = $data['title'];
263: }
264: }
265: return $result;
266: }
267:
268: 269: 270: 271: 272: 273:
274: public function isZeroSubTotal($store = null)
275: {
276: return Mage::getStoreConfig(Mage_Payment_Model_Method_Free::XML_PATH_PAYMENT_FREE_ACTIVE, $store);
277: }
278:
279: 280: 281: 282: 283: 284:
285: public function getZeroSubTotalOrderStatus($store = null)
286: {
287: return Mage::getStoreConfig(Mage_Payment_Model_Method_Free::XML_PATH_PAYMENT_FREE_ORDER_STATUS, $store);
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function getZeroSubTotalPaymentAutomaticInvoice($store = null)
297: {
298: return Mage::getStoreConfig(Mage_Payment_Model_Method_Free::XML_PATH_PAYMENT_FREE_PAYMENT_ACTION, $store);
299: }
300: }
301: