Search code examples
paypalpaypal-ipneclipse-pdtdonations

PayPal IPN and PDT - Cannot get buyer postal address on return_url page or email


I am setting up a basic donation system and need to send a confirmation email to the client but cannot get the buyer postal address to show on return_url page of the website, or to show in the email to send to the client. The custom values for the taxpayer yes/no radio selects won't follow through either in the email. I am using a custom form on our website with fields to fill in the donation amount, name, address and if they pay tax (simple radio select.) Not sure if I'm using it right but I have both IPN and PDT settings on the site site and setup within the PayPal account.

Donation Form:

<form class="dsForm" name="details" action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="if ( !CheckForm() ) return false;">
       <input type="hidden" name="cmd" value="_donations">
       <input type="hidden" id="business" name="business" value="[email protected]">
       <input type="hidden" id="item_name" name="item_name" value="Website Donation - Water">
       <input type="hidden" name="notify_url" value="http://www.urltoipnscript.com/ipn.php">
       <input type="hidden" id="item_number" name="item_number" value="3">
       <input type="hidden" name="no_note" value="1">
       <input type="hidden" id="currency_code" name="currency_code" value="GBP">
       <input type="hidden" name="no_shipping" value="2">
       <input type="hidden" name="tax" value="0">
       <input type="hidden" name="bn" value="IC_Sample">
       <input type="hidden" value="donation" name="custom">

       <ul id="donate-form">
        <li><label>Amount:</label>
            <input type="text" id="amount" name="amount" value="25.00" size='9'>
           <select id='currency' onchange='CheckCurrency()'>
                <option value="GBP">GBP</option>
                <option value="EUR">EUR</option>
                <option value="AUD">AUD</option>
                <option value="USD">USD</option>
           </select>
       </li>
        <li><label>Program:</label>
        <select name='program' id='program' onchange='ProgramChange();' >
            <option value=''>Please Select</option>
            <option id="education" value='4'>Education</option>
            <option id="water" value='3' selected="selected">Water</option>
            <option id="health" value='2'>Health Promotion</option>
            <option id="community" value='18'>Community Based Projects</option>
            <option id="sponsorship" value='8'>Child Sponsorship</option>
        </select>
       </li>

        <li><label>Firstname:</label>
        <input type='text' name='first_name' size='30' />
       </li>
        <li><label>Surname:</label>
        <input type='text' name='last_name' size='30' />
       </li>
        <li><label>Address 1:</label>
        <input type='text' name='address1' size='30' />
       </li>
        <li><label>Address 2:</label>
        <input type='text' name='address2' size='30' />
       </li>
        <li><label>Town/City:</label>
        <input type='text' name='city' size='30' />
       </li>
        <li><label>Postcode/Zip:</label>
        <input type='text' name='zip' size='30' />
       </li>
       <li><label>Country:</label>
        <select name='country' id='country'  >
        <option value='' selected='selected'>Please Select</option>
        <option value='AL'>Albania
        </option><option value='DZ'>Algeria
        </option><option value='GB'>United Kingdom
        </option><option value='US'>United States
        etc...
       </li>
       <div id='ukTaxOptions' style='display:none'>
       <label style="width:auto; margin-top:20px; margin-bottom:10px;font-weight: bold;" > Please choose an appropriate option below:</label >
        <li>
        <label style="width:auto" >         
    <input id='gbTaxPayer_1' name='gbTaxPayer_radio' type='radio' value='1'  onchange="SetPayPalCustom('UK')" />
    <input id="gbTaxPayer" name="gbTaxPayer" type="hidden" value="1" /> I am a UK taxpayer and would like to Gift Aid all donations I have made to Fields Of Life in the last four years and all donations I make in the future, until I notify you otherwise *
        </label>
        </li>

        <li><label style="width:auto">
        <input id='gbTaxPayer_' name='gbTaxPayer_radio' type='radio' value=''  checked  onchange="SetPayPalCustom('')" />I am not a UK taxpayer
        </label></li>

        <li><label style="width:auto">* I understand that I must have paid an amount of income tax or capital gains tax at least equal to the tax you reclaim on my donations</label></li>

        </div>
        <input type="hidden" name="hosted_button_id" value="hidden from stackflow">
        <input type="image" src="http://www.hiddenfromsov.com/mybtn.png" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">

       <li><img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif width="1" height="1" /></li>
       </ul>

IPN Script:

        <?php

    $raw_post_data = file_get_contents('php://input');
    $raw_post_array = explode('&', $raw_post_data);
    $myPost = array();
    foreach ($raw_post_array as $keyval) {
      $keyval = explode ('=', $keyval);
      if (count($keyval) == 2)
         $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    if(function_exists('get_magic_quotes_gpc')) {
       $get_magic_quotes_exists = true;
    } 
    foreach ($myPost as $key => $value) {        
       if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
            $value = urlencode(stripslashes($value)); 
       } else {
            $value = urlencode($value);
       }
       $req .= "&$key=$value";
    }

    $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    if( !($res = curl_exec($ch)) ) {
        curl_close($ch);
        exit;
    }
    curl_close($ch);

    if (strcmp ($res, "VERIFIED") == 0) {


        // assign posted variables to local variables
        $item_name = $_POST['item_name'];
        $item_number = $_POST['item_number'];
        $payment_status = $_POST['payment_status'];
        $payment_amount = $_POST['mc_gross'];
        $payment_currency = $_POST['mc_currency'];
        $txn_id = $_POST['txn_id'];
        $receiver_email = $_POST['receiver_email'];
        $payer_email = $_POST['payer_email'];
        $address_street = $_POST['address_street'];
        $address_city = $_POST['address_city'];
        $address_state = $_POST['address_state'];
        $address_zip = $_POST['address_zip'];
        $address_country = $_POST['address_country'];
        $address_status = $_POST['address_status'];
        $gbtax = $_POST['gbTaxPayer_radio'];
    } else if (strcmp ($res, "INVALID") == 0) {
        // log for manual investigation
    }

Code on success return_url page

        //PDT
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-synch';
    $tx_token = $_GET['tx'];
    $auth_token = "I've copied this the paypal settings";
    $req .= "&tx=$tx_token&at=$auth_token";

    // 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 ('www.paypal.com', 80, $errno, $errstr, 30);
    // $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

    if (!$fp) {
    // HTTP ERROR
    } else {
    fputs ($fp, $header . $req);
    // read the body data
    $res = '';
    $headerdone = false;
    while (!feof($fp)) {
    $line = fgets ($fp, 1024);
    if (strcmp($line, "\r\n") == 0) {
    // read the header
    $headerdone = true;
    }
    else if ($headerdone)
    {
    // header has been read. now read the contents
    $res .= $line;
     }
    }

    // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0) {
    for ($i=1; $i<count($lines);$i++){
    list($key,$val) = explode("=", $lines[$i]);
    $keyarray[urldecode($key)] = urldecode($val);
    }

    // check the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment
    $item_number = $_GET['item_number'];
    $address12 = $_GET['address_street'];
    $firstname = $keyarray['first_name'];
    $lastname = $keyarray['last_name'];
    $payer_email = $keyarray['payer_email'];
    $amount = $keyarray['mc_gross'];
    $payment_date = $keyarray['payment_date'];
    $payment_status = $keyarray['payment_status'];
    $payment_type = $keyarray['payment_type'];
    $mc_currency = $keyarray['mc_currency'];
    $transactionid = $keyarray['txn_id'];
    $itemname = $keyarray['item_name'];
    $address1 = $keyarray['address1'];
    $address_street = $keyarray['address_street'];
    $address_city = $keyarray['address_city'];
    $address_state = $keyarray['address_state'];
    $address_zip = $keyarray['address_zip'];
    $address_country = $keyarray['address_country'];
    $gbtax = $keyarray['gbTaxPayer_radio'];

    echo ("<p><strong>Payment Details</strong></p>\n");
    echo ("<ul>\n");
    echo ("<li><b>Address</b>: $address12</li>\n");
    echo ("<li><b>Item Number</b>: $item_number</li>\n");
    echo ("<li><b>Donation Type</b>: $itemname</li>\n");
    echo ("<li><b>Date</b>: $payment_date</li>\n");
    echo ("<li><b>Name</b>: $firstname $lastname ($payer_email)</li>\n");
    echo ("<li><b>Street Address</b>: $address1</li>\n");
    echo ("<li><b>Town/City</b>: $address_city</li>\n");
    echo ("<li><b>County/State</b>: $address_state</li>\n");
    echo ("<li><b>Postcode/ZIP</b>: $address_zip</li>\n");
    echo ("<li><b>Country</b>: $address_country</li>\n");
    echo ("<li><b>Amount</b>: &pound;$amount</li>\n");
    echo ("<li><b>Payment status</b>: $payment_status</li>\n");
    echo ("<li><b>Transaction ID</b>: $transactionid</li>\n");
    echo ("<li><b>UK Tax Payer?</b> $gbtax</li>\n");
    echo ("</ul>\n");

    // send e-mail
    $today = date("F j, Y, g:i a");
    mail("[email protected]", "Donation made - $itemname", "A donation was made on $today \n Payment Details \r\n\r\n Donation type: $itemname \r\n\r\n Name: $firstname $lastname \n Amount: $amount \n Donator Email: $payer_email \r\n\r\n Address: $address1 \n $address_city \n $address_state \n $address_zip \n $address_country \r\n\r\n Payment date: $payment_date \n Payment status: $payment_status \n Currency: $mc_currency \n Transaction ID: $transactionid \n UK Tax Payer? $gbtax \n", "From: Charity Donation <[email protected]>");
    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
    // log for manual investigation
     }
    }
    fclose ($fp);

At the moment all I need to get working is the address to show on the email, return_url page and PayPal website (currently says Postal Address: Not Specified.)


Solution

  • Are you entering in an address when testing? Try passing over the variable "no_shipping" and setting the value to "2", and see if this makes a difference. Also PayPal will not pass back variables/values that are not valid PayPal variables, meaning you would not be able to make up your own variables and pass them over.