cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

simulation-client.go (2880B)


      1 // This file is part of taler-cashless2ecash.
      2 // Copyright (C) 2024 Joel Häberli
      3 //
      4 // taler-cashless2ecash is free software: you can redistribute it and/or modify it
      5 // under the terms of the GNU Affero General Public License as published
      6 // by the Free Software Foundation, either version 3 of the License,
      7 // or (at your option) any later version.
      8 //
      9 // taler-cashless2ecash is distributed in the hope that it will be useful, but
     10 // WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 // Affero General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU Affero General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 //
     17 // SPDX-License-Identifier: AGPL3.0-or-later
     18 
     19 package internal_provider_simulation
     20 
     21 import (
     22 	"bytes"
     23 	internal_utils "c2ec/internal/utils"
     24 	"c2ec/pkg/db"
     25 	"c2ec/pkg/provider"
     26 	"fmt"
     27 	"time"
     28 )
     29 
     30 type SimulationTransaction struct {
     31 	provider.ProviderTransaction
     32 
     33 	allow bool
     34 }
     35 
     36 type SimulationClient struct {
     37 	provider.ProviderClient
     38 
     39 	// toggle this to simulate failed transactions.
     40 	AllowNextWithdrawal bool
     41 
     42 	// simulates the provider client fetching confirmation at the providers backend.
     43 	providerBackendConfirmationDelayMs int
     44 }
     45 
     46 func (st *SimulationTransaction) AllowWithdrawal() bool {
     47 
     48 	return st.allow
     49 }
     50 
     51 func (st *SimulationTransaction) AbortWithdrawal() bool {
     52 
     53 	return false
     54 }
     55 
     56 func (st *SimulationTransaction) Confirm(w *db.Withdrawal) error {
     57 
     58 	return nil
     59 }
     60 
     61 func (st *SimulationTransaction) Bytes() []byte {
     62 
     63 	return bytes.NewBufferString("this is a simulated transaction and therefore has no content.").Bytes()
     64 }
     65 
     66 func (sc *SimulationClient) FormatPayto(w *db.Withdrawal) string {
     67 
     68 	return fmt.Sprintf("payto://void/%s", *w.ProviderTransactionId)
     69 }
     70 
     71 func (sc *SimulationClient) SetupClient(p *db.Provider) error {
     72 
     73 	internal_utils.LogInfo("simulation-client", "setting up simulation client. probably not what you want in production")
     74 	fmt.Println("setting up simulation client. probably not what you want in production")
     75 
     76 	sc.AllowNextWithdrawal = true
     77 	sc.providerBackendConfirmationDelayMs = 1000 // one second, might be a lot but for testing this is fine.
     78 	provider.PROVIDER_CLIENTS["Simulation"] = sc
     79 	return nil
     80 }
     81 
     82 func (sc *SimulationClient) GetTransaction(transactionId string) (provider.ProviderTransaction, error) {
     83 
     84 	internal_utils.LogInfo("simulation-client", "getting transaction from simulation provider")
     85 	time.Sleep(time.Duration(sc.providerBackendConfirmationDelayMs) * time.Millisecond)
     86 	st := new(SimulationTransaction)
     87 	st.allow = sc.AllowNextWithdrawal
     88 	return st, nil
     89 }
     90 
     91 func (*SimulationClient) Refund(transactionId string) error {
     92 
     93 	internal_utils.LogInfo("simulation-client", "refund triggered for simulation provider with transaction id: "+transactionId)
     94 	return nil
     95 }