passing reserve details to the wallet via DOM event (#4047)

This commit is contained in:
Marcello Stanisci 2015-11-12 18:21:15 +01:00
parent 5a12cbac29
commit d6a222beca
2 changed files with 34 additions and 13 deletions

View File

@ -43,13 +43,26 @@ $reserve_pk = $_POST['reserve_pk'];
$kudos_amount = $_POST['kudos_amount']; $kudos_amount = $_POST['kudos_amount'];
$mint = $_SERVER['SERVER_NAME']; $mint = $_SERVER['SERVER_NAME'];
// check if the webform has given a well formed amount
$ret = preg_match ('/[0-9]+(\.[0-9][0-9]?)? [A-Z]+/', $kudos_amount, $matches);
if ($matches[0] != $_POST['kudos_amount'])
{
http_response_code(400); // BAD REQUEST
echo "Malformed amount given";
return;
}
$amount_chunks = preg_split('/[ \.]/', $_POST['kudos_amount']);
$amount_fraction = 0;
if (count($amount_chunks) > 2)
$amount_fraction = (double) ("0." . $amount_chunks[1]);
$amount_fraction = $amount_fraction * 1000000;
// pack the JSON // pack the JSON
$json = json_encode (array ('reserve_pub' => $reserve_pk, $json = json_encode (array ('reserve_pub' => $reserve_pk,
'execution_date' => "/Date(" . time() . ")/", 'execution_date' => "/Date(" . time() . ")/",
'wire' => array ('type' => 'test'), 'wire' => array ('type' => 'test'),
'amount' => array ('value' => intval($kudos_amount), 'amount' => array ('value' => intval($amount_chunks[0]),
'fraction' => 0, 'fraction' => $amount_fraction,
'currency' => 'KUDOS'))); // TODO 'KUDOS' example needs 'KUDOS' denom keys .. 'currency' => $amount_chunks[count($amount_chunks) - 1])));
// craft the HTTP request // craft the HTTP request
$req = new http\Client\Request ("POST", $req = new http\Client\Request ("POST",

View File

@ -48,19 +48,21 @@
<input id="wire-funds-button" type="button" value="Wire funds" disabled=true <input id="wire-funds-button" type="button" value="Wire funds" disabled=true
onclick='wire_funds();'></input> onclick='wire_funds();'></input>
</div> </div>
<form id="reserve-form" name="tform" action="/fake_wire_transfer.php" method="POST"> <form id="reserve-form" name="tform" action="/fake_wire_transfer.php" method="POST" onsubmit="signal_reserve()">
<div class="participation" id="fake-wire"> <div class="participation" id="fake-wire">
<br> <br>
Paste your reserve public key here (right-click, "paste"): Paste your reserve public key here (right-click, "paste"):
<input type="text" name="reserve_pk"></input> <input type="text" id="reserve-pk-input" name="reserve_pk"></input>
<br> <br>
Amount to credit to your reserve: Amount to credit to your reserve:
<select id="amount" name="kudos_amount"> <select id="reserve-amn-id" name="kudos_amount">
<option value="1">1 KUDOS</option> <option value="1 KUDOS">1 KUDOS</option>
<option value="2">2 KUDOS</option> <option value="2 KUDOS">2 KUDOS</option>
<option value="5">5 KUDOS</option> <option value="3.01 KUDOS">3.01 KUDOS</option>
<option value="10">10 KUDOS</option> <option value="5 KUDOS">5 KUDOS</option>
<option value="1000">1000 KUDOS</option> <option value="10 KUDOS">10 KUDOS</option>
<option value="10.50 KUDOS">10.50 KUDOS</option>
<option value="1000 KUDOS">1000 KUDOS</option>
</select> </select>
<br> <br>
<input type="submit" value="Submit"></input> <input type="submit" value="Submit"></input>
@ -74,6 +76,11 @@
- it gets installed - it gets installed
- it gets a 'taler-wire-mfirst' event */ - it gets a 'taler-wire-mfirst' event */
function signal_reserve(){
var reserve_submitted = new Event("reserve-submitted");
document.body.dispatchEvent(reserve_submitted);
};
document.body.addEventListener("taler-wallet-present", document.body.addEventListener("taler-wallet-present",
has_taler_wallet_cb, has_taler_wallet_cb,
false); false);
@ -89,9 +96,10 @@
function wire_funds(){ function wire_funds(){
let wallet_param = { var wallet_param = {
'currencies' : new Array("KUDOS"), 'currencies' : new Array("KUDOS"),
'input_name' : 'reserve-pk-id', 'input_amount' : 'reserve-amn-id',
'input_pub' : 'reserve-pk-input',
'base_url' : window.location.host 'base_url' : window.location.host
}; };
var trigger = new CustomEvent("taler-create-reserve", {detail: wallet_param}); var trigger = new CustomEvent("taler-create-reserve", {detail: wallet_param});