Skip to content

Instantly share code, notes, and snippets.

@jgermade
Created October 5, 2016 06:11
Show Gist options
  • Save jgermade/741148afbff381a3ab3d4917b361c61d to your computer and use it in GitHub Desktop.
Save jgermade/741148afbff381a3ab3d4917b361c61d to your computer and use it in GitHub Desktop.
<?php
// si tienes instalado php-curl, puedes usar la siguente función:
function authorize_order ($aplazame_private_key, $order_id, $is_production = false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.aplazame.com/orders/$order_id/authorize");
curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// esto es para no incluir la cabecera de la respuesta
curl_setopt($ch, CURLOPT_HEADER, false);
// devuelve la respuesta como string en lugar de imprimirlo directamente
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// cabeceras con modo y credenciales
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.aplazame'.( $is_production ? '' : '.sandbox' ).'.v1+json',
'Authorization: Bearer '.$aplazame_private_key,
'Host: api.aplazame.com'
) );
$data = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = array(
'config' => curl_getinfo($ch),
'status' => $status,
'data' => json_decode($data),
'succeeded' => ( $status >= 200 && $status < 400 )
);
curl_close($ch);
return $result;
}
// ejemplo de uso
$request = json_decode( file_get_contents('php://input') );
$order = getOrder($request->checkout_token);
if( !isset($order) ) { // si el pedido no existe
http_response_code(404);
return;
}
$confirm = authorize_order(
'->AccessToken<-',
$order->id
// omitiendo el último parámetro se usa el modo sandbox por defecto
);
if( $confirm->succeeded ) {
$order->confirm();
http_response_code(204); // 204 = OK, sin devolver datos
} else {
http_response_code(400);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment