Created
August 21, 2018 14:06
-
-
Save daviddsp/a6dd8e0d0f6099c46d43aa79b947e1f4 to your computer and use it in GitHub Desktop.
controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers; | |
| use Laravel\Lumen\Routing\Controller as BaseController; | |
| class Controller extends BaseController | |
| { | |
| public static function index($trx_id, $monto) | |
| { | |
| $respuesta = Controller::CrearTransaccion($trx_id, $monto); | |
| return json_encode($respuesta); | |
| /*if ($respuesta->{'token'} != null){ | |
| // Esta es la URL a redirigir al cliente para que continue y efectue el pago en el medio que corresponde | |
| $url = "https://sandbox.puntopagos.com/transaccion/procesar/".$respuesta->{'token'}; | |
| ob_start(); | |
| header("Location: $url"); | |
| ob_flush(); | |
| } | |
| else{ | |
| echo $respuesta->{'error'}; | |
| }*/ | |
| } | |
| public static function Consultar($token, $trx_id, $monto) { | |
| $respuesta = Controller::ConsultarTransaccion($token, $trx_id, $monto); | |
| print_r($respuesta); | |
| } | |
| /** | |
| * Crea la transaccion para después redireccionar a PuntoPagos donde se realizará la selección | |
| * de el medio de pago | |
| * @param sring $trx_id Id de la transaccion asignado por la aplicacion cliente | |
| * @param sring $monto Monto del pago a cobrar a través de PuntoPagos | |
| * @return array Respuesta | |
| */ | |
| public static function CrearTransaccion($trx_id, $monto) | |
| { | |
| //print_r($trx_id);exit; | |
| $funcion = 'transaccion/crear'; | |
| $monto_str = number_format($monto, 2, '.', ''); | |
| $data = '{"trx_id":"'.$trx_id.'","monto":"'.$monto_str.'"}'; | |
| $header_array = Controller::TraerHeader($funcion, $trx_id, $monto_str); | |
| return json_decode(Controller::ExecuteCommand('https://sandbox.puntopagos.com'.'/'.$funcion, $header_array, $data)); | |
| } | |
| /** | |
| * Crea la transaccion ya habiendo seleccionado un medio de pago en la aplicacion, haciendo | |
| * la redireccion correspondiente al medio de pago elegido | |
| * @param sring $trx_id Id de la transaccion asignado por la aplicacion cliente | |
| * @param sring $medio_pago Medio de pago elegido por el usuario, segun codificacion disponible en la documentacion de PuntoPagos | |
| * @param sring $monto Monto del pago a cobrar a través de PuntoPagos | |
| * @return array Respuesta | |
| */ | |
| public static function CrearTransaccionMP($trx_id, $medio_pago, $monto) | |
| { | |
| $funcion = 'transaccion/crear'; | |
| $monto_str = number_format($monto, 2, '.', ''); | |
| $data = '{"trx_id":"'.$trx_id.'","medio_pago":"'.$medio_pago.'","monto":"'.$monto_str.'"}'; | |
| $header_array = Controller::TraerHeader($funcion, $trx_id, $monto_str); | |
| return json_decode(Controller::ExecuteCommand('https://sandbox.puntopagos.com'.'/'.$funcion, $header_array, $data)); | |
| } | |
| /** | |
| * Metodo para consultar el estado de una transacción en particular | |
| * la redireccion correspondiente al medio de pago elegido | |
| * @param sring $token Token de la transaccion | |
| * @param sring $trx_id Id de la transaccion asignado por la aplicacion cliente | |
| * @param sring $monto Monto del pago de la transaccion a consultar | |
| * @return array Respuesta | |
| */ | |
| public static function ConsultarTransaccion($token, $trx_id, $monto){ | |
| $funcion = 'transaccion'; | |
| $header_funcion = 'transaccion/traer'; | |
| $monto_str = number_format($monto, 2, '.', ''); | |
| $header_array = Controller::TraerHeaderConsulta($header_funcion, $token, $trx_id, $monto_str); | |
| return json_decode(Controller::ExecuteCommandGET('https://sandbox.puntopagos.com'.'/'.$funcion.'/'.$token, $header_array)); | |
| } | |
| public static function FirmarMensaje($str) { | |
| $signature = base64_encode(hash_hmac('sha1', $str, env('PUNTOPAGOS_SECRET'), true)); | |
| return "PP ".env('PUNTOPAGOS_KEY').":".$signature; | |
| } | |
| public static function TraerHeader($funcion, $trx_id, $monto_str) | |
| { | |
| $fecha = date("D, d M Y H:i:s", time())." GMT"; | |
| $mensaje = $funcion."\n".$trx_id."\n".$monto_str."\n".$fecha; | |
| $firma = Controller::FirmarMensaje($mensaje); | |
| $header_array = array('Accept: application/json', | |
| "Content-Type: application/json; charset=utf-8", | |
| 'Accept-Charset: utf-8', | |
| 'Fecha: '. $fecha, | |
| 'Autorizacion:'.$firma); | |
| return $header_array; | |
| } | |
| public static function TraerHeaderConsulta($funcion, $token, $trx_id, $monto_str) { | |
| $fecha = date("D, d M Y H:i:s", time())." GMT"; | |
| $mensaje = $funcion."\n".$token."\n".$trx_id."\n".$monto_str."\n".$fecha; | |
| $firma = Controller::FirmarMensaje($mensaje); | |
| $header_array = array('Accept: application/json', | |
| "Content-Type: application/json; charset=utf-8", | |
| 'Accept-Charset: utf-8', | |
| 'Fecha: '. $fecha, | |
| 'Autorizacion:'.$firma); | |
| return $header_array; | |
| } | |
| public static function ExecuteCommand($url, $header_array, $data) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array); | |
| curl_setopt($ch, CURLOPT_URL,$url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); | |
| curl_setopt($ch, CURLOPT_POST, 1); | |
| curl_setopt($ch, CURLOPT_USERAGENT, 'PuntoPagos-curl'); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
| //execute post | |
| $result = curl_exec($ch); | |
| $error = curl_error($ch); | |
| curl_close($ch); | |
| if($result){ | |
| return $result; | |
| } | |
| else{ | |
| return $error; | |
| } | |
| } | |
| public static function ExecuteCommandGET($url, $header_array) { | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array); | |
| curl_setopt($ch, CURLOPT_URL,$url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); | |
| curl_setopt($ch, CURLOPT_USERAGENT, 'PuntoPagos-curl'); | |
| //execute get | |
| $result = curl_exec($ch); | |
| $error = curl_error($ch); | |
| curl_close($ch); | |
| if($result){ | |
| return $result; | |
| } | |
| else{ | |
| return $error; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment