Saturday, 23 December 2017
Friday, 22 December 2017
STEP 6 – SETTING UP THE PAYMENTS TABLE
To store payment details in the database a payments table must be created in your database. The following MYSQL code will create a payments table.
1 | CREATE TABLE IF NOT EXISTS `payments` ( |
2 | `id` int (6) NOT NULL AUTO_INCREMENT, |
3 | `txnid` varchar (20) NOT NULL , |
4 | `payment_amount` decimal (7,2) NOT NULL , |
5 | `payment_status` varchar (25) NOT NULL , |
6 | `itemid` varchar (25) NOT NULL , |
7 | `createdtime` datetime NOT NULL , |
8 | PRIMARY KEY (`id`) |
9 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; |
STEP 7 – SANDBOX TESTING / GOING LIVE
PayPal Sandbox offers all of the functionality of PayPal, but the information involves all “Fake accounts” created by the developer. You can create fake buyer and fake seller profiles, then test your PayPal integration through your development website.
During the testing phase of the development process the PayPal links should be prefixed to www.sandbox.paypal.com. You can visit the PayPal Developer website and sign up for a free PayPal Sandbox account
Once the payment process is ready to be used by real customers you will need to remove the sandbox from each PayPal link to www.paypal.com.
That’s it; you’re ready to start taking payments online through your website.
PAYPAL INTEGRATION – SOURCE FILES
STEP 4 – PAYMENTS.PHP (THE RESPONSE)
The next part of the payments.php page handles the response from PayPal. The response is re-assigned to variables and then posted back to PayPal for verification using fsockopen.
If the response is VERIFIED then a validation check can be performed. The check_txnid and check_price functions are called to check that the correct Transaction ID and Price have been returned. The updatePayments function can finally be called to store the details of the payment in the payments table (Step 6).
STEP 5 – FUNCTIONS.PHP
The payments.php page calls upon a number of functions used to validate the returned data and store the response in the database.
1 | // functions.php |
2 | function check_txnid( $tnxid ){ |
3 | global $link ; |
4 | return true; |
5 | $valid_txnid = true; |
6 | //get result set |
7 | $sql = mysql_query( "SELECT * FROM `payments` WHERE txnid = '$tnxid'" , $link ); |
8 | if ( $row = mysql_fetch_array( $sql )) { |
9 | $valid_txnid = false; |
10 | } |
11 | return $valid_txnid ; |
12 | } |
13 |
14 | function check_price( $price , $id ){ |
15 | $valid_price = false; |
16 | //you could use the below to check whether the correct price has been paid for the product |
17 | |
18 | /* |
19 | $sql = mysql_query("SELECT amount FROM `products` WHERE id = '$id'"); |
20 | if (mysql_num_rows($sql) != 0) { |
21 | while ($row = mysql_fetch_array($sql)) { |
22 | $num = (float)$row['amount']; |
23 | if($num == $price){ |
24 | $valid_price = true; |
25 | } |
26 | } |
27 | } |
28 | return $valid_price; |
29 | */ |
30 | return true; |
31 | } |
32 |
33 | function updatePayments( $data ){ |
34 | global $link ; |
35 | |
36 | if ( is_array ( $data )) { |
37 | $sql = mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES ( |
38 | '".$data[' txn_id ']."' , |
39 | '".$data[' payment_amount ']."' , |
40 | '".$data[' payment_status ']."' , |
41 | '".$data[' item_number ']."' , |
42 | '".date("Y-m-d H:i:s")."' |
43 | )", $link ); |
44 | return mysql_insert_id( $link ); |
45 | } |
46 | } |
Subscribe to:
Posts (Atom)