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 […]

#PHP, #paypal  /   4

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

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();

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 4

  • Kevin Uriel Fonseca February 4, 2018 at 06:47

    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

      • Kevin Uriel Fonseca February 7, 2018 at 13:14

        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:

        function archivo_enviado($postID){
        	$archivo_directory = 'archivo_enviado_por_email';
        	$archivo_enviado = get_post_meta($postID, $archivo_directory, true);
        	if($archivo_enviado == ''){
        		delete_post_meta($postID, $archivo_directory);
        		add_post_meta($postID, $archivo_directory, '#');
        		return '#';
        		}
        	return $archivo_enviado;
        	}

        and then the email that should be sent after purchase was successful:

        if( strtolower( $_POST['payment_status'] ) == 'completed' && $r['item_number'] ) {
        	/*
        	 * On success - send the product by email.
        	 */
         
        	// Download Button - From Custom Field
        	$archivo_enviado = get_post_meta($post->ID, 'archivo_enviado_por_email', true);
         
        	if ($archivo_enviado == '#') :
        	echo 'We are sorry, we are unable to send you a link due to a server error. Please contact the webmaster at '.bloginfo('admin_email');
        	else :
         
        	$email = '';
        	if( is_user_logged_in() ) {
        		$user = get_user_by('id', get_current_user_id() );
        		$email = $user->user_email;
        	}
         
        	$to = $email;
        	$subject = 'Purchase from'.bloginfo('name').'';
        	$message = 'The product is attached on this email. Thank you for doing bussiness with me.';
        	$headers = 'From:'.bloginfo('admin_email').'\r\n';
        	// Send File by Email
        	$attachments = archivo_enviado(get_the_ID());
        	//$attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );
         
        	wp_mail( $_POST['stripeEmail'], $to, $subject, $message, $headers, $attachments);
         
        	endif;
         
        	exit();

        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 use get_option() instead.

        And I’m not sure if you’re correctly sending $attachments to wp_mail() function.

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email