Skip to content

Instantly share code, notes, and snippets.

@Falsen
Last active August 27, 2019 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Falsen/a4530a36c277f202e5820ec34d7073f5 to your computer and use it in GitHub Desktop.
Save Falsen/a4530a36c277f202e5820ec34d7073f5 to your computer and use it in GitHub Desktop.
Simple NANO implementation for paying for files
<?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
]
];
?>
<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 -->
<?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>
@mickmon
Copy link

mickmon commented Aug 27, 2019

Awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment