Payment Processing With Paypal

 

For now plaincart only accept online payment using Paypal. The reason is that they provide pretty extensive resource for developers. Paypal provide the developers with a "sandbox" that allow developers to test their payment module without any actual money transferred.

Paypal provide many ways to accept money but the one used for this tutorial is the Instant Payment Notification
( IPN ). The reason i choose IPN is it seem to be the easiest way to integrate paypal to this shopping cart. The details on setting up the business account and the developer account are described at the end of this checkout discussion. If you want to read it first just click here

There are three files needed for using Paypal IPN and they are put in include/paypal/. The files are :

  • paypal.inc.php : containing the configuration variables and paypal specific functions
  • payment.php : this is the form that contain the hidden inputs to send to paypal server
  • ipn.php : checks the POST from paypal and update the database accordingly
Before going straight to the code please take a look at the picture below. It displays the checkout flow starting from the checkout confirmation page up to the thankyou page. The ones with blue background takes place on the merchant site ( plaincart.com ) and the ones with white background takes place in Paypal website.

Paypal Checkout Process

Here is what happen on each stage :

1. Checkout Confirmation

After rechecking the shipping and payment information on this page the customer click on the "Confim Order" button.



2. Submit Payment Info

An HTML form is generated containing the payment information ( order id, amount, etc ). You will probaly only see a message like this : "Processing Transaction ... ". But if you take a look at the source you can see the HTML code containing the form and some hidden inputs. This form is submitted to paypal site during page load so if you have a fast connection you may only see it for a split second.

The file that generate these hidden inputs is include/paypal/payment.php which is included from checkout.php. You can see that payment.php is not doing anything complex. It just prints the hidden inputs with values coming from the paypal configuration in paypal.inc.php plus the order id, order amount and the item name ( which is hardcoded to "Plaincart Purchase" ).


3. Paypal Login Page

Once on the Paypal site you will be asked to login or create a new account. I already made a test account for this so you don't need to signup with paypal just to test this. You can use use this information to login :
Email Address: testme@phpwebcommerce.com
PayPal Password: phpwebco

4. Payment Detail

After login you will see the payment detail page. It look something like this :

Paypal Payment Screen
There's an optional message box that the customer ( you ) can fill in. Paypal will pass this message along with other payment information to merchat server ( this site ). When you go to the order page in the admin section you can see the message on the order detail page. To continue with the payment process just click on the "Pay" button.

5. Payment Confirmation

The next screen you will see is the payment confirmation screen from paypal :

Paypal Payment Confirmation


6. Thank You Page

Once you click the "Continue" button Paypal will redirect the customer back to the merchant site ( this site ). Since i'm not using a secure connection ( no https:// stuff ) there will be a warning message about you're being redirected to unsecure site. You can ignore it and just click on "Continue" and you will see the thankyou page. Here is the screenshot :

Thankyou Page

The thankyou page can only be accessed when an order id is found in the session. The order id is put in the session variable after you click the "Confirm" button  on the checkout confirmation page. If no order id is found then the customer is sent directly to the shop main page.

On this last step of checkout an email is sent to the shop admin notifying about the new order. This behaviour can be changed from the admin page on the shop configuration section.

Here is the code for the thankyou page

Source : success.php

<?php
require_once 'library/config.php';

// if no order id defined in the session
// redirect to main page
if (!isset($_SESSION['orderId'])) {
   header('Location: ' . WEB_ROOT);
   exit;
}

$pageTitle = 'Checkout Completed Successfully';
require_once 'include/header.php';

// send notification email
if ($shopConfig['sendOrderEmail'] == 'y') {
   $subject = "[New Order] " . $_SESSION['orderId'];
   $email = $shopConfig['email'];
   $message = "You have a new order. Check the order detail here \n
               http://" . $_SERVER['HTTP_HOST'] . WEB_ROOT .
               'admin/order/index.php?view=detail&oid=' .
               $_SESSION['orderId'] ;
   mail($email, $subject, $message, "From: $email\r\nReturn-path: $email");
}

unset($_SESSION['orderId']);
?>
<p>&nbsp;</p><table width="500" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td align="left" valign="top" bgcolor="#333333"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" bgcolor="#EEEEEE"> <p>&nbsp;</p>
<p>Thank you for shopping with us! We will send the purchased
item(s) immediately. To continue shopping please <a href="index.php">click
here</a></p>
<p>&nbsp;</p></td>
</tr>
</table></td>
</tr>
</table>
<br>
<br>
<?php
require_once 'include/footer.php';
?>

This is really an oversimplified thank you page. Normally an online shop also provide a link where the customer can check her order online or a notice that an order confirmation email has been sent ,etc. However i haven't made any of those so for now this is sufficient.


There's a "hidden step" between step 4 and 5. The customer can't see it because it happen between Paypal server and the merchant server. On this "hidden step" these things happen :

  1. Paypal send a POST request to merchat server to notify about the payment.
  2. Then our server send a reply POST to let Paypal know that we have receive it.
  3. Paypal send a reply notifying the payment status whether it's verified or not

We will take a deeper look at these on the next page

 

 

 

Online Shop - Checkout Confirmation Page PHP MySQL Shopping Cart Tutorial : Online Shop - Using Paypal Online Shop - Handling Paypal IPN
Google
 
Web www.phpwebcommerce.com
 

At long last i'm finally able to update this site. Now the shopping cart can handle payment through paypal. As always you have any critiques, questions, comments or problems about this tutorial please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a lot :-)

 

Get custom programming done at ScriptLance.com!

 

My Experimental Lenses



PHP MySQL Shopping Cart Tutorial
Copyright © 2005 - 2008 www.phpwebcommerce.com