diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-08-18 22:56:03 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-08-18 22:56:03 +0200 |
commit | 69bbbd9a4d9dd7bc2bd15cea190743f5c5276636 (patch) | |
tree | 2b5492b8afb1c7a3d9f109273d5ebe4ca5021e74 /src/frontend/checkout.php | |
parent | 7e253f1d8b8d93348d8bd1b1144eaee446061dd0 (diff) | |
download | merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.tar.gz merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.tar.bz2 merchant-69bbbd9a4d9dd7bc2bd15cea190743f5c5276636.zip |
clean up logic, more comments, etc.
Diffstat (limited to 'src/frontend/checkout.php')
-rw-r--r-- | src/frontend/checkout.php | 165 |
1 files changed, 97 insertions, 68 deletions
diff --git a/src/frontend/checkout.php b/src/frontend/checkout.php index 44049987..75bd6adb 100644 --- a/src/frontend/checkout.php +++ b/src/frontend/checkout.php @@ -34,109 +34,138 @@ payments methods and actually implementing just Taler; technically the steps are: - 1. retrieve the name of who will receive this donation - 2. show a menu with all the required payments means - 3. create a session - 4. (JavaScript) implement the "checkout" button for the sole Taler way. - Actually, this button's duty is to notice this web portal that the customer - wants to see a certificate, and optionally to pay. - - --> + 1. retrieve the information about the donation from the + form and remember it in a PHP session + 2. show a menu with all the required payments system options, + only showing "Taler" if the wallet is present + 3. (JavaScript) implement the "checkout" button for Taler, + for the demo we ignore the other payment options. +--> <?php - -// getting the donation receiver's name -$got_donation = $_POST['group0']; -// create session -session_start(); -$_SESSION['maydonate'] = true; + // get the donation information from form + $donation_receiver = $_POST['donation_receiver']; + $donation_amount = $_POST['donation_amount']; + + // create PHP session and store donation information in session + session_start(); + $_SESSION['receiver'] = $donation_receiver; + $_SESSION['amount'] = $donation_amount; ?> <form name="tform" action="" method="POST"> -<div id="opt-form" align="left"><br> -<input type="radio" name="group1" value="Lisa">Lisa<br> -<input type="radio" name="group1" value="You Card" checked>You Card<br> -<input type="radio" name="group1" value="Card Me">Card Me<br> -<input id="t-button-id" type="radio" name="group1" value="Taler" disabled="true">Taler<br> -<input type="button" onclick="ok(this.form)" value="Ok"> -</div> + <div id="opt-form" align="left"><br> + <input type="radio" name="payment_system" value="lisa" checked>Lisa</input> + <br> + <input type="radio" name="payment_system" value="ycard">You Card</input> + <br> + <input type="radio" name="payment_system" value="cardme">Card Me</input> + <br> + <input type="radio" name="payment_system" value="taler" + id="taler-radio-button-id" disabled="true">Taler</input> + <br> + <input type="button" onclick="pay(this.form)" value="Ok"> + </div> </form> <script type="text/javascript"> -// trigger certificate generation on the server, and signal certificate -// arrival "here" in the client. - -function ok(form){ - for(var cnt=0; cnt < form.group1.length; cnt++){ - var choice = form.group1[cnt]; - if(choice.checked){ - if(choice.value == "Taler"){ - var cert = new XMLHttpRequest(); - /* request certificate */ - cert.open("POST", "/cert.php", true); - cert.onload = function (e) { - if (cert.readyState == 4) { - if (cert.status == 200){ - /* display certificate (i.e. it sends the JSON string - to the (XUL) extension) */ - sendContract(cert.responseText); - } - else alert("No certificate gotten, status " + cert.status); - } - }; - - cert.onerror = function (e){ - alert(cert.statusText); - }; - - cert.send(null); - } - else alert(choice.value + ": NOT available "); - } - } +/* We got a JSON contract from the merchant, + pass it to the extension */ +function handle_contract(json_contract) +{ + var cevent = new CustomEvent('taler-contract', { 'detail' : jsonContract }); + document.body.dispatchEvent(cevent); }; +/* Trigger Taler contract generation on the server, and pass the + contract to the extension once we got it. */ +function taler_pay(form) +{ + var contract_request = new XMLHttpRequest(); + contract_request.open("POST", "/generate_taler_contract.php", true); + contract_request.onload = function (e) + { + if (contract_request.readyState == 4) + { + if (contract_request.status == 200) + { + /* display contract_requestificate (i.e. it sends the JSON string + to the (XUL) extension) */ + handle_contract(contract_request.responseText); + } + else + { + alert("Failed to receive contract from server. Status was " + contract_request.status); + } + } + }; + contract_request.onerror = function (e) + { + alert(contract_request.statusText); + }; + contract_request.send(null); +} + + +/* This function is called when the user presses the + 'Ok' button. We are now supposed to trigger the + "corret" payment system logic. For this demo, we + only handle "taler". */ +function pay(form) +{ + for (var cnt=0; cnt < form.payment_system.length; cnt++) + { + var choice = form.payment_system[cnt]; + + if (choice.checked) + { + if (choice.value == "taler") + { + taler_pay(form); + } + else + { + alert(choice.value + ": NOT available in this demo!"); + } + } + } +}; -/* the following event gets fired whenever a customer has a Taler +/* The following event gets fired whenever a customer has a Taler wallet installed in his browser. In that case, the webmaster can decide whether or not displaying Taler as a payment option */ - -function has_taler_wallet_cb(aEvent){ +function has_taler_wallet_cb(event) +{ // event awaited by the wallet to change its button's color var eve = new Event('taler-currency'); document.body.dispatchEvent(eve); // enable the Taler payment option from the form - var tbutton = document.getElementById("t-button-id"); + var tbutton = document.getElementById("taler-radio-button-id"); tbutton.removeAttribute("disabled"); }; -// The Taler extension was unloaded, disable the option -function taler_wallet_unload_cb(aEvent){ - var tbutton = document.getElementById("t-button-id"); +/* Function called when the Taler extension was unloaded, + here we disable the option */ +function taler_wallet_unload_cb(event) +{ + var tbutton = document.getElementById("taler-radio-button-id"); tbutton.addAttribute("disabled"); }; - -function sendContract(jsonContract){ - - var cevent = new CustomEvent('taler-contract', { 'detail' : jsonContract }); - document.body.dispatchEvent(cevent); -}; /* FIXME: these triggers do not work when I enable/disable the extension... */ -// to be triggered by the wallet +// Register event to be triggered by the wallet (when present) document.body.addEventListener("taler-wallet", has_taler_wallet_cb, false); -// to be triggered by the wallet when it is unloaded +// Register event to be triggered by the wallet when it is unloaded document.body.addEventListener("taler-unload", taler_wallet_unload_cb, false); - </script> </body> </html> |