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: abstract class Mage_Paypal_Model_Api_Abstract extends Varien_Object
33: {
34: 35: 36: 37:
38: protected $_config = null;
39:
40: 41: 42: 43:
44: protected $_globalMap = array();
45:
46: 47: 48: 49: 50:
51: protected $_exportToRequestFilters = array();
52:
53: 54: 55: 56: 57:
58: protected $_importFromRequestFilters = array();
59:
60: 61: 62: 63:
64: protected $_lineItemExportItemsFormat = array();
65: protected $_lineItemExportItemsFilters = array();
66: protected $_lineItemTotalExportMap = array();
67:
68: 69: 70: 71: 72:
73: protected $_cart = null;
74:
75: 76: 77: 78:
79: protected $_shippingOptionsExportItemsFormat = array();
80:
81: 82: 83: 84: 85:
86: protected $_recurringPaymentProfiles = array();
87:
88: 89: 90: 91: 92:
93: protected $_debugReplacePrivateDataKeys = array();
94:
95: 96: 97: 98: 99:
100: public function getApiUsername()
101: {
102: return $this->_config->apiUsername;
103: }
104:
105: 106: 107: 108: 109:
110: public function getApiPassword()
111: {
112: return $this->_config->apiPassword;
113: }
114:
115: 116: 117: 118: 119:
120: public function getApiSignature()
121: {
122: return $this->_config->apiSignature;
123: }
124:
125: 126: 127: 128: 129:
130: public function getApiCertificate()
131: {
132: return $this->_config->getApiCertificate();
133: }
134:
135: 136: 137: 138: 139:
140: public function getBuildNotationCode()
141: {
142: return $this->_config->getBuildNotationCode();
143: }
144:
145: 146: 147: 148: 149:
150: public function getUseProxy()
151: {
152: return $this->_getDataOrConfig('use_proxy', false);
153: }
154:
155: 156: 157: 158: 159:
160: public function getProxyHost()
161: {
162: return $this->_getDataOrConfig('proxy_host', '127.0.0.1');
163: }
164:
165: 166: 167: 168: 169:
170: public function getProxyPort()
171: {
172: return $this->_getDataOrConfig('proxy_port', '808');
173: }
174:
175: 176: 177: 178: 179:
180: public function getDebug()
181: {
182: return $this->getDebugFlag();
183: }
184:
185: 186: 187: 188: 189:
190: public function getPageStyle()
191: {
192: return $this->_getDataOrConfig('page_style');
193: }
194:
195: 196: 197: 198: 199:
200: public function getHdrimg()
201: {
202: return $this->_getDataOrConfig('paypal_hdrimg');
203: }
204:
205: 206: 207: 208: 209:
210: public function getHdrbordercolor()
211: {
212: return $this->_getDataOrConfig('paypal_hdrbordercolor');
213: }
214:
215: 216: 217: 218: 219:
220: public function getHdrbackcolor()
221: {
222: return $this->_getDataOrConfig('paypal_hdrbackcolor');
223: }
224:
225: 226: 227: 228: 229:
230: public function getPayflowcolor()
231: {
232: return $this->_getDataOrConfig('paypal_payflowcolor');
233: }
234:
235: 236: 237: 238: 239:
240: public function getPaymentAction()
241: {
242: return $this->_getDataOrConfig('payment_action');
243: }
244:
245: 246: 247:
248: public function getBusinessAccount()
249: {
250: return $this->_getDataOrConfig('business_account');
251: }
252:
253: 254: 255: 256: 257: 258: 259:
260: public function &import($to, array $publicMap = array())
261: {
262: return Varien_Object_Mapper::accumulateByMap(array($this, 'getDataUsingMethod'), $to, $publicMap);
263: }
264:
265: 266: 267: 268: 269: 270: 271:
272: public function export($from, array $publicMap = array())
273: {
274: Varien_Object_Mapper::accumulateByMap($from, array($this, 'setDataUsingMethod'), $publicMap);
275: return $this;
276: }
277:
278: 279: 280: 281: 282: 283:
284: public function setPaypalCart(Mage_Paypal_Model_Cart $cart)
285: {
286: $this->_cart = $cart;
287: return $this;
288: }
289:
290: 291: 292: 293: 294:
295: public function setConfigObject(Mage_Paypal_Model_Config $config)
296: {
297: $this->_config = $config;
298: return $this;
299: }
300:
301: 302: 303: 304: 305:
306: public function getLocaleCode()
307: {
308: return Mage::app()->getLocale()->getLocaleCode();
309: }
310:
311: 312: 313:
314: public function getFraudManagementFiltersEnabled()
315: {
316: return 1;
317: }
318:
319: 320: 321: 322: 323: 324:
325: public function addRecurringPaymentProfiles(array $items)
326: {
327: if ($items) {
328: $this->_recurringPaymentProfiles = $items;
329: }
330: return $this;
331: }
332:
333: 334: 335: 336: 337: 338: 339:
340: protected function &_exportToRequest(array $privateRequestMap, array $request = array())
341: {
342: $map = array();
343: foreach ($privateRequestMap as $key) {
344: if (isset($this->_globalMap[$key])) {
345: $map[$this->_globalMap[$key]] = $key;
346: }
347: }
348: $result = Varien_Object_Mapper::accumulateByMap(array($this, 'getDataUsingMethod'), $request, $map);
349: foreach ($privateRequestMap as $key) {
350: if (isset($this->_exportToRequestFilters[$key]) && isset($result[$key])) {
351: $callback = $this->_exportToRequestFilters[$key];
352: $privateKey = $result[$key];
353: $publicKey = $map[$this->_globalMap[$key]];
354: $result[$key] = call_user_func(array($this, $callback), $privateKey, $publicKey);
355: }
356: }
357: return $result;
358: }
359:
360: 361: 362: 363: 364: 365:
366: protected function _importFromResponse(array $privateResponseMap, array $response)
367: {
368: $map = array();
369: foreach ($privateResponseMap as $key) {
370: if (isset($this->_globalMap[$key])) {
371: $map[$key] = $this->_globalMap[$key];
372: }
373: if (isset($response[$key]) && isset($this->_importFromRequestFilters[$key])) {
374: $callback = $this->_importFromRequestFilters[$key];
375: $response[$key] = call_user_func(array($this, $callback), $response[$key], $key, $map[$key]);
376: }
377: }
378: Varien_Object_Mapper::accumulateByMap($response, array($this, 'setDataUsingMethod'), $map);
379: }
380:
381: 382: 383: 384: 385: 386: 387: 388: 389:
390: protected function _exportLineItems(array &$request, $i = 0)
391: {
392: if (!$this->_cart) {
393: return;
394: }
395:
396:
397: if ($this->_lineItemTotalExportMap) {
398: foreach ($this->_cart->getTotals() as $key => $total) {
399: if (isset($this->_lineItemTotalExportMap[$key])) {
400: $privateKey = $this->_lineItemTotalExportMap[$key];
401: $request[$privateKey] = $this->_filterAmount($total);
402: }
403: }
404: }
405:
406:
407: $items = $this->_cart->getItems();
408: if (empty($items) || !$this->getIsLineItemsEnabled()) {
409: return;
410: }
411: $result = null;
412: foreach ($items as $item) {
413: foreach ($this->_lineItemExportItemsFormat as $publicKey => $privateFormat) {
414: $result = true;
415: $value = $item->getDataUsingMethod($publicKey);
416: if (isset($this->_lineItemExportItemsFilters[$publicKey])) {
417: $callback = $this->_lineItemExportItemsFilters[$publicKey];
418: $value = call_user_func(array($this, $callback), $value);
419: }
420: if (is_float($value)) {
421: $value = $this->_filterAmount($value);
422: }
423: $request[sprintf($privateFormat, $i)] = $value;
424: }
425: $i++;
426: }
427: return $result;
428: }
429:
430: 431: 432: 433: 434: 435: 436: 437:
438: protected function _exportShippingOptions(array &$request, $i = 0)
439: {
440: $options = $this->getShippingOptions();
441: if (empty($options)) {
442: return false;
443: }
444: foreach ($options as $option) {
445: foreach ($this->_shippingOptionsExportItemsFormat as $publicKey => $privateFormat) {
446: $value = $option->getDataUsingMethod($publicKey);
447: if (is_float($value)) {
448: $value = $this->_filterAmount($value);
449: }
450: if (is_bool($value)) {
451: $value = $this->_filterBool($value);
452: }
453: $request[sprintf($privateFormat, $i)] = $value;
454: }
455: $i++;
456: }
457: return true;
458: }
459:
460: 461: 462: 463: 464:
465: protected function _filterAmount($value)
466: {
467: return sprintf('%.2F', $value);
468: }
469:
470: 471: 472: 473: 474: 475:
476: protected function _filterBool($value)
477: {
478: return ($value) ? 'true' : 'false';
479: }
480:
481: 482: 483: 484: 485: 486:
487: protected function _filterInt($value)
488: {
489: return (int)$value;
490: }
491:
492: 493: 494: 495: 496: 497: 498:
499: protected function _getDataOrConfig($key, $default = null)
500: {
501: if ($this->hasData($key)) {
502: return $this->getData($key);
503: }
504: return $this->_config->$key ? $this->_config->$key : $default;
505: }
506:
507:
508: 509: 510: 511: 512: 513:
514: protected function _lookupRegionCodeFromAddress(Varien_Object $address)
515: {
516: if ($regionId = $address->getData('region_id')) {
517: $region = Mage::getModel('directory/region')->load($regionId);
518: if ($region->getId()) {
519: return $region->getCode();
520: }
521: }
522: return '';
523: }
524:
525: 526: 527: 528: 529: 530: 531:
532: protected function _importStreetFromAddress(Varien_Object $address, array &$to)
533: {
534: $keys = func_get_args(); array_shift($keys); array_shift($keys);
535: $street = $address->getStreet();
536: if (!$keys || !$street || !is_array($street)) {
537: return;
538: }
539:
540: $street = Mage::helper('customer/address')
541: ->convertStreetLines($address->getStreet(), count($keys));
542:
543: $i = 0;
544: foreach ($keys as $key) {
545: $to[$key] = isset($street[$i]) ? $street[$i]: '';
546: $i++;
547: }
548: }
549:
550: 551: 552: 553: 554: 555:
556: protected function _buildQuery($request)
557: {
558: return http_build_query($request);
559: }
560:
561: 562: 563: 564: 565: 566: 567:
568: protected function _filterQty($value)
569: {
570: return intval($value);
571: }
572:
573: 574: 575: 576: 577:
578: protected function _debug($debugData)
579: {
580: if ($this->getDebugFlag()) {
581: Mage::getModel('core/log_adapter', 'payment_' . $this->_config->getMethodCode() . '.log')
582: ->setFilterDataKeys($this->_debugReplacePrivateDataKeys)
583: ->log($debugData);
584: }
585: }
586:
587: 588: 589: 590: 591:
592: public function getDebugFlag()
593: {
594: return $this->_config->debug;
595: }
596:
597: 598: 599: 600: 601:
602: public function getUseCertAuthentication()
603: {
604: return (bool)$this->_config->apiAuthentication;
605: }
606: }
607: