Question

Does anyone know who to translate this to PHP

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {accessToken}' \
-d '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://example.com/your_redirect_url/",
    "cancel_url":"http://example.com/your_cancel_url/"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      }
    }
  ]
}'

I really can't wrap my head around this.

ThankS!

Was it helpful?

Solution

I assume you're running this from a script or command-line.

-H refers to a Header option and -d refers to the data portion of the command.

I translated the JSON String you have into a PHP Associative Array to transform back into JSON String (safety!).

PHP Code:

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer": array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(
        array(
            "amount" => array({
                "total" => "7.47",
                "currency" => "USD"
            )
        )
    )
);
$data_string = json_encode($data);

$ch = curl_init( "https://api.sandbox.paypal.com/v1/payments/payment" );
curl_setopt_array( $ch, array(
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string)),
        'Authorization: Bearer ' . $accessToken      //Need to set $accessToken
    ),
    CURLOPT_POSTFIELDS => $data_string,
    CURLOPT_RETURNTRANSFER => true
));

$result = curl_exec( $ch );   //Make it all happen and store response

cURL manual page and PHP cURL Functions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top