<?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['description'];

#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/thank-you-alot.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." Membership ".$userid) {
}

if($item == $sitename." Last Offer - ".$userid) {


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



buycommission($user['referid'], $amount);
$procommission=0;
$jvcommission=0;
$superjvcommission=0;				


otocommission ( $user ['referid'], $userid, $oto );				
$projvcom = 0;
$jvjvcom = 0;
$superjvjvcom = 0;
$prosupercom = 0;
$jvsupercom = 0;
$superjv2supercom = 0;


}

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

$sql = mysql_query ( "SELECT * FROM downsell LIMIT 1" );
$oto = mysql_fetch_array ( $sql );

//otocommission ( $user ['referid'], $userid, $oto );

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

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

$count = $oto ['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', '" . $oto ['banner_views'] . "', '0')" );

$count --;

}

}

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

$count = $oto ['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', '" . $oto ['button_views'] . "', '0')" );

$count --;

}

}

if ($oto ['traffic_num'] and $oto ['traffic_views']) {

$count = $oto ['traffic_num'];

while ( $count > 0 ) {

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

$count --;

}

}

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

$count = $oto ['ptc_num'];

while ( $count > 0 ) {

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

$count --;

}

}

if ($oto ['headline_num'] and $oto ['headline_views']) {

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

}

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

$count = $oto ['hotlink_num'];

while ( $count > 0 ) {

mysql_query ( "insert into hotlinks (userid,max) values('" . dbput ( $userid ) . "','" . $oto ['hotlink_views'] . "')" );

$count --;

}

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

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

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

$count --;
}
}

if ($oto ['botnav_num']) {

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

$count --;
}
}

if ($oto ['solo_num']) {

$count = $oto ['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 ($oto ['cashsolo_num']) {

$count = $oto ['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 ($oto ['hpcsolo_num']) {

$count = $oto ['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 ($oto ['hpsolo_num']) {

$count = $oto ['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 ($oto ['snfsolo_num']) {

$count = $oto ['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 ($oto ['snfcsolo_num']) {

$count = $oto ['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 ($oto ['ssolo_num']) {

$count = $oto ['ssolo_num'];

while ( $count > 0 ) {

mysql_query ( "INSERT INTO `supersolos` (userid, site) VALUES ('" . dbput ( $userid ) . "','treasure')" );

$count --;

}

}

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

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

}


//end sample IPN code



echo "<meta http-equiv=\"refresh\" content=\"5; url=$domain/thank-you-alot.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
}
?>