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_Request extends Varien_Object
36: {
37: 38: 39: 40: 41:
42: protected $_order;
43:
44: 45: 46: 47: 48:
49: protected $_paymentMethod;
50:
51: 52: 53: 54: 55:
56: protected $_buttonVarFormat = 'L_BUTTONVAR%d';
57:
58: 59: 60: 61: 62:
63: protected $_notButtonVars = array (
64: 'METHOD', 'BUTTONCODE', 'BUTTONTYPE');
65:
66: 67: 68: 69: 70:
71: public function getRequestData()
72: {
73: $requestData = array();
74: if (!empty($this->_data)) {
75:
76:
77: $i = 0;
78: foreach ($this->_data as $key => $value) {
79: if (in_array($key, $this->_notButtonVars)) {
80: $requestData[$key] = $value;
81: } else {
82: $varKey = sprintf($this->_buttonVarFormat, $i);
83: $requestData[$varKey] = $key . '=' . $value;
84: $i++;
85: }
86: }
87: }
88:
89: return $requestData;
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function setPaymentMethod($paymentMethod)
99: {
100: $this->_paymentMethod = $paymentMethod;
101: $requestData = $this->_getPaymentData($paymentMethod);
102: $this->addData($requestData);
103:
104: return $this;
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function setOrder($order)
114: {
115: $this->_order = $order;
116: $requestData = $this->_getOrderData($order);
117: $this->addData($requestData);
118:
119: return $this;
120: }
121:
122: 123: 124: 125: 126: 127:
128: protected function _getPaymentData(Mage_Paypal_Model_Hostedpro $paymentMethod)
129: {
130: $request = array(
131: 'paymentaction' => strtolower($paymentMethod->getConfigData('payment_action')),
132: 'notify_url' => $paymentMethod->getNotifyUrl(),
133: 'cancel_return' => $paymentMethod->getCancelUrl(),
134: 'return' => $paymentMethod->getReturnUrl(),
135: 'lc' => $paymentMethod->getMerchantCountry(),
136:
137: 'template' => 'templateD',
138: 'showBillingAddress' => 'false',
139: 'showShippingAddress' => 'false',
140: 'showBillingEmail' => 'false',
141: 'showBillingPhone' => 'false',
142: 'showCustomerName' => 'false',
143: 'showCardInfo' => 'true',
144: 'showHostedThankyouPage'=> 'false'
145: );
146:
147: return $request;
148: }
149:
150: 151: 152: 153: 154: 155:
156: protected function _getOrderData(Mage_Sales_Model_Order $order)
157: {
158: $request = array(
159: 'subtotal' => $this->_formatPrice(
160: $this->_formatPrice($order->getPayment()->getBaseAmountAuthorized()) -
161: $this->_formatPrice($order->getBaseTaxAmount()) -
162: $this->_formatPrice($order->getBaseShippingAmount())
163: ),
164: 'tax' => $this->_formatPrice($order->getBaseTaxAmount()),
165: 'shipping' => $this->_formatPrice($order->getBaseShippingAmount()),
166: 'invoice' => $order->getIncrementId(),
167: 'address_override' => 'false',
168: 'currency_code' => $order->getBaseCurrencyCode(),
169: 'buyer_email' => $order->getCustomerEmail()
170: );
171:
172:
173: if ($billingAddress = $order->getBillingAddress()) {
174: $request = array_merge($request, $this->_getBillingAddress($billingAddress));
175: }
176:
177:
178: if ($shippingAddress = $order->getShippingAddress()) {
179: $request = array_merge($request, $this->_getShippingAddress($shippingAddress));
180: }
181:
182: return $request;
183: }
184:
185: 186: 187: 188: 189: 190:
191: protected function _getShippingAddress(Varien_Object $address)
192: {
193: $request = array(
194: 'first_name'=> $address->getFirstname(),
195: 'last_name' => $address->getLastname(),
196: 'city' => $address->getCity(),
197: 'state' => $address->getRegion(),
198: 'zip' => $address->getPostcode(),
199: 'country' => $address->getCountry(),
200: );
201:
202:
203: $street = Mage::helper('customer/address')
204: ->convertStreetLines($address->getStreet(), 2);
205:
206: $request['address1'] = isset($street[0]) ? $street[0]: '';
207: $request['address2'] = isset($street[1]) ? $street[1]: '';
208:
209: return $request;
210: }
211:
212: 213: 214: 215: 216: 217:
218: protected function _getBillingAddress(Varien_Object $address)
219: {
220: $request = array(
221: 'billing_first_name'=> $address->getFirstname(),
222: 'billing_last_name' => $address->getLastname(),
223: 'billing_city' => $address->getCity(),
224: 'billing_state' => $address->getRegion(),
225: 'billing_zip' => $address->getPostcode(),
226: 'billing_country' => $address->getCountry(),
227: );
228:
229:
230: $street = Mage::helper('customer/address')
231: ->convertStreetLines($address->getStreet(), 2);
232:
233: $request['billing_address1'] = isset($street[0]) ? $street[0]: '';
234: $request['billing_address2'] = isset($street[1]) ? $street[1]: '';
235:
236: return $request;
237: }
238:
239: 240: 241: 242: 243: 244:
245: protected function _formatPrice($string)
246: {
247: return sprintf('%.2F', $string);
248: }
249: }
250: