gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taldir] branch master updated: use taler-go amount


From: gnunet
Subject: [taler-taldir] branch master updated: use taler-go amount
Date: Sun, 17 Jul 2022 13:16:23 +0200

This is an automated email from the git hooks/post-receive script.

martin-schanzenbach pushed a commit to branch master
in repository taldir.

The following commit(s) were added to refs/heads/master by this push:
     new 02ffc8a  use taler-go amount
02ffc8a is described below

commit 02ffc8a6db99a67a3e0f126f20f1c77cd9621896
Author: Martin Schanzenbach <schanzen@gnunet.org>
AuthorDate: Sun Jul 17 13:16:20 2022 +0200

    use taler-go amount
---
 go.mod                |  1 +
 pkg/rest/taldir.go    | 16 ++++++------
 pkg/taler/merchant.go |  5 ++--
 pkg/util/helper.go    | 69 +++++++++++++++------------------------------------
 4 files changed, 33 insertions(+), 58 deletions(-)

diff --git a/go.mod b/go.mod
index 2aac5b0..f2adedb 100644
--- a/go.mod
+++ b/go.mod
@@ -16,4 +16,5 @@ require (
        gopkg.in/ini.v1 v1.66.4
        gorm.io/driver/postgres v1.3.4
        gorm.io/gorm v1.23.4
+       taler.net/taler-go.git v0.0.0-20220717105356-903c2fdca4c5 // indirect
 )
diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go
index 3977ba8..028fe3b 100644
--- a/pkg/rest/taldir.go
+++ b/pkg/rest/taldir.go
@@ -506,16 +506,16 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
   // FIXME: integer arithmetic
   fixedCost := t.Cfg.Section("taldir-" + 
vars["method"]).Key("challenge_fee").MustString("KUDOS:0")
   sliceDuration := time.Duration(validation.Duration * 1000)
-  cost, currency, err := util.CalculateCost(t.MonthlyFee,
-                                            fixedCost,
-                                            sliceDuration,
-                                            monthDuration)
+  cost, err := util.CalculateCost(t.MonthlyFee,
+                                  fixedCost,
+                                  sliceDuration,
+                                  monthDuration)
   if err != nil {
+    fmt.Println(err)
     w.WriteHeader(http.StatusInternalServerError)
     return
   }
-
-  if cost > 0 {
+  if !cost.IsZero() {
     if validationExists {
       if order.ID != validation.OrderID {
         w.WriteHeader(http.StatusConflict)
@@ -524,8 +524,9 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     }
     if len(validation.OrderID) == 0 {
       // Add new order for new validations
-      orderID, newOrderErr := t.Merchant.AddNewOrder(cost, currency)
+      orderID, newOrderErr := t.Merchant.AddNewOrder(*cost)
       if newOrderErr != nil {
+        fmt.Println(newOrderErr)
         w.WriteHeader(http.StatusInternalServerError)
         return
       }
@@ -537,6 +538,7 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r 
*http.Request){
     // FIXME: Remember that it was activated and paid
     payto, paytoErr := t.Merchant.IsOrderPaid(validation.OrderID)
     if paytoErr != nil {
+      fmt.Println(paytoErr)
       w.WriteHeader(http.StatusInternalServerError)
       log.Println(paytoErr)
       return
diff --git a/pkg/taler/merchant.go b/pkg/taler/merchant.go
index 989306d..1606b3d 100644
--- a/pkg/taler/merchant.go
+++ b/pkg/taler/merchant.go
@@ -7,6 +7,7 @@ import (
   "fmt"
   "errors"
   "io/ioutil"
+  talerutil "taler.net/taler-go.git/pkg/util"
 )
 
 type PostOrderRequest struct {
@@ -136,11 +137,11 @@ func (m *Merchant) IsOrderPaid(orderId string) (string, 
error) {
   return "", nil
 }
 
-func (m *Merchant) AddNewOrder(cost float64, currency string) (string, error) {
+func (m *Merchant) AddNewOrder(cost talerutil.Amount) (string, error) {
   var newOrder PostOrderRequest
   var orderDetail MinimalOrderDetail
   var orderResponse PostOrderResponse
-  orderDetail.Amount = fmt.Sprintf("%s:%f", currency, cost)
+  orderDetail.Amount = cost.String()
   // FIXME get from cfg
   orderDetail.Summary = "This is an order to a TalDir registration"
   newOrder.order = orderDetail
diff --git a/pkg/util/helper.go b/pkg/util/helper.go
index 03fb92b..9243c5e 100644
--- a/pkg/util/helper.go
+++ b/pkg/util/helper.go
@@ -23,11 +23,9 @@ import (
   "fmt"
   "crypto/sha512"
   "math/rand"
-  "errors"
-  "strings"
-  "strconv"
   "time"
   gnunetutil "git.gnunet.org/gnunet-go.git/pkg/util"
+  talerutil "taler.net/taler-go.git/pkg/util"
 )
 
 
@@ -55,57 +53,30 @@ func GenerateChallenge(bytes int) string {
 }
 
 // Check if this is a non-zero, positive amount
-func AmountIsNonZero(amount string) bool {
-  amountFloat, _ := AmountToFloat(amount)
-  return amountFloat > 0
-}
-
-func AmountCurrency(amount string) (string, error) {
-  s := strings.Split(amount, ":")
-  if len(s) != 2 {
-    return "", errors.New("Malformed amount")
-  }
-  return s[0], nil
-}
-
-
-func AmountToFloat(amount string) (float64, error) {
-  s := strings.Split(amount, ":")
-  if len(s) != 2 {
-    return 0.0, errors.New("Malformed amount")
-  }
-  amountFloat, err := strconv.ParseFloat(s[1], 64)
-  if err != nil {
-    return 0.0, errors.New("Malformed value in amount")
+func CalculateCost(sliceCostAmount string, fixedCostAmount string, howLong 
time.Duration, sliceDuration time.Duration) (*talerutil.Amount, error) {
+  sliceCount := int(float64(howLong.Microseconds()) / 
float64(sliceDuration.Microseconds()))
+  sliceCost, err := talerutil.ParseAmount(sliceCostAmount)
+  if nil != err {
+    return nil, err
   }
-  return amountFloat, nil
-}
-
-func ParseAmount(amount string) (float64, string, error) {
-  cur, err := AmountCurrency(amount)
+  fixedCost, err := talerutil.ParseAmount(fixedCostAmount)
   if nil != err {
-    return 0.0, "", errors.New("Currency in amount malformed")
+    return nil, err
   }
-  val, err := AmountToFloat(amount)
-  if err != nil {
-    return 0.0, "", err
+  sum := &talerutil.Amount{
+    Currency: sliceCost.Currency,
+    Value: 0,
+    Fraction: 0,
   }
-  return val, cur, nil
-}
-
-// Check if this is a non-zero, positive amount
-func CalculateCost(sliceCostAmount string, fixedCostAmount string, howLong 
time.Duration, sliceDuration time.Duration) (float64, string, error) {
-  sliceCount := float64(howLong.Microseconds()) / 
float64(sliceDuration.Microseconds())
-  sliceCost, curA, err := ParseAmount(sliceCostAmount)
-  if nil != err {
-    return 0.0, "", errors.New("Currency in amount malformed")
+  for i := 0; i < sliceCount; i++ {
+    sum, err = sum.Add(*sliceCost)
+    if nil != err {
+      return nil, err
+    }
   }
-  fixedCost, curB, err := ParseAmount(fixedCostAmount)
+  sum, err = sum.Add(*fixedCost)
   if nil != err {
-    return 0.0, "", errors.New("Currency in amount malformed")
-  }
-  if curA != curB {
-    return 0.0, "", errors.New("Currency in amounts different")
+    return nil, err
   }
-  return (sliceCount * sliceCost) + fixedCost, curA, nil
+  return sum, nil
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]