<?php 

require_once('../stripe_lib/vendor/autoload.php');

include_once('../classes/database.class.php');

$action = $_POST['action'];

if ($action == 'payment') {
	# code...
	$email = $_POST['email'];
	$token = $_POST['token'];
	$created = $_POST['created'];
	$type = $_POST['type'];
	$country = $_POST['country'];
	$brand = $_POST['brand'];
	$amount = $_POST['amount'];
	$result = array();

	try
    {

    	$stripe = array(
		  "secret_key"      => "sk_test_lpEsQu6gZI7obbde7o3IdDbv",
		  "publishable_key" => "pk_test_9qxDTxk50XPjrLElngKJ42Gp"
		);

		\Stripe\Stripe::setApiKey($stripe['secret_key']);
		

        $customer = \Stripe\Customer::create(array(
            'email' => $email,
            'source'  => $token
        ));


        $amountforcc = floatval($amount) * 100;

      $charge = \Stripe\Charge::create(array(
          'customer' => $customer->id,
          'amount'   => $amountforcc,
          'currency' => 'usd'
      ));

      if ($charge['status'] == 'succeeded') {

      		$db = new database();


      		$stmt = $db->db->prepare("INSERT INTO payments (token, email, type, country, brand, amount, created, status) VALUES ('$token', '$email', '$type', '$country', '$brand', '$amount', '$created', '0')");

      		if ($stmt->execute()) {
      			# code...

      			// Set content-type header for sending HTML email
				$headers = "MIME-Version: 1.0" . "\r\n";
				$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

				// Additional headers
				$headers .= 'From: Invata<invata.io>' . "\r\n";


      			$regUrl = "https://trading.invata.io/registration.php?token=".$token;

      			$message = '<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>

	<div style="background-color: #181818;padding-top: 40px;padding-bottom: 40px;text-align: center;">
		<img src="https://trading.invata.io/images/invatalogo.png" style="width: 100px;">
	</div>
	<p style="color: #181818;font-weight: 700;">Dear User,</p>
	<p style="color: #181818">Thank you for taking your interest in Invata.io. You have sucessfully paid for invata.io Registration</p>
	<p style="color: #181818">For Registration click button below</p>
	<div style="text-align: center;">
		<br><br>
	<a href="'.$regUrl.'" target="_blank" style="background-color: #181818;color: #fff;text-align: center;padding: 10px 40px;text-decoration: none;">Click Here</a>
	</div>
	<p style="color: #181818">Or you can manually open link</p>
	<br>
	<div style="text-align: center;">
	<a href="https://trading.invata.io/registration.php" target="_blank">https://trading.invata.io/registration.php</a>
	</div>
	<p style="color: #181818">and enter your payment code</p>
	
	<p style="color: #181818;text-align: center;">'.$token.'</p>
</body>
</html>';


        	if (mail($email,"Invata Registration", $message, $headers)) {
        		# code...
        		$result = array('result'=>'success');

        	}else{

        		$result = array('result'=>'failed', 'err'=>'Some error occured');
        	}

      			

      		}else{
      			
      			$result = array('result'=>'failed', 'err'=>'Some error occured');

      		}

      		

      }else{

      	$result = array('result'=>'failed', 'err'=>'Some error occured');

      }


    }catch(Stripe_CardError $e) {


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

		$result = array('result'=>'failed', 'err'=>$err);


     }catch (Stripe_InvalidRequestError $e) {
                          
		$body = $e->getJsonBody();
		$err  = $body['error'];
		$result = array('result'=>'failed', 'err'=>$err);


		} catch (Stripe_AuthenticationError $e) {
		// Authentication with Stripe's API failed
		// (maybe you changed API keys recently)
		$body = $e->getJsonBody();
		$err  = $body['error'];
		$result = array('result'=>'failed', 'err'=>$err);

		} catch (Stripe_ApiConnectionError $e) {
		// Network communication with Stripe failed

		$body = $e->getJsonBody();
		$err  = $body['error'];
		$result = array('result'=>'failed', 'err'=>$err);


		} catch (Stripe_Error $e) {

		$body = $e->getJsonBody();
		$err  = $body['error'];
		$result = array('result'=>'failed', 'err'=>$err);


		} catch (Exception $e) {

		$body = $e->getJsonBody();
		$err  = $body['error'];
		$result = array('result'=>'failed', 'err'=>$err);


		}

		print_r(json_encode($result));
}


?>