3

I am trying to integrate Paypal with PHP, but right now I am getting the following message:

"The token passed in was not found in the system"

Here is my code. Where I am wrong?

Here is my code for getting the token, which I am getting successfully.

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sandbox.paypal.com/v1/oauth2/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_USERPWD => $PAYPAL_CLIENT_ID.":".$PAYPAL_SECRET,
    CURLOPT_POSTFIELDS => "grant_type=client_credentials",
    CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Accept-Language: en_US"
    ),
));
$result= curl_exec($curl);
$array=json_decode($result, true); 
$token=$array['access_token'];

And here I am trying to integrate v1/payments API:

$ch = curl_init();
$data = '{
"intent": "sale",
    "payer": {
    "payment_method": "credit_card",
    "payer_info": {
    "email": "'.$email.'",
    "shipping_address": {
    "recipient_name": "'.$fname.' '.$lname.'",
    "line1": "'.$address.'",
    "city": "'.$city.'",
    "country_code": "'.$country.'",
    "postal_code": "'.$zip.'",
    "state": "'.$state.'",
    "phone": "'.$phone.'"
},
"billing_address": {
"line1": "'.$address.'",
"city": "'.$city.'",
"state": "'.$state.'",
"postal_code": "'.$zip.'",
"country_code": "'.$country.'",
"phone": "'.$phone.'"
}
},
"funding_instruments": [{
"credit_card": {
"number": "'. $ccnum.'",
"type": "'.$credit_card_type.'",
"expire_month": "'.$ccmo.'",
"expire_year": "'.$ccyr.'",
"cvv2": "'.$cvv2_number.'",
"first_name": "'.$first_name.'",
"last_name": "'.$last_name.'",
"billing_address": {
"line1": "'.$address.'",
"city": "'.$city.'",
"country_code": "'.$country.'",
"postal_code": "'.$zip.'",
"state": "'.$state.'",
"phone": "'.$phone.'"
            }
        }
    }]
},
"transactions": [{
"amount": {
"total": "'.$cost.'",
"currency": "GBP"
},
"description": "This is member subscription payment at Thecodehelpers.",
"item_list": {
"shipping_address": {
"recipient_name": "'.$fname.' '.$lname.'",
"line1": "'.$address.'",
"city": "'.$city.'",
"country_code": "'.$country.'",
"postal_code": "'.$zip.'",
"state": "'.$state.'",
"phone": "'.$phone.'"
}
}
}]
}';

//curl_setopt($ch, CURLOPT_URL, $PAYPAL_API_URL."v1/payments/payment");
curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Authorization: Bearer ".$token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//curl_close($ch);
$json = json_decode($result);

//$state=$json->state;
echo "<pre>";
print_r($json);
exit;
Preston PHX
  • 27,642
  • 4
  • 24
  • 44
Amy
  • 186
  • 8

1 Answers1

5

You used a sandbox API client ID and secret to request an access_token in sandbox mode (https://api.sandbox.paypal.com/v1/oauth2/token)

Then you attempted to use a sandbox access token for a live/production API call (https://api.paypal.com/v1/payments/payment)

Sandbox tokens (as well as all other identifiers) only work in the sandbox environment, and live ones only work in live. The two environments are completely separated.


Moreover, v1/payments is a deprecated API, and should not be used for any new integrations. Use the current v2/checkout/orders API to create and capture an order instead.


For payer approval, use https://developer.paypal.com/demo/checkout/#/pattern/server . However, you must remove all echo and print statements from your PHP to do so; only a JSON string must be outputted when your create or capture routes are called.


Late edit: The standard integration guide and integration builder were expanded to better cover a JS SDK + server backend integration. The guide's sample backend code is given in node.js (since javascript is widely understood) but you can implement those server routes from any backend environment.

Preston PHX
  • 27,642
  • 4
  • 24
  • 44
  • i replaced url with " https://api.sandbox.paypal.com/v1/payments/payment" But now i am getting following message [field] => payer.funding_instruments[0].credit_card [issue] => Invalid expiration (cannot be in the past) – Amy Jun 03 '22 at 05:21
  • You're using a deprecated API and furthermore that API's support for processing cards was removed long ago, read the notice at https://developer.paypal.com/docs/api/payments/v1/ . Current supported alternatives are a full Braintree gateway integration, the simple black "Debit or Credit Card" button of a simple JS SDK integration using the orders API (see https://developer.paypal.com/demo/checkout/#/pattern/server and click on the black button ), or the Advanced Integration. See the options at https://developer.paypal.com/docs/checkout/ – Preston PHX Jun 03 '22 at 05:26
  • not understand your point, just tell me can we do this via api ? if yes then how can i do this ? kindly tell me code so i can check and i can implement in my side – Amy Jun 03 '22 at 05:42
  • See above for the options available to you. All of them use APIs. – Preston PHX Jun 03 '22 at 05:53
  • not working , kindly give me exact url so i can follow – Amy Jun 03 '22 at 06:37
  • Sorry, I cannot research the best options for your use case. The above links are enough to get started. – Preston PHX Jun 03 '22 at 06:39