<?php
	include 'header.php';
 

	include 'emailsetting.php';

	/*STRIPE*/
	require_once('stripe-php-5.7.0/init.php');
	$mail->SetFrom($from_email, $naziv_firme);
	$mail->AltBody = "Driving payment";
	$mail->Subject = "Driving payment at ".$naziv_firme." website";
?>
 
<div class="header1">Credit card payment</div>
 
<?php

$ime = '';
$prezime = '';
$email = '';
$telefon = '';

$imeErr = ''; $prezimeErr = ''; $emailErr = ''; $telefonErr = ''; $cijenaErr = '';
$testIme=0; $testPrezime=0; $testEmail=0; $testTelefon=0; $testCijena = 0;
	
//PROVJERE
	if (isset($_POST['submitCreditcard']) or isset($_POST['stripeToken'])) {
		include 'validacije.php';

		if (empty($_POST["cijena"])) {
    		$cijenaErr = "Amount is required!";
  		} else {
    		$cijena = test_input($_POST["cijena"]);
			$testCijena = 1;
			if (!filter_var($cijena, FILTER_VALIDATE_INT,  array("options" => array("min_range" => 1, "max_range" => 30000)))) {
      			$cijenaErr = "Amount must be rounded number (integer) between 1 and 30000 (EUR).";
	 			$testCijena = 0;
    		}
		}
	}
	
	//echo $testIme.$testPrezime.$testEmail.$testTelefon.$testCijena;
	if($testIme == 1 and $testPrezime == 1 and $testEmail == 1 and $testTelefon == 1 and $testCijena == 1){		
		if (!empty($_POST['stripeToken'])) {
			
			$token = $_POST['stripeToken'];
			//$stripeEmail = $_POST['stripeEmail'];
			
			//**********************************************
			//*****************STRIPE CHARGE*********************
			//**********************************************
			//echo "Token: ".$token."<br />";
			

			\Stripe\Stripe::setApiKey( $key_sk);
			
			try {
				$charge = \Stripe\Charge::create(array(
  					"amount" => $cijena*100,
  					"currency" => "eur",
  					"description" => $ime." ".$prezime." (".$telefon.") - Driving payment on ".$naziv_firme." website",
  					"source" => $token,
				));
				
				/*if ($charge->card->address_zip_check == "fail") {
					throw new Exception("zip_check_invalid");
				} else if ($charge->card->address_line1_check == "fail") {
					throw new Exception("address_check_invalid");
				} else if ($charge->card->cvc_check == "fail") {
					throw new Exception("cvc_check_invalid");
				}*/
				 	
				$result = "";
				
				echo "<div class='success' style=''>Vielen Dank! Sie haben erfolgreich eine Zahlung geleistet! <br />Thank you! You have successfully made a payment! </div>";
				//echo $stripeEmail;
	
				//formiram tijelo mejla	
				$body = 'Eine zusätzliche Zahlung wurde über die Website vorgenommen! <br /><br />';
				$body .= '<b>Grundlegende Informationen:</b> <br />';
				$body .= 'Kunde/Customer: '.$ime.' '.$prezime.'<br />';
				$body .= 'Telefonnummer/Telephone: '.$telefon.'<br />';
				$body .= 'E-mail: '.$email.'<br /><br />';
				$body .= 'BETRAG/AMOUNT: <span style="color:#ff0000; font-weight:bold;">'.$cijena.' EUR</span><br /><br />';
				$body .= 'Freundliche Grüße!/Best regards!';
				
				$body2 = 'Dear '.$ime.',<br />';
				$body2 .= 'You uccessfully made a payment at '.$naziv_firme.' website <br />';
				$body2 .= 'Amount/Betrag: <span style="font-weight:bold;">'.$cijena.' EUR</span><br /><br />';
				$body2 .= 'Vielen Dank für die Zahlung und danke für Ihr Vertrauen.<br />Thank you for making the payment and thank you for your trust.<br /><br />';
				$body2 .= 'Best regards!<br />';
				$body2 .= $naziv_firme;
				

				//mail za primaoca
				$mail->MsgHTML($body);
				$address = $vlasnik_email;
				$mail->AddAddress($address, "");
    			if($mail->Send()){
    	     		$mail->ClearAddresses();
     	     		$mail->ClearAttachments();
             		//echo 'POSLAT ';
        		}else{
             		$mail->ClearAddresses();
             		$mail->ClearAttachments();
    	     		echo '<div class="decline">'.$error_email.' '.$vlasnik_tel.'</div>';
       			}
				
				//mail za uplatioca
				$mail->MsgHTML($body2);
				$address = $email;
				$mail->AddAddress($address, "");
    			if($mail->Send()){
    	     		$mail->ClearAddresses();
     	     		$mail->ClearAttachments();
             		//echo 'POSLAT ';
        		}else{
             		$mail->ClearAddresses();
             		$mail->ClearAttachments();
    	     		echo '<div class="decline">'.$error_email.' '.$vlasnik_tel.'</div>';
       			}
				
				 	
			} catch(Stripe_CardError $e) {			
					$error = $e->getMessage();
					$result = "Declined";
			} catch (Stripe_InvalidRequestError $e) {
					$error = $e->getMessage();
					$result = "Declined";		  
			} catch (Stripe_AuthenticationError $e) {
					$error = $e->getMessage();
					$result = "Declined";
			} catch (Stripe_ApiConnectionError $e) {
					$error = $e->getMessage();
					$result = "Declined";
			} catch (Stripe_Error $e) {
					$error = $e->getMessage();
					$result = "Declined";
			} catch (Exception $e) {
					$error = $e->getMessage();
					$result = "Declined";
			}
	
			if($result == "Declined"){
				echo "<div class='decline'>Error: ".$error."</div>";
			}
	
			 
			
			exit;		
		
		}else{ //end charge
		
		
			//**********************************************
			//*****************CHECKOUT*********************
			//**********************************************
			?>
		
			<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

				<div class="prikaz_all">
					<div class="podnaslov">Kunde/Customer</div>
					<div class="clr m_padding_bottom">
						<div class="prikaz_lijevo colspan40 user_info">Name:</div>
						<div class="prikaz_desno colspan60 user_info boldirano"><?php echo $ime;?></div>
					</div>
					<div class="clr m_padding_bottom">
						<div class="prikaz_lijevo colspan40 user_info">Nachname/Surname:</div>
						<div class="prikaz_desno colspan60 user_info boldirano"><?php echo $prezime;?></div>
					</div>
					<div class="clr m_padding_bottom">
						<div class="prikaz_lijevo colspan40 user_info">E-mail:</div>
						<div class="prikaz_desno colspan60 user_info boldirano"><?php echo $email;?></div>
					</div>
					<div class="clr m_padding_bottom">
						<div class="prikaz_lijevo colspan40 user_info">Telefon/Telephone:</div>
						<div class="prikaz_desno colspan60 user_info boldirano"><?php echo $telefon;?></div>
					</div>
				</div>
 
			 
			

				<div class="prikaz_all">
				<table class="tabela_ukupno" cellspacing="0" style="margin-top:30px;">
					<tr>
						<th>Dienstleistung/Service</td>
						<th>Gesamtsumme/Total</td>
					</tr>
					<tr>
						<td>Transfer x 1</td>
						<td>EUR <?php echo number_format($cijena, 2);?></td>
					</tr>
					<tr>
						<td>Zwischensumme/Subtotal</td>
						<td>EUR <?php echo number_format($cijena, 2);?></td>
					</tr>
					<tr>
						<td style="font-weight:bold;">Gesamtsumme/Total</td>
						<td style="font-weight:bold;">EUR <?php echo number_format($cijena, 2); ?> (Von Steuern befreit/Exempt from taxes)</td>
					</tr>
				</table>
			</div>
				
			
			<div class="payment" style="border:0px solid #ff0000; width:100%; text-align:center; margin:50px 0px;">
			
  			<script
 				src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    			data-key= <?php echo $key_pk;?>
    			data-amount="<?php echo $cijena*100;?>"
    			data-name="<?php echo $naziv_firme; ?>"
				data-currency="EUR"
    			data-description="Payment via Stripe"
				data-image= <?php echo $url_pocetna."/app/images/icon.png"; ?>
    			data-locale="auto"
    			data-zip-code="true"
				data-label="Pay with card!">
  			</script>
			</div>

			<input type="text" name="ime" value="<?php echo $ime; ?>" style="display:none;" >
			<input type="text" name="prezime" value="<?php echo $prezime; ?>" style="display:none;" >				 
			<input type="text" name="email" value="<?php echo $email; ?>" style="display:none;" >
			<input type="text" name="telefon" value="<?php echo $telefon; ?>" style="display:none;" >				 
			<input type="text" name="cijena" value="<?php echo $cijena;?>" style="display:none;" >
					 
		</form>
		
<?php		
		
		
		}//end checkout
		
	}else{ //ako nije ispunjen uslov forme ?>
		
		<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
			<div class="prikaz_all">
				<div class="colspan50 edit_profile">
					<div class="login_label">Vorname/First Name*</div>					
					<div class="error"><?php echo $imeErr;?></div>		
					<input type="text" name="ime" value="<?php echo $ime;?>" placeholder="<?php echo "Vorname/First Name";?>">
 
					<div class="login_label">Nachname/Surname*</div>
					<div class="error"><?php echo $prezimeErr;?></div>	
					<input type="text" name="prezime" value="<?php echo $prezime;?>" placeholder="<?php echo "Nachname/Surname";?>">
 
				 
					<div class="login_label">E-mail*</div>
					<div class="error"><?php echo $emailErr;?></div>
					<input type="text" name="email" value="<?php echo $email;?>" placeholder="E-mail" >
				 
				 	<div class="login_label">Telefon/Telephone*</div>
					<div class="error"><?php echo $telefonErr;?></div>
					<input type="text" name="telefon" value="<?php echo $telefon;?>" placeholder="Telephone">

				 
				 
					<div class="login_label">Amount/Betrag (EUR)</div>
					<div class="error"><?php echo $cijenaErr;?></div>
					<input type="text" name="cijena" value="<?php echo $cijena;?>" style="width:100%;" >	
				</div>
			</div> 

			<div class="prikaz_all">
					
				<div class="colspan50 edit_profile">
  					<input  class="dugme_submit"  type="submit" name="submitCreditcard" value="<?php echo "PAYMENT";?>" />		
				</div>
			</div>
		</form>
		
	<?php
	}
	?>
			
	


</body>


