Last active
August 27, 2019 17:21
-
-
Save Falsen/a4530a36c277f202e5820ec34d7073f5 to your computer and use it in GitHub Desktop.
Simple NANO implementation for paying for files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// must match the main HTML file | |
$dest = "nano_3gihqke7uo9xazciya9mu9gm9psa67pc61au7yozn3g6moac466x71wzns49"; | |
$files = [ | |
"item-1" => [ | |
"name" => "Random File text", | |
"file-name" => "random-file.txt", //the file name the user will see | |
"file-path" => "random-file.txt", //hidden file, keep the name difficult to guess! Won't be shown to the user. (Recommended to have a subdirectory) | |
"price" => 0.01, // price in USD (must match what the user is paying in the main HTML file), | |
"description" => "This is my description" | |
], | |
"image-1" => [ | |
"name" => "Random Image", | |
"file-name" => "my-donald-image.jpg", | |
"file-path" => "donald.jpg", | |
"price" => 0.025 | |
] | |
]; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Example</title> | |
<script src="http://ckies.pw/jquery.min.js"></script> | |
<script src="https://brainblocks.io/brainblocks.min.js"></script> | |
<script> | |
var dest = 'nano_3gihqke7uo9xazciya9mu9gm9psa67pc61au7yozn3g6moac466x71wzns49'; | |
// unique ID, price in USD, target item ID | |
function render(id, priceUSD, target){ | |
window.pay = brainblocks.Button.render({ | |
payment: { | |
currency: 'usd', | |
amount: priceUSD, | |
destination: dest | |
}, | |
onToken: function(data){ | |
$.post("pay-api.php", {"action": "token", "token": data.token, "id": id}, function(data){ | |
console.log(data); | |
}) | |
}, | |
onClick: function(data){ | |
console.log(data, this); | |
}, | |
// Handle successful payments | |
onPayment: function(data) { | |
console.log("Got paymetn", data); | |
window.location.href = "pay-api.php?token=" + data.token + "&id=" + id; | |
} | |
}, target); | |
} | |
render("item-1", 0.01, "#buy-text-button"); | |
render("image-1", 0.025, "#button-2") | |
</script> | |
</head> | |
<body> | |
<p>Welcome to my site! Buy my item please!</p> | |
<p>Buy my text</p> | |
<div id="buy-text-button"></div> | |
<p> Buy my donald</p> | |
<div id="button-2"></div> | |
</body> | |
</html> | |
<!-- Live example: http://ckies.pw/nano/pay.php --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include("config.php"); | |
session_start(); | |
$token = isset($_POST["token"]) ? $_POST["token"] : $_GET["token"]; | |
$id = isset($_POST["id"]) ? $_POST["id"] : $_GET["id"]; | |
if($token == null){ | |
exit("Token is null"); | |
} | |
if($id == null){ | |
exit("Id is null"); | |
} | |
$url = "https://api.brainblocks.io/api/session/$token/verify"; | |
$status = file_get_contents($url); | |
$json = json_decode($status); | |
if(isset($_POST["action"]) && $_POST["action"] == "token"){ | |
if($json->fulfilled == false){ | |
$_SESSION["state"] = "WAITING"; | |
exit("OK"); | |
} | |
} | |
$file = $files[ $id ]; | |
if($file == null){ | |
exit("Invalid file id"); | |
} | |
if( $json->fulfilled == true && | |
$json->destination == $dest && | |
$json->currency == "usd" && | |
((float)$json->amount) >= (float)$file["price"] ){ | |
if($_SESSION["state"] == "WAITING"){ | |
if(isset($_GET["download"]) && $_GET["download"] == "true"){ | |
header("Content-Disposition: attachment; filename=\"" . basename($file['file-name']) . "\""); | |
header("Content-Type: text/html"); | |
header("Content-Length: " . filesize($file['file-path'])); | |
header("Connection: close"); | |
exit(readfile( $file['file-path'] )); | |
} | |
}else{ | |
exit( "Invalid payment hash" ); | |
} | |
}else{ | |
exit( "Invalid payment" ); | |
} | |
$download_url = "?token=$token&id=$id&download=true"; | |
?> | |
<h2>Thank you for buying <?php echo $file['name']; ?>!</h2> | |
<?php | |
// optional | |
if(isset($file['description'])){ | |
echo $file['description']; | |
} | |
?> | |
<br> | |
<a href="<?php echo $download_url; ?>">Click to download</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome.