How to sell products on the website via PayPal
Okay, there are two payment options:
- Somebody buys something on your website, you receive the notification by email and after that you can ship the product to the customer (or send it by email).
- Somebody buys something on your website, then the website receives payment notification and automatically send the product to the customer.
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



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
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello, I’ve been able to follow the tutorial with not problem but I’m doing it in my local server, on step 2 should I put localhost/wordpress/themename/includes/paypal.php?
Or in the live server(blogpersonal.net/portfolio) should I do something like blogpersonal.net/portfolio/includes/paypal? or what is the proper way to achieve this?
Thanks,
Kevin
Hey Kevin,
Step 2 won’t work on your local server, but on the live server should be ok this one: http://blogpersonal.net/portfolio/includes/paypal.php
Sorry to bother you again but it appears that the function in the Third step is not working as it’s supposed to.
I’m getting a value from a custom field to be able to send the product by email:
This the custom field:
and then the email that should be sent after purchase was successful:
Can you please tell me where to edit my code?…
Thanks,
Kevin
Hey Kevin,
bloginfo()
function doesn’t return the result, it PRINTS it. You must useget_option()
instead.And I’m not sure if you’re correctly sending
$attachments
towp_mail()
function.