What are the recommended steps to take when sending data to PayPal in PHP code without utilizing a PayPal button?

When sending data to PayPal in PHP without using a PayPal button, you can utilize PayPal's REST API to create and execute payments. This involves sending a POST request with the necessary payment details to PayPal's API endpoint. You will also need to handle the response from PayPal to retrieve the payment status and other relevant information.

<?php

$api_endpoint = 'https://api.sandbox.paypal.com/v1/payments/payment';

$paypal_data = array(
    'intent' => 'sale',
    'payer' => array(
        'payment_method' => 'paypal'
    ),
    'transactions' => array(
        array(
            'amount' => array(
                'total' => '10.00',
                'currency' => 'USD'
            ),
            'description' => 'Payment description'
        )
    ),
    'redirect_urls' => array(
        'return_url' => 'http://example.com/success',
        'cancel_url' => 'http://example.com/cancel'
    )
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer YOUR_ACCESS_TOKEN'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($paypal_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$payment_response = json_decode($response, true);

// Handle the payment response from PayPal
if(isset($payment_response['state']) && $payment_response['state'] == 'approved') {
    // Payment was successful
    $payment_id = $payment_response['id'];
    // Additional processing here
} else {
    // Payment failed
    // Handle error
}

?>