How to sell products on the website via PayPal

Okay, there are two payment options:

I think the second way is absolutely better because it works without you — you can snowboarding or be asleep at this time :)

So, how to make it work?

Step 1. Everything begins with a form

That’s your payment form, more information about PayPal form parameters you can find here and here.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
	<input type="hidden" name="cmd" value="_xclick">
 
	<!-- The PayPal account to pay -->
	<input type="hidden" name="business" id="business" value="YOUR PAYPAL EMAIL HERE" />
 
	<!-- Amount price -->
	<input type="hidden" name="amount" id="amount" value="9.00" />
 
	<!-- It will be shown on PayPal checkout page -->
	<input type="hidden" name="item_name" id="item_name" value="ITEM NAME" />
 
	<!-- Specify your product ID here to process it later -->
	<input type="hidden" name="item_number" id="item_number" value="1038" />
 
	<!-- Charset -->
	<input type="hidden" name="charset" value="utf-8">
 
	<!-- Thank You page, the customer will be redirected after the payment -->
	<input type="hidden" name="return" value="https://rudrastyh.com/thank-you">
 
	<button>Buy Now</button> 
</form>

Paypal IPN (Instant payment notifications) mechanism

Step 2. The IPN settings

Scroll Summary page to the bottom to find Seller Preferences link.
On the Seller Preferences page find «Instant Payment Notifications»
Specify your notification URL here.

Step 3. The code

The last step is to process PayPal notifications. This is the content of /shop/paypal.php file.

<?php
$r = array(
    'status' => $_POST['payment_status'],
    'payer_email'      => $_POST['payer_email']
);
if( isset( $_POST['item_number'] ) ) {
	$r['item_number'] = intval($_POST['item_number']);
} elseif ( isset( $_POST['item_number1'] ) ) {
	$r['item_number'] = intval($_POST['item_number1']);
}
if( strtolower( $_POST['payment_status'] ) == 'completed' && $r['item_number'] ) {
	/*
	 * On success - do some stuff here, you can:
	 * register new user
	 * send the product by email
	 * redirect customer to a product page
	 * etc
	 */
	exit();
}
// uncomment the following lines if you want to receive the debug message each time
// $headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
// $headers .= "From: paypal.php <no-reply@rudrastyh.com>\r\n";
// $msg = print_r($_POST, true);
// mail("misha@rudrastyh.com", 'IPN debug message', $msg, $headers);
exit();
Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me