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_GoogleCheckout_RedirectController extends Mage_Core_Controller_Front_Action
33: {
34: 35: 36: 37: 38:
39: protected function _getApi ()
40: {
41: $session = Mage::getSingleton('checkout/session');
42: $api = Mage::getModel('googlecheckout/api');
43:
44: $quote = $session->getQuote();
45:
46: if (!$quote->hasItems()) {
47: $this->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
48: $api->setError(true);
49: }
50:
51: $storeQuote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
52: $storeQuote->merge($quote);
53: $storeQuote
54: ->setItemsCount($quote->getItemsCount())
55: ->setItemsQty($quote->getItemsQty())
56: ->setChangedFlag(false);
57: $storeQuote->save();
58:
59: $baseCurrency = $quote->getBaseCurrencyCode();
60: $currency = Mage::app()->getStore($quote->getStoreId())->getBaseCurrency();
61:
62:
63: 64: 65: 66:
67: if ($quote->isVirtual()) {
68: $quote->getBillingAddress()->setPaymentMethod('googlecheckout');
69: } else {
70: $quote->getShippingAddress()->setPaymentMethod('googlecheckout');
71: }
72:
73: $quote->collectTotals()->save();
74:
75: if (!$api->getError()) {
76: $api = $api->setAnalyticsData($this->getRequest()->getPost('analyticsdata'))
77: ->checkout($quote);
78:
79: $response = $api->getResponse();
80: if ($api->getError()) {
81: Mage::getSingleton('checkout/session')->addError($api->getError());
82: } else {
83: $quote->setIsActive(false)->save();
84: $session->replaceQuote($storeQuote);
85: Mage::getModel('checkout/cart')->init()->save();
86: if (Mage::getStoreConfigFlag('google/checkout/hide_cart_contents')) {
87: $session->setGoogleCheckoutQuoteId($session->getQuoteId());
88: $session->setQuoteId(null);
89: }
90: }
91: }
92: return $api;
93: }
94:
95: public function checkoutAction()
96: {
97: $session = Mage::getSingleton('checkout/session');
98: Mage::dispatchEvent('googlecheckout_checkout_before', array('quote' => $session->getQuote()));
99: $api = $this->_getApi();
100:
101: if ($api->getError()) {
102: $url = Mage::getUrl('checkout/cart');
103: } else {
104: $url = $api->getRedirectUrl();
105: }
106: $this->getResponse()->setRedirect($url);
107: }
108:
109: 110: 111: 112:
113: public function redirectAction()
114: {
115: $api = $this->_getApi();
116:
117: if ($api->getError()) {
118: $this->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
119: return;
120: } else {
121: $url = $api->getRedirectUrl();
122: $this->loadLayout();
123: $this->getLayout()->getBlock('googlecheckout_redirect')->setRedirectUrl($url);
124: $this->renderLayout();
125: }
126: }
127:
128: public function cartAction()
129: {
130: if (Mage::getStoreConfigFlag('google/checkout/hide_cart_contents')) {
131: $session = Mage::getSingleton('checkout/session');
132: if ($session->getQuoteId()) {
133: $session->getQuote()->delete();
134: }
135: $session->setQuoteId($session->getGoogleCheckoutQuoteId());
136: $session->setGoogleCheckoutQuoteId(null);
137: }
138:
139: $this->_redirect('checkout/cart');
140: }
141:
142: public function continueAction()
143: {
144: $session = Mage::getSingleton('checkout/session');
145:
146: if ($quoteId = $session->getGoogleCheckoutQuoteId()) {
147: $quote = Mage::getModel('sales/quote')->load($quoteId)
148: ->setIsActive(false)->save();
149: }
150: $session->clear();
151:
152: if (Mage::getStoreConfigFlag('google/checkout/hide_cart_contents')) {
153: $session->setGoogleCheckoutQuoteId(null);
154: }
155:
156: $url = Mage::getStoreConfig('google/checkout/continue_shopping_url');
157: if (empty($url)) {
158: $this->_redirect('');
159: } elseif (substr($url, 0, 4) === 'http') {
160: $this->getResponse()->setRedirect($url);
161: } else {
162: $this->_redirect($url);
163: }
164: }
165:
166: 167: 168: 169:
170: public function redirectLogin()
171: {
172: $this->setFlag('', 'no-dispatch', true);
173: $this->getResponse()->setRedirect(
174: Mage::helper('core/url')->addRequestParam(
175: Mage::helper('customer')->getLoginUrl(),
176: array('context' => 'checkout')
177: )
178: );
179: }
180:
181: }
182: