<? include("../settings.php"); ?>
<?
	require_once('config.php');
	
	// Secret Key
	Stripe::setApiKey($stripe['secret_key']);
	
	//print_r($_POST);
	$token  = $_POST['stripeToken'];

	$customer = Stripe_Customer::create(array(
	  'email' => $_POST['stripeEmail'],
	  'source'  => $token
	));

	$charge = Stripe_Charge::create(array(
	  'customer' => $customer->id,
	  'amount'   => $_REQUEST['total_payment']*100,
	  //'amount'   => 100,
	  'currency' => 'sgd',
	  "description" => "SICC11",
	  "statement_descriptor" => "SICC11",
	));

	$bind = array(
		":search1"=>$_REQUEST['registrant_id']
	);
	$update = array(
		"stripe_email"=>$_POST['stripeEmail'],
		"stripe_id"=>$charge->id,
		"registrant_paid"=>"PAID"
	);
	

	if ($charge->status == "succeeded"){
		
		$db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);  
		
		$bind = array(":search1"=>$_REQUEST['registrant_id']);
		$results = $db->select("tbl_registrant","registrant_id = :search1",$bind);
		$registrant = $results[0];
		$bind = array(":search1"=>"creditcard");
		$results = $db->select("tbl_email","email_description = :search1",$bind);
		$result = $results[0];
		$subject = $result['email_subject'];
		$message = $result['email_content'];
		$message = str_replace("@registrant_salutation@",$registrant['registrant_salutation'],$message);
		$message = str_replace("@registrant_firstname@",$registrant['registrant_firstname'],$message);
		$message = str_replace("@registrant_lastname@",$registrant['registrant_lastname'],$message);
		$email = $registrant['registrant_email'];
		$mail->IsHTML(true);
		$mail->SetFrom("SICC11_secretariat@events-sp.com","Ms Sylvia Chan");
		$mail->AddAddress($email);
		$mail->Subject = $subject;
		$mail->Body = $message;
		$mail->Send(); // send message
	}else{
		
		$message = "Sorry, we have encountered some issues with your payment. Please try again later.";
	}  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SICC11</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

	<style>
		.row > div > img { width: 100%; display: block; }
	</style>
    <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
      <link href="css/bootstrap-theme.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
      
        <noscript>
            <p>This site is best viewed with Javascript.</p>
        </noscript>
  </head>
  <body>
<div class="well">
<div class="content" bgcolor="#FFFFD0">
  <div class="col-12 feature">
 	<form class="form-horizontal" method="post" action="charge.php">
		
		<br/>
        
		<div class="panel panel-primary">
            <div class="panel-heading  text-center">Acknowledgement</div>
            <div class="panel-body">
                <div class="panel-body">
                <div class="col-lg-1"></div>
                <div class="col-lg-10">
                   <?=$message;?>	
                </table>
                </div>
                <div class="col-lg-1"></div>
            </div>
            </div>
        </div> 
        </form>
    </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.2.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>

  </div>
</div>
</body>
</html>