summaryrefslogtreecommitdiff
path: root/src/frontend/checkout.php
blob: db4d27b200ef9ca8479e6d9e76ccae85ac50dbe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<title>Choose payment method</title>
    <script>
        /*
        @licstart  The following is the entire license notice for the
        JavaScript code in this page.

        Copyright (C) 2014,2015 GNUnet e.V.

        The JavaScript code in this page is free software: you can
        redistribute it and/or modify it under the terms of the GNU
        Lesser General Public License (GNU LGPL) as published by the Free Software
        Foundation, either version 3 of the License, or (at your option)
        any later version.  The code is distributed WITHOUT ANY WARRANTY;
        without even the implied warranty of MERCHANTABILITY or FITNESS
        FOR A PARTICULAR PURPOSE.  See the GNU LGPL for more details.

        As additional permission under GNU LGPL version 3 section 7, you
        may distribute non-source (e.g., minimized or compacted) forms of
        that code without the copy of the GNU LGPL normally required by
        section 4, provided you include this license notice and a URL
        through which recipients can access the Corresponding Source.

        @licend  The above is the entire license notice
        for the JavaScript code in this page.
        */
    </script>
</head>
<body onload="signal_me()">
<!-- 
  This page's main aim is to show to the customer all the accepted
  payments methods and actually implementing just Taler; technically
  the steps are:
  
  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
  // get the donation information from form
  $donation_receiver = $_POST['donation_receiver'];
  $donation_amount = $_POST['donation_amount'];
  $donation_currency = $_POST['donation_currency'];

  // create PHP session and store donation information in session
  session_start();
  $_SESSION['receiver'] = $donation_receiver;
  $_SESSION['amount'] = $donation_amount;
  $_SESSION['currency'] = $donation_currency;
?>

<form name="tform" action="" method="POST">
  <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">

/* 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: json_contract,
			       target: "/taler/pay"});

  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 extension) alert (contract_request.responseText); */
        handle_contract(contract_request.responseText);
	
      }
      else 
      {
        alert("No contract got from merchant.\n" + contract_request.responseText);
      }
    }
  };
  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
   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)
{

  // enable the Taler payment option from the form
  var tbutton = document.getElementById("taler-radio-button-id");
  tbutton.removeAttribute("disabled");
 
  if (aEvent.type == "taler-wallet-wfirst"){
    var eve = new Event('taler-payment-wfirst');
    document.body.dispatchEvent(eve);
    }

};

/* Function called when the Taler extension was unloaded, 
   here we disable the option */
function taler_wallet_unload_cb(aEvent)
{
  var tbutton = document.getElementById("taler-radio-button-id");
  tbutton.setAttribute("disabled", "true");
};

/* The merchant signals its taler-friendlyness to the client */
function signal_me()
{
  var eve = new Event('taler-payment-mfirst');
  document.body.dispatchEvent(eve);
};


function test_without_wallet(){
  var tbutton = document.getElementById("taler-radio-button-id");
  tbutton.removeAttribute("disabled");
};

test_without_wallet();

// Register event to be triggered by the wallet as a response to our
// first event
document.body.addEventListener("taler-wallet-mfirst", has_taler_wallet_cb, false);

// The following callback is used to allow the button to change its
// color whenever the user navigates away from this page
document.body.addEventListener("taler-shutdown",
  function(){
    var unloadEvent = new Event('taler-unload');
    document.body.dispatchEvent(unloadEvent);
  },
  false);

// event awaited by the wallet to change its button's color
// alert("sending");
// var eve = new Event('taler-payment-mfirst');
// document.body.dispatchEvent(eve);

// Register event to be triggered by the wallet when it gets enabled while
// the user is on the payment page
document.body.addEventListener("taler-wallet-wfirst", has_taler_wallet_cb, false);

// 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>