Wednesday, May 16, 2012

Paypal direct post integration

  1. Create a paypal account(test sandbox)
  2. if test mode use: <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="formPayPal">
  3. live mode use: <form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="formPayPal">
 <input type="hidden" name="cmd" value="_xclick"/>
    <input type="hidden" name="business" value="PAYPAL_ID"/>
    <input type="hidden" name="currency_code" value="$"/>
    <input type="hidden" name="item_name" value="Payment for Order100"/>
    <input type="hidden" name="item_number" value="100"/>
    <input type="hidden" name="amount" value="100.50"/>
    <input type="hidden" name="zip" value="2102"/>
    <input type="hidden" name="night_phone_a" value=""/>
    <input type="hidden" name="day_phone_a" value=""/>
    <input type="hidden" name="return" value="http://dom.com/paypalreturn.php"/>
    <input type="hidden" name="notify_url" value="http://dom.com/paypalreturn.php"/>

In paypalreturn.php file write the following to retreieve paypal info. 
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
       $value = urlencode(stripslashes($value));
       $req .= "&$key=$value";
}
$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";
if(GATEWAY_TESTING == 1) {
       $fp = fsockopen ('www.sandbox.paypal.com', 443, $errno, $errstr, 30);
} else {
       $fp = fsockopen ('www.paypal.com', 443, $errno, $errstr, 30);
}
 if (!$fp) {
      $pGateResponseText = "HTTP ERROR: fail connect to PayPal!!!";
       return;
 } else{
       fputs ($fp, $header . $req);
        while (!feof($fp)) {
                    $res = fgets ($fp, 1024);
                    if ((strcmp ($res, "VERIFIED") == 0) || GATEWAY_TESTING === 1) {
                        $item_name = $_POST['item_name'];
                        $item_number = $_POST['item_number'];
                        $payment_status = trim($_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'];
                        $order_number = $item_number; 
               }
        }
}




No comments:

Post a Comment