Хотя бы просто обработка MtGox redeemable code.
<?php
$ratio = 1;
$mail = array (
'from' => '',
'to' => '',
'subject' => '',
'parameters' => '',
);
$mtgox_key = '';
$mtgox_secret = base64_decode ('');
$logfile = 'mtgox.log';
$mtgox_error = false;
function mtgox_error ($m, $curl = null) {
global $mtgox_error;
if ($curl) $m .= ': ' . curl_error ($curl);
$mtgox_error = "$m\n";
}
function mtgox_log ($result = null, $amount = null) {
global $code, $msg, $mtgox_error, $logfile;
$m = array (
'time' => time (),
'code' => $code,
'msg' => $msg,
'addr' => $_SERVER ['REMOTE_ADDR'],
);
if (isset ($result)) $m ['result'] = $result;
if (is_numeric ($amount)) $m ['amount'] = $amount;
if (is_string ($mtgox_error)) $m ['error'] = $mtgox_error;
$f = fopen ($logfile, 'at');
if ($f) {
fwrite ($f, serialize ($m) . "\n");
fclose ($f);
}
}
function mtgox0 ($method, $data = array ()) {
global $mtgox_key, $mtgox_secret;
$curl = curl_init ("https://mtgox.com/api/0/$method.php");
if (! $curl) return mtgox_error ('curl_init () failed');
if (! curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true)) return mtgox_error ('curl_setopt (CURLOPT_RETURNTRANSFER) failed', $curl);
if (! curl_setopt ($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; ' . php_uname ('s') . '; PHP/' . phpversion () . ')')) mtgox_error ('curl_setopt (CURLOPT_USERAGENT) failed', $curl);
$r = explode (' ', microtime ());
$data ['nonce'] = $r [1] . substr ((int) (($r [0] + 1) * 1000000), 1);
$r = http_build_query ($data);
if (! curl_setopt ($curl, CURLOPT_POSTFIELDS, $r)) return mtgox_error ('curl_setopt (CURLOPT_POSTFIELDS) failed', $curl);
if (! curl_setopt ($curl, CURLOPT_HTTPHEADER, array (
"Rest-Key: $mtgox_key",
'Rest-Sign: ' . base64_encode (hash_hmac ('sha512', $r, $mtgox_secret, true)),
))) return mtgox_error ('curl_setopt (CURLOPT_HTTPHEADER) failed', $curl);
$r = curl_exec ($curl);
if (curl_errno ($curl)) return mtgox_error ('curl_exec () failed', $curl);
if (! is_string ($r)) return mtgox_error ('CURL did not return data');
$d = json_decode ($r, true);
$e = json_last_error ();
if ($e) return mtgox_error ("json_decode () failed: $e; raw data: $r");
return $d;
}
$msg = array_key_exists ('msg', $_POST) ? trim ($_POST ['msg']) : '';
$code = array_key_exists ('code', $_POST) ? trim ($_POST ['code']) : '';
if (array_key_exists ('more', $_POST)) $code = '';
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html version="-//W3C//DTD XHTML 1.1//EN" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<title>Код Mt.Gox</title>
<style type="text/css">
body {
background: #67727B;
font-family: "Lucida Grande", Tahoma, Arial, Verdana, sans-serif;
}
#container {
width: 35em;
margin: 20px auto;
background: black;
padding-top: 40px;
box-shadow: 0 0 5px #000;
}
form, textarea, input {
color: #000;
}
form, textarea, input.text {
background: #FFF;
}
form {
padding: 1em 3em 2em;
}
.field {
margin: 1em 0;
}
textarea, input.text {
font-family: inherit;
font-size: inherit;
width: 28em;
box-shadow: inset 0 1px 1px #CCC;
border-bottom: 1px solid #DDD;
border-left: 1px solid #C3C3C3;
border-right: 1px solid #C3C3C3;
border-top: 1px solid #7C7C7C;
display: block;
margin-top: .2em;
}
.error {
color: red;
}
</style>
</head>
<body>
<div id="container">
<form action="<?php echo htmlspecialchars (basename ($_SERVER ['PHP_SELF'])); ?>" method="post">
<?php
if (strlen ($msg) && strlen ($code)) {
mtgox_log ();
?>
<input name="msg" type="hidden" value="<?php echo htmlspecialchars ($msg, ENT_NOQUOTES); ?>" />
<input name="code" type="hidden" value="<?php echo htmlspecialchars ($code); ?>" />
<table>
<tr><td align="right">Сообщение:</td><td align="left"><pre><?php echo htmlspecialchars ($msg, ENT_NOQUOTES); ?></pre></td></tr>
<tr><td align="right">Код:</td><td align="left"><?php echo htmlspecialchars ($code, ENT_NOQUOTES); ?></td></tr>
<?php
$success = false;
$error = '';
$r = mtgox0 ('redeemCode', array ('code' => $code));
if ($r) {
if (is_array ($r)) {
if (array_key_exists ('amount', $r) && array_key_exists ('currency', $r) && is_numeric ($r ['amount']) && $r ['amount']) {
$c = $r ['currency'];
$amount = number_format ($r ['amount'] * $ratio, 2);
mtgox_log ($r, $amount);
$m =
"К зачислению: $amount $c\n" .
"\nСообщение\n$msg\n\n" .
"Код: $code\n" .
"\nMt. Gox\n";
foreach ($r as $n => $v) $m .= "$n: $v\n";
$m .= "\nIP-адрес: " . $_SERVER ['REMOTE_ADDR'] . "\n";
$m = str_replace ("\r\n", "\n", $m);
$m = str_replace ("\r", "\n", $m);
$m = str_replace ("\n.", "\n..", $m);
$m = str_replace ("\n", "\r\n", $m);
$success = mail ($mail ['to'], $mail ['subject'], $m, "Content-Type: text/plain; charset=UTF-8\r\nFrom: " . $mail ['from'], $mail ['parameters']);
echo '<tr><td align="right">Цена кода:</td><td align="left">', htmlspecialchars ($r ['amount'] . ' ' . $c, ENT_NOQUOTES), '</td></tr>';
echo '<tr><td align="right">Будет зачислено:</td><td align="left">', htmlspecialchars ($amount . ' ' . $c, ENT_NOQUOTES), '</td></tr>';
if (array_key_exists ('reference', $r))
echo '<tr><td align="right">Код операции:</td><td align="left">', htmlspecialchars ($r ['reference'], ENT_NOQUOTES), '</td></tr>';
}
if (array_key_exists ('error', $r) && is_string ($r ['error'])) $error = 'Сообщение от Mt. Gox: ' . $r ['error'];
} else mtgox_log ($r);
} else mtgox_log ();
?>
</table>
<?php
if (! $success) echo '<p class="error">' . (strlen ($error) ? htmlspecialchars ($error, ENT_NOQUOTES) : 'Произошла ошибка. Пожалуйста, свяжитесь со службой поддержки.') . '</p><input type="submit" value="Попробовать ещё раз" /> ';
echo '<input name="more" type="submit" value="Использовать другой код" />';
} else {
?>
<div class="field">
<label>Сообщение<textarea name="msg" cols="33" rows="10"><?php echo htmlspecialchars ($msg, ENT_NOQUOTES); ?></textarea></label>
<?php if (array_key_exists ('msg', $_POST) && ! strlen ($msg)) echo '<div class="error">Напишите сообщение.</div>'; ?>
</div>
<div class="field">
<label>Код Mt. Gox<input name="code" class="text" value="<?php echo htmlspecialchars ($code); ?>" /></label>
<?php if (array_key_exists ('code', $_POST) && ! strlen ($code) && ! array_key_exists ('more', $_POST)) echo '<div class="error">Введите код.</div>'; ?>
</div>
<div><input type="submit" /></div>
<?php
}
?>
</form>
</div>
</body>
</html>