skip to Main Content

I am trying to implement PayPal API for creating orders but I’m having a hard time understanding V2 from V1 and how to run the code since the Sandbox uses JSON and I’m using cURL converted to PHP.

https://developer.paypal.com/docs/api/orders/v2/#orders_create

https://incarnate.github.io/curl-to-php/

This is a picture of my PayPal Sandbox You can see that after I hit "Send" I get results in the bottom window which generates the links including the "approve" link which takes the customer to the checkout page to pay.

So how do I echo out this specific link?

I tried doing like

$letssee = $response['data']['approve'];

echo "<a href='$letssee'>Pay Now!</a>";

or

$letssee = $response['links']['approve'];

echo "<a href='$letssee'>Pay Now!</a>";
```php

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
  array (
  "intent" => "CAPTURE",
  "purchase_units" => 
    array (
      "amount" =>
      array (
        "currency_code" => "USD",
        "value" => "100.00"
      )
    )
  )
));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer XXXXX';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

$response = json_decode($result, true);

print_r($response);

$letssee = $response['data']['links'][0];

echo "<a class="lel" href='$letssee'>Pay Now!</a>";

?>

```

EDIT When I format the postfields like so, I get all results but I still cant get the specific link.

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{nn  "intent": "CAPTURE",nn  "purchase_units": [nn    {nn      "amount": {nn        "currency_code": "USD",nn        "value": "100.00"nn      }nn    }nn  ]nn}");

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer xxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

$response = json_decode($result, true);

print_r($response);

$letssee = $response['data']['links'][0];

echo "<a class='lel' href='$letssee'>Pay Now!</a>";

?>

The result..

Array ( [id] => 7T223580KS69XXXX [status] => CREATED [links] => Array ( [0] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => self [method] => GET ) [1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX [rel] => approve [method] => GET ) [2] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => update [method] => PATCH ) [3] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX/capture [rel] => capture [method] => POST ) ) ) 

The link I’m trying to get: [1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX

2

Answers


  1. Array ( [name] => INVALID_REQUEST [message] => Request is not well-formed, syntactically incorrect, or violates schema. [debug_id] => b660cf44ac36b [details] => Array ( [0] => Array ( [field] => /purchase_units [location] => body [issue] => MALFORMED_REQUEST_JSON [description] => The request JSON is not well formed. ) ) [links] => Array ( [0] => Array ( [href] => https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON [rel] => information_link [encType] => application/json ) ) )

    This error is occuring because purchase_units in the request must be an indexed array, the zeroeth element of which then contains a JSON object (an associative array in php)

    So, something like this (not tested):

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
      array (
        "intent" => "CAPTURE",
        "purchase_units" => array (
          array(
            "amount" => array (
              "currency_code" => "USD",
              "value" => "100.00"
            )
          )
        )
      )
    ));  
    
    Login or Signup to reply.
  2. <?php
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{nn  "intent": "CAPTURE",nn  "purchase_units": [nn    {nn      "amount": {nn        "currency_code": "USD",nn        "value": "100.00"nn      }nn    }nn  ]nn}");
    
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: Bearer xxxx';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    
    curl_close($ch);
    
    $response = json_decode($result, true);
    
    print_r($response);
    
    $letssee = $response['data']['links'][11,002.93[https://www.paypal.com/invoice/p/#44L6UCQ45BS7KLNS
    
    > ``
    
    ][1];
    
    echo "<a class='lel' href='$letssee'>Pay Now!</a>";
    
    ?>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search