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_Paypal_Model_Hostedpro extends Mage_Paypal_Model_Direct
36: {
37: 38: 39: 40: 41:
42: const BM_BUTTON_CODE = 'TOKEN';
43:
44: 45: 46: 47: 48:
49: const BM_BUTTON_TYPE = 'PAYMENT';
50:
51: 52: 53: 54: 55:
56: const BM_BUTTON_METHOD = 'BMCreateButton';
57:
58: 59: 60:
61: protected $_code = Mage_Paypal_Model_Config::METHOD_HOSTEDPRO;
62:
63: protected $_formBlockType = 'paypal/hosted_pro_form';
64: protected $_infoBlockType = 'paypal/hosted_pro_info';
65:
66: 67: 68:
69: protected $_canUseInternal = false;
70: protected $_canUseForMultishipping = false;
71: protected $_canSaveCc = false;
72: protected $_isInitializeNeeded = true;
73:
74: 75: 76: 77: 78: 79:
80: public function getAllowedCcTypes()
81: {
82: return true;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function getMerchantCountry()
92: {
93: return $this->_pro->getConfig()->getMerchantCountry();
94: }
95:
96: 97: 98: 99: 100:
101: public function validate()
102: {
103: return true;
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function initialize($paymentAction, $stateObject)
113: {
114: switch ($paymentAction) {
115: case Mage_Paypal_Model_Config::PAYMENT_ACTION_AUTH:
116: case Mage_Paypal_Model_Config::PAYMENT_ACTION_SALE:
117: $payment = $this->getInfoInstance();
118: $order = $payment->getOrder();
119: $order->setCanSendNewEmailFlag(false);
120: $payment->setAmountAuthorized($order->getTotalDue());
121: $payment->setBaseAmountAuthorized($order->getBaseTotalDue());
122:
123: $this->_setPaymentFormUrl($payment);
124:
125: $stateObject->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT);
126: $stateObject->setStatus('pending_payment');
127: $stateObject->setIsNotified(false);
128: break;
129: default:
130: break;
131: }
132: }
133:
134: 135: 136: 137: 138:
139: protected function _setPaymentFormUrl(Mage_Payment_Model_Info $payment)
140: {
141: $request = $this->_buildFormUrlRequest($payment);
142: $response = $this->_sendFormUrlRequest($request);
143: if ($response) {
144: $payment->setAdditionalInformation('secure_form_url', $response);
145: } else {
146: Mage::throwException('Cannot get secure form URL from PayPal');
147: }
148: }
149:
150: 151: 152: 153: 154: 155:
156: protected function _buildFormUrlRequest(Mage_Payment_Model_Info $payment)
157: {
158: $request = $this->_buildBasicRequest()
159: ->setOrder($payment->getOrder())
160: ->setPaymentMethod($this);
161:
162: return $request;
163: }
164:
165: 166: 167: 168: 169: 170:
171: protected function _sendFormUrlRequest(Mage_Paypal_Model_Hostedpro_Request $request)
172: {
173: $api = $this->_pro->getApi();
174: $response = $api->call(self::BM_BUTTON_METHOD, $request->getRequestData());
175:
176: if (!isset($response['EMAILLINK'])) {
177: return false;
178: }
179: return $response['EMAILLINK'];
180: }
181:
182: 183: 184: 185: 186:
187: protected function _buildBasicRequest()
188: {
189: $request = Mage::getModel('paypal/hostedpro_request');
190: $request->setData(array(
191: 'METHOD' => self::BM_BUTTON_METHOD,
192: 'BUTTONCODE' => self::BM_BUTTON_CODE,
193: 'BUTTONTYPE' => self::BM_BUTTON_TYPE
194: ));
195: return $request;
196: }
197:
198: 199: 200: 201: 202: 203:
204: public function getReturnUrl($storeId = null)
205: {
206: return $this->_getUrl('paypal/hostedpro/return', $storeId);
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function getNotifyUrl($storeId = null)
216: {
217: return $this->_getUrl('paypal/ipn', $storeId, false);
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function getCancelUrl($storeId = null)
227: {
228: return $this->_getUrl('paypal/hostedpro/cancel', $storeId);
229: }
230:
231: 232: 233: 234: 235: 236: 237: 238:
239: protected function _getUrl($path, $storeId, $secure = null)
240: {
241: $store = Mage::app()->getStore($storeId);
242: return Mage::getUrl($path, array(
243: "_store" => $store,
244: "_secure" => is_null($secure) ? $store->isCurrentlySecure() : $secure
245: ));
246: }
247: }
248: