summaryrefslogtreecommitdiff
path: root/copylib
diff options
context:
space:
mode:
Diffstat (limited to 'copylib')
-rw-r--r--copylib/config.php25
-rw-r--r--copylib/merchants.php161
-rw-r--r--copylib/taler-presence.js61
-rw-r--r--copylib/util.php73
4 files changed, 320 insertions, 0 deletions
diff --git a/copylib/config.php b/copylib/config.php
new file mode 100644
index 00000000..c6029c0c
--- /dev/null
+++ b/copylib/config.php
@@ -0,0 +1,25 @@
+<?php
+
+$REFUND_DELTA = 'P3M';
+// set to false when done with local tests
+$explicit_currency = "PUDOS";
+$MERCHANT_CURRENCY = $explicit_currency;
+
+$host = $_SERVER["HTTP_HOST"];
+switch ($host) {
+case "blog.demo.taler.net":
+case "shop.demo.taler.net":
+ $MERCHANT_CURRENCY = "KUDOS";
+ break;
+case "blog.test.taler.net":
+case "shop.test.taler.net":
+ $MERCHANT_CURRENCY = "PUDOS";
+ break;
+default:
+ if(false == $explicit_currency){
+ http_response_code (500);
+ echo "<p>Bank configuration error: No currency for domain $host</p>\n";
+ die();
+ }
+}
+?>
diff --git a/copylib/merchants.php b/copylib/merchants.php
new file mode 100644
index 00000000..fddbda85
--- /dev/null
+++ b/copylib/merchants.php
@@ -0,0 +1,161 @@
+<?php
+
+/**
+ * Return a contract proposition to forward to the backend
+ * Note that `teatax` is an associative array representing a
+ * Taler-style amount (so it has the usual <amount,fration,currency>
+ * triple). Moreover, `teatax` should be a *list* of taxes
+ */
+function _generate_contract($args){
+ $contract = array ('amount' =>
+ array ('value' => $args['amount_value'],
+ 'fraction' => $args['amount_fraction'],
+ 'currency' => $args['currency']),
+ 'max_fee' =>
+ array ('value' => 3,
+ 'fraction' => 01010,
+ 'currency' => $args['currency']),
+ 'transaction_id' => $args['transaction_id'],
+ 'products' => array (
+ array ('description' => $args['description'],
+ 'quantity' => 1,
+ 'price' =>
+ array ('value' => $args['amount_value'],
+ 'fraction' => $args['amount_fraction'],
+ 'currency' => $args['currency']),
+ 'product_id' => $args['product_id'],
+ 'taxes' => $args['taxes'],
+ 'delivery_date' => "Some Date Format",
+ 'delivery_location' => 'LNAME1')),
+ 'timestamp' => "/Date(" . $args['now']->getTimestamp() . ")/",
+ 'expiry' =>
+ "/Date(" . $args['now']->add(new DateInterval('P2W'))->getTimestamp() . ")/",
+ 'refund_deadline' =>
+ "/Date(" . $args['now']->add(new DateInterval($args['refund_delta']))->getTimestamp() . ")/",
+ 'repurchase_correlation_id' => $args['correlation_id'],
+ 'fulfillment_url' => $args['fulfillment_url'],
+ 'merchant' =>
+ array ('address' => 'LNAME2',
+ 'name' => $args['merchant_name'],
+ 'jurisdiction' => 'LNAME3'),
+ 'locations' =>
+ array ('LNAME1' =>
+ array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908,
+ 'street' => 'test street',
+ 'street number' => 20),
+ 'LNAME2' =>
+ array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908,
+ 'street' => 'test street',
+ 'street number' => 20),
+ 'LNAME3' =>
+ array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908)));
+ $json = json_encode (array ('contract' => $contract), JSON_PRETTY_PRINT);
+ return $json;
+}
+
+/**
+ * Return a contract proposition to forward to the backend
+ * Note that `teatax` is an associative array representing a
+ * Taler-style amount (so it has the usual <amount,fration,currency>
+ * triple). Moreover, `teatax` should be a *list* of taxes
+ */
+function generate_contract($amount_value,
+ $amount_fraction,
+ $currency,
+ $transaction_id,
+ $desc,
+ $p_id,
+ $corr_id,
+ $taxes,
+ $now,
+ $fulfillment_url){
+ include("../frontend_lib/config.php");
+ $contract = array ('amount' => array ('value' => $amount_value,
+ 'fraction' => $amount_fraction,
+ 'currency' => $currency),
+ 'max_fee' => array ('value' => 3,
+ 'fraction' => 01010,
+ 'currency' => $currency),
+ 'transaction_id' => $transaction_id,
+ 'products' => array (
+ array ('description' => $desc,
+ 'quantity' => 1,
+ 'price' => array ('value' => $amount_value,
+ 'fraction' => $amount_fraction,
+ 'currency' => $currency),
+ 'product_id' => $p_id,
+ 'taxes' => $taxes,
+ 'delivery_date' => "Some Date Format",
+ 'delivery_location' => 'LNAME1')),
+ 'timestamp' => "/Date(" . $now->getTimestamp() . ")/",
+ 'expiry' => "/Date(" . $now->add(new DateInterval('P2W'))->getTimestamp() . ")/",
+ 'refund_deadline' => "/Date(" . $now->add(new DateInterval($REFUND_DELTA))->getTimestamp() . ")/",
+ 'repurchase_correlation_id' => $corr_id,
+ 'fulfillment_url' => $fulfillment_url,
+ 'merchant' => array ('address' => 'LNAME2',
+ 'name' => 'Free Software Foundation (demo)',
+ 'jurisdiction' => 'LNAME3'),
+
+ 'locations' => array ('LNAME1' => array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908,
+ 'street' => 'test street',
+ 'street number' => 20),
+ 'LNAME2' => array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908,
+ 'street' => 'test street',
+ 'street number' => 20),
+ 'LNAME3' => array ('country' => 'Test Country',
+ 'city' => 'Test City',
+ 'state' => 'Test State',
+ 'region' => 'Test Region',
+ 'province' => 'Test Province',
+ 'ZIP code' => 4908)));
+ $json = json_encode (array ('contract' => $contract), JSON_PRETTY_PRINT);
+ return $json;
+}
+
+
+
+/**
+ * Feed `$json` to the backend and return the "(pecl) http response object"
+ * corresponding to the `$backend_relative_url` call
+ */
+function give_to_backend($backend_host, $backend_relative_url, $json){
+ $url = (new http\URL("http://$backend_host"))
+ ->mod(array ("path" => $backend_relative_url), http\Url::JOIN_PATH);
+
+ $req = new http\Client\Request("POST",
+ $url,
+ array ("Content-Type" => "application/json"));
+
+ $req->getBody()->append($json);
+
+ // Execute the HTTP request
+ $client = new http\Client;
+ $client->enqueue($req)->send();
+ return $client->getResponse();
+}
+?>
diff --git a/copylib/taler-presence.js b/copylib/taler-presence.js
new file mode 100644
index 00000000..2562238b
--- /dev/null
+++ b/copylib/taler-presence.js
@@ -0,0 +1,61 @@
+/* @licstart The following is the entire license notice for the
+ JavaScript code in this page.
+
+ Copyright (C) 2016 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 2.1 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 2.1 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.
+*/
+
+function handleInstall() {
+ var show = document.getElementsByClassName("taler-installed-show");
+ var hide = document.getElementsByClassName("taler-installed-hide");
+ for (var i = 0; i < show.length; i++) {
+ show[i].style.display = "";
+ }
+ for (var i = 0; i < hide.length; i++) {
+ hide[i].style.display = "none";
+ }
+};
+
+function handleUninstall() {
+ var show = document.getElementsByClassName("taler-installed-show");
+ var hide = document.getElementsByClassName("taler-installed-hide");
+ for (var i = 0; i < show.length; i++) {
+ show[i].style.display = "none";
+ }
+ for (var i = 0; i < hide.length; i++) {
+ hide[i].style.display = "";
+ }
+};
+
+function probeTaler() {
+ var eve = new Event("taler-probe");
+ console.log("probing taler");
+ document.dispatchEvent(eve);
+}
+
+document.addEventListener("taler-wallet-present", handleInstall, false);
+document.addEventListener("taler-unload", handleUninstall, false);
+document.addEventListener("taler-load", handleInstall, false);
+
+function initTaler() {
+ handleUninstall();
+ probeTaler();
+}
+
+window.addEventListener("load", initTaler, false);
diff --git a/copylib/util.php b/copylib/util.php
new file mode 100644
index 00000000..3a01f7db
--- /dev/null
+++ b/copylib/util.php
@@ -0,0 +1,73 @@
+<?php
+
+function get(&$var, $default=null) {
+ return isset($var) ? $var : $default;
+}
+
+function &pull(&$arr, $idx, $default) {
+ if (!isset($arr[$idx])) {
+ $arr[$idx] = $default;
+ }
+ return $arr[$idx];
+}
+
+function message_from_missing_param($missing, $link, $link_name="home page"){
+ return "<p>Bad request, no $missing given. Return to <a href=\"$link\">$link_name</a></p>";
+}
+
+function article_state_to_str($article_state){
+ if(null == $article_state || !isset($article_state))
+ return "undefined state";
+ $str = "Is payed? ";
+ $str .= $article_state['ispayed'] ? "true," : "false,";
+ if(!isset($article_state['hc']))
+ $str .= " no hashcode for this article";
+ else $str .= " " . $article_state['hc'];
+ return $str;
+}
+
+function log_string($str){
+ file_put_contents("/tmp/blog.dbg", $str . "\n", FILE_APPEND);
+}
+
+function get_full_uri(){
+
+ return $_SERVER['REQUEST_SCHEME'] . '://'
+ . $_SERVER['HTTP_HOST']
+ . $_SERVER['REQUEST_URI'];
+}
+
+function url_join($base, $path, $strip=false) {
+ $flags = $strip ? (http\Url::STRIP_PATH|http\URL::STRIP_QUERY) : 0;
+ return (new http\URL($base, null, $flags))
+ ->mod(array ("path" => $path), http\Url::JOIN_PATH|http\URL::SANITIZE_PATH)
+ ->toString();
+}
+
+// Get a url with a path relative to the
+// current script's path.
+function url_rel($path, $strip=false) {
+ return url_join(
+ $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
+ $path,
+ $strip);
+}
+
+function template($file, $array) {
+ if (file_exists($file)) {
+ $output = file_get_contents($file);
+ foreach ($array as $key => $val) {
+ $replace = '{'.$key.'}';
+ $output = str_replace($replace, $val, $output);
+ }
+ return $output;
+ }
+}
+
+function str_to_dom($str){
+ $doc = new DOMDocument();
+ $doc->loadHTML($str);
+ return $doc;
+
+}
+?>