Displaying Paypal IPN payment data in Adobe Flex via AMFPHP
This article explains how to access paypal payment data sent from IPN (instant payment notification) to a MySQL database using AMHPHP and displayed in a Flex front-end. The end result in this example is a fully customizable packing slip application that can search the database, display data as a packing slip, and print the packing slip. Of course, it is easy to see that using this method, one could manipulate MySQL data in any number of ways.
Example (there are two transactions. For the transaction IDs, enter 1234 or 5678 - right click for source)
This method contains several steps, so I'll put them in three categories:
1)Setting up IPN
2)Setting Up AMFPHP
3)Building the Flex Application
Setting up IPN:
About IPN:
“Instant Payment Notification (IPN) is PayPal's interface for handling real-time purchase confirmation and server-to-server communications. IPN delivers immediate notification and confirmation of PayPal payments you receive and provides status and additional data on pending, cancelled, or failed transactions.”
You can do all kinds of stuff with IPN, but here, we're using it to store transaction information on our own database. When someone checks out of our paypal store, paypal sends the transaction data to our database (and emails us a copy).
IPN Manual
https://www.paypal.com/us/cgi-bin/webscr?cmd=p/xcl/rec/ipn-manual-outside
IPN code samples
https://www.paypal.com/us/cgi-bin/webscr?cmd=p/xcl/rec/ipn-code-outside
Basically (for us) this consists of two php scripts, one that handles the verification of the IPN, and if verified, populates the database. The other script emails you the result, whether the transaction is verified or not. The script I'm using here are slightly altered versions of scripts I got here:
https://paypaltech.com/SG2/
Let's begin. First you should have a paypal shopping cart system of some type. This implementation used FlexPal:
http://www.tetraktysdesign.com/flexpal/
Note: In order for IPN to be called, you need to add the following variable to the cart data you send to paypal (however you are doing it):
¬ify_url=http://www.path/to/your/paypalphpIPN.php
Now,
1)set up a MySQL database. I use phpMyAdmin http://www.phpmyadmin.net/home_page/index.php to administer the database.
with phpMyAdmin, you just select the database, then click the 'import'tab, browse to the .sql file, and import it. You can also just use the command line sql client too, but that is beyond the scope of this article.
2)set up the following table structure (you can just import this .sql file to your database.):
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `yourDbName`
--
-- --------------------------------------------------------
--
-- Table structure for table `paypal_cart_info`
--
CREATE TABLE `paypal_cart_info` (
`txnid` varchar(30) NOT NULL default '',
`itemname` varchar(255) NOT NULL default '',
`itemnumber` varchar(50) default NULL,
`amount` decimal(250,2) NOT NULL default '0',
`os0` varchar(20) default NULL,
`on0` varchar(50) default NULL,
`os1` varchar(20) default NULL,
`on1` varchar(50) default NULL,
`quantity` char(3) NOT NULL default '',
`invoice` varchar(255) NOT NULL default '',
`custom` varchar(255) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `paypal_cart_info`
--
INSERT INTO `paypal_cart_info` (`txnid`, `itemname`, `itemnumber`, `amount`, `os0`, `on0`, `os1`, `on1`, `quantity`, `invoice`, `custom`) VALUES
('1234', 'Sample Item 1', '001', 5.95, '', '', '', '', '1', '', ''),
('1234', 'Sample Item 2', '002', 4.99, '', '', '', '', '1', '', ''),
('1234', 'sample Item 3', '003', 1.50, '', '', '', '', '1', '', ''),
('5678', 'Sample Item 4', '004', 15.95, '', '', '', '', '1', '', ''),
('5678', 'Sample Item 5', '005', 4.99, '', '', '', '', '1', '', '');
-- --------------------------------------------------------
--
-- Table structure for table `paypal_payment_info`
--
CREATE TABLE `paypal_payment_info` (
`firstname` varchar(100) NOT NULL default '',
`lastname` varchar(100) NOT NULL default '',
`buyer_email` varchar(100) NOT NULL default '',
`street` varchar(100) NOT NULL default '',
`city` varchar(50) NOT NULL default '',
`state` char(3) NOT NULL default '',
`zipcode` varchar(11) NOT NULL default '',
`memo` varchar(255) default NULL,
`itemname` varchar(255) default NULL,
`itemnumber` varchar(50) default NULL,
`os0` varchar(20) default NULL,
`on0` varchar(50) default NULL,
`os1` varchar(20) default NULL,
`on1` varchar(50) default NULL,
`quantity` char(3) default NULL,
`paymentdate` varchar(50) NOT NULL default '',
`paymenttype` varchar(10) NOT NULL default '',
`txnid` varchar(30) NOT NULL default '',
`mc_gross` decimal(250,2) NOT NULL default '0',
`mc_shipping` decimal(250,2) NOT NULL default '0',
`mc_handling` decimal(250,2) NOT NULL default '0',
`mc_fee` decimal(250,2) NOT NULL default '0',
`paymentstatus` varchar(15) NOT NULL default '',
`pendingreason` varchar(10) default NULL,
`txntype` varchar(10) NOT NULL default '',
`tax` decimal(250,2) NOT NULL default '0',
`mc_currency` varchar(5) NOT NULL default '',
`reasoncode` varchar(20) NOT NULL default '',
`custom` varchar(255) NOT NULL default '',
`country` varchar(20) NOT NULL default '',
`datecreation` date NOT NULL default '0000-00-00'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `paypal_payment_info`
--
INSERT INTO `paypal_payment_info` (`firstname`, `lastname`, `buyer_email`, `street`, `city`, `state`, `zipcode`, `memo`, `itemname`, `itemnumber`, `os0`, `on0`, `os1`, `on1`, `quantity`, `paymentdate`, `paymenttype`, `txnid`, `mc_gross`, `mc_shipping`, `mc_handling`, `mc_fee`, `paymentstatus`, `pendingreason`, `txntype`, `tax`, `mc_currency`, `reasoncode`, `custom`, `country`, `datecreation`) VALUES
('Elvis', 'McMahon', 'elvis@email.com', '1234 Sample St. #109', 'San Francisco', 'CA', '94321', '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '15:10:51 Jun 25, 2008 PDT', 'instant', '1234', 18.83, 2.00, 3.00, 0.61, 'Completed', '', '', 1.39, '', '', '', 'United States', '2008-06-25'),
('Michael', 'Jordan', 'jman@email.com', '12232 Main St.', 'San Jose', 'CA', '91111', '', '', '', '', '', '', '', '', '19:45:07 Jun 29, 2008 PDT', 'instant', '5678', 28.01, 2.00, 3.00, 1.29, 'Completed', '', '', 2.07, '', '', '', 'United States', '2008-06-29');
-- --------------------------------------------------------
--
-- Table structure for table `paypal_subscription_info`
--
CREATE TABLE `paypal_subscription_info` (
`subscr_id` varchar(255) NOT NULL default '',
`sub_event` varchar(50) NOT NULL default '',
`subscr_date` varchar(255) NOT NULL default '',
`subscr_effective` varchar(255) NOT NULL default '',
`period1` varchar(255) NOT NULL default '',
`period2` varchar(255) NOT NULL default '',
`period3` varchar(255) NOT NULL default '',
`amount1` varchar(255) NOT NULL default '',
`amount2` varchar(255) NOT NULL default '',
`amount3` varchar(255) NOT NULL default '',
`mc_amount1` varchar(255) NOT NULL default '',
`mc_amount2` varchar(255) NOT NULL default '',
`mc_amount3` varchar(255) NOT NULL default '',
`recurring` varchar(255) NOT NULL default '',
`reattempt` varchar(255) NOT NULL default '',
`retry_at` varchar(255) NOT NULL default '',
`recur_times` varchar(255) NOT NULL default '',
`username` varchar(255) NOT NULL default '',
`password` varchar(255) default NULL,
`payment_txn_id` varchar(50) NOT NULL default '',
`subscriber_emailaddress` varchar(255) NOT NULL default '',
`datecreation` date NOT NULL default '0000-00-00'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `paypal_subscription_info`
--
After you have set up your database, upload the IPN folder to your server (you will have to change the hostname, user, password, and database info in the file IPNphp.php.)
Setting up AMFPHP:
For more info on AMFPHP goto http://www.amfphp.org/
A zip of the source files are located in the src folder for this example.
1)Unzip and upload amfphp to your server.
2)upload the two files called PaymentInfo.php and CartInfo.php into the folder amfphp/services. (they are already in there, but wanted to let you know this is where these services files go when you create them).
Building the Flex Application;
For this, just right click on the example and view the source. Main aspects of the code are:
the call to the amfphp gateway:
public var gateway : RemotingConnection;
public function initApplication()
{
gateway = new RemotingConnection( "http://www.path/to/your/amfphp/gateway.php" );
gateway.call( "CartInfo.getUsers", new Responder(onResult, onFault));
}
And I also add a printJob so you can print the packingslip:
private function doPrint():void {
// Create an instance of the FlexPrintJob class.
var printJob:FlexPrintJob = new FlexPrintJob();
// Start the print job.
if (printJob.start() != true) return;
// Add the object to print. Do not scale it.
printJob.addObject(mainCanvas, FlexPrintJobScaleType.SHOW_ALL);
// Send the job to the printer.
printJob.send();}
This is more of a proof of concept (actually just playing around with amfphp – I know the flex code is messy), there may be better ways to do this. Please ask if you need any clarification on the steps involved... thanks,
- pythagoras
source: http://blog.flexcommunity.net