How to save E-Junkie Transactions and manage users using a WordPress Blog ?

by KishPress on November 13, 2010

There are many of us who use E-Junkie to sell digital downloads. I also use this service for selling my Plugins. I need to save all the transcations and save the users for getting them registered to my support forum. Registration is open only through a custom registration fees where the user needs to provide only a username and password. Even though we can auto-register all the customers using a auto generated username, I decided to have a manual registration. All the other details of the user like the email, name, etc are taken from the transaction.

Step 1 – Setup your callback URL at E-Junkie

You need to setup the call back url when you add the product. You can create a file and save it as a WordPress plugin

Step 2 – Creating the Plugin File

$root = dirname(dirname(dirname(dirname(__FILE__))));
file_exists($root.'/wp-load.php') ? require_once($root.'/wp-load.php') : require_once($root.'/wp-config.php');

if($_REQUEST['txn_id'] && $_POST['handshake']) {
	$secret=md5("your@ejunkieaccountemail".md5("your_ejunkie_password"));
	if($_POST['handshake']!==$secret){
	    exit;
	}
	  else {
		  kish_ej_insert_transaction($_POST);
	}
}

Inserting E-junkie Transaction

function kish_ej_insert_transaction($arr) {
	global $wpdb;
	$sql = "INSERT INTO " . $wpdb->base_prefix."kish_ej_transactions (mc_gross, invoice, payer_id, payment_date, payment_status, first_name, ";
	$sql.= "payer_status, business, quantity, verify_sign, payer_email, txn_id, last_name, receiver_email, payment_fee, receiver_id, item_name, item_number, residence_country, payment_gross, item_name1, buyer_ip, from_name, from_email) ";
	$sql.= "VALUES ('".$arr['mc_gross']."', '".$arr['invoice']."', '".$arr['payer_id']."', '".$arr['payment_date']."', '".$arr['payment_status']."', '".$arr['first_name']."', ";
	$sql.= "'".$arr['payer_status']."', '".$arr['business']."', '".$arr['quantity']."', '".$arr['verify_sign']."', '".$arr['payer_email']."', ";
	$sql.= "'".$arr['txn_id']."', '".$arr['last_name']."', '".$arr['receiver_email']."', '".$arr['payment_fee']."', '".$arr['receiver_id']."', ";
	$sql.= "'".$arr['item_name']."', '".$arr['item_number']."', '".$arr['residence_country']."', '".$arr['payment_gross']."', '".$arr['item_name1']."', '".$arr['buyer_ip']."', '".$arr['from_name']."', '".$arr['from_email']."')";
	//wp_mail( 'your@email.com', 'Transaction To be saved Kishore', $sql, $headers, $attachments );
	mysql_query($sql, $wpdb->dbh);
}

Now before inserting this, you need to create the table to insert the transactions

function kish_ej_create_table($arr) {
	global $wpdb;
	$sql = "CREATE TABLE IF NOT EXISTS `".$wpdb->base_prefix ."kish_ej_transactions` (`kish_ej_id` INT NOT NULL AUTO_INCREMENT ,";
	foreach($arr as $key => $value) {
		$sql .="`{$key}` VARCHAR( 200 ) NOT NULL ,";
	}
	$sql .=" PRIMARY KEY (  `kish_ej_id` ) ,UNIQUE (`kish_ej_id`));" ;
	//wp_mail( 'kishore@asokans.com', 'Info', $sql, $headers, $attachments );
	$wpdb->query( $sql );
	update_site_option('kish_ej', 'installed');
}

Function to check the transaction

function kishpress_check_txn_email($txnid, $email) {
	global $wpdb;
	$sql="SELECT kish_ej_id FROM `".$wpdb->base_prefix ."kish_ej_transactions` WHERE txn_id = '{$txnid}' AND payer_email = '{$email}' LIMIT 0,1";
	$results=$wpdb->get_results($sql, OBJECT);
	if($results) {
		return true;
	}
	else {
		return false;
	}
}

Getting the Transaction details of a user by transaction ID and Email

function kishpress_get_user_details($txnid, $email) {
	global $wpdb;
	$sql="SELECT * FROM `".$wpdb->base_prefix ."kish_ej_transactions` WHERE txn_id = '{$txnid}' AND payer_email = '{$email}' LIMIT 0,1";
	$results=$wpdb->get_results($sql, OBJECT);
	if($results) {
		return $results;
	}
	else {
		return false;
	}
}

Registering user by verifying the transaction and email details

if($_POST['req']=='regnewuser') {
	if(kishpress_check_txn_email($_POST['txnid'], $_POST['email'])) {
		require_once(ABSPATH . WPINC . '/registration.php');
		$user_id = username_exists($_POST['uname']);
		if ( !strlen($user_id) ) {
			$emailuid=email_exists($_POST['email']);
			if ( !strlen($emailuid) ) {
				$random_password = $_POST['pword'];
				$user_id = wp_create_user( $_POST['uname'], $random_password, $_POST['email'] );
				$uinfo = kishpress_get_user_details($_POST['txnid'], $_POST['email']);
				$ui = $uinfo[0];
				wp_update_user( array ('ID' => $user_id, 'first_name' => $ui->first_name, 'last_name' => $ui->last_name) ) ;
				echo "<p>You have successfully register, you can now login with the registered information - <a href="http://kishpress.com/wp-login.php?redirect_to=http://kishpress.com/forum/">Login Now</a></p>";
			}
			else {
				$uinfo = kishpress_get_user_details($_POST['txnid'], $_POST['email']);
				$ui = $uinfo[0];
				wp_update_user( array ('ID' =&gt; $emailuid, 'first_name' =&gt; $ui-&gt;first_name, 'last_name' =&gt; $ui-&gt;last_name) ) ;
				echo $_POST['email'] . " already registered - you can now login with the registered information - <a href="http://kishpress.com/wp-login.php?redirect_to=http://kishpress.com/forum/">Login Now</a>";
				echo " | <a href="http://kishpress.com/wp-login.php?action=lostpassword">Lost Password</a>";
			}
		}
		else {
			$emailuid=email_exists($_POST['email']);
			if (strlen($emailuid) ) {
				$uinfo = kishpress_get_user_details($_POST['txnid'], $_POST['email']);
				$ui = $uinfo[0];
				wp_update_user( array ('ID' =&gt; $emailuid, 'first_name' =&gt; $ui-&gt;first_name, 'last_name' =&gt; $ui-&gt;last_name) ) ;
				echo "You are already registered. If can login using the registered information - <a href="http://kishpress.com/wp-login.php?redirect_to=http://kishpress.com/forum/">Login Now</a>";
				echo " | <a href="http://kishpress.com/wp-login.php?action=lostpassword">Lost Password</a>";
			}
			else {
				echo $_POST['uname']." already exisits, Please try a different username!!";
			}
		}
	}
	else {
		echo "Sorry Transaction Id / Email Doest not match!!";
	}
}

You can put down all these function to register and validate the users. Also you have the transaction details which can be used to send updates, news letters, etc

Related posts:

  1. How to convert your blog commentators to your YMLP subscribers If you are using YourMailingListProvider for news letters, then you...
  2. WordPress Comment Moderation Tool (WordPress Plugin) There are many blog owners who get tons of comments...
  3. Taking care of your WordPress Blog Search Engine Optimization I have been using WordPress for more than 6 years...
  4. Best Blog Posting tool for iPad I was trying to write new posts using Kish Multi...
  5. WordPress comment Moderation Plugin Kish Multi Pro is not just for doing new Posts,...

{ 1 comment… read it below or add one }

Adrian April 29, 2011 at 11:09 am

Hi there,It doesn’t work, in the code are some bugs, and also ti doesn’t create users.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: