<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];

// required for making tables easier
require_once( $this_file . 'wp-config.php' );

// or an alternative way
// require_once( '../../../wp-config.php' );

global $wpdb;

$aOptions = SofaShoppr::initOptions( false );
$paypal_mode = $aOptions[ 'paypal_mode' ];
$entrycurrency  = $aOptions[ 'entrycurrency' ];

$owner = get_userdata( 1 ); // to admin
$to = $owner->user_email;

if( $paypal_mode == 'sandbox' ) $paypalaction = 'ssl://www.sandbox.paypal.com';
else if( $paypal_mode == 'paypal' ) $paypalaction = 'ssl://www.paypal.com';

// 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( $paypalaction, 443, $errno, $errstr, 30 );

// assign posted variables to local variables
$txn_id = urldecode( stripslashes( $_POST[ 'txn_id' ] ) );
$payment_amount = urldecode( stripslashes( $_POST[ 'mc_gross' ] ) );
$receiver_email = urldecode( stripslashes( $_POST[ 'receiver_email' ] ) );
$payment_date = current_time( mysql );
$first_name = urldecode( stripslashes( $_POST[ 'first_name' ] ) );
$last_name = urldecode( stripslashes( $_POST[ 'last_name' ] ) );
$entire_name = $first_name . ' ' . $last_name;
$payer_status = urldecode( stripslashes( $_POST[ 'payer_status' ] ) );
$custom = urldecode( stripslashes( $_POST[ 'custom' ] ) );
$payer_email = urldecode( stripslashes( $_POST[ 'payer_email' ] ) );
$contact_phone = urldecode( stripslashes( $_POST[ 'contact_phone' ] ) );
$address_street = urldecode( stripslashes( $_POST[ 'address_street' ] ) );
$address_zip = urldecode( stripslashes( $_POST[ 'address_zip' ] ) );
$address_city = urldecode( stripslashes( $_POST[ 'address_city' ] ) );
$address_country = urldecode( stripslashes( $_POST[ 'address_country' ] ) );
$address_country_code = urldecode( stripslashes( $_POST[ 'address_country_code' ] ) );
$entire_country = $address_country . ', ' . $address_country_code;
$mc_currency = urldecode( stripslashes( $_POST[ 'mc_currency' ] ) );
if( $fp ) {
	fputs( $fp, $header . $req );
	while( !feof( $fp ) ) {
		$res = fgets( $fp, 1024 );
		if( strcmp( $res, 'VERIFIED' ) == 0 ) {			
			// allready exist
			$check = $wpdb->get_var( "SELECT * FROM " . $wpdb->prefix . "purchases_history WHERE unique_code = $txn_id" );
			
			if( $check == 0 ) {
				// PAYMENT VALIDATED & VERIFIED!
				$wpdb->insert( ( $wpdb->prefix . 'purchases_history' ), array( 'trans_date' => $payment_date, 'unique_code' => $txn_id, 'trans_status' => $payer_status, 'trans_basket' => $custom, 'user_name' => $entire_name, 'user_email' => $payer_email, 'user_phone' => $contact_phone, 'user_address' => $address_street, 'user_zip' => $address_zip, 'user_city' => $address_city, 'user_country' => $entire_country, 'currency' => $mc_currency, 'gross' => $payment_amount, 'flag_shipped' => 0 ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%d' ) );
				
				// send notification
				$subject = __( "Purchase made", "sofa_shoppr" ) . ', ' . get_bloginfo( 'name' );
				$message = __( "Just to let you know that your order has been received. On a side note, shopping and payment details will be sent by PayPal.", "sofa_shoppr" );
				$message .= '<br /><br />';
				$message .= __( "Thank you very much!", "sofa_shoppr" );
				$message .= '<br />' . get_bloginfo( 'url' );
				$headers = "MIME-Version: 1.0\n" . "From: <$to>\n" . "Content-Type: text/html; charset=\"" . get_option( 'blog_charset' ) . "\"\n";

				// send mail
				wp_mail( $payer_email, $subject, $message, $headers );
			} else {
				// send notification
				$subject = __( "Purchase not accepted", "sofa_shoppr" ) . ', ' . get_bloginfo( 'name' );
				$message = __( "We noticed that you tried to pay twice for the same order which is not allowed. Please set a new order if you like, you may ignore this message.", "sofa_shoppr" );
				$message .= '<br /><br />';
				$message .= __( "Thank you very much!", "sofa_shoppr" );
				$message .= '<br />' . get_bloginfo( 'url' );
				$headers = "MIME-Version: 1.0\n" . "From: <$to>\n" . "Content-Type: text/html; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
				// send mail
				wp_mail( $payer_email, $subject, $message, $headers );
			}
		} else if( strcmp( $res, 'INVALID' ) == 0 ) {
				// send notification
				$subject = __( "Purchase not accepted", "sofa_shoppr" ) . ', ' . get_bloginfo( 'name' );
				$message = __( "PayPal did not accept your payment due to the fact your PayPal account is not valid. We are sorry but your order has to be disregarded.", "sofa_shoppr" );
				$message .= '<br /><br />';
				$message .= __( "Thank you very much!", "sofa_shoppr" );
				$message .= '<br />' . get_bloginfo( 'url' );
				$headers = "MIME-Version: 1.0\n" . "From: <$to>\n" . "Content-Type: text/html; charset=\"" . get_option( 'blog_charset' ) . "\"\n";
				// send mail
				wp_mail( $payer_email, $subject, $message, $headers );
		}
	}
	fclose( $fp );
}
?>