10 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/* PHP Paypal IPN Integration Class Demonstration File
|
|
|
4 |
* 4.16.2005 - Micah Carrick, email@micahcarrick.com
|
|
|
5 |
*
|
|
|
6 |
* This file demonstrates the usage of paypal.class.php, a class designed
|
|
|
7 |
* to aid in the interfacing between your website, paypal, and the instant
|
|
|
8 |
* payment notification (IPN) interface. This single file serves as 4
|
|
|
9 |
* virtual pages depending on the "action" varialble passed in the URL. It's
|
|
|
10 |
* the processing page which processes form data being submitted to paypal, it
|
|
|
11 |
* is the page paypal returns a user to upon success, it's the page paypal
|
|
|
12 |
* returns a user to upon canceling an order, and finally, it's the page that
|
|
|
13 |
* handles the IPN request from Paypal.
|
|
|
14 |
*
|
|
|
15 |
* I tried to comment this file, aswell as the acutall class file, as well as
|
|
|
16 |
* I possibly could. Please email me with questions, comments, and suggestions.
|
|
|
17 |
* See the header of paypal.class.php for additional resources and information.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
// Setup class
|
|
|
21 |
require_once('paypal.class.php'); // include the class file
|
|
|
22 |
$p = new paypal_class; // initiate an instance of the class
|
|
|
23 |
$p->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; // testing paypal url
|
|
|
24 |
//$p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr'; // paypal url
|
|
|
25 |
|
|
|
26 |
// setup a variable for this script (ie: 'http://www.micahcarrick.com/paypal.php')
|
|
|
27 |
$this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
|
|
|
28 |
|
|
|
29 |
// if there is not action variable, set the default action of 'process'
|
|
|
30 |
if (empty($_GET['action'])) $_GET['action'] = 'process';
|
|
|
31 |
|
|
|
32 |
switch ($_GET['action']) {
|
|
|
33 |
|
|
|
34 |
case 'process': // Process and order...
|
|
|
35 |
|
|
|
36 |
// There should be no output at this point. To process the POST data,
|
|
|
37 |
// the submit_paypal_post() function will output all the HTML tags which
|
|
|
38 |
// contains a FORM which is submited instantaneously using the BODY onload
|
|
|
39 |
// attribute. In other words, don't echo or printf anything when you're
|
|
|
40 |
// going to be calling the submit_paypal_post() function.
|
|
|
41 |
|
|
|
42 |
// This is where you would have your form validation and all that jazz.
|
|
|
43 |
// You would take your POST vars and load them into the class like below,
|
|
|
44 |
// only using the POST values instead of constant string expressions.
|
|
|
45 |
|
|
|
46 |
// For example, after ensureing all the POST variables from your custom
|
|
|
47 |
// order form are valid, you might have:
|
|
|
48 |
//
|
|
|
49 |
// $p->add_field('first_name', $_POST['first_name']);
|
|
|
50 |
// $p->add_field('last_name', $_POST['last_name']);
|
|
|
51 |
|
|
|
52 |
$p->add_field('business', 'YOUR PAYPAL (OR SANDBOX) EMAIL ADDRESS HERE!');
|
|
|
53 |
$p->add_field('return', $this_script.'?action=success');
|
|
|
54 |
$p->add_field('cancel_return', $this_script.'?action=cancel');
|
|
|
55 |
$p->add_field('notify_url', $this_script.'?action=ipn');
|
|
|
56 |
$p->add_field('item_name', 'Paypal Test Transaction');
|
|
|
57 |
$p->add_field('amount', '1.99');
|
|
|
58 |
|
|
|
59 |
$p->submit_paypal_post(); // submit the fields to paypal
|
|
|
60 |
//$p->dump_fields(); // for debugging, output a table of all the fields
|
|
|
61 |
break;
|
|
|
62 |
|
|
|
63 |
case 'success': // Order was successful...
|
|
|
64 |
|
|
|
65 |
// This is where you would probably want to thank the user for their order
|
|
|
66 |
// or what have you. The order information at this point is in POST
|
|
|
67 |
// variables. However, you don't want to "process" the order until you
|
|
|
68 |
// get validation from the IPN. That's where you would have the code to
|
|
|
69 |
// email an admin, update the database with payment status, activate a
|
|
|
70 |
// membership, etc.
|
|
|
71 |
|
|
|
72 |
echo "<html><head><title>Success</title></head><body><h3>Thank you for your order.</h3>";
|
|
|
73 |
foreach ($_POST as $key => $value) { echo "$key: $value<br>"; }
|
|
|
74 |
echo "</body></html>";
|
|
|
75 |
|
|
|
76 |
// You could also simply re-direct them to another page, or your own
|
|
|
77 |
// order status page which presents the user with the status of their
|
|
|
78 |
// order based on a database (which can be modified with the IPN code
|
|
|
79 |
// below).
|
|
|
80 |
|
|
|
81 |
break;
|
|
|
82 |
|
|
|
83 |
case 'cancel': // Order was canceled...
|
|
|
84 |
|
|
|
85 |
// The order was canceled before being completed.
|
|
|
86 |
|
|
|
87 |
echo "<html><head><title>Canceled</title></head><body><h3>The order was canceled.</h3>";
|
|
|
88 |
echo "</body></html>";
|
|
|
89 |
|
|
|
90 |
break;
|
|
|
91 |
|
|
|
92 |
case 'ipn': // Paypal is calling page for IPN validation...
|
|
|
93 |
|
|
|
94 |
// It's important to remember that paypal calling this script. There
|
|
|
95 |
// is no output here. This is where you validate the IPN data and if it's
|
|
|
96 |
// valid, update your database to signify that the user has payed. If
|
|
|
97 |
// you try and use an echo or printf function here it's not going to do you
|
|
|
98 |
// a bit of good. This is on the "backend". That is why, by default, the
|
|
|
99 |
// class logs all IPN data to a text file.
|
|
|
100 |
|
|
|
101 |
if ($p->validate_ipn()) {
|
|
|
102 |
|
|
|
103 |
// Payment has been recieved and IPN is verified. This is where you
|
|
|
104 |
// update your database to activate or process the order, or setup
|
|
|
105 |
// the database with the user's order details, email an administrator,
|
|
|
106 |
// etc. You can access a slew of information via the ipn_data() array.
|
|
|
107 |
|
|
|
108 |
// Check the paypal documentation for specifics on what information
|
|
|
109 |
// is available in the IPN POST variables. Basically, all the POST vars
|
|
|
110 |
// which paypal sends, which we send back for validation, are now stored
|
|
|
111 |
// in the ipn_data() array.
|
|
|
112 |
|
|
|
113 |
// For this example, we'll just email ourselves ALL the data.
|
|
|
114 |
$subject = 'Instant Payment Notification - Recieved Payment';
|
|
|
115 |
$to = 'YOUR EMAIL ADDRESS HERE'; // your email
|
|
|
116 |
$body = "An instant payment notification was successfully recieved\n";
|
|
|
117 |
$body .= "from ".$p->ipn_data['payer_email']." on ".date('m/d/Y');
|
|
|
118 |
$body .= " at ".date('g:i A')."\n\nDetails:\n";
|
|
|
119 |
|
|
|
120 |
foreach ($p->ipn_data as $key => $value) { $body .= "\n$key: $value"; }
|
|
|
121 |
mail($to, $subject, $body);
|
|
|
122 |
}
|
|
|
123 |
break;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
?>
|