|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | +************************************************************************ |
| 5 | +Copyright [2015] [PagSeguro Internet Ltda.] |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +************************************************************************ |
| 19 | +*/ |
| 20 | + |
| 21 | +use Mage_Payment_Model_Method_Abstract as MethodAbstract; |
| 22 | + |
| 23 | +/** |
| 24 | + * PagSeguro installments model |
| 25 | + */ |
| 26 | +class UOL_PagSeguro_Model_InstallmentsMethod extends MethodAbstract |
| 27 | +{ |
| 28 | + protected $_code = 'pagseguro'; |
| 29 | + /** |
| 30 | + * Construct |
| 31 | + */ |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + $this->helper = Mage::helper('pagseguro'); |
| 35 | + include_once(Mage::getBaseDir('lib') . '/PagSeguroLibrary/PagSeguroLibrary.php'); |
| 36 | + include_once(Mage::getBaseDir('code') . '/community/UOL/PagSeguro/Model/Defines.php'); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Check if installments show is enabled |
| 41 | + */ |
| 42 | + public function enabled() |
| 43 | + { |
| 44 | + return (Mage::getStoreConfig('payment/pagseguro/active') == 1 && |
| 45 | + Mage::getStoreConfig('payment/pagseguro/installments') == 1) ? |
| 46 | + true : |
| 47 | + false; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the bigger installments list returned by the PagSeguro service |
| 52 | + * @param mixed $amount |
| 53 | + * @param string $brand |
| 54 | + * @return array | false |
| 55 | + */ |
| 56 | + public function create($amount, $brand = '') |
| 57 | + { |
| 58 | + $this->helper = Mage::helper('pagseguro'); |
| 59 | + $this->setPagSeguroConfig(); |
| 60 | + $credentials = $this->getCredentialsInformation(); |
| 61 | + |
| 62 | + try { |
| 63 | + $session = PagSeguroSessionService::getSession($credentials); |
| 64 | + $installments = PagSeguroInstallmentService::getInstallments( |
| 65 | + $credentials, |
| 66 | + $session, |
| 67 | + $amount, |
| 68 | + $brand |
| 69 | + ); |
| 70 | + |
| 71 | + $format = $this->output($installments, true); |
| 72 | + return $format['installments']; |
| 73 | + |
| 74 | + } catch (Exception $exception) { |
| 75 | + Mage::log($exception->getMessage()); |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the bigger installments list in the installments |
| 82 | + * @param array $installments |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + private function getMaxInstallment($installments) |
| 86 | + { |
| 87 | + $final = $current = array('brand' => '', 'start' => 0, 'final' => 0, 'quantity' => 0); |
| 88 | + |
| 89 | + foreach ($installments as $key => $installment) { |
| 90 | + if ($current['brand'] !== $installment->getCardBrand()) { |
| 91 | + $current['brand'] = $installment->getCardBrand(); |
| 92 | + $current['start'] = $key; |
| 93 | + } |
| 94 | + $current['quantity'] = $installment->getQuantity(); |
| 95 | + $current['end'] = $key; |
| 96 | + if ($current['quantity'] > $final['quantity']) { |
| 97 | + $final = $current; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return array_slice( |
| 102 | + $installments, |
| 103 | + $final['start'], |
| 104 | + $final['end'] - $final['start'] + 1 |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Set Config's to PagSeguro API |
| 110 | + */ |
| 111 | + private function setPagSeguroConfig() |
| 112 | + { |
| 113 | + $activeLog = $this->getConfigData('log'); |
| 114 | + $charset = $this->getConfigData('charset'); |
| 115 | + |
| 116 | + //Module version |
| 117 | + PagSeguroLibrary::setModuleVersion('magento' . ':' . Mage::helper('pagseguro')->getVersion()); |
| 118 | + |
| 119 | + //CMS version |
| 120 | + PagSeguroLibrary::setCMSVersion('magento' . ':' . Mage::getVersion()); |
| 121 | + |
| 122 | + //Setup Charset |
| 123 | + if ($charset != null and !empty($charset)) { |
| 124 | + PagSeguroConfig::setApplicationCharset($charset); |
| 125 | + } |
| 126 | + |
| 127 | + //Setup Log |
| 128 | + if ($activeLog == 1) { |
| 129 | + $logFile = $this->getConfigData('log_file'); |
| 130 | + |
| 131 | + if (self::checkFile(Mage::getBaseDir() . '/' . $logFile)) { |
| 132 | + PagSeguroConfig::activeLog(Mage::getBaseDir() . '/' . $logFile); |
| 133 | + } else { |
| 134 | + PagSeguroConfig::activeLog(); //Default Log |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Get the access credential |
| 141 | + * @return PagSeguroAccountCredentials |
| 142 | + */ |
| 143 | + public function getCredentialsInformation() |
| 144 | + { |
| 145 | + $email = $this->getConfigData('email'); |
| 146 | + $token = $this->getConfigData('token'); |
| 147 | + $credentials = new PagSeguroAccountCredentials($email, $token); |
| 148 | + |
| 149 | + return $credentials; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * If file not exist, try create. |
| 154 | + * @param string $file |
| 155 | + * @return boolean $fileExist |
| 156 | + */ |
| 157 | + private static function checkFile($file) |
| 158 | + { |
| 159 | + try { |
| 160 | + $f = fopen($file, 'a'); |
| 161 | + $fileExist = true; |
| 162 | + fclose($f); |
| 163 | + } catch (Exception $ex) { |
| 164 | + $fileExist = false; |
| 165 | + Mage::logException($ex); |
| 166 | + } |
| 167 | + |
| 168 | + return $fileExist; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Return a formated output of installments |
| 173 | + * |
| 174 | + * @param array $installments |
| 175 | + * @param bool $maxInstallment |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + private function output($installments, $maxInstallment) |
| 179 | + { |
| 180 | + return ($maxInstallment) ? |
| 181 | + $this->formatOutput($this->getMaxInstallment($installments)) : |
| 182 | + $this->formatOutput($installments); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Format the installment to the be show in the view |
| 187 | + * @param array $installments |
| 188 | + * @return array |
| 189 | + */ |
| 190 | + private function formatOutput($installments) |
| 191 | + { |
| 192 | + $response = $this->getOptions(); |
| 193 | + foreach($installments as $installment) { |
| 194 | + $response['installments'][] = $this->formatInstallments($installment); |
| 195 | + } |
| 196 | + return $response; |
| 197 | + } |
| 198 | + /** |
| 199 | + * Format a installment for output |
| 200 | + * |
| 201 | + * @param $installment |
| 202 | + * @return array |
| 203 | + */ |
| 204 | + private function formatInstallments($installment) |
| 205 | + { |
| 206 | + return array( |
| 207 | + 'quantity' => $installment->getQuantity(), |
| 208 | + 'amount' => $installment->getInstallmentAmount(), |
| 209 | + 'totalAmount' => PagSeguroHelper::decimalFormat($installment->getTotalAmount()), |
| 210 | + 'text' => str_replace('.', ',', $this->getInstallmentText($installment)) |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Mount the text message of the installment |
| 216 | + * @param object $installment |
| 217 | + * @return string |
| 218 | + */ |
| 219 | + private function getInstallmentText($installment) |
| 220 | + { |
| 221 | + return sprintf( |
| 222 | + "%s x de R$ %.2f %s juros", |
| 223 | + $installment->getQuantity(), |
| 224 | + $installment->getInstallmentAmount(), |
| 225 | + $this->getInterestFreeText($installment->getInterestFree())); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Get the string relative to if it is an interest free or not |
| 230 | + * @param string $insterestFree |
| 231 | + * @return string |
| 232 | + */ |
| 233 | + private function getInterestFreeText($insterestFree) |
| 234 | + { |
| 235 | + return ($insterestFree == 'true') ? 'sem' : 'com'; |
| 236 | + } |
| 237 | +} |
0 commit comments