Niffe33
Niffe33

Reputation: 43

Status on paypal is still APPROVED

I have a problem with payment integrationwith PayPal. I am using REST API and this my code for creating an order:

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sandbox.paypal.com/v2/checkout/orders",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "reference_id": "PUHF",
      "amount": {
        "currency_code": "PLN",
        "value": "100.00"
      }
    }
  ],
  "application_context": {
    "return_url": "http://www.mywebside.com",
    "cancel_url": ""
  }
}',
  CURLOPT_HTTPHEADER => array(
    'accept: application/json',
    'accept-language: en_US',
    'authorization: Bearer '.$access_token.'',
    'content-type: application/json'
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

I work in a sandbox environment. I go to the payment page and transfer virtual money. When it redirects me to my site, then I check the order status. Status has value = "APPROVED" not "COMPLETED" and money is also not credited to the account. What it depends on?

Upvotes: 0

Views: 802

Answers (1)

Preston PHX
Preston PHX

Reputation: 30379

You need two API calls, one to 'Set Up Transaction' and create the order, followed by one to 'Capture Transaction' after the approval, as documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/

If you do not capture an order, it will stay in an approved state.

For the best user experience, do not use any redirects. At all. Keep your site loaded in the background, and present the user with a modern in-context login for the approval. Here is the UI for that: https://developer.paypal.com/demo/checkout/#/pattern/server

Upvotes: 1

Related Questions