taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

commit b31794a373023c3fafe985671e5268c82d215d46
parent 2c4721e89958f721bc763c1cb49ae5be4b41c8b8
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Tue, 19 Jul 2022 14:09:54 +0200

refactor merchant into taler-go

Diffstat:
Mgo.mod | 2+-
Mpkg/rest/taldir.go | 6+++---
Dpkg/taler/merchant.go | 156-------------------------------------------------------------------------------
3 files changed, 4 insertions(+), 160 deletions(-)

diff --git a/go.mod b/go.mod @@ -16,5 +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 + taler.net/taler-go.git v0.0.0-20220719120550-b4025837dad5 ) diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go @@ -48,7 +48,7 @@ import ( "gorm.io/gorm/logger" "taler.net/taldir/internal/gana" "taler.net/taldir/internal/util" - "taler.net/taldir/pkg/taler" + "taler.net/taler-go.git/pkg/merchant" ) // Taldir is the primary object of the Taldir service @@ -88,7 +88,7 @@ type Taldir struct { ChallengeBytes int // Merchant object - Merchant taler.Merchant + Merchant merchant.Merchant // Monthly fee amount MonthlyFee string @@ -776,6 +776,6 @@ func (t *Taldir) Initialize(cfgfile string) { } merchURL := t.Cfg.Section("taldir").Key("merchant_baseurl_private").MustString("http://merchant.taldir/instances/myInstance") merchToken := t.Cfg.Section("taldir").Key("merchant_token").MustString("secretAccessToken") - t.Merchant = taler.NewMerchant(merchURL, merchToken) + t.Merchant = merchant.NewMerchant(merchURL, merchToken) t.setupHandlers() } diff --git a/pkg/taler/merchant.go b/pkg/taler/merchant.go @@ -1,156 +0,0 @@ -package taler - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "net/http" - talerutil "taler.net/taler-go.git/pkg/util" -) - -type PostOrderRequest struct { - // The order must at least contain the minimal - // order detail, but can override all. - order MinimalOrderDetail - - // If set, the backend will then set the refund deadline to the current - // time plus the specified delay. If it's not set, refunds will not be - // possible. - RefundDelay int64 `json:"refund_delay,omitempty"` - - // Specifies the payment target preferred by the client. Can be used - // to select among the various (active) wire methods supported by the instance. - PaymentTarget string `json:"payment_target,omitempty"` - - // Specifies that some products are to be included in the - // order from the inventory. For these inventory management - // is performed (so the products must be in stock) and - // details are completed from the product data of the backend. - // FIXME: Not sure we actually need this for now - //InventoryProducts []MinimalInventoryProduct `json:"inventory_products,omitempty"` - - // Specifies a lock identifier that was used to - // lock a product in the inventory. Only useful if - // inventory_products is set. Used in case a frontend - // reserved quantities of the individual products while - // the shopping cart was being built. Multiple UUIDs can - // be used in case different UUIDs were used for different - // products (i.e. in case the user started with multiple - // shopping sessions that were combined during checkout). - LockUuids []string `json:"lock_uuids"` - - // Should a token for claiming the order be generated? - // False can make sense if the ORDER_ID is sufficiently - // high entropy to prevent adversarial claims (like it is - // if the backend auto-generates one). Default is 'true'. - CreateToken bool `json:"create_token,omitempty"` -} - -type MinimalOrderDetail struct { - // Amount to be paid by the customer. - Amount string - - // Short summary of the order. - Summary string -} - -// NOTE: Part of the above but optional -type FulfillmentMetadata struct { - // See documentation of fulfillment_url in ContractTerms. - // Either fulfillment_url or fulfillment_message must be specified. - FulfillmentUrl string `json:"fulfillment_url,omitempty"` - - // See documentation of fulfillment_message in ContractTerms. - // Either fulfillment_url or fulfillment_message must be specified. - FulfillmentMessage string `json:"fulfillment_message,omitempty"` -} - -type PostOrderResponse struct { - // Order ID of the response that was just created. - OrderId string `json:"order_id"` -} - -type PostOrderResponseToken struct { - // Token that authorizes the wallet to claim the order. - // Provided only if "create_token" was set to 'true' - // in the request. - Token string -} - -type CheckPaymentStatusResponse struct { - // Status of the order - OrderStatus string `json:"order_status"` -} - -type CheckPaymentPaytoResponse struct { - // Status of the order - TalerPayUri string `json:"taler_pay_uri"` -} - -type Merchant struct { - - // The host of this merchant - BaseUrlPrivate string - - // The access token to use for the private API - AccessToken string -} - -func NewMerchant(merchBaseUrlPrivate string, merchAccessToken string) Merchant { - return Merchant{ - BaseUrlPrivate: merchBaseUrlPrivate, - AccessToken: merchAccessToken, - } -} - -func (m *Merchant) IsOrderPaid(orderId string) (string, error) { - var orderPaidResponse CheckPaymentStatusResponse - var paytoResponse CheckPaymentPaytoResponse - resp, err := http.Get(m.BaseUrlPrivate + "/private/orders/" + orderId) - if nil != err { - return "", err - } - defer resp.Body.Close() - if http.StatusOK != resp.StatusCode { - message := fmt.Sprintf("Expected response code %d. Got %d", http.StatusOK, resp.StatusCode) - return "", errors.New(message) - } - respData, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - err = json.NewDecoder(bytes.NewReader(respData)).Decode(&orderPaidResponse) - if err != nil { - return "", err - } - if orderPaidResponse.OrderStatus != "paid" { - err = json.NewDecoder(bytes.NewReader(respData)).Decode(&paytoResponse) - return paytoResponse.TalerPayUri, err - } - return "", nil -} - -func (m *Merchant) AddNewOrder(cost talerutil.Amount) (string, error) { - var newOrder PostOrderRequest - var orderDetail MinimalOrderDetail - var orderResponse PostOrderResponse - orderDetail.Amount = cost.String() - // FIXME get from cfg - orderDetail.Summary = "This is an order to a TalDir registration" - newOrder.order = orderDetail - reqString, _ := json.Marshal(newOrder) - resp, err := http.Post(m.BaseUrlPrivate+"/private/orders", "application/json", bytes.NewBuffer(reqString)) - - if nil != err { - return "", err - } - defer resp.Body.Close() - if http.StatusOK != resp.StatusCode { - message := fmt.Sprintf("Expected response code %d. Got %d", http.StatusOK, resp.StatusCode) - return "", errors.New(message) - } - err = json.NewDecoder(resp.Body).Decode(&orderResponse) - return orderResponse.OrderId, err -}