Technology Powered Knowledge Base
Click Here To Make FREE Audio & Video Call From Web Browser!

Paypal One Time and Recurring Payment using Web Form and IPN

VN:F [1.9.22_1171]
Rating: +45 (from 45 votes)
VN:F [1.9.22_1171]
Rating: 9.7/10 (50 votes cast)

Paypal have various type of payment integration methods. They support Simple Button, HTML Form, API as payment integration methods. Merchant can chose how buyer can pay from Merchant site or via Paypal Site. NVP, SOAP etc supported protocols for Paypal API.
 
Here we will understand how can build a payment system with HTML Form, Fields and IPN. This method categorized under Paypal Website Payments Standard. After selection of product/quantity/amount buyer will be redirected to Paypal page and they can pay with Credit Card or via Paypal account, after successful payment they will again back to merchant site and can see Thank You Page. Here we will understand One Time and Recurring both type of payment methods.
 

Configuration Parameter:


<?php
// sandbox test environment
$paypal_endpoint="https://www.sandbox.paypal.com/cgi-bin/webscr";
$paypal_merchant="sandbox_merchant@sandbox_merchant.com";

// live payment environment
/*
$paypal_endpoint="https://www.paypal.com/cgi-bin/webscr";
$paypal_merchant="real_merchant@real_domain.com";
*/

// webapp url configuration
$webapp_return="http://www.merchant_domain.com/payment/return.php";
$webapp_cancel="http://www.merchant_domain.com/payment/cancel.php";
$webapp_notify="http://www.merchant_domain.com/payment/notify.php";
?>

 

Notification/IPN File:


<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
	$value = urlencode(stripslashes($value));
	$req .= "&$key=$value";
}

// 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 ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (!$fp) {
	// if any http error occured and check error
}else{
	fputs($fp, $header . $req);
	while(!feof($fp)){
		$res = fgets ($fp, 1024);
		if(strcmp($res, "VERIFIED") == 0){
			// check the payment_status is successfully 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
		}else if(strcmp ($res, "INVALID") == 0){
			// invalid error occured  and check error
		}
	}
	fclose ($fp);
}
?>

 

One Time Payment:


<form action="<?php echo $paypal_endpoint; ?>" name="contributiontracking" method="post">
    <input type="hidden" name="business" value="<?php echo $paypal_merchant; ?>" />
    
    <input type="hidden" name="return" value="<?php echo $webapp_return; ?>" />
    <input type="hidden" name="cancel_return" value="<?php echo $webapp_cancel; ?>" />
    <input type="hidden" name="notify_url" value="<?php echo $webapp_notify; ?>" />
    <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="no_note" value="1" />
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" value="USD" name="currency_code">
	<input type="hidden" name="country" value="IN" />
    
    <input type="hidden" name="item_name" value="Sample Item" />
    <label>Item Amount:</label>
    <input type="text" name="amount" value="1.00" />
    
    <input type="hidden" name="custom" value="" />
    <input type="submit" name="submit" value="Pay" />
</form>

 

Recurring Payment:


<form action="<?php echo $paypal_endpoint; ?>" name="contributiontracking" method="post">
    <input type="hidden" value="<?php echo $paypal_merchant; ?>" name="business">
    
    <input type="hidden" name="return" value="<?php echo $webapp_return; ?>" />
    <input type="hidden" name="cancel_return" value="<?php echo $webapp_cancel; ?>" />
    <input type="hidden" name="notify_url" value="<?php echo $webapp_notify; ?>" />
    <input type="hidden" name="cmd" value="_xclick-subscriptions" />
    <input type="hidden" name="no_note" value="1" />
    <input type="hidden" name="no_shipping" value="1">
    <input type="hidden" name="currency_code" value="USD">
	<input type="hidden" name="country" value="IN" />
    
    <input type="hidden" value="Sample Item" name="item_name">
    <!-- for trial/first term -->
    <!--
    <input type="hidden" name="a1" value="0.00" />
    <input type="hidden" name="p1" value="2" />
    <input type="hidden" name="t1" value="D" />
    -->
    <!-- for next phase term -->
    <!--
    <input type="hidden" name="a2" value="2.00" />
    <input type="hidden" name="p2" value="3" />
    <input type="hidden" name="t2" value="D" />
    -->
    <!-- for final phase term --> 
    <label>Recurring Amount:</label>
    <input type="text" name="a3" value="3.00" />
    <input type="hidden" name="p3" value="1" />
    <label>Cycle Span:</label>
    <select name="t3">
        <option value="D">Daily</option>
        <option value="W" selected>Weekly</option>
        <option value="M">Monthly</option>
        <option value="Y">Yearly</option>
    </select>
	
    <input type="hidden" name="src" value="1" />
    <label>Total Cycle:</label>
    <input type="text" name="srt" value="2" />
    <input type="hidden" name="sra" value="1" />
    
    <input type="hidden" name="custom" value="" />
    <input type="submit" name="submit" value="Pay" />
</form>

 

After click on “Pay Now” button from Paypal Site by default paypal show a thank you page, after click on “Back to Merchant Site” user can back to actual merchant site. If we want automatically back to Merchant Site only after click on “Pay Now” then we need to modify some settings on Paypal Merchant Account. After sing-in to Paypal Merchant Account, go to My Account ~> Profile ~> More Options ~> Selling Preference ~> Auto Return and make it enable. That’s it.

Paypal One Time and Recurring Payment using Web Form and IPN, 9.7 out of 10 based on 50 ratings



Sign Up     Sign In