Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269

    Integrazione paypal in php

    Salve ragazzi qualcuno può darmi qualche dritta riguardo all'integrazione tra pay-pal e php, praticamente io ho un form in flash, quando clicco invia, invio il tutto ad un file XML.
    Questo file XML interagisce con un file php che legge i contenuti del file .xml.
    Adesso vorrei capire come fare interagire il file .php con paypal.

    Qualcuno può aiutarmi?

    Grazie anticipatamente,
    gaten
    Con i sogni possiamo conoscere il futuro...

  2. #2
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Basta leggere la documentazione ufficiale. link, ci sono anche codici di esempio.

  3. #3
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    In realtà sono riuscito a capire più o meno come viene effettuato il tutto, grazie a questa ottima guida, sono riuscito anche a crearmi degli account di esempio con sendbox.

    link: http://samples.geekality.net/pdt/

    Però non riesco a capire: E' abbastanza sicuro, nel senso che, vedo che ritorna tramie $_GET l'id della transazione, questo non potrebbe far si che qualcuno tramite $_GET possa aggirare il reale pagamento? Inoltre, grazie per il link sulla documentazione(che avevo già visto), ma non riesco a capire quale scegliere:

    API PayPal: Interfaccia Coppia Nome-Valore
    API PayPal: Interfaccia SOAP
    Pagamenti su sito web

    ???

    per adesso ho scaricato "pagamenti su sito web"
    Con i sogni possiamo conoscere il futuro...

  4. #4
    Moderatore di PHP L'avatar di Alhazred
    Registrato dal
    Oct 2003
    Messaggi
    12,505
    Se ti salvi i codici delle transazioni nel db, ogni volta che ti arriva una conferma di pagamento verifichi se il codice è già nel db, se non c'è, tutto ok, se invece c'è, qualcuno sta cercando di fregarti.

  5. #5
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Per quel che ho capito io, qui lui se effettua la transazione correttamente su paypal, salva l'id in una variabile $_SESSION e verifica l'esistenza di questa variabile quando torna sul mio sito.
    Con i sogni possiamo conoscere il futuro...

  6. #6
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Ho creato il tutto seguendo un tutorial, ecco il codice:

    index.html
    codice:
    <form class="paypal" action="payments.php" method="post" id="paypal_form" target="_blank">    
    	<input type="hidden" name="cmd" value="_xclick" /> 
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="lc" value="UK" />
        <input type="hidden" name="currency_code" value="EUR" />
        <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />
        <input type="hidden" name="first_name" value="Customer's First Name"  />
        <input type="hidden" name="last_name" value="Customer's Last Name"  />
        <input type="hidden" name="payer_email" value="customer@example.com"  />
        <input type="hidden" name="item_number" value="123456" / >
        <input type="submit"  value="Submit Payment"/>
    </form>
    payments.php
    Codice PHP:
    // Database variables
    $host "localhost"//database location
    $user "xxx"//database username
    $pass "xxx"//database password
    $db_name "xxx"//database name

    // PayPal settings
    $paypal_email 'paypal@example.com';
    $return_url 'http://gaetanodemitri.altervista.org/pay/payment-successful.htm';
    $cancel_url 'http://gaetanodemitri.altervista.org/pay/payment-cancelled.htm';
    $notify_url 'http://gaetanodemitri.altervista.org/pay/payments.php';

    $item_name 'Nome del prodotto';
    $item_amount 1.00;

    // Include Functions
    include("functions.php");

    //Database Connection
    $link mysql_connect($host$user$pass);
    mysql_select_db($db_name);


    // Check if paypal request or response
    if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

        
    // Firstly Append paypal account to querystring
        
    $querystring .= "?business=".urlencode($paypal_email)."&";    
        
        
    // Append amount& currency (£) to quersytring so it cannot be edited in html
        
        //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
        
    $querystring .= "item_name=".urlencode($item_name)."&";
        
    $querystring .= "amount=".urlencode($item_amount)."&";
        
        
    //loop for posted values and append to querystring
        
    foreach($_POST as $key => $value){
            
    $value urlencode(stripslashes($value));
            
    $querystring .= "$key=$value&";
        }
        
        
    // Append paypal return addresses
        
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
        
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
        
    $querystring .= "notify_url=".urlencode($notify_url);
        
        
    // Append querystring with custom field
        //$querystring .= "&custom=".USERID;
        
        // Redirect to paypal IPN
        
    header('location:[url]https://www.sandbox.paypal.com/cgi-bin/webscr[/url]'.$querystring);
        exit();

    }else{

        
    // Response from Paypal

        // read the post from PayPal system and add 'cmd'
        
    $req 'cmd=_notify-validate';
        foreach (
    $_POST as $key => $value) {
            
    $value urlencode(stripslashes($value));
            
    $value preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
            
    $req .= "&$key=$value";
        }
        
        
    // assign posted variables to local variables
        
    $data['item_name']            = $_POST['item_name'];
        
    $data['item_number']         = $_POST['item_number'];
        
    $data['payment_status']     = $_POST['payment_status'];
        
    $data['payment_amount']     = $_POST['mc_gross'];
        
    $data['payment_currency']    = $_POST['mc_currency'];
        
    $data['txn_id']                = $_POST['txn_id'];
        
    $data['receiver_email']     = $_POST['receiver_email'];
        
    $data['payer_email']         = $_POST['payer_email'];
        
    $data['custom']             = $_POST['custom'];
            
        
    // post back to PayPal system to validate
        
    $header "POST /cgi-bin/webscr HTTP/1.0\r\n";
        
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        
    $header .= "Content-Length: " strlen($req) . "\r\n\r\n";
        
        
    $fp fsockopen ('ssl://www.sandbox.paypal.com'443$errno$errstr30);    
        
        if (!
    $fp) {
            
    // HTTP ERROR
        
    } else {    
            
            
    fputs ($fp$header $req);
            while (!
    feof($fp)) {
                
    $res fgets ($fp1024);
                if (
    strcmp($res"VERIFIED") == 0) {
                
                    
    // Used for debugging
                    
    @mail("gaetano.demitri@gmail.com""PAYPAL DEBUGGING""Verified Response
    data = <pre>"
    .print_r($posttrue)."</pre>");
                            
                    
    // Validate payment (Check unique txnid & correct price)
                    
    $valid_txnid check_txnid($data['txn_id']);
                    
    $valid_price check_price($data['payment_amount'], $data['item_number']);
                    
    // PAYMENT VALIDATED & VERIFIED!
                    
    if($valid_txnid && $valid_price){        
                            
                        
    $orderid updatePayments($data);        
                        if(
    $orderid){    
                            echo 
    "Payment has been made & successfully inserted into the Database";                    
                        }else{                                
                            echo 
    "Error inserting into DB";
                            
    // E-mail admin or alert user
                        
    }
                    }else{                    
                        
    // Payment made but data has been changed
                        // E-mail admin or alert user
                    
    }                        
                
                }else if (
    strcmp ($res"INVALID") == 0) {
                
                    
    // PAYMENT INVALID & INVESTIGATE MANUALY! 
                    // E-mail admin or alert user
                    
                    // Used for debugging
                    
    @mail("gaetano.demitri@gmail.com""PAYPAL DEBUGGING""Invalid Response
    data = <pre>"
    .print_r($posttrue)."</pre>");
                }        
            }        
        
    fclose ($fp);
        }    

    Funziona tutto perfettamente, però c'è un problema.

    Non mi inserisce i record all'interno della tabella "payments" presente nel database "my_gaetanodemitri". Non riesco a capire il perchè.

    functions.php
    Codice PHP:
    // functions.php
    function check_txnid($tnxid){
        global 
    $link;
        return 
    true;
        
    $valid_txnid true;
        
    //get result set
        
    $sql mysql_query("SELECT * FROM `payments` WHERE txnid = '$tnxid'"$link);        
        if(
    $row mysql_fetch_array($sql)) {
            
    $valid_txnid false;
        }
        return 
    $valid_txnid;
    }

    function 
    check_price($price$id){
        
    $valid_price false;
        
    //you could use the below to check whether the correct price has been paid for the product
        
        /* 
        $sql = mysql_query("SELECT amount FROM `products` WHERE id = '$id'");        
        if (mysql_numrows($sql) != 0) {
            while ($row = mysql_fetch_array($sql)) {
                $num = (float)$row['amount'];
                if($num == $price){
                    $valid_price = true;
                }
            }
        }
        return $valid_price;
        */
        
    return true;
    }

    function 
    updatePayments($data){    
        global 
    $link;
        if(
    is_array($data)){                
            
    $sql mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                    '"
    .$data['txn_id']."' ,
                    '"
    .$data['payment_amount']."' ,
                    '"
    .$data['payment_status']."' ,
                    '"
    .$data['item_number']."' ,
                    '"
    .date("Y-m-d H:i:s")."' 
                    )"
    $link);
        return 
    mysql_insert_id($link);
        }

    Praticamente la primva volta lui entra nel primo if, presente in payments.php
    codice:
    if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
    Però dopo non riesce più ad andare nell'else una volta che vengono settate "txn_id" e "txn_type", come posso risolvere questa cosa?
    Con i sogni possiamo conoscere il futuro...

  7. #7
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Da quanto ho capito e come se non venisse eseguito questo:

    Codice PHP:
     $fp fsockopen ('ssl://www.sandbox.paypal.com'443$errno$errstr30); 

    Ho provato a mettere anche al posto del commento "HTTP ERROR", la seguente stringa:
    Codice PHP:
    echo "ERROR: ".$errno." - ".$errstr."
    \n"

    ma niente...
    Con i sogni possiamo conoscere il futuro...

  8. #8
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Ho provato a segure anche questo tutorial, funziona anche qui perfettamente, ma non salva i record sul database...
    Con i sogni possiamo conoscere il futuro...

  9. #9
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    up
    Con i sogni possiamo conoscere il futuro...

  10. #10
    Utente di HTML.it
    Registrato dal
    Apr 2011
    Messaggi
    111
    Anchio ho avuto problemi con altervista e fsockopen.
    prova ad usare il curl:
    else{

    // Response from Paypal

    $ch = curl_init(); // Starts the curl handler
    $url = "https://www.paypal.com/cgi-bin/webscr";
    curl_setopt($ch, CURLOPT_URL,$url); // Sets the paypal address for curl
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Returns result to a variable instead of echoing
    curl_setopt($ch, CURLOPT_TIMEOUT, 3); // Sets a time limit for curl in seconds (do not set too low)
    curl_setopt($ch, CURLOPT_POST, 1); // Set curl to send data using post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req); // Add the request parameters to the post
    $result = curl_exec($ch); // run the curl process (and return the result to $result
    curl_close($ch);
    //$subject = "$add Problema IPN1";
    //$message = "Si è verificato un problema nella seguente transazione:\r\n\r\n";
    //$message .="Result". $result ." --REQ ". $req;
    //mail(ADMIN_MAIL,$subject,$message,"From: noreply<$from>");
    if (strcmp ($result, "VERIFIED") == 0) // It may seem strange but this function returns 0 if the result matches the string So you MUST check it is 0 and not just do strcmp ($result, "VERIFIED") (the if will fail as it will equate the result as false)
    {
    return TRUE;
    }
    else
    { $this->sendReport();
    return FALSE;

    }

    }
    $req = 'cmd=_notify-validate';

    // Run through the posted array
    foreach ($_POST as $key => $value)
    {
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value)
    {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }
    }
    E' probabile che lo devi adattare un po al tuo caso specifico, prova e fammi sapere

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.