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_Sales_Billing_AgreementController extends Mage_Core_Controller_Front_Action
33: {
34: 35: 36: 37:
38: public function indexAction()
39: {
40: $this->_title($this->__('Billing Agreements'));
41: $this->loadLayout();
42: $this->_initLayoutMessages('customer/session');
43: $this->renderLayout();
44: }
45:
46: 47: 48: 49: 50:
51: public function preDispatch()
52: {
53: parent::preDispatch();
54: if (!$this->getRequest()->isDispatched()) {
55: return;
56: }
57: if (!$this->_getSession()->authenticate($this)) {
58: $this->setFlag('', 'no-dispatch', true);
59: }
60: }
61:
62: 63: 64: 65:
66: public function viewAction()
67: {
68: if (!$agreement = $this->_initAgreement()) {
69: return;
70: }
71: $this->_title($this->__('Billing Agreements'))
72: ->_title($this->__('Billing Agreement # %s', $agreement->getReferenceId()));
73: $this->loadLayout();
74: $this->_initLayoutMessages('customer/session');
75: $navigationBlock = $this->getLayout()->getBlock('customer_account_navigation');
76: if ($navigationBlock) {
77: $navigationBlock->setActive('sales/billing_agreement/');
78: }
79: $this->renderLayout();
80: }
81:
82: 83: 84: 85:
86: public function startWizardAction()
87: {
88: $agreement = Mage::getModel('sales/billing_agreement');
89: $paymentCode = $this->getRequest()->getParam('payment_method');
90: if ($paymentCode) {
91: try {
92: $agreement->setStoreId(Mage::app()->getStore()->getId())
93: ->setMethodCode($paymentCode)
94: ->setReturnUrl(Mage::getUrl('*/*/returnWizard', array('payment_method' => $paymentCode)))
95: ->setCancelUrl(Mage::getUrl('*/*/cancelWizard', array('payment_method' => $paymentCode)));
96:
97: $this->_redirectUrl($agreement->initToken());
98: return $this;
99: } catch (Mage_Core_Exception $e) {
100: $this->_getSession()->addError($e->getMessage());
101: } catch (Exception $e) {
102: Mage::logException($e);
103: $this->_getSession()->addError($this->__('Failed to start billing agreement wizard.'));
104: }
105: }
106: $this->_redirect('*/*/');
107: }
108:
109: 110: 111: 112:
113: public function returnWizardAction()
114: {
115: $agreement = Mage::getModel('sales/billing_agreement');
116: $paymentCode = $this->getRequest()->getParam('payment_method');
117: $token = $this->getRequest()->getParam('token');
118: if ($token && $paymentCode) {
119: try {
120: $agreement->setStoreId(Mage::app()->getStore()->getId())
121: ->setToken($token)
122: ->setMethodCode($paymentCode)
123: ->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
124: ->place();
125: $this->_getSession()->addSuccess(
126: $this->__('The billing agreement "%s" has been created.', $agreement->getReferenceId())
127: );
128: $this->_redirect('*/*/view', array('agreement' => $agreement->getId()));
129: return;
130: } catch (Mage_Core_Exception $e) {
131: $this->_getSession()->addError($e->getMessage());
132: } catch (Exception $e) {
133: Mage::logException($e);
134: $this->_getSession()->addError($this->__('Failed to finish billing agreement wizard.'));
135: }
136: $this->_redirect('*/*/index');
137: }
138: }
139:
140: 141: 142: 143:
144: public function cancelWizardAction()
145: {
146: $this->_redirect('*/*/index');
147: }
148:
149: 150: 151: 152: 153:
154: public function cancelAction()
155: {
156: $agreement = $this->_initAgreement();
157: if ($agreement && $agreement->canCancel()) {
158: try {
159: $agreement->cancel();
160: $this->_getSession()->addNotice($this->__('The billing agreement "%s" has been canceled.', $agreement->getReferenceId()));
161: } catch (Mage_Core_Exception $e) {
162: $this->_getSession()->addError($e->getMessage());
163: } catch (Exception $e) {
164: Mage::logException($e);
165: $this->_getSession()->addError($this->__('Failed to cancel the billing agreement.'));
166: }
167: }
168: $this->_redirect('*/*/view', array('_current' => true));
169: }
170:
171: 172: 173: 174: 175:
176: protected function _initAgreement()
177: {
178: $agreementId = $this->getRequest()->getParam('agreement');
179: if ($agreementId) {
180: $billingAgreement = Mage::getModel('sales/billing_agreement')->load($agreementId);
181: if (!$billingAgreement->getAgreementId()) {
182: $this->_getSession()->addError($this->__('Wrong billing agreement ID specified.'));
183: $this->_redirect('*/*/');
184: return false;
185: }
186: }
187: Mage::register('current_billing_agreement', $billingAgreement);
188: return $billingAgreement;
189: }
190:
191: 192: 193: 194: 195:
196: protected function _getSession()
197: {
198: return Mage::getSingleton('customer/session');
199: }
200: }
201: