redsys - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of redsys.php

  1. <?php
  2.  
  3. class RedsysAPI{
  4.  
  5.         /******  Array de DatosEntrada ******/
  6.     var $vars_pay = array();
  7.        
  8.         /******  Set parameter ******/
  9.         function setParameter($key,$value){
  10.                 $this->vars_pay[$key]=$value;
  11.         }
  12.  
  13.         /******  Get parameter ******/
  14.         function getParameter($key){
  15.                 return $this->vars_pay[$key];
  16.         }
  17.        
  18.        
  19.         //////////////////////////////////////////////////////////////////////////////////////////////
  20.         //////////////////////////////////////////////////////////////////////////////////////////////
  21.         ////////////                                    FUNCIONES AUXILIARES:                                                    ////////////
  22.         //////////////////////////////////////////////////////////////////////////////////////////////
  23.         //////////////////////////////////////////////////////////////////////////////////////////////
  24.        
  25.  
  26.         /******  3DES Function  ******/
  27.         function encrypt_3DES($message, $key){
  28.                 // Se establece un IV por defecto
  29.                 $bytes = array(0,0,0,0,0,0,0,0); //byte [] IV = {0, 0, 0, 0, 0, 0, 0, 0}
  30.                 $iv = implode(array_map("chr", $bytes)); //PHP 4 >= 4.0.2
  31.  
  32.                 // Se cifra
  33.                 $long = ceil(strlen($message) / 16) * 16;
  34.                 $ciphertext = substr(openssl_encrypt($message . str_repeat("\0", $long - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, $iv), 0, $long);
  35.                
  36.                 return $ciphertext;
  37.         }
  38.  
  39.         /******  Base64 Functions  ******/
  40.         function base64_url_encode($input){
  41.                 return strtr(base64_encode($input), '+/', '-_');
  42.         }
  43.         function encodeBase64($data){
  44.                 $data = base64_encode($data);
  45.                 return $data;
  46.         }
  47.         function base64_url_decode($input){
  48.                 return base64_decode(strtr($input, '-_', '+/'));
  49.         }
  50.         function decodeBase64($data){
  51.                 $data = base64_decode($data);
  52.                 return $data;
  53.         }
  54.  
  55.         /******  MAC Function ******/
  56.         function mac256($ent,$key){
  57.                 $res = hash_hmac('sha256', $ent, $key, true);//(PHP 5 >= 5.1.2)
  58.                 return $res;
  59.         }
  60.  
  61.        
  62.         //////////////////////////////////////////////////////////////////////////////////////////////
  63.         //////////////////////////////////////////////////////////////////////////////////////////////
  64.         ////////////       FUNCIONES PARA LA GENERACIÓN DEL FORMULARIO DE PAGO:                        ////////////
  65.         //////////////////////////////////////////////////////////////////////////////////////////////
  66.         //////////////////////////////////////////////////////////////////////////////////////////////
  67.        
  68.         /******  Obtener Número de pedido ******/
  69.         function getOrder(){
  70.                 $numPedido = "";
  71.                 if(empty($this->vars_pay['DS_MERCHANT_ORDER'])){
  72.                         $numPedido = $this->vars_pay['Ds_Merchant_Order'];
  73.                 } else {
  74.                         $numPedido = $this->vars_pay['DS_MERCHANT_ORDER'];
  75.                 }
  76.                 return $numPedido;
  77.         }
  78.         /******  Convertir Array en Objeto JSON ******/
  79.         function arrayToJson(){
  80.                 $json = json_encode($this->vars_pay); //(PHP 5 >= 5.2.0)
  81.                 return $json;
  82.         }
  83.         function createMerchantParameters(){
  84.                 // Se transforma el array de datos en un objeto Json
  85.                 $json = $this->arrayToJson();
  86.                 // Se codifican los datos Base64
  87.                 return $this->encodeBase64($json);
  88.         }
  89.         function createMerchantSignature($key){
  90.                 // Se decodifica la clave Base64
  91.                 $key = $this->decodeBase64($key);
  92.                 // Se genera el parámetro Ds_MerchantParameters
  93.                 $ent = $this->createMerchantParameters();
  94.                 // Se diversifica la clave con el Número de Pedido
  95.                 $key = $this->encrypt_3DES($this->getOrder(), $key);
  96.                 // MAC256 del parámetro Ds_MerchantParameters
  97.                 $res = $this->mac256($ent, $key);
  98.                 // Se codifican los datos Base64
  99.                 return $this->encodeBase64($res);
  100.         }
  101.        
  102.  
  103.  
  104.         //////////////////////////////////////////////////////////////////////////////////////////////
  105.         //////////////////////////////////////////////////////////////////////////////////////////////
  106.         //////////// FUNCIONES PARA LA RECEPCIÓN DE DATOS DE PAGO (Notif, URLOK y URLKO): ////////////
  107.         //////////////////////////////////////////////////////////////////////////////////////////////
  108.         //////////////////////////////////////////////////////////////////////////////////////////////
  109.  
  110.         /******  Obtener Número de pedido ******/
  111.         function getOrderNotif(){
  112.                 $numPedido = "";
  113.                 if(empty($this->vars_pay['Ds_Order'])){
  114.                         $numPedido = $this->vars_pay['DS_ORDER'];
  115.                 } else {
  116.                         $numPedido = $this->vars_pay['Ds_Order'];
  117.                 }
  118.                 return $numPedido;
  119.         }
  120.         function getOrderNotifSOAP($datos){
  121.                 $posPedidoIni = strrpos($datos, "<Ds_Order>");
  122.                 $tamPedidoIni = strlen("<Ds_Order>");
  123.                 $posPedidoFin = strrpos($datos, "</Ds_Order>");
  124.                 return substr($datos,$posPedidoIni + $tamPedidoIni,$posPedidoFin - ($posPedidoIni + $tamPedidoIni));
  125.         }
  126.         function getRequestNotifSOAP($datos){
  127.                 $posReqIni = strrpos($datos, "<Request");
  128.                 $posReqFin = strrpos($datos, "</Request>");
  129.                 $tamReqFin = strlen("</Request>");
  130.                 return substr($datos,$posReqIni,($posReqFin + $tamReqFin) - $posReqIni);
  131.         }
  132.         function getResponseNotifSOAP($datos){
  133.                 $posReqIni = strrpos($datos, "<Response");
  134.                 $posReqFin = strrpos($datos, "</Response>");
  135.                 $tamReqFin = strlen("</Response>");
  136.                 return substr($datos,$posReqIni,($posReqFin + $tamReqFin) - $posReqIni);
  137.         }
  138.         /******  Convertir String en Array ******/
  139.         function stringToArray($datosDecod){
  140.                 $this->vars_pay = json_decode($datosDecod, true); //(PHP 5 >= 5.2.0)
  141.         }
  142.         function decodeMerchantParameters($datos){
  143.                 // Se decodifican los datos Base64
  144.                 $decodec = $this->base64_url_decode($datos);
  145.                 // Los datos decodificados se pasan al array de datos
  146.                 $this->stringToArray($decodec);
  147.                 return $decodec;      
  148.         }
  149.         function createMerchantSignatureNotif($key, $datos){
  150.                 // Se decodifica la clave Base64
  151.                 $key = $this->decodeBase64($key);
  152.                 // Se decodifican los datos Base64
  153.                 $decodec = $this->base64_url_decode($datos);
  154.                 // Los datos decodificados se pasan al array de datos
  155.                 $this->stringToArray($decodec);
  156.                 // Se diversifica la clave con el Número de Pedido
  157.                 $key = $this->encrypt_3DES($this->getOrderNotif(), $key);
  158.                 // MAC256 del parámetro Ds_Parameters que envía Redsys
  159.                 $res = $this->mac256($datos, $key);
  160.                 // Se codifican los datos Base64
  161.                 return $this->base64_url_encode($res);
  162.         }
  163.         /******  Notificaciones SOAP ENTRADA ******/
  164.         function createMerchantSignatureNotifSOAPRequest($key, $datos){
  165.                 // Se decodifica la clave Base64
  166.                 $key = $this->decodeBase64($key);
  167.                 // Se obtienen los datos del Request
  168.                 $datos = $this->getRequestNotifSOAP($datos);
  169.                 // Se diversifica la clave con el Número de Pedido
  170.                 $key = $this->encrypt_3DES($this->getOrderNotifSOAP($datos), $key);
  171.                 // MAC256 del parámetro Ds_Parameters que envía Redsys
  172.                 $res = $this->mac256($datos, $key);
  173.                 // Se codifican los datos Base64
  174.                 return $this->encodeBase64($res);     
  175.         }
  176.         /******  Notificaciones SOAP SALIDA ******/
  177.         function createMerchantSignatureNotifSOAPResponse($key, $datos, $numPedido){
  178.                 // Se decodifica la clave Base64
  179.                 $key = $this->decodeBase64($key);
  180.                 // Se obtienen los datos del Request
  181.                 $datos = $this->getResponseNotifSOAP($datos);
  182.                 // Se diversifica la clave con el Número de Pedido
  183.                 $key = $this->encrypt_3DES($numPedido, $key);
  184.                 // MAC256 del parámetro Ds_Parameters que envía Redsys
  185.                 $res = $this->mac256($datos, $key);
  186.                 // Se codifican los datos Base64
  187.                 return $this->encodeBase64($res);     
  188.         }
  189. }
  190.  
  191. $miObj = new RedsysAPI;
  192.                
  193. // Valores de entrada
  194. $fuc="327234688";
  195. $terminal="1";
  196. $moneda="978";
  197. $trans="0";
  198. $url="";
  199. $urlOKKO="";
  200. $id="22843564";
  201. $amount="145";
  202.  
  203. // Se Rellenan los campos
  204. $miObj->setParameter("DS_MERCHANT_AMOUNT",$amount);
  205. $miObj->setParameter("DS_MERCHANT_ORDER", $id);
  206. $miObj->setParameter("DS_MERCHANT_MERCHANTCODE",$fuc);
  207. $miObj->setParameter("DS_MERCHANT_CURRENCY",$moneda);
  208. $miObj->setParameter("DS_MERCHANT_TRANSACTIONTYPE",$trans);
  209. $miObj->setParameter("DS_MERCHANT_TERMINAL",$terminal);
  210. $miObj->setParameter("DS_MERCHANT_MERCHANTURL",$url);
  211. $miObj->setParameter("DS_MERCHANT_URLOK",$urlOKKO);            
  212. $miObj->setParameter("DS_MERCHANT_URLKO",$urlOKKO);
  213.  
  214. //Datos de configuración
  215. $version="HMAC_SHA256_V1";
  216. $kc = 'sq7HjrUOBfKmC576ILgskD5srU870gJ7';//Clave recuperada de CANALES
  217. // Se generan los parámetros de la petición
  218. $request = "";
  219. $params = $miObj->createMerchantParameters();
  220. $signature = $miObj->createMerchantSignature($kc);
  221.  
  222. echo $version . "\n" . $params . "\n" . $signature;
  223.  
  224. ?>
File Description
  • redsys
  • PHP Code
  • 09 Apr-2018
  • 7.8 Kb
You can Share it: