2013-04-30 22 views
7

Io non sono chiare come mettere questo esempio di codice nella struttura CURL di PHP, in particolare, la maniglia -d.Effettuare il pagamento Paypal tramite API REST in PHP

curl -v https://api.sandbox.paypal.com/v1/payments/payment \ 
-H 'Content-Type:application/json' \ 
-H 'Authorization:Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK' \ 
-d '{ 
    "intent":"sale", 
    "redirect_urls":{ 
    "return_url":"http://<return URL here>", 
    "cancel_url":"http://<cancel URL here>" 
    }, 
    "payer":{ 
    "payment_method":"paypal" 
    }, 
    "transactions":[ 
    { 
     "amount":{ 
     "total":"7.47", 
     "currency":"USD" 
     }, 
     "description":"This is the payment transaction description." 
    } 
    ] 
}' 

Ho usato il seguente chiamata di prova ok, ma non so come estendere che per l'esempio precedente.

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); 

$result = curl_exec($ch); 

if(empty($result))die("Error: No response."); 
else 
{ 
    $json = json_decode($result); 
    print_r($json->access_token); 
} 

risposta

11

Prova in questo modo, -d sono i dati che desideri pubblicare. le intestazioni personalizzate sono impostate con CURLOPT_HTTPHEADER.

$data = '{ 
    "intent":"sale", 
    "redirect_urls":{ 
    "return_url":"http://<return URL here>", 
    "cancel_url":"http://<cancel URL here>" 
    }, 
    "payer":{ 
    "payment_method":"paypal" 
    }, 
    "transactions":[ 
    { 
     "amount":{ 
     "total":"7.47", 
     "currency":"USD" 
     }, 
     "description":"This is the payment transaction description." 
    } 
    ] 
}'; 

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json", 
    "Authorization: Bearer EOjEJigcsRhdOgD7_76lPfrr45UfuI43zzNzTktUk1MK", 
    "Content-length: ".strlen($data)) 
); 

Date un'occhiata alle opzioni qui, http://php.net/manual/en/function.curl-setopt.php Set curl_setopt ($ ch, CURLOPT_HEADER, false); per esempio se non hai bisogno di intestazioni restituite.

+0

Si tratta di una soluzione completa? Non ricevo una risposta. Ho cambiato l'autorizzazione al tipo e accesso al token recuperato da un codice molto simile a quello del primo esempio. – RevNoah

+0

Sta dando errore: nessuna risposta. –

+0

Sta dando solo 0 in risposta ... Si prega di suggerire per il corretto codice – Shalini

Problemi correlati