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: class Mage_Checkout_Model_Cart_Payment_Api extends Mage_Checkout_Model_Api_Resource
36: {
37:
38: protected function _preparePaymentData($data)
39: {
40: if (!(is_array($data) && is_null($data[0]))) {
41: return array();
42: }
43:
44: return $data;
45: }
46:
47: 48: 49: 50: 51:
52: protected function _canUsePaymentMethod($method, $quote)
53: {
54: if ( !($method->isGateway() || $method->canUseInternal()) ) {
55: return false;
56: }
57:
58: if (!$method->canUseForCountry($quote->getBillingAddress()->getCountry())) {
59: return false;
60: }
61:
62: if (!$method->canUseForCurrency(Mage::app()->getStore($quote->getStoreId())->getBaseCurrencyCode())) {
63: return false;
64: }
65:
66: 67: 68:
69: $total = $quote->getBaseGrandTotal();
70: $minTotal = $method->getConfigData('min_order_total');
71: $maxTotal = $method->getConfigData('max_order_total');
72:
73: if ((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
74: return false;
75: }
76:
77: return true;
78: }
79:
80: protected function _getPaymentMethodAvailableCcTypes($method)
81: {
82: $ccTypes = Mage::getSingleton('payment/config')->getCcTypes();
83: $methodCcTypes = explode(',',$method->getConfigData('cctypes'));
84: foreach ($ccTypes as $code=>$title) {
85: if (!in_array($code, $methodCcTypes)) {
86: unset($ccTypes[$code]);
87: }
88: }
89: if (empty($ccTypes)) {
90: return null;
91: }
92:
93: return $ccTypes;
94: }
95:
96: 97: 98: 99: 100:
101: public function getPaymentMethodsList($quoteId, $store=null)
102: {
103: $quote = $this->_getQuote($quoteId, $store);
104: $store = $quote->getStoreId();
105:
106: $total = $quote->getBaseSubtotal();
107:
108: $methodsResult = array();
109: $methods = Mage::helper('payment')->getStoreMethods($store, $quote);
110: foreach ($methods as $key=>$method) {
111:
112: if ($this->_canUsePaymentMethod($method, $quote)
113: && ($total != 0
114: || $method->getCode() == 'free'
115: || ($quote->hasRecurringItems() && $method->canManageRecurringProfiles()))) {
116: $methodsResult[] =
117: array(
118: "code" => $method->getCode(),
119: "title" => $method->getTitle(),
120: "ccTypes" => $this->_getPaymentMethodAvailableCcTypes($method)
121: );
122: }
123: }
124:
125: return $methodsResult;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function setPaymentMethod($quoteId, $paymentData, $store=null)
135: {
136: $quote = $this->_getQuote($quoteId, $store);
137: $store = $quote->getStoreId();
138:
139: $paymentData = $this->_preparePaymentData($paymentData);
140:
141: if (empty($paymentData)) {
142: $this->_fault("payment_method_empty");
143: }
144:
145: if ($quote->isVirtual()) {
146:
147: if (is_null($quote->getBillingAddress()->getId()) ) {
148: $this->_fault('billing_address_is_not_set');
149: }
150: $quote->getBillingAddress()->setPaymentMethod(isset($paymentData['method']) ? $paymentData['method'] : null);
151: } else {
152:
153: if (is_null($quote->getShippingAddress()->getId()) ) {
154: $this->_fault('shipping_address_is_not_set');
155: }
156: $quote->getShippingAddress()->setPaymentMethod(isset($paymentData['method']) ? $paymentData['method'] : null);
157: }
158:
159: if (!$quote->isVirtual() && $quote->getShippingAddress()) {
160: $quote->getShippingAddress()->setCollectShippingRates(true);
161: }
162:
163: $total = $quote->getBaseSubtotal();
164: $methods = Mage::helper('payment')->getStoreMethods($store, $quote);
165: foreach ($methods as $key=>$method) {
166: if ($method->getCode() == $paymentData['method']) {
167:
168: if (!($this->_canUsePaymentMethod($method, $quote)
169: && ($total != 0
170: || $method->getCode() == 'free'
171: || ($quote->hasRecurringItems() && $method->canManageRecurringProfiles())))) {
172: $this->_fault("method_not_allowed");
173: }
174: }
175: }
176:
177: try {
178: $payment = $quote->getPayment();
179: $payment->importData($paymentData);
180:
181:
182: $quote->setTotalsCollectedFlag(false)
183: ->collectTotals()
184: ->save();
185: } catch (Mage_Core_Exception $e) {
186: $this->_fault('payment_method_is_not_set', $e->getMessage());
187: }
188: return true;
189: }
190:
191: }
192: