|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Gento <desarrollo@gento.com.ar>, Inc. All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Gento\Redsys\Controller\Response; |
| 8 | + |
| 9 | +use Gento\Redsys\Model\Payment\Redsys; |
| 10 | +use Gento\Redsys\Model\Payment\RedsysFactory; |
| 11 | +use Gento\Redsys\Model\Response; |
| 12 | +use Gento\Redsys\Model\ResponseFactory; |
| 13 | +use Gento\Redsys\Model\Tool; |
| 14 | +use Gento\Redsys\Model\ToolFactory; |
| 15 | +use Magento\Framework\App\Action\Action; |
| 16 | +use Magento\Framework\App\Action\Context; |
| 17 | +use Magento\Framework\Event\Manager; |
| 18 | +use Magento\Framework\Exception\LocalizedException; |
| 19 | +use Magento\Sales\Model\Order; |
| 20 | +use Magento\Sales\Model\Order\Payment; |
| 21 | +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class Callback extends Action |
| 25 | +{ |
| 26 | + |
| 27 | + protected $_order = null; |
| 28 | + |
| 29 | + /** @var LoggerInterface */ |
| 30 | + protected $logger; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + Context $context, |
| 34 | + CollectionFactory $orderCollectionFactory, |
| 35 | + ToolFactory $paymentTool, |
| 36 | + ResponseFactory $responseFactory, |
| 37 | + Manager $eventManager, |
| 38 | + LoggerInterface $logger, |
| 39 | + RedsysFactory $redsysPaymentFactory |
| 40 | + ) { |
| 41 | + $this->eventManager = $eventManager; |
| 42 | + $this->logger = $logger; |
| 43 | + $this->paymentTool = $paymentTool->create(); |
| 44 | + $this->redsysPaymentFactory = $redsysPaymentFactory; |
| 45 | + $this->responseFactory = $responseFactory; |
| 46 | + $this->_orderCollectionFactory = $orderCollectionFactory; |
| 47 | + parent::__construct($context); |
| 48 | + } |
| 49 | + |
| 50 | + public function execute() |
| 51 | + { |
| 52 | + /** @todo Si no es POST deberia redireccionar o morir en un mensaje */ |
| 53 | + |
| 54 | + /** @var Order */ |
| 55 | + $order = $this->_loadOrder(); |
| 56 | + |
| 57 | + /** @var Payment */ |
| 58 | + $payment = $order->getPayment(); |
| 59 | + |
| 60 | + /** @var Redsys */ |
| 61 | + $method = $payment->getMethodInstance(); |
| 62 | + |
| 63 | + $postParameters = $this->getRequest()->getPost('Ds_MerchantParameters'); |
| 64 | + $signature = $this->getRequest()->getPost('Ds_Signature'); |
| 65 | + $response_data = $this->_processParams($order, $postParameters, $signature); |
| 66 | + |
| 67 | + $params = $response_data->getData(); |
| 68 | + |
| 69 | + if (!$response_data->checkAmount()) { |
| 70 | + $response_data->setAutorizado(false); |
| 71 | + } |
| 72 | + |
| 73 | + $this->paymentTool->setRedsysResponse($payment, $params); |
| 74 | + |
| 75 | + if ($response_data->isAutorizado()) { |
| 76 | + $event = 'gento_redsys_orden_autorizada'; |
| 77 | + if ($response_data->getField('DS_TRANSACTIONTYPE') == Tool::TRANSACTION_TYPE_PREAUTHORIZATION) { |
| 78 | + $event = 'gento_redsys_orden_preautorizada'; |
| 79 | + } |
| 80 | + $this->eventManager->dispatch($event, [ |
| 81 | + 'response_data' => $response_data, |
| 82 | + 'order' => $order, |
| 83 | + ]); |
| 84 | + } elseif ($response_data->isProcesado()) { |
| 85 | + $enviarMail = (bool) $method->getSendEmailOrderConfirmation(); |
| 86 | + |
| 87 | + $comentario = __('La orden fue cancelada desde Redsys'); |
| 88 | + $comentario .= '<br />' . __($response_data->getComentario(), $response_data->getCodigo()); |
| 89 | + $comentario .= '<br />' . __('Codigo: %1', $response_data->getCodigo()); |
| 90 | + |
| 91 | + $order->cancel(); |
| 92 | + $order->addStatusToHistory('redsys_ko_payment', $comentario, true); |
| 93 | + $order->save(); |
| 94 | + |
| 95 | + if ($enviarMail) { |
| 96 | + $order->sendOrderUpdateEmail(true, $comentario); |
| 97 | + } |
| 98 | + } else { |
| 99 | + $mensaje = __('La orden #%1 no ha podido ser procesada', $order->getRealOrderId()); |
| 100 | + } |
| 101 | + return $mensaje; |
| 102 | + } |
| 103 | + |
| 104 | + protected function _loadOrder() |
| 105 | + { |
| 106 | + $protect_code = $this->_getProtectCode(); |
| 107 | + |
| 108 | + if ($this->_order == null) { |
| 109 | + if (!$protect_code) { |
| 110 | + throw new LocalizedException(__('Codigo protegido invalido')); |
| 111 | + } |
| 112 | + $this->_order = $this->_orderCollectionFactory->create() |
| 113 | + ->addAttributeToSelect('*') |
| 114 | + ->addFieldToFilter('protect_code', $protect_code) |
| 115 | + ->getLastItem(); |
| 116 | + } |
| 117 | + |
| 118 | + if ($this->_order == null || !$this->_order->getId()) { |
| 119 | + throw new LocalizedException(__('Orden invalida con codigo %1', $protect_code)); |
| 120 | + } |
| 121 | + return $this->_order; |
| 122 | + } |
| 123 | + |
| 124 | + protected function _getProtectCode() |
| 125 | + { |
| 126 | + return $this->getRequest()->getParam(Redsys::PROTECT_CODE_QUERY_PARAM); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return Response |
| 131 | + */ |
| 132 | + public function _processParams(Order $order, $postParameters, $signature) |
| 133 | + { |
| 134 | + /** @var Response */ |
| 135 | + $response_data = $this->responseFactory->create(); |
| 136 | + $response_data->setOrder($order) |
| 137 | + ->setMerchantParameters($postParameters) |
| 138 | + ->setSignature($signature); |
| 139 | + |
| 140 | + if (!$response_data->firmaValida()) { |
| 141 | + $response_data->setData([ |
| 142 | + 'procesado' => true, |
| 143 | + 'autorizado' => false, |
| 144 | + 'comentario' => __('Firma invalida'), |
| 145 | + ]); |
| 146 | + } else { |
| 147 | + $comment = null; |
| 148 | + $autorizada = true; |
| 149 | + $response = $response_data->getField('Ds_Response'); |
| 150 | + $payMethod = $response_data->getField('Ds_PayMethod'); |
| 151 | + $monto = ((int) $response_data->getField('Ds_Amount')) / 100; |
| 152 | + |
| 153 | + if ($response >= '0000' && $response <= '0099') { |
| 154 | + $comment = __('Transaccion autorizada para pagos y preautorizaciones (codigo: %1)'); |
| 155 | + } elseif ($response == '0900') { |
| 156 | + $comment = __('Transaccion autorizada para devoluciones y confirmaciones (codigo: %1)'); |
| 157 | + } elseif ($response == '0930') { |
| 158 | + if ($payMethod == 'R') { |
| 159 | + $comment = __('Pago realizado por Transferencia bancaria'); |
| 160 | + } else { |
| 161 | + $comment = __('Pago realizado por Domiciliacion bancaria'); |
| 162 | + } |
| 163 | + } else { |
| 164 | + $autorizada = false; |
| 165 | + $comment = $this->comentarioReponse($response, $payMethod); |
| 166 | + } |
| 167 | + |
| 168 | + $response_data->addData([ |
| 169 | + 'procesado' => true, |
| 170 | + 'autorizado' => $autorizada, |
| 171 | + 'comentario' => $comment, |
| 172 | + 'codigo' => $response, |
| 173 | + 'monto' => $monto, |
| 174 | + ]); |
| 175 | + } |
| 176 | + return $response_data; |
| 177 | + } |
| 178 | + |
| 179 | + public function comentarioReponse($Ds_Response, $Ds_pay_method = '') |
| 180 | + { |
| 181 | + switch ($Ds_Response) { |
| 182 | + case '101': |
| 183 | + return __('Tarjeta caducada'); |
| 184 | + case '102': |
| 185 | + return __('Tarjeta en excepcion transitoria o bajo sospecha de fraude'); |
| 186 | + case '104': |
| 187 | + return __('Operacion no permitida para esa tarjeta o terminal'); |
| 188 | + case '106': |
| 189 | + return __('Intentos de PIN excedidos'); |
| 190 | + case '116': |
| 191 | + return __('Disponible insuficiente'); |
| 192 | + case '118': |
| 193 | + return __('Tarjeta no registrada'); |
| 194 | + case '125': |
| 195 | + return __('Tarjeta no efectiva'); |
| 196 | + case '129': |
| 197 | + return __('Codigo de seguridad (CVV2/CVC2) incorrecto'); |
| 198 | + case '180': |
| 199 | + return __('Tarjeta ajena al servicio'); |
| 200 | + case '184': |
| 201 | + return __('Error en la autenticacion del titular'); |
| 202 | + case '190': |
| 203 | + return __('Denegacion sin especificar Motivo'); |
| 204 | + case '191': |
| 205 | + return __('Fecha de caducidad erronea'); |
| 206 | + case '202': |
| 207 | + return __('Tarjeta en excepcion transitoria o bajo sospecha de fraude con retirada de tarjeta'); |
| 208 | + case '904': |
| 209 | + return __('Comercio no registrado en FUC'); |
| 210 | + case '909': |
| 211 | + return __('Error de sistema'); |
| 212 | + case '912': |
| 213 | + case '9912': |
| 214 | + return __('Emisor no disponible'); |
| 215 | + case '0930': |
| 216 | + if ($Ds_pay_method == 'R') { |
| 217 | + return __('Realizado por Transferencia bancaria'); |
| 218 | + } else { |
| 219 | + return __('Realizado por Domiciliacion bancaria'); |
| 220 | + } |
| 221 | + case '950': |
| 222 | + return __('Operacion de devolucion no permitida'); |
| 223 | + case '9064': |
| 224 | + return __('Numero de posiciones de la tarjeta incorrecto'); |
| 225 | + case '9078': |
| 226 | + return __('Tipo de operacion no permitida para esa tarjeta'); |
| 227 | + case '9093': |
| 228 | + return __('Tarjeta no existente'); |
| 229 | + case '9218': |
| 230 | + return __('El comercio no permite op. seguras por entrada /operaciones'); |
| 231 | + case '9997': |
| 232 | + return __('Se esta procesando otra transaccion en SIS con la misma tarjeta'); |
| 233 | + case '9998': |
| 234 | + return __('Operacion en proceso de solicitud de datos de tarjeta'); |
| 235 | + case '9999': |
| 236 | + return __('Operacion que ha sido redirigida al emisor a autenticar'); |
| 237 | + case '9253': |
| 238 | + return __('Tarjeta no cumple el check-digit'); |
| 239 | + case '9256': |
| 240 | + return __('El comercio no puede realizar preautorizaciones'); |
| 241 | + case '9257': |
| 242 | + return __('Esta tarjeta no permite operativa de preautorizaciones'); |
| 243 | + case '9261': |
| 244 | + return __('Operacion detenida por superar el control de restricciones en la entrada al SIS'); |
| 245 | + case '9913': |
| 246 | + return __('Error en la confirmacion que el comercio envia al TPV Virtual'); |
| 247 | + case '9914': |
| 248 | + return __('Confirmacion "KO" del comercio'); |
| 249 | + case '9928': |
| 250 | + return __('Anulacion de autorizacion en diferido realizada por el SIS (proceso batch)'); |
| 251 | + case '9929': |
| 252 | + return __('Anulacion de autorizacion en diferido realizada por el comercio'); |
| 253 | + case '9104': |
| 254 | + return __('Comercio con "titular seguro" y titular sin clave de compra segura'); |
| 255 | + case '9915': |
| 256 | + return __('A peticion del usuario se ha cancelado el pago'); |
| 257 | + case '9094': |
| 258 | + return __('Rechazo servidores internacionales'); |
| 259 | + case '944': |
| 260 | + return __('Sesion Incorrecta'); |
| 261 | + case '913': |
| 262 | + return __('Pedido repetido'); |
| 263 | + } |
| 264 | + return __('Transaccion denegada'); |
| 265 | + } |
| 266 | +} |
0 commit comments