If you've ever wanted to have a shopping cart solution that works in REAL time, but didn't want to (or couldn't) get a merchant account, your alternative is PayPal. PayPal is the leading money processing site on the web, with millions of transactions each day! A short time ago, PayPal introduced Instant Payment Notification ("IPN") which allowed vendors the ability to sell products from their web sites and then update their databases in real time when customers checked out. Let me warn you that using PayPal's IPN system is a tough thing to do, it's not as easy as they make it appear. In order to make this task less difficult I decided to write a tutorial on how to implement it for your website. This is probably the only tutorial online showing you how to implement the PayPal IPN, as most people would rather have you pay them to implement it for you! So let's begin with the tutorial.

The first thing you will need to do is to get a FREE PayPal account. Now keep in mind that to accept payments you will have to put a credit card and a bank account on record. This is good for you (if you're like me, you will want to get the Paypal MasterCard Debit Card which allows you to access your Paypal funds instantly through MasterCard). Once you have those steps in place, the next thing is to begin your IPN modifications. That will be the first part of this tutorial.

Go to www.paypal.com

Log into your account and then click on the "Sell" tab on the upper right hand corner:

Next click on the Instant Payment Notification link:

Then click on the Technical Overview section:

Next you will click on the link titled "Start Using IPN".

Then click on the "EDIT" button:

Now you will need to enter two values:
1) Make sure this is "checked".
2) Enter the URL to the file that you will create in this tutorial, it's usually recommended you place it in the CART folder under the name ipn.cfm.
3) Click the Save Button.

Note: Click the "read the instructions" link above the "SAVE" button for further information.

That's it, you have now enabled PayPal's IPN and are ready to use it! Now I'll show you the back-end code you'll need to begin accessing real time payments.

The first thing you need to understand is that PayPal will send you information to the link you place above (The file you will now create) as POST form submission. That data will be available for you to do as you wish.... this tutorial will demonstrate how you use that data to finalize an order and allow the customer to download the software they've purchased.

The first thing you need to do is to create a string with values you will pass back to PayPal for verification.

<!-- read post from PayPal system and add 'cmd' -->
<CFSET str="cmd=_notify-validate">
<CFLOOP INDEX="TheField" list="#Form.FieldNames#">
    <CFSET str = str &
"&#LCase(TheField)#=#URLEncodedFormat(Evaluate(TheField))#">
</CFLOOP>
<CFIF IsDefined(
"FORM.payment_date")>
    <CFSET str = str &
"&payment_date=#URLEncodedFormat(Form.payment_date)#">
</CFIF>
<CFIF IsDefined(
"FORM.subscr_date")>
    <CFSET str = str &
"&subscr_date=#URLEncodedFormat(Form.subscr_date)#">
</CFIF>

Next we will post that information back to PayPal to verify that this is a VALID order and that the values we have are correct.
<!-- post back to PayPal system to validate -->
<CFHTTP URL="https://www.paypal.com/cgi-bin/webscr?#str#" METHOD="GET" RESOLVEURL="false">
</CFHTTP>


<!-- check notification validation -->
<CFIF #CFHTTP.FileContent# is
"VERIFIED">
    <!-- check that payment_status=Completed -->
    <cfif FORM.payment_status eq "Completed">
    <!-- check that receiver_email is your email address -->
 
    <cfif #FORM.RECEIVER_EMAIL# eq "mypaypalemail@mysite.com">
        <!-- process payment -->

        <cftry>
            <cfquery name="qInsertOrder" datasource="MyDSN">
                INSERT INTO Orders(
                                        receiver_email,
                                        item_number,
                                        quantity,
                                        payment_status,
                                        payment_date,
                                        payment_gross,
                                        payment_fee,
                                        txn_id,
                                        txn_type,
                                        payment_type,
                                        payer_id
                                        )
                              VALUES(
                                        '#FORM.RECEIVER_EMAIL#',
                                        '#FORM.ITEM_NUMBER#',
                                        '#FORM.QUANTITY#',
                                        '#FORM.PAYMENT_STATUS#',
                                        #CreateODBCDateTime(Now())#,
                                        '#FORM.PAYMENT_GROSS#',
                                        '#FORM.PAYMENT_FEE#',
                                        '#FORM.TXN_ID#',
                                        '#FORM.TXN_TYPE#',
                                        '#FORM.PAYMENT_TYPE#',
                                        '#FORM.PAYER_ID#'
                                        )
               </cfquery>
        <cfcatch>

        <!--- let's log all errors --->
        <cffile action="append"
   
                  file="D:\paypal_logs\paypal_log.txt"
                  output=
"Error order info">
        </cfcatch>
    </cftry>

Now let's generate a password for this customer, this will be used to allow them to download their software, you can also (as shown) generate serial numbers for their software at this step, to make sure that they get a serial number when registering their software!

    <cfset password = #Evaluate("#RandRange(1,10000)# * #RandRange(1,10000)#")#>
    <cfset serial_number =
"#RandRange(1,10000)#-0-#Left(first_name,1)#">
    <cftry>
        <cfquery name=
"qInsertCustomer" datasource="YourDSN">
                    INSERT INTO Customers(
                                                        first_name,
                                                        last_name,
                                                        email,
                                                        serial_number,
                                                        address,
                                                        city,
                                                        state,
                                                        zip,
                                                        serial_used,
                                                        payer_id,
                                                        password
                                                        )
                                             VALUES(
                                                        '#FORM.first_name#',
                                                        '#FORM.last_name#',
                                                        '#FORM.payer_email#',
                                                        '#serial_number#',
                                                        '#FORM.address_street#',
                                                        '#FORM.address_city#',
                                                        '#FORM.address_state#',
                                                        '#FORM.address_zip#',
                                                        0,
                                                        '#FORM.payer_id#',
                                                        '#password#'
                                                        )
        </cfquery>
        <cfcatch>

              <!--- log any errors when inserting this customer --->
            <cffile action="append"
                      file=
"D:\paypal_logs\paypal_log.txt"
                      output=
"Error inserting customer">
        </cfcatch>
    </cftry>

Now send the customer an email, thanking them for their order and with further information about their purchase:
    <!--- send user an email --->
    <cftry>
        <cfmail from=
"you@yoursite.com" to="#FORM.payer_email#" subject="MySite.Com Software Purchase!">
            Thank you for purchasing software from MySite.Com

            You may download your software by going to:
            http://www.yoursite.com/download/

            Your username is: #payer_email#
            Your password is: #password#

            If you have questions or need help, please go to:
            http://www.mysite.com/support/

            Thanks again for your purchase!
            MySite.Com, Inc.
            http://www.mysite.com
        </cfmail>
     <!--- Now send yourself an email alerting that there was a sale done --->
     <cfmail from="me@mysite.com" to="me@mysite.com" subject="Someone Purchased From The Website!">
      An order was placed by: #FORM.first_name#' #FORM.last_name#

        Item Purchased:
        Item Number: #FORM.ITEM_NUMBER#
        Purchase Price: #DOLLARFORMAT(FORM.PAYMENT_GROSS)#
        PayPal Fee: #DollarFORMAT(FORM.PAYMENT_FEE)#
        ==========================
        Profit Fee: #DollarFormat(Evaluate("#FORM.PAYMENT_GROSS# - #FORM.PAYMENT_FEE#"))#

        A Reminder Email:
        http://www.mysite.com
      </cfmail>
      <cfcatch>

        <!--- Log Any Errors Sending Emails --->
        <cffile action="append"
                  file=
"D:\paypal_logs\paypal_log.txt"
                  output=
"Error sending email">
        </cfcatch>
    </cftry>
  </cfif>
</cfif>

<CFELSEIF #CFHTTP.FileContent# is "INVALID">
    <!-- log for investigation -->

     Something that was purchased was invalid, either the order or the information provided. This is usually good to log in case someone is trying to purchase with stolen card numbers, etc. Here simply place a QUERY tag that insert the data above into a database.


<CFELSE>
   <!-- error -->

    This usually means that something went wrong along the way, you can use this area to log it and keep for your records.


</CFIF>

That's pretty much it, with this tutorial you can now begin taking instant credit card payments via PayPal securely and process the orders on the fly! Something that was purchased was invalid, either the order or the information provided. This is usually good to log in case someone is trying to purchase with stolen card numbers, etc. Here simply place a QUERY tag that insert the data above into a database. What's the exact procedure of placing the QUERY tag from Paypal IPN message so that it inserts the data into a database? Great many thanks Radek

  • For all those who suffer Try this it worked for me!!! I tried to make it as simple as possible Paypal says the funds went into your account
    Invalid Info

  • Advertisement

    Sponsored By...
    Dripping Springs, Texas - Deep Tissue Massages and Swedish Massages!