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_Api extends Mage_Checkout_Model_Api_Resource
36: {
37: public function __construct()
38: {
39: $this->_storeIdSessionField = "cart_store_id";
40: $this->_attributesMap['quote'] = array('quote_id' => 'entity_id');
41: $this->_attributesMap['quote_customer'] = array('customer_id' => 'entity_id');
42: $this->_attributesMap['quote_address'] = array('address_id' => 'entity_id');
43: $this->_attributesMap['quote_payment'] = array('payment_id' => 'entity_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function create($store = null)
53: {
54: $storeId = $this->_getStoreId($store);
55:
56: try {
57:
58: $quote = Mage::getModel('sales/quote');
59: $quote->setStoreId($storeId)
60: ->setIsActive(false)
61: ->setIsMultiShipping(false)
62: ->save();
63: } catch (Mage_Core_Exception $e) {
64: $this->_fault('create_quote_fault', $e->getMessage());
65: }
66: return (int) $quote->getId();
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: public function info($quoteId, $store = null)
77: {
78: $quote = $this->_getQuote($quoteId, $store);
79:
80: if ($quote->getGiftMessageId() > 0) {
81: $quote->setGiftMessage(
82: Mage::getSingleton('giftmessage/message')->load($quote->getGiftMessageId())->getMessage()
83: );
84: }
85:
86: $result = $this->_getAttributes($quote, 'quote');
87: $result['shipping_address'] = $this->_getAttributes($quote->getShippingAddress(), 'quote_address');
88: $result['billing_address'] = $this->_getAttributes($quote->getBillingAddress(), 'quote_address');
89: $result['items'] = array();
90:
91: foreach ($quote->getAllItems() as $item) {
92: if ($item->getGiftMessageId() > 0) {
93: $item->setGiftMessage(
94: Mage::getSingleton('giftmessage/message')->load($item->getGiftMessageId())->getMessage()
95: );
96: }
97:
98: $result['items'][] = $this->_getAttributes($item, 'quote_item');
99: }
100:
101: $result['payment'] = $this->_getAttributes($quote->getPayment(), 'quote_payment');
102:
103: return $result;
104: }
105:
106: 107: 108: 109: 110:
111: public function totals($quoteId, $store = null)
112: {
113: $quote = $this->_getQuote($quoteId, $store);
114:
115: $totals = $quote->getTotals();
116:
117: $totalsResult = array();
118: foreach ($totals as $total) {
119: $totalsResult[] = array(
120: "title" => $total->getTitle(),
121: "amount" => $total->getValue()
122: );
123: }
124: return $totalsResult;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134:
135: public function createOrder($quoteId, $store = null, $agreements = null)
136: {
137: $requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds();
138: if (!empty($requiredAgreements)) {
139: $diff = array_diff($agreements, $requiredAgreements);
140: if (!empty($diff)) {
141: $this->_fault('required_agreements_are_not_all');
142: }
143: }
144:
145: $quote = $this->_getQuote($quoteId, $store);
146: if ($quote->getIsMultiShipping()) {
147: $this->_fault('invalid_checkout_type');
148: }
149: if ($quote->getCheckoutMethod() == Mage_Checkout_Model_Api_Resource_Customer::MODE_GUEST
150: && !Mage::helper('checkout')->isAllowedGuestCheckout($quote, $quote->getStoreId())) {
151: $this->_fault('guest_checkout_is_not_enabled');
152: }
153:
154:
155: $customerResource = Mage::getModel("checkout/api_resource_customer");
156: $isNewCustomer = $customerResource->prepareCustomerForQuote($quote);
157:
158: try {
159: $quote->collectTotals();
160:
161: $service = Mage::getModel('sales/service_quote', $quote);
162: $service->submitAll();
163:
164: if ($isNewCustomer) {
165: try {
166: $customerResource->involveNewCustomer($quote);
167: } catch (Exception $e) {
168: Mage::logException($e);
169: }
170: }
171:
172: $order = $service->getOrder();
173: if ($order) {
174: Mage::dispatchEvent('checkout_type_onepage_save_order_after',
175: array('order' => $order, 'quote' => $quote));
176:
177: try {
178: $order->sendNewOrderEmail();
179: } catch (Exception $e) {
180: Mage::logException($e);
181: }
182: }
183:
184: Mage::dispatchEvent(
185: 'checkout_submit_all_after',
186: array('order' => $order, 'quote' => $quote)
187: );
188: } catch (Mage_Core_Exception $e) {
189: $this->_fault('create_order_fault', $e->getMessage());
190: }
191:
192: return $order->getIncrementId();
193: }
194:
195: 196: 197: 198: 199:
200: public function licenseAgreement($quoteId, $store = null)
201: {
202: $quote = $this->_getQuote($quoteId, $store);
203: $storeId = $quote->getStoreId();
204:
205: $agreements = array();
206: if (Mage::getStoreConfigFlag('checkout/options/enable_agreements')) {
207: $agreementsCollection = Mage::getModel('checkout/agreement')->getCollection()
208: ->addStoreFilter($storeId)
209: ->addFieldToFilter('is_active', 1);
210:
211: foreach ($agreementsCollection as $_a) {
212:
213: $agreements[] = $this->_getAttributes($_a, "quote_agreement");
214: }
215: }
216:
217: return $agreements;
218: }
219: }
220: