PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Braintree/Payments/Block/Creditcard/Management.php

https://gitlab.com/vincent.perdereau/picandparts
PHP | 295 lines | 168 code | 20 blank | 107 comment | 24 complexity | 7a83ece4355ae0610b741af1e88b3a5e MD5 | raw file
  1. <?php
  2. /**
  3. * Braintree Payments Extension
  4. *
  5. * This source file is subject to the Braintree Payment System Agreement (https://www.braintreepayments.com/legal)
  6. *
  7. * DISCLAIMER
  8. * This file will not be supported if it is modified.
  9. *
  10. * @copyright Copyright (c) 2015 Braintree. (https://www.braintreepayments.com/)
  11. */
  12. class Braintree_Payments_Block_Creditcard_Management extends Mage_Core_Block_Template
  13. {
  14. const TYPE_NEW_CART = 'cart';
  15. const TYPE_NEW_CUSTOMER = 'customer';
  16. const TYPE_EDIT = 'edit';
  17. protected $_braintree;
  18. /**
  19. * Internal constructor. Set template, model
  20. */
  21. protected function _construct()
  22. {
  23. parent::_construct();
  24. $this->setTemplate('braintree/creditcard/index.phtml');
  25. $this->_braintree = Mage::getModel('braintree_payments/creditcard');
  26. }
  27. /**
  28. * Returns credit card
  29. *
  30. * @return Braintree_CreditCard
  31. */
  32. public function creditCard()
  33. {
  34. $result = Mage::registry('braintree_result');
  35. if (!empty($result)) {
  36. $token = ($result->success) ? $result->creditCard->token : $result->params['paymentMethodToken'];
  37. } else {
  38. $token = Mage::app()->getRequest()->getParam('token');
  39. }
  40. return $this->_braintree->storedPaymentMethod($token);
  41. }
  42. /**
  43. * Returns value for post param
  44. *
  45. * @param string $index
  46. * @param string $default
  47. * @return string | null
  48. */
  49. public function getPostParam($index, $default='')
  50. {
  51. $result = Mage::registry('braintree_result');
  52. if (!empty($result)) {
  53. $indices = explode('.', $index);
  54. $value = $result->params;
  55. foreach ($indices as $key) {
  56. if (isset($value[$key]) && !is_array($value[$key])) {
  57. return $value[$key];
  58. }
  59. }
  60. }
  61. return $default;
  62. }
  63. /**
  64. * If make default should be shown or not
  65. *
  66. * @return boolean
  67. */
  68. public function canShowMakeDefault()
  69. {
  70. if ($this->getType() == self::TYPE_NEW_CART || $this->getType() == self::TYPE_EDIT) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * Returns page title
  77. *
  78. * @return string
  79. */
  80. public function getTitle()
  81. {
  82. $title = '';
  83. if ($this->getType() == self::TYPE_EDIT) {
  84. $title = 'Edit Credit Card';
  85. } else {
  86. $title = 'Add Credit Card';
  87. }
  88. return $this->__($title);
  89. }
  90. /**
  91. * If cart is edited
  92. *
  93. * @return boolean
  94. */
  95. public function isEditMode()
  96. {
  97. if ($this->getType() == self::TYPE_EDIT) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * Returns html select for country
  104. *
  105. * @param string $name
  106. * @param string $id
  107. * @param string $default
  108. * @param string $title
  109. * @return string
  110. */
  111. public function countrySelect($name, $id, $default = '', $title = 'Country')
  112. {
  113. return Mage::app()->getLayout()->createBlock('braintree_payments/directory_data')
  114. ->getCountryHtmlSelect($default, $name, $id, $title);
  115. }
  116. /**
  117. * Returns url for edit
  118. *
  119. * @param string $token
  120. * @return string
  121. */
  122. public function getEditUrl($token)
  123. {
  124. return $this->getUrl('customer/creditcard/edit', array('token' => $token));
  125. }
  126. /**
  127. * Returns url for delete
  128. *
  129. * @param string $token
  130. * @return string
  131. */
  132. public function getDeleteUrl($token)
  133. {
  134. return $this->getUrl('customer/creditcard/delete', array('token' => $token));
  135. }
  136. /**
  137. * Returns url for add
  138. *
  139. * @return string
  140. */
  141. public function getAddUrl()
  142. {
  143. return $this->getUrl('customer/creditcard/new');
  144. }
  145. /**
  146. * Returns url for add
  147. *
  148. * @return string
  149. */
  150. public function getDeleteConfirmUrl()
  151. {
  152. return $this->getUrl('customer/creditcard/deleteconfirm');
  153. }
  154. /**
  155. * Returns url for edit form
  156. *
  157. * @return string
  158. */
  159. public function getFormAction()
  160. {
  161. if ($this->getType() == self::TYPE_EDIT) {
  162. $url = $this->getUrl(
  163. 'customer/creditcard/update',
  164. array('token' => Mage::app()->getRequest()->getParam('token'))
  165. );
  166. } else {
  167. $url = $this->getUrl('customer/creditcard/save');
  168. }
  169. return $url;
  170. }
  171. /**
  172. * Returns url for edit form
  173. *
  174. * @return string
  175. */
  176. public function getBackUrl()
  177. {
  178. return $this->getUrl('customer/creditcard/index');
  179. }
  180. /**
  181. * Returns region code by name
  182. *
  183. * @param string $region
  184. * @return string
  185. */
  186. public function getRegionIdByName($region)
  187. {
  188. if ($region) {
  189. $collection = Mage::getModel('directory/region')
  190. ->getCollection()
  191. ->addRegionNameFilter($region)
  192. ->removeAllFieldsFromSelect()
  193. ->setPageSize(1)
  194. ->setCurPage(1);
  195. if ($collection->getSize()) {
  196. return $collection->getFirstItem()->getId();
  197. }
  198. }
  199. return '';
  200. }
  201. /**
  202. * Retrieve credit card expire months
  203. *
  204. * @return array
  205. */
  206. public function getCcMonths()
  207. {
  208. $months = $this->getData('cc_months');
  209. if (is_null($months)) {
  210. $months[0] = $this->__('Month');
  211. $months = array_merge($months, Mage::getSingleton('payment/config')->getMonths());
  212. $this->setData('cc_months', $months);
  213. }
  214. return $months;
  215. }
  216. /**
  217. * Retrieve credit card expire years
  218. *
  219. * @return array
  220. */
  221. public function getCcYears()
  222. {
  223. $years = $this->getData('cc_years');
  224. if (is_null($years)) {
  225. $years = Mage::getSingleton('payment/config')->getYears();
  226. $years = array(0=>$this->__('Year'))+$years;
  227. $this->setData('cc_years', $years);
  228. }
  229. return $years;
  230. }
  231. /**
  232. * If CVV for card is required
  233. *
  234. * @return boolean
  235. */
  236. public function hasVerification()
  237. {
  238. return Mage::getStoreConfigFlag('payment/braintree/useccv');
  239. }
  240. /**
  241. * Gets default card data or empty strings if not applicable
  242. *
  243. * @return Varien_Object
  244. */
  245. public function getDefaultCartData()
  246. {
  247. $params = new Varien_Object();
  248. if ($this->isEditMode()) {
  249. $creditCard = $this->creditCard();
  250. $billingAddress = $creditCard->billingAddress;
  251. $params->setExpDate($this->escapeHtml($creditCard->expirationDate));
  252. $params->setIsCCDefault($creditCard->isDefault() ? true : false);
  253. $params->setCartholder($this->escapeHtml($creditCard->cardholderName));
  254. $params->setFirstName($this->escapeHtml($billingAddress->firstName));
  255. $params->setLastName($this->escapeHtml($billingAddress->lastName));
  256. $params->setCompany($this->escapeHtml($billingAddress->company));
  257. $params->setLocality($this->escapeHtml($billingAddress->locality));
  258. $params->setRegion($this->escapeHtml($billingAddress->region));
  259. $params->setPostalCode($this->escapeHtml($billingAddress->postalCode));
  260. $params->setStreetAddress($this->escapeHtml($billingAddress->streetAddress));
  261. $params->setExtendedAddress($this->escapeHtml($billingAddress->extendedAddress));
  262. $params->setCountryCodeAlpha2($this->escapeHtml($billingAddress->countryCodeAlpha2));
  263. $params->setRegionId($this->getRegionIdByName($billingAddress->region));
  264. }
  265. if ($params->getExpDate()) {
  266. list($defaultExpMonth, $defaultExpYear) = explode('/', $this->escapeHtml($params->getExpDate()));
  267. $params->setExpMonth($defaultExpMonth);
  268. $params->setExpYear($defaultExpYear);
  269. } else {
  270. $params->setExpMonth(Mage::helper('braintree_payments')->getTodayMonth());
  271. $params->setExpYear(Mage::helper('braintree_payments')->getTodayYear());
  272. }
  273. return $params;
  274. }
  275. }