PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Paypal/Model/Api/Abstract.php

https://bitbucket.org/jit_bec/shopifine
PHP | 606 lines | 270 code | 52 blank | 284 comment | 24 complexity | c76fab7463b5e121ee9e1dc13d5f1ad9 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Paypal
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Abstract class for Paypal API wrappers
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. abstract class Mage_Paypal_Model_Api_Abstract extends Varien_Object
  32. {
  33. /**
  34. * Config instance
  35. * @var Mage_Paypal_Model_Config
  36. */
  37. protected $_config = null;
  38. /**
  39. * Global private to public interface map
  40. * @var array
  41. */
  42. protected $_globalMap = array();
  43. /**
  44. * Filter callbacks for exporting $this data to API call
  45. *
  46. * @var array
  47. */
  48. protected $_exportToRequestFilters = array();
  49. /**
  50. * Filter callbacks for importing API result to $this data
  51. *
  52. * @var array
  53. */
  54. protected $_importFromRequestFilters = array();
  55. /**
  56. * Line items export to request mapping settings
  57. * @var array
  58. */
  59. protected $_lineItemExportItemsFormat = array();
  60. protected $_lineItemExportItemsFilters = array();
  61. protected $_lineItemTotalExportMap = array();
  62. /**
  63. * PayPal shopping cart instance
  64. *
  65. * @var Mage_Paypal_Model_Cart
  66. */
  67. protected $_cart = null;
  68. /**
  69. * Shipping options export to request mapping settings
  70. * @var array
  71. */
  72. protected $_shippingOptionsExportItemsFormat = array();
  73. /**
  74. * Imported recurring profiles array
  75. *
  76. * @var array
  77. */
  78. protected $_recurringPaymentProfiles = array();
  79. /**
  80. * Fields that should be replaced in debug with '***'
  81. *
  82. * @var array
  83. */
  84. protected $_debugReplacePrivateDataKeys = array();
  85. /**
  86. * Return Paypal Api user name based on config data
  87. *
  88. * @return string
  89. */
  90. public function getApiUsername()
  91. {
  92. return $this->_config->apiUsername;
  93. }
  94. /**
  95. * Return Paypal Api password based on config data
  96. *
  97. * @return string
  98. */
  99. public function getApiPassword()
  100. {
  101. return $this->_config->apiPassword;
  102. }
  103. /**
  104. * Return Paypal Api signature based on config data
  105. *
  106. * @return string
  107. */
  108. public function getApiSignature()
  109. {
  110. return $this->_config->apiSignature;
  111. }
  112. /**
  113. * Return Paypal Api certificate based on config data
  114. *
  115. * @return string
  116. */
  117. public function getApiCertificate()
  118. {
  119. return $this->_config->getApiCertificate();
  120. }
  121. /**
  122. * BN code getter
  123. *
  124. * @return string
  125. */
  126. public function getBuildNotationCode()
  127. {
  128. return $this->_config->getBuildNotationCode();
  129. }
  130. /**
  131. * Return Paypal Api proxy status based on config data
  132. *
  133. * @return bool
  134. */
  135. public function getUseProxy()
  136. {
  137. return $this->_getDataOrConfig('use_proxy', false);
  138. }
  139. /**
  140. * Return Paypal Api proxy host based on config data
  141. *
  142. * @return string
  143. */
  144. public function getProxyHost()
  145. {
  146. return $this->_getDataOrConfig('proxy_host', '127.0.0.1');
  147. }
  148. /**
  149. * Return Paypal Api proxy port based on config data
  150. *
  151. * @return string
  152. */
  153. public function getProxyPort()
  154. {
  155. return $this->_getDataOrConfig('proxy_port', '808');
  156. }
  157. /**
  158. * @deprecated after 1.4.1.0
  159. *
  160. * @return bool
  161. */
  162. public function getDebug()
  163. {
  164. return $this->getDebugFlag();
  165. }
  166. /**
  167. * PayPal page CSS getter
  168. *
  169. * @return string
  170. */
  171. public function getPageStyle()
  172. {
  173. return $this->_getDataOrConfig('page_style');
  174. }
  175. /**
  176. * PayPal page header image URL getter
  177. *
  178. * @return string
  179. */
  180. public function getHdrimg()
  181. {
  182. return $this->_getDataOrConfig('paypal_hdrimg');
  183. }
  184. /**
  185. * PayPal page header border color getter
  186. *
  187. * @return string
  188. */
  189. public function getHdrbordercolor()
  190. {
  191. return $this->_getDataOrConfig('paypal_hdrbordercolor');
  192. }
  193. /**
  194. * PayPal page header background color getter
  195. *
  196. * @return string
  197. */
  198. public function getHdrbackcolor()
  199. {
  200. return $this->_getDataOrConfig('paypal_hdrbackcolor');
  201. }
  202. /**
  203. * PayPal page "payflow color" (?) getter
  204. *
  205. * @return string
  206. */
  207. public function getPayflowcolor()
  208. {
  209. return $this->_getDataOrConfig('paypal_payflowcolor');
  210. }
  211. /**
  212. * Payment action getter
  213. *
  214. * @return string
  215. */
  216. public function getPaymentAction()
  217. {
  218. return $this->_getDataOrConfig('payment_action');
  219. }
  220. /**
  221. * PayPal merchant email getter
  222. */
  223. public function getBusinessAccount()
  224. {
  225. return $this->_getDataOrConfig('business_account');
  226. }
  227. /**
  228. * Import $this public data to specified object or array
  229. *
  230. * @param array|Varien_Object $to
  231. * @param array $publicMap
  232. * @return array|Varien_Object
  233. */
  234. public function &import($to, array $publicMap = array())
  235. {
  236. return Varien_Object_Mapper::accumulateByMap(array($this, 'getDataUsingMethod'), $to, $publicMap);
  237. }
  238. /**
  239. * Export $this public data from specified object or array
  240. *
  241. * @param array|Varien_Object $from
  242. * @param array $publicMap
  243. * @return Mage_Paypal_Model_Api_Abstract
  244. */
  245. public function export($from, array $publicMap = array())
  246. {
  247. Varien_Object_Mapper::accumulateByMap($from, array($this, 'setDataUsingMethod'), $publicMap);
  248. return $this;
  249. }
  250. /**
  251. * Set PayPal cart instance
  252. *
  253. * @param Mage_Paypal_Model_Cart $cart
  254. * @return Mage_Paypal_Model_Api_Abstract
  255. */
  256. public function setPaypalCart(Mage_Paypal_Model_Cart $cart)
  257. {
  258. $this->_cart = $cart;
  259. return $this;
  260. }
  261. /**
  262. * Config instance setter
  263. * @param Mage_Paypal_Model_Config $config
  264. * @return Mage_Paypal_Model_Api_Abstract
  265. */
  266. public function setConfigObject(Mage_Paypal_Model_Config $config)
  267. {
  268. $this->_config = $config;
  269. return $this;
  270. }
  271. /**
  272. * Current locale code getter
  273. *
  274. * @return string
  275. */
  276. public function getLocaleCode()
  277. {
  278. return Mage::app()->getLocale()->getLocaleCode();
  279. }
  280. /**
  281. * Always take into accoun
  282. */
  283. public function getFraudManagementFiltersEnabled()
  284. {
  285. return 1;
  286. }
  287. /**
  288. * Set recurring profiles
  289. *
  290. * @param array $items
  291. * @return Mage_Paypal_Model_Api_Abstract
  292. */
  293. public function addRecurringPaymentProfiles(array $items)
  294. {
  295. if ($items) {
  296. $this->_recurringPaymentProfiles = $items;
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Export $this public data to private request array
  302. *
  303. * @param array $internalRequestMap
  304. * @param array $request
  305. * @return array
  306. */
  307. protected function &_exportToRequest(array $privateRequestMap, array $request = array())
  308. {
  309. $map = array();
  310. foreach ($privateRequestMap as $key) {
  311. if (isset($this->_globalMap[$key])) {
  312. $map[$this->_globalMap[$key]] = $key;
  313. }
  314. }
  315. $result = Varien_Object_Mapper::accumulateByMap(array($this, 'getDataUsingMethod'), $request, $map);
  316. foreach ($privateRequestMap as $key) {
  317. if (isset($this->_exportToRequestFilters[$key]) && isset($result[$key])) {
  318. $callback = $this->_exportToRequestFilters[$key];
  319. $privateKey = $result[$key];
  320. $publicKey = $map[$this->_globalMap[$key]];
  321. $result[$key] = call_user_func(array($this, $callback), $privateKey, $publicKey);
  322. }
  323. }
  324. return $result;
  325. }
  326. /**
  327. * Import $this public data from a private response array
  328. *
  329. * @param array $privateResponseMap
  330. * @param array $response
  331. */
  332. protected function _importFromResponse(array $privateResponseMap, array $response)
  333. {
  334. $map = array();
  335. foreach ($privateResponseMap as $key) {
  336. if (isset($this->_globalMap[$key])) {
  337. $map[$key] = $this->_globalMap[$key];
  338. }
  339. if (isset($response[$key]) && isset($this->_importFromRequestFilters[$key])) {
  340. $callback = $this->_importFromRequestFilters[$key];
  341. $response[$key] = call_user_func(array($this, $callback), $response[$key], $key, $map[$key]);
  342. }
  343. }
  344. Varien_Object_Mapper::accumulateByMap($response, array($this, 'setDataUsingMethod'), $map);
  345. }
  346. /**
  347. * Prepare line items request
  348. *
  349. * Returns true if there were line items added
  350. *
  351. * @param array &$request
  352. * @param int $i
  353. * @return true|bool
  354. */
  355. protected function _exportLineItems(array &$request, $i = 0)
  356. {
  357. if (!$this->_cart) {
  358. return;
  359. }
  360. // always add cart totals, even if line items are not requested
  361. if ($this->_lineItemTotalExportMap) {
  362. foreach ($this->_cart->getTotals() as $key => $total) {
  363. if (isset($this->_lineItemTotalExportMap[$key])) { // !empty($total)
  364. $privateKey = $this->_lineItemTotalExportMap[$key];
  365. $request[$privateKey] = $this->_filterAmount($total);
  366. }
  367. }
  368. }
  369. // add cart line items
  370. $items = $this->_cart->getItems();
  371. if (empty($items) || !$this->getIsLineItemsEnabled()) {
  372. return;
  373. }
  374. $result = null;
  375. foreach ($items as $item) {
  376. foreach ($this->_lineItemExportItemsFormat as $publicKey => $privateFormat) {
  377. $result = true;
  378. $value = $item->getDataUsingMethod($publicKey);
  379. if (isset($this->_lineItemExportItemsFilters[$publicKey])) {
  380. $callback = $this->_lineItemExportItemsFilters[$publicKey];
  381. $value = call_user_func(array($this, $callback), $value);
  382. }
  383. if (is_float($value)) {
  384. $value = $this->_filterAmount($value);
  385. }
  386. $request[sprintf($privateFormat, $i)] = $value;
  387. }
  388. $i++;
  389. }
  390. return $result;
  391. }
  392. /**
  393. * Prepare shipping options request
  394. * Returns false if there are no shipping options
  395. *
  396. * @param array &$request
  397. * @param int $i
  398. * @return bool
  399. */
  400. protected function _exportShippingOptions(array &$request, $i = 0)
  401. {
  402. $options = $this->getShippingOptions();
  403. if (empty($options)) {
  404. return false;
  405. }
  406. foreach ($options as $option) {
  407. foreach ($this->_shippingOptionsExportItemsFormat as $publicKey => $privateFormat) {
  408. $value = $option->getDataUsingMethod($publicKey);
  409. if (is_float($value)) {
  410. $value = $this->_filterAmount($value);
  411. }
  412. if (is_bool($value)) {
  413. $value = $this->_filterBool($value);
  414. }
  415. $request[sprintf($privateFormat, $i)] = $value;
  416. }
  417. $i++;
  418. }
  419. return true;
  420. }
  421. /**
  422. * Filter amounts in API calls
  423. * @param float|string $value
  424. * @return string
  425. */
  426. protected function _filterAmount($value)
  427. {
  428. return sprintf('%.2F', $value);
  429. }
  430. /**
  431. * Filter boolean values in API calls
  432. *
  433. * @param mixed $value
  434. * @return string
  435. */
  436. protected function _filterBool($value)
  437. {
  438. return ($value) ? 'true' : 'false';
  439. }
  440. /**
  441. * Filter int values in API calls
  442. *
  443. * @param mixed $value
  444. * @return int
  445. */
  446. protected function _filterInt($value)
  447. {
  448. return (int)$value;
  449. }
  450. /**
  451. * Unified getter that looks in data or falls back to config
  452. *
  453. * @param string $key
  454. * @param mixed $default
  455. * @return mixed
  456. */
  457. protected function _getDataOrConfig($key, $default = null)
  458. {
  459. if ($this->hasData($key)) {
  460. return $this->getData($key);
  461. }
  462. return $this->_config->$key ? $this->_config->$key : $default;
  463. }
  464. /**
  465. * region_id workaround: PayPal requires state code, try to find one in the address
  466. *
  467. * @param Varien_Object $address
  468. * @return string
  469. */
  470. protected function _lookupRegionCodeFromAddress(Varien_Object $address)
  471. {
  472. if ($regionId = $address->getData('region_id')) {
  473. $region = Mage::getModel('directory/region')->load($regionId);
  474. if ($region->getId()) {
  475. return $region->getCode();
  476. }
  477. }
  478. return '';
  479. }
  480. /**
  481. * Street address workaround: divides address lines into parts by specified keys
  482. * (keys should go as 3rd, 4th[...] parameters)
  483. *
  484. * @param Varien_Object $address
  485. * @param array $request
  486. */
  487. protected function _importStreetFromAddress(Varien_Object $address, array &$to)
  488. {
  489. $keys = func_get_args(); array_shift($keys); array_shift($keys);
  490. $street = $address->getStreet();
  491. if (!$keys || !$street || !is_array($street)) {
  492. return;
  493. }
  494. $street = Mage::helper('customer/address')
  495. ->convertStreetLines($address->getStreet(), count($keys));
  496. $i = 0;
  497. foreach ($keys as $key) {
  498. $to[$key] = isset($street[$i]) ? $street[$i]: '';
  499. $i++;
  500. }
  501. }
  502. /**
  503. * Build query string from request
  504. *
  505. * @param array $request
  506. * @return string
  507. */
  508. protected function _buildQuery($request)
  509. {
  510. return http_build_query($request);
  511. }
  512. /**
  513. * Filter qty in API calls
  514. * Paypal note: The value for quantity must be a positive integer. Null, zero, or negative numbers are not allowed.
  515. *
  516. * @param float|string|int $value
  517. * @return string
  518. */
  519. protected function _filterQty($value)
  520. {
  521. return intval($value);
  522. }
  523. /**
  524. * Log debug data to file
  525. *
  526. * @param mixed $debugData
  527. */
  528. protected function _debug($debugData)
  529. {
  530. if ($this->getDebugFlag()) {
  531. Mage::getModel('core/log_adapter', 'payment_' . $this->_config->getMethodCode() . '.log')
  532. ->setFilterDataKeys($this->_debugReplacePrivateDataKeys)
  533. ->log($debugData);
  534. }
  535. }
  536. /**
  537. * Define if debugging is enabled
  538. *
  539. * @return bool
  540. */
  541. public function getDebugFlag()
  542. {
  543. return $this->_config->debug;
  544. }
  545. /**
  546. * Check whether API certificate authentication should be used
  547. *
  548. * @return bool
  549. */
  550. public function getUseCertAuthentication()
  551. {
  552. return (bool)$this->_config->apiAuthentication;
  553. }
  554. }