views:

646

answers:

3

I want to create a simple transaction on my Web Site where after the person's transaction completes, I want paypal to redirect the user to go to a place on my site and I want PayPal to provide me with details so I can use PHP to parse it and email them the link to their purchase. I'm not sure what notify_url does? Thanks

+1  A: 

I am not sure what you are asking exactly but I do have experience with using Paypal to accept payments. To answer your question about notify_url, it specifies the URL to be redirected to as soon as your customer finishes his/her transaction. This can also be set in the merchant console which is what I do.

Check this link out for html var definitions. HTML var definitions: Paypal HTML Variables

I would provide more links but I can't at the moment since I don't have the rep. Also research Payment Data Transfer and Instant Payment Notification at the PayPal dev website.

Brandon
+3  A: 

PayPal works like this:

You have a form with a "buy" button. When that is clicked, it sends information (product, price, your account name, etc) to PayPal.

The buyer then agrees to pay you and when the transaction is completed, PayPal send an "IPN" (instant payment notification) to your notify URL - it sends POST data to that URL for your server to process. You reply to PayPal to ask if they sent the POST data (rather than an imposter) and if they then answer that it is a real transaction, you can release the product to the customer. Note that this all happens in the background while your buyer is still "at" the PayPal website.

There is a final optional stage, which is that PayPal returns the buyer to your website. In this case, they send the buyer back to your "return" url, and they can (optionally) pass back the transaction data again, (they call this PDT). And you can again check with Paypal if this is a valid transaction and provide a download etc at that point.

The most difficult bit that nobody explains is that the buyer doesn't get redirected to your notify URL. i.e. the "visitor" to your website's notify URL is PayPal, not the buyer, so this doesn't happen as part of your buyer's session. If you wish to persist a session across the three parts of this process, then you need to create a means of tracking the buyer in your form, and pass that to PayPal in a field of the form called "custom". This data is passed back to you in the IPN and PDT data, and you can use this to re-establish a connection with the original user session.

You really need to implement both IPN and PDT - if the IPN email fails then you have PDT as a backup. And if the user closes their web browser before they are redirected back to your PDT page, then you have sent an IPN email as a backup.

Search on IPN and PDT and you'll find quite a lot of information. PayPal also have full documentation and example scripts.

Here's a PHP script that does pretty much exactly what you asked for. Note that it doesn't handle PDT, so the risk is that the IPN email might fail to reach the buyer and they will have paid but will have received nothing. But this is a good start.

Jason Williams
A: 

Notify URL should lead to the script that saves the returned data from PayPal, such as:

   /** Fetch order from PayPal (IPN reply)
    * @return int received ID of inserted row if received correctly, 0 otherwise
    */
   function FetchOrder()
   {
   $transactionID=$_POST["txn_id"];
   $item=$_POST["item_name"];
   $amount=$_POST["mc_gross"];
   $currency=$_POST["mc_currency"];
   $datefields=explode(" ",$_POST["payment_date"]);
   $time=$datefields[0];
   $date=str_replace(",","",$datefields[2])." ".$datefields[1]." ".$datefields[3];
   $timestamp=strtotime($date." ".$time);
   $status=$_POST["payment_status"];
   $firstname=$_POST["first_name"];
   $lastname=$_POST["last_name"];
   $email=$_POST["payer_email"];
   $custom=$_POST["option_selection1"];
   if ($transactionID AND $amount)
      {
      // query to save data
      return $this->insertID;
      }
   else
      {
      return 0;
      }
   }

You can also choose to verify an order later on:

/** Verify PayPal order (IPN)
    * PayPal returns VERIFIED or INVALID on request
    * @return bool verified 1 if verified, 0 if invalid
    */
   function VerifyOrder()
   {
   $_POST["cmd"]="_notify-validate";
   $ch=curl_init();
   curl_setopt($ch,CURLOPT_HEADER,0);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch,CURLOPT_USERAGENT,"your agent - replace");
   curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com/cgi-bin/webscr");
   curl_setopt($ch,CURLOPT_POST, 1);
   foreach ($_POST as $key=>$value)
      {
      $string.="&".$key."=".urlencode(stripslashes($value));
      }
   curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
   $result=curl_exec($ch);
   if ($result=="VERIFIED") return 1;
   else return 0;
   }
dusoft