Home arrow PHP arrow Creating a Paypal IPN System in PHP - Part Two

Creating a Paypal IPN System in PHP - Part Two

This is the start of the actual creation of the PayPal IPN system using PHP. If you are looking for the entire script, you can download it at the end of this tutorial series. It is recommended that you read and understand the concept behind those scripts before testing the script on your own server. As noted in the title, this is the second part of a series.

TABLE OF CONTENTS:
  1. Creating a Paypal IPN System in PHP - Part Two
  2. Explaining the Code
By: Codex-M
Rating: starstarstarstarstar / 3
January 11, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Create "Buy Now" Buttons

The first step in this process is to create "Buy Now" buttons.

1. Log in to the PayPal Sandbox: https://developer.paypal.com/

2. Click "Test Accounts."

3. Check the radio button for your test and verified "Business" account.

4. Click "Enter Sandbox Test Site."

5. Log in using your "Business" account credentials, which you created in the first part of this series.

6. Click "Merchant Services."

7. Under "Create Buttons," click "Buy Now."

8. Fill in "Item name," e.g Codex Ebook, "Price," e.g 30 and "Currency," e.g. USD.

The Item ID and the rest, which are not specified above, are optional. However, make sure "use my secure merchant account ID" is selected.

9. Click "Step 2," and uncheck "Save Button at PayPal."

10. Click "Step 3," and since this tutorial is for selling digital products such as downloads (e.g ebook, mp3, etc) using the PayPal IPN system, set the answer to "NO" for the following items:

  • Do you want to let your customer change order quantities?
  • Can your customer add special instructions in a message to you?
  • Do you need your customers shipping address?

11. Check "Take customers to this URL when they cancel their checkout." For the purpose of this tutorial, it will be set to:

http://www.php-developer.org/paypal_ipn_demo/

12. Check "Take customers to this URL when they finish checkout." In this tutorial, this URL will be used:

http://www.php-developer.org/paypal_ipn_demo/customerdownload.php

13. Check "Add advanced variables," and add the following:

cbt=CLICK HERE TO DOWNLOAD YOUR EBOOK
invoice=1
custom=1

Some of those values will be changed later. Finally, click "Create Button." Click "Remove code protection" and click "select code." Copy the resulting HTML code to a text editor and save it. Since your button is not protected, you will rely on IPN and PHP validation to authenticate and validate a customer transaction.

Create a Shopping Page for Your Products

Now that you have created the "Buy Now" buttons, you can create the shopping page for your products.

Let's name this file index.php; you will place it inside a folder named "paypal_ipn_demo" in your test server.

Here is the complete working HTML code and script (index.php):

<html>
<head>
<title>Example Shopping Page-PayPal IPN Demo</title>
</head>
<body>
<h3>Buy my ebook</a>
A newly released ebook that I wrote, Codex ebook for $30:
<br />
<br />
<?php
//connect to MySQL database
include '/home/www/php-developer.org/paypal_ipn_demo/connect.php';
//invoice number and random key generator function
include '/home/www/php-developer.org/paypal_ipn_demo/invoicenumbergenerator.php';
?>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="6GTE64BZPKGFG">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Codex Ebook">
<input type="hidden" name="amount" value="30.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://www.php-developer.org/paypal_ipn_demo/customerdownload.php">
<input type="hidden" name="cancel_return" value="http://www.php-developer.org/paypal_ipn_demo/">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
<input type="hidden" name="cbt" value="CLICK HERE TO DOWNLOAD YOUR EBOOK">
<input type="hidden" name="invoice" value="<?php invoicenumber(); ?>">
<input type="hidden" name="custom" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<br /><br />
Or my previous book entitled "How to behave like a child" for $15.
<br /><br />
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="6GTE64BZPKGFG">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="How to behave like a child">
<input type="hidden" name="amount" value="15.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="1">
<input type="hidden" name="return" value="http://www.php-developer.org/paypal_ipn_demo/customerdownload.php">
<input type="hidden" name="cancel_return" value="http://www.php-developer.org/paypal_ipn_demo/">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
<input type="hidden" name="cbt" value="CLICK HERE TO DOWNLOAD YOUR EBOOK">
<input type="hidden" name="invoice" value="<?php invoicenumber(); ?>">
<input type="hidden" name="custom" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
<br /><br />
You can pay safely with PayPal. Thank you for your interest in my books.
<?php
//close database connections
mysql_close($dbhandle);
?>
</body>
</html>



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: