necesito armar la siguiente:
curl -v -X POST https://api.sandbox.paypal.com/v1/vault/credit-cards/ \ -H "Content-Type: application/json" \ -H "Authorization: Bearer Access-Token " \ -d '{ "number": "4417119669820331", "type": "visa", "expire_month": 11, "expire_year": 2015, "first_name": "Joe", "last_name": "Shopper", "billing_address": { "line1": "52 N Main St.", "city": "Johnstown", "country_code": "US", "postal_code": "43210", "state": "OH", "phone": "408-334-8890" } }'
He logrado hacer lo siguiente, pero no funciona:
public function addUser($pan, $expdate, $cvv)
{
//1-> Filtro los datos
$pan = preg_replace('/\s+/', '', $pan);
$expdate = preg_replace('/\s+/', '', $expdate);
$cvv = preg_replace('/\s+/', '', $cvv);
//2-> Defino variables necesarias
$url = "https://api.sandbox.paypal.com/v1/vault/credit-cards";
$authorization = "Authorization: Bearer ".$this->token;
//3-> Configuro la petición
$ch = curl_init(); // abrimos la sesión cURL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE); //Indicamos que es POST
//4-> Se arman los parametros para ser enviados
$params = "[number=$pan";
$params.= "&type=\"visa\"";
$params.= "&expire_month=".substr($expdate, 4, 5);
$params.= "&expire_year=".substr($expdate, 0, 4);
$params.= "&first_name=\"\"";
$params.= "&last_name_name=\"\"";
/* $params.= [
"line=\"\"",
"city=\"\"",
"country_code=\"\"",
"postal_code=\"\"",
"phone=\"\"",
x ];
*/
$params.= "[";
$params.="line=\"\"";
$params.="&city=\"\"";
$params.="&country_code=\"\"";
$params.="&postal_code=\"\"";
$params.="&phone=\"\"";
$params.= "]]";
//$params.= "&billing_address"=>array("line1=\"\""."&city=\"\""."country_code=\"\""."&postal_code=\"\""."&state=\"\""."&phone=\"$
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // asigno los parámetros
// body
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization));
// recibimos la respuesta y la guardamos en una variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$remote_server_output = curl_exec ($ch);
// cerramos la sesión cURL
curl_close ($ch);
//Descodifico la respuesta
$res = json_decode($remote_server_output, true);
//Retorno la respuesta
return $res;
}
Actualización Me da el siguiente error usando el metodo http_build_query:
$auxiliar = array("number"=>"$pan", "type"=>"visa", "expire_month"=>substr($expdate, 4, 5),
"expire_year"=>substr($expdate, 0, 4), "first_name"=>"Pepe", "last_name"=>"Pepito", "billing_address"=>array("line1"=>"J2",
"city"=>"Barcelona",
"country_code"=>"ES",
"postal_code"=>"08002",
"state"=>"Cataluya",
"phone"=>"333333333"
));
**
(
[name] => VALIDATION_ERROR
[debug_id] => 6fa659ed4c0e
[message] => Invalid request - see details
[details] => Array
(
[0] => Array
(
[location] => body
[issue] => Unrecognized token 'number': was expecting 'null', 'true', 'false' or NaN line: 1, column: 8
)
)
)
**
La solución es enviar un json y no parámetros. Para eso hay que editar el código de @Jakala y agregar:
1- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($params) , $authorization));
2- eliminar el http_build_query
por $params = json_encode($params);