<div id="pjStripeRedirect"><?php __('plugin_stripe_redirecting'); ?></div>
<script>
(function () {
	function post(url, body, cb) {
		var xhr = new XMLHttpRequest();
		xhr.open("POST", url, true);
		xhr.setRequestHeader("Content-Type", "application/json");
		xhr.onreadystatechange = function () {
			if (xhr.readyState === 4) { cb(xhr.responseText); }
		};
		xhr.send(JSON.stringify(body));
	}

	post("<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjStripe&action=pjActionCreateOrder", {
		cart: {
			reference_id: "<?php echo addslashes(@$tpl['arr']['reference_id']); ?>",
			amount: "<?php echo addslashes(@$tpl['arr']['amount']); ?>",
			currency: "<?php echo addslashes(@$tpl['arr']['currency_code']); ?>",
			description: "<?php echo addslashes(htmlspecialchars(@$tpl['arr']['item_name'])); ?>",
			return_url: "<?php echo addslashes(@$tpl['arr']['return_url']); ?>"
		}
	}, function (text) {
		try {
			var res = JSON.parse(text);
			if (res && res.url) {
				window.location.href = res.url;
			} else {
				document.getElementById("pjStripeRedirect").innerHTML = (res && res.error) ? res.error : "Error";
			}
		} catch (e) {
			document.getElementById("pjStripeRedirect").innerHTML = "Error";
		}
	});
})();
</script>
