Skip to content

Instantly share code, notes, and snippets.

Created March 20, 2013 02:27
Show Gist options
  • Save anonymous/74c1d02f1c13bf085b61 to your computer and use it in GitHub Desktop.
Save anonymous/74c1d02f1c13bf085b61 to your computer and use it in GitHub Desktop.
<?php
//Check if form was submitted
if (!isset($_GET['refund_addr'])) {
//Protection from XSS attacks
$url = htmlentities($_SERVER['PHP_SELF']);
//Print out the form using heredoc syntax
echo <<< EOT
<form method="GET" action="$url">
Enter an address where we can send you refunds if necessary:
<input type="text" name="refund_addr">
<br>
<input type="submit" value="Click to Pay">
</form>
EOT;
return;
}
try {
//Open the database - EDIT THIS TO CONNECT TO YOUR DB
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
} catch(Exception $e) {
die($error);
}
//Create new invoice in table
$stmt = $db->prepare("INSERT INTO invoices VALUES(null, :refund_addr, 0)");
$stmt->execute(array(':refund_addr' => $_GET['refund_addr']));
//Get the ID of the invoice
$invoice_id = $db->lastInsertId();
$secret = 'ZzsMLGKe162CfA5EcG6j';
$my_address = '1Pqh1kXjMVa8KvSJFg9DoUoKz9G1kNiYvo';
$my_callback_url = 'https://mystore.com?invoice_id='.$invoice_id.'&secret='.$secret;
$root_url = 'http://blockchain.info/api/receive';
$parameters = 'method=create&address=' . $my_address .'&shared=false&callback='. urlencode($my_callback_url);
$response = file_get_contents($root_url . '?' . $parameters);
$object = json_decode($response);
echo "Your invoice ID is $invoice_id. Send payment to " . $object->input_address;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment