#API PROBLEM

12 messages · Page 1 of 1 (latest)

wet sleet
#

When I try to make payment it works but paypal doesn't receive money and after a while my payment is cancelled, I really don't know how paypal api works, here is the code:

define('access', 'api');
require_once('api.php');

if (!@include_once(rtrim(str_replace('\\', '/', dirname(__DIR__)), '/') . '/includes/webengine.php')) throw new Exception('Could not load WebEngine.');

$cfg = loadConfigurations('donation.paypal');
if (!is_array($cfg)) {
    header("HTTP/1.1 500 Internal Server Error");
    die('Invalid configuration format.');
}

$requiredKeys = ['paypal_enable_sandbox', 'paypal_currency', 'paypal_email', 'paypal_title', 'paypal_conversion_rate'];
foreach ($requiredKeys as $key) {
    if (!isset($cfg[$key])) {
        die("Configuration key missing: $key");
    }
}```
#
$clientId = $enable_sandbox ? PAYPAL_SANDBOX_CLIENT_ID : PAYPAL_LIVE_CLIENT_ID;
$clientSecret = $enable_sandbox ? PAYPAL_SANDBOX_CLIENT_SECRET : PAYPAL_LIVE_CLIENT_SECRET;

$accessToken = getPayPalAccessToken($clientId, $clientSecret, $enable_sandbox);
if (!$accessToken) {
    throw new Exception('Unable to retrieve PayPal access token');
}

if (isset($_POST['amount']) && is_numeric($_POST['amount']) && floatval($_POST['amount']) > 0) {
    $amount = $_POST['amount'];

    $order_id = md5(time());
    $currency = $cfg['paypal_currency'];
    $user_id = $_SESSION['userid']; // L'ID utente per il sistema

    $approvalUrl = createPayPalOrder($accessToken, $amount, $currency);

    if (!$approvalUrl) {
        throw new Exception('Failed to create PayPal order');
    }

    header('Location: ' . $approvalUrl);
    exit();
} else {
    die('Errore: l\'importo del pagamento non è impostato o non valido.');
}```
#
    $url = $sandbox ? 'https://api.sandbox.paypal.com/v1/oauth2/token' : 'https://api.paypal.com/v1/oauth2/token';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        saveLog("cURL error: " . curl_error($ch));
        curl_close($ch);
        return false;
    }

    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code !== 200) {
        saveLog("PayPal token request failed with HTTP code " . $http_code . " Response: " . $response);
        curl_close($ch);
        return false;
    }

    $json = json_decode($response);
    curl_close($ch);
    return $json->access_token ?? false;
}```
#
    $url = 'https://api.paypal.com/v2/checkout/orders'; 
    $headers = [
        "Content-Type: application/json",
        "Authorization: Bearer " . $accessToken
    ];

    $body = json_encode([
        'intent' => 'CAPTURE',
        'purchase_units' => [
            [
                'amount' => [
                    'currency_code' => $currency,
                    'value' => $amount
                ]
            ]
        ],
        'application_context' => [
            'return_url' => 'https://muonline.test/api/success.php',
            'cancel_url' => 'https://muonline.test/cancel.php' 
        ]
    ]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        curl_close($ch);
        return false;
    }

    $json = json_decode($response);
    curl_close($ch);

    // Trova l'URL di approvazione nella risposta
    if (!empty($json->links)) {
        foreach ($json->links as $link) {
            if ($link->rel === 'approve') {
                return $link->href; // Restituisci l'URL di approvazione
            }
        }
    }

    return false; // Ritorna false se non si trova l'URL
}```
frigid escarp
#

Perhaps toss something about PayPal in the description/topic?
Sounds like you need to debug and that may require you to login to a dashboard with PayPal?

#

(*The same place they issued the dev keys?)

wet sleet
#

i don't understand sorry @frigid escarp

#

how?

empty ibex
#

php is dead just like me sending back response AMF what a disaster

#

everyone will forget about php sooner or later and you will be stuck like me

#

in nowadays you have to switch languages to such as nodejs, javascript, python

#

php is only good for chatgpt