Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

PayPal Rest API Live and sandbox payment not captured

New member
Joined
Feb 14, 2023
Messages
7
Developed PHP Paypal Integration via REST API. when creating payment intent to get url to redirect user to Payment gateway it works fine. I get the redirect url as well. Example redirect url live mode

https://www.paypal.com/checkoutnow?token=7JR976187U6560045

But when we go to Payment page we can select either to logged in to Paypal account or pay as a guest using credit or debit card.

But for the logged in user it shows select the payment source (card) to pay but when we click on proceed or review it always not going to proceed to next step or to thank you page it reload back to same page without showing any error or warning.

This happens in Sandbox mode as well.

When we select pay via Credit card without logging in it it loads the card details entering page but after adding the cart it will not accept the payment and shows card was declined message. Cards has funds. Something happening in Sandbox with test card details.

below is sample code used for generate payment intent.

Code:
//first get the access token
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.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_POSTFIELDS => "grant_type=client_credentials",
        CURLOPT_HTTPHEADER => array(
            "Authorization: Basic " . base64_encode(PAYPAL_ID.":".PAYPAL_SECRET),
            "Content-Type: application/x-www-form-urlencoded"
        ),
    ));
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

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

    $responseData = json_decode($response);
    $accessToken = $responseData->access_token;

        $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => "https://api.paypal.com/v2/checkout/orders",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($requestBody),
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json",
            "Authorization: Bearer $accessToken"
        ],
    ]);

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

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
        exit();
    }
    $responseData = json_decode($response);
    //var_dump($responseData);

    if ($responseData->status !== 'CREATED') {
        echo "Order creation failed: " . $responseData->debug_id;
        exit();
    }

    $orderId = $responseData->id;
    $_SESSION['paypal_id'] = $orderId;
    $approveUrl = '';
    foreach ($responseData->links as $link) {
        if ($link->rel === 'approve') {
            $approveUrl = $link->href;
            break;
        }
    }
    if (!$approveUrl) {
        echo "Approve URL not found";
        exit();
    }
    $data_back = array();
    $data_back['url']  = $approveUrl;

Tried both live mode and sandbox mode.
 
New member
Joined
Feb 14, 2023
Messages
10
You are missing the capture step.
When redirecting away from your site to PayPal (not recommended), the order you created must have a return_url so that the payer can be redirected back after approving the payment. Then you need to capture the payment with another API call.
Rather than redirecting away, the best user experience is to pair API order creation and capture with the JS SDK for the approval flow. This keeps your site loaded in the background at all times, and is documented here (the sample there uses Node.js for the backend, but you can of course implement it in any environment including PHP).
There will be no PayPal transaction until you capture the payment. After the capture API call you can then display a message of success or failure. The server route that does the capture API call should also verify the captured amount was correct in the API response before storing the result as a successful payment and doing anything automated with the result.
 
Top