<?php
include "config.php";
include "Stripe/init.php";
include "Stripe/syek.php";
\Stripe\Stripe::setApiKey("$secret");

// Token is created using Stripe.js or Checkout!
// Get the payment token submitted by the form:

$token        = $_POST['stripeToken'];
$email        = $_POST['stripeEmail'];
$uid          = $_POST['uid'];
$currency     = $_POST['currency'];
$amount       = $_POST['amount'];
$name         = $_POST['name'];
$item         = $_POST['name'];
$description  = $_POST['description'];
$userid 	  = $_POST['userid'];

#This is the standard try catch block stripe suggests
try{
$charge = \Stripe\Charge::create(array(
"amount"        => $amount,
"currency"      => $currency,
"description"   => $name,
"receipt_email" => $email,
"metadata" => array("order_id" => $orderid),
"source" => $token,
));

date_default_timezone_set('America/New_York');
$orderid 	  = $_POST['orderid'];
$ip 	      = $_POST['ip'];
$email	      = $_POST['stripeEmail'];
$today = date('Y-m-d h:i:s',strtotime("now"));

$amount = $amount/100;
$netamount = $amount*97/100-0.30;

include "secureheader.php";
include "style.php";

echo "<div align=\"center\"><span class=\"copy\">Thank you. Your payment has been completed.<br>A receipt for your payment has been sent to $email<br>Returning you back to the <a href=\"$domain/members/index.php\" class=\"copy\">site</a> in 5 seconds.</span></div><br><br>";

include "securefooter.php";

//sample IPN code below change to your own


//add_income($amount);

$query = "select * from members where userid='".$userid."'";
$result = mysql_query ($query) or die ("Query failed");
$numrows = @ mysql_num_rows($result);

if ($numrows == 1) {
$user = mysql_fetch_array($result);
if($item == $sitename." Special Offer - ".$userid) {

if ($mpool == "1") {
ppbuypool ( $userid, $payment_amount );
}

buycommission ( $user ['referid'], $payment_amount );

$projvcom = 0;
$jvjvcom = 0;
$superjvjvcom = 0;
$prosupercom = 0;
$jvsupercom = 0;
$superjv2supercom = 0;

$item2 = str_replace($sitename." Special Offer ", "", $description);
$item2 = substr($item2,0,-(strlen($userid)+3));

mysql_query ( "INSERT INTO transactions VALUES ('id', '" .  ( $userid ) . "','Stripe Payment - Special Offer','" . time () . "','$$amount')" );

$sql = mysql_query ( "SELECT * FROM offerpage LIMIT 1" );
$offer = mysql_fetch_array ( $sql );
$offer = $offer;

if(strlen($item2)>1) {
$item2 = substr($item2,3);

$item3 = explode("/ ", $item2);
foreach($item3 as $each) {
$upgrade = explode(" ", $each);

if($upgrade[2]==1) {
mysql_query("UPDATE members SET points=points+'".$upgrade[0]*$offer['upgrade1_points']."' WHERE userid='$userid'");

    
} elseif($upgrade[2]==2) {
$count = $upgrade[0]*$offer['upgrade2_solos'];
while($count > 0) {
mysql_query("INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '".$userid."', '0', '', '', '0', '0')");
//mysql_query("INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count--;

}	

} elseif($upgrade[2]==3) {
$count = $upgrade[0]*$offer['upgrade3_banners'];
while($count > 0) {
mysql_query("INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '".$userid."', '0', '0', '0', '".$offer['upgrade3_views']."', '0')");
$count--;

}	

} elseif($upgrade[2]==4) {
$count = $upgrade[0]*$offer['upgrade4_traffic'];
while($count > 0) {
mysql_query("insert into tlinks (userid,paid) values('$userid','".$offer['upgrade4_views']."')");
$count--;

}	

} elseif($upgrade[2]==5) {
$count = $upgrade[0]*$offer['upgrade5_login'];
while($count > 0) {
mysql_query("insert into loginads (userid,max) values('$userid','".$offer['upgrade5_views']."')");
$count--;

}					

    
} elseif($upgrade[2]==6) {
$count = $upgrade[0]*$offer['upgrade6_hotlink'];
while($count > 0) {
mysql_query("insert into hotlinks (userid,max) values('$userid','".$offer['upgrade6_views']."')");
$count--;

}					

    
} elseif($upgrade[2]==7) {
$count = $upgrade[0]*$offer['upgrade7_button'];
while($count > 0) {
mysql_query("INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '".$userid."', '0', '0', '0', '".$offer['upgrade7_views']."', '0')");
$count--;

}					

    
} elseif($upgrade[2]==8) {
$count = $upgrade[0]*$offer['upgrade8_headline'];
while($count > 0) {
mysql_query("insert into headlineads (userid,max,purchase) values('$userid','".$offer['upgrade8_views']."',NOW())");
$count--;

}					

    
} elseif($upgrade[2]==9) {
$count = $upgrade[0]*$offer['upgrade9_ptc'];
while($count > 0) {
mysql_query("insert into ptcads (userid,paid) values('$userid','".$offer['upgrade9_views']."')");
$count--;

}					

    
} elseif($upgrade[2]==10) {
$count = $upgrade[0]*$offer['upgrade10_cashsolos'];
while($count > 0) {
mysql_query("INSERT INTO `cashsolos` (`userid`) VALUES ('".$userid."')");
$count--;

}					
}
}
}



if ($offer ['points'])
mysql_query ( "UPDATE members SET points=points+" . $offer ['points'] . " WHERE userid='" . dbput ( $userid ) . "'" );

if ($offer ['banner_num'] and $offer ['banner_views']) {
$count = $offer ['banner_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . dbput ( $userid ) . "', '0', '0', '0', '" . $offer ['banner_views'] . "', '0')" );
$count --;

}
}

if ($offer ['button_num'] and $offer ['button_views']) {
$count = $offer ['button_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . dbput ( $userid ) . "', '0', '0', '0', '" . $offer ['button_views'] . "', '0')" );
$count --;

}
}

if ($offer ['traffic_num'] and $offer ['traffic_views']) {
$count = $offer ['traffic_num'];
while ( $count > 0 ) {
mysql_query ( "insert into tlinks (userid,paid) values('" . dbput ( $userid ) . "','" . $offer ['traffic_views'] . "')" );
$count --;

}
}

if ($offer ['login_num'] and $offer ['login_views']) {
$count = $offer ['login_num'];
while ( $count > 0 ) {
mysql_query ( "insert into loginads (userid,max) values('" . dbput ( $userid ) . "','" . $offer ['login_views'] . "')" );
$count --;

}
}

if ($offer ['hotlink_num'] and $offer ['hotlink_views']) {
$count = $offer ['hotlink_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO hotlinks (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hotlink_views'] . "','" . time () . "')" );
$count --;

}
}

if ($offer ['headline_num'] and $offer ['headline_views']) {
$count = $offer ['headline_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO headlineads (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['headline_views'] . "','" . time () . "')" );
$count --;

}
}

if ($offer ['solo_num']) {
$count = $offer ['solo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['hheadlinead_num'] and $offer ['hheadlinead_views']) {
$count = $offer ['hheadlinead_num'];
while ( $count > 0 ) {
mysql_query ( "insert into hheadlineads (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hheadlinead_views'] . "',NOW())" );
$count --;

}
}

if ($offer ['hheaderad_num'] and $offer ['hheaderad_views']) {
$count = $offer ['hheaderad_num'];
while ( $count > 0 ) {
mysql_query ( "insert into hheaderads (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hheaderad_views'] . "',NOW())" );
$count --;

}
}

if ($offer ['cashsolo_num']) {
$count = $offer ['cashsolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `cashsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['hpcsolo_num']) {
$count = $offer ['hpcsolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `hpcsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['hpsolo_num']) {
$count = $offer ['hpsolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `hpsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['snfsolo_num']) {
$count = $offer ['snfsolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `snfsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['snfcsolo_num']) {
$count = $offer ['snfcsolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `snfcsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );
$count --;

}
}

if ($offer ['upgrade_pro'] == "1") {
upgrade_jv ( $userid );
}

if ($offer ['upgrade_pro'] == "2") {
upgrade_superjv ( $userid );
}

if ($offer ['ptc_num'] and $offer ['ptc_views']) {

$count = $offer ['ptc_num'];

while ( $count > 0 ) {

mysql_query ( "insert into ptcads (userid,paid) values('" . dbput ( $userid ) . "','" . $offer ['ptc_views'] . "')" );
$count --;

}
}

if ($offer ['topnav_num']) {
$count = $offer ['topnav_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `topnav` (`userid` , `date`) VALUES ('" . dbput ( $userid ) . "', '" . time () . "')" );
$count --;

    
}
}

if ($offer ['botnav_num']) {
$count = $offer ['botnav_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `botnav` (`userid` , `date`) VALUES ('" . dbput ( $userid ) . "', '" . time () . "')" );
$count --;

    
}
}

if ($offer ['ssolo_num']) {
$count = $offer ['ssolo_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `supersolos` (userid, site) VALUES ('" . dbput ( $userid ) . "','treasure')" );
$count --;

    
}
}
    
    
elseif ($item == $sitename . " Special Offer Ad Package") {

mysql_query ( "INSERT INTO transactions VALUES ('id', '" . dbput ( $userid ) . "','Stripe Payment - Special Offer Ad Package','" . time () . "','$$amount')" );

$sql = mysql_query ( "SELECT * FROM offerpage2 LIMIT 1" );

$offer = mysql_fetch_array ( $sql );

if ($offer ['points'])
mysql_query ( "UPDATE members SET points=points+" . $offer ['points'] . " WHERE userid='" . dbput ( $userid ) . "'" );

if ($offer ['banner_num'] and $offer ['banner_views']) {

$count = $offer ['banner_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . dbput ( $userid ) . "', '0', '0', '0', '" . $offer ['banner_views'] . "', '0')" );

$count --;

}

}

if ($offer ['button_num'] and $offer ['button_views']) {

$count = $offer ['button_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . dbput ( $userid ) . "', '0', '0', '0', '" . $offer ['button_views'] . "', '0')" );
$count --;
}
}
if ($offer ['traffic_num'] and $offer ['traffic_views']) {

$count = $offer ['traffic_num'];

while ( $count > 0 ) {

mysql_query ( "insert into tlinks (userid,paid) values('" . dbput ( $userid ) . "','" . $offer ['traffic_views'] . "')" );

$count --;

}

}

if ($offer ['login_num'] and $offer ['login_views']) {

$count = $offer ['login_num'];
while ( $count > 0 ) {
mysql_query ( "insert into loginads (userid,max) values('" . dbput ( $userid ) . "','" . $offer ['login_views'] . "')" );
$count --;
}
}
if ($offer ['headline_num'] and $offer ['headline_views']) {

$count = $offer ['headline_num'];
while ( $count > 0 ) {
mysql_query ( "insert into headlineads (userid,max) values('" . dbput ( $userid ) . "','" . $offer ['headline_views'] . "')" );
$count --;
}

}

if ($offer ['hotlink_num'] and $offer ['hotlink_views']) {

$count = $offer ['hotlink_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO hotlinks (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hotlink_views'] . "','" . time () . "')" );
$count --;
}
}
if ($offer ['solo_num']) {

$count = $offer ['solo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}

if ($offer ['hheadlinead_num'] and $offer ['hheadlinead_views']) {
$count = $offer ['hheadlinead_num'];
while ( $count > 0 ) {
mysql_query ( "insert into hheadlineads (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hheadlinead_views'] . "',NOW())" );
$count --;
}
}
if ($offer ['hheaderad_num'] and $offer ['hheaderad_views']) {
$count = $offer ['hheaderad_num'];
while ( $count > 0 ) {
mysql_query ( "insert into hheaderads (userid,max,purchase) values('" . dbput ( $userid ) . "','" . $offer ['hheaderad_views'] . "',NOW())" );
$count --;
}
}
if ($offer ['cashsolo_num']) {

$count = $offer ['cashsolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `cashsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}
if ($offer ['hpcsolo_num']) {

$count = $offer ['hpcsolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `hpcsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}
if ($offer ['hpsolo_num']) {

$count = $offer ['hpsolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `hpsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}
if ($offer ['snfsolo_num']) {

$count = $offer ['snfsolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `snfsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}
if ($offer ['snfcsolo_num']) {

$count = $offer ['snfcsolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `snfcsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . dbput ( $userid ) . "', '0', '', '', '0', '0')" );

$count --;

}

}

if ($offer ['upgrade_pro'] == "1") {
upgrade_jv ( $userid );
}

if ($offer ['upgrade_pro'] == "2") {
upgrade_superjv ( $userid );
}
if ($offer ['ptc_num'] and $offer ['ptc_views']) {

$count = $offer ['ptc_num'];

while ( $count > 0 ) {

mysql_query ( "insert into ptcads (userid,paid) values('" . dbput ( $userid ) . "','" . $offer ['ptc_views'] . "')" );

$count --;

}

}

if ($offer ['topnav_num']) {

$count = $offer ['topnav_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `topnav` (`userid` , `date`) VALUES ('" . dbput ( $userid ) . "', '" . time () . "')" );

$count --;
}
}

if ($offer ['botnav_num']) {

$count = $offer ['botnav_num'];
while ( $count > 0 ) {
mysql_query ( "INSERT INTO `botnav` (`userid` , `date`) VALUES ('" . dbput ( $userid ) . "', '" . time () . "')" );

$count --;


}

}

}

}
}


//end sample IPN code



echo "<meta http-equiv=\"refresh\" content=\"5; url=$domain/members/index.php\">";

} catch(Stripe_CardError $e) {

$error = $e->getMessage();
// Since it's a decline, Stripe_CardError will be caught
$body = $e->getJsonBody();
$err  = $body['error'];

print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
print('Param is:' . $err['param'] . "\n");
print('Message is:' . $err['message'] . "\n");
} catch (Stripe_InvalidRequestError $e) {

// Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
}
?>