Previously on the blog
RSS feed
  1. Using POG with Flex
  2. Optimizing your web application
  3. Regenerating large objects
  4. PHP4 or PHP5
  5. New and Improved
  6. Evolution of a cube
  7. POG Museum
  8. POG 3.0 alpha
  9. Initial Performance results Part 2
  10. Initial performance results
  11. Proposal: POG 3.0 object model
  12. Suggest a feature
  13. A new year, A new POG release
  14. Many-Many relations
  15. POG 2.5 Released
  16. POG 2.5 beta
  17. Automatic table alignment
  18. New version: 2.1.2 released
  19. RSS should work well now
  20. RSS feed glitches
  21. What's new in 2.1.0
  22. PHP Objects 2.1.0 (preview)
  23. PHP Object relations FAQ
  24. PHP Object Relations
  25. Searching base64 encoded text
  26. How to debug POG-generated objects
  27. POG UI Tips
  28. Featuring Of Interest links
  29. PHP CRUD
  30. POG 2.0.1: A better code generator
  31. A look at the POG SOAP API
  32. POG 2.0.0 released
  33. Coming soon: Generate parent-child objects
  34. Generated abstraction v/s dynamic abstraction
  35. Zend Framework preview
  36. Coming soon: Generate Objects through SOAP
  37. Easily save images and files to a database
  38. PHP, Paypal & POG
  39. Five advanced Code Generator tips
  40. PHP Pagination using generated objects
  41. PHP Code Generator benchmarks
  42. Representing database objects using an AJAX Tree interface
  43. Using SETUP in a production environment
  44. Description of the generated object package
  45. Introducing PHP Object Generator version 1.6
  46. Using AJAX and PHP Object Generator
  47. When to use Object->SaveNew()
  48. Generating PHP objects in 2006
  49. Happy Holidays
  50. A short video of the POG Setup process
  51. A sneak peek at POG 1.6
  52. POG Tip: Field limits
  53. Previous versions.
  54. Searching the blog and tutorials sections
  55. Generating code with "Other" SQL data types
  56. Five general POG tips
  57. POG source code locations
  58. Microsoft SQL 2005 Express Edition
  59. Impatiently awaiting PHP 5.1 and PDO
  60. Php Object Generator goes open source
  61. POG generates PDO compatible code
  62. Oracle to offer free database
  63. POG Google group
  64. Database Wrappers and POG
  65. Revisions
  66. The generator blog
  67. An explanation of the 'Escape' function.
  68. Mirror, mirror
  69. Using POG to solve real world problems
  70. A php object-relational database tool
  71. A simple and flexible Object Oriented approach to PHP


Want more Php Object Generator?
Back to the Code Generator
The POG Google group
The POG tutorials/code samples
The POG mirror site




PHP, Paypal & POG

written 4074 days ago

A very common task a PHP programmer usually faces is creating a payment form which uses Paypal as payment gateway. For instance, let’s assume you have a payment form which needs to be filled by customers

The work-flow of such payment systems is usually:

  1. Collect the user contact information from your form.
  2. Validate and store the information provided by the user to your database
  3. Redirect the user to the payment gateway to process the payment. (in our case, Paypal)
  4. Use the automated merchant tools available to update your local record of the user once the payment goes through.

Implementing this work-flow is easy as POG makes step 2 and 4 somewhat easier.

Step 1:
Create a POG object to store the form information. We create a “Buyer” object with a bunch of attributes. You can take a look at the source code for this article at the end of the article.

Create your payment form.

Give your form inputs the same names as the corresponding Object attributes. In our case, we created a “Buyer” object and then created a payment form where the input names matched the attribute names of the buyer object. This will ease the way we assign the supplied information to the object, as shown further down.

Step2:
Once the user submits the form, we instantiate a ‘buyer’ object as follows and store the information. Because we the object attributes and form inputs match, we can use PHP variable variables to shorten the code:

$buyer = new Buyer();
$keys = array_keys($_POST);
foreach ($keys as $key => $value)
{

$buyer->{$value} = (isset($_POST[$value]) ? $_POST[$value] : ‘’);
}
$buyer->SaveNew();

Step3:
The second thing we do is switch out the submit button and replace it with a paypal button that will redirect the user to Paypal.com so that they can complete the financial transaction. Your paypal button should look something like this.

To create your own paypal button, you can either adapt the button above or create your own from your Paypal account

The important things to note in the payment buttons are:

  1. The notify_url variable which should match the URL of the your back-end notifications script.
  2. The custom variable which you can use to pass any variable which is returned to you whenever there’s a notification from Paypal. Use your ‘Buyer’ object ID here since it’s a unique value from which you can retrieve the entire buyer object later on and update it with the paid status.

Step4:
Paypal allows you to setup up an Instant Payment Notification (IPN) service which is triggered whenever a transaction goes through successfully. More details can be found in Paypal’s integration guide

Essentially, in from your paypal account, you assign a URL which Paypal will notify whenever a payment transaction goes through.

Here is a sample PHP code which handles the IPN:

// post back to PayPal system to validate
$header .= “POST /cgi-bin/webscr HTTP/1.0\r\n”;
$header .= “Content-Type: application/x-www-form-urlencoded\r\n”;
$header .= “Content-Length: ” . strlen($req) . ”\r\n\r\n”;
$fp = fsockopen (‘www.paypal.com’, 80, $errno, $errstr, 30);

$custom = $_POST[‘custom’];

if (!$fp) {
// HTTP ERROR
echo “error”;
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, “VERIFIED”) == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment

$buyer = new Buyer();
$buyer->Get($custom);
$buyer->Paid = true;
$buyer->Save();

}

The important part of this script has been highlighted. After all checks and verifications have been made, we only need to update our buyer record so that it is marked as “PAID”. To achieve this, we simply use the custom variable we passed when the transaction was first initiated to retrieve the appropriate Buyer object. We then simply update the Paid attribute to true, and save the object back to the database.

We hope this tutorial will give you an idea how POG can be integrated with an existing payment system on your website. In our case, we used Paypal as an example since it is the most common payment gateway these days.

You can also download the source code for this article, which requires only a few modifications to be fully operational.

If you have any question about this article or can’t get the source code working, feel free to place a message on the POG Google group or simply add a comment to this blog.


I am using b2evolution for my blog, and have been trying unsuccessfully to post a message using the Buy Now button from PayPal. The message doesn’t like form or input variables, and in all honesty, I am not a programmer and don’t know very much about PHP at all. PayPal provides a developers kit for PHP, and I took a look at it, but it’s no different than picking up a book of Latin and making sense of it. If you have any suggestions, I’m more than listening. Thanks, Paula


Paula St. Peter - Travis    Nov 17, 07:12 PM    #

first be indulgent about my english (i’m french) i’am looking for a php code that might be very easy for you, i’ve built a website with a shopping cart but now i want to integrate paypal to give my customer another way to pay.So when the customer is checking out he is asked to choose and option to pay his cart.when he click on “pay with paypal or credit cartd” i would like to find a way to dynamically build this button in function of the customer’s shopping cart button so that he would be using a single button to send informations about a multiple-items-shopping-cart to paypal


franck    Oct 4, 06:28 AM    #

  Textile Help
About Php Object Generator
This is a weblog about the Php Object Generator (POG) project, OO PHP, databases and Php code generators in general.

Php Object Generator, (POG) is an open source PHP code generator which automatically generates clean & tested Object Oriented code for your PHP4/PHP5 application.

Subscribe to our RSS feed

Feedback, Feature Requests, Bugs to:
The POG Google group

Send us a Hello through email