cashless2ecash

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

wallee-client_test.go (5580B)


      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_wallee
     20 
     21 import (
     22 	internal_utils "c2ec/internal/utils"
     23 	"errors"
     24 	"fmt"
     25 	"strconv"
     26 	"strings"
     27 	"testing"
     28 )
     29 
     30 // integration tests shall be executed manually
     31 // because of needed stuff credentials.
     32 const ENABLE_WALLEE_INTEGRATION_TEST = false
     33 
     34 // configure the INT_* constants to to run integration tests
     35 // be aware that this can possibly trigger and tamper real
     36 // transactions (in case of the refund).
     37 const INT_TEST_SPACE_ID = 0
     38 const INT_TEST_USER_ID = 0
     39 const INT_TEST_ACCESS_TOKEN = ""
     40 
     41 const INT_TEST_REFUND_AMOUNT = "0"
     42 const INT_TEST_REFUND_EXT_ID = "" // can be anything -> idempotency
     43 const INT_TEST_REFUND_MERCHANT_REFERENCE = ""
     44 const INT_TEST_REFUND_TRANSACTION_ID = 0
     45 
     46 func TestCutSchemeAndHost(t *testing.T) {
     47 
     48 	urls := []string{
     49 		"https://app-wallee.com/api/transaction/search?spaceId=54275",
     50 		"https://app-wallee.com/api/transaction/search?spaceId=54275?spaceId=54275&id=212156032",
     51 		"/api/transaction/search?spaceId=54275?spaceId=54275&id=212156032",
     52 		"http://test.com.ag.ch.de-en/api/transaction/search?spaceId=54275?spaceId=54275&id=212156032",
     53 	}
     54 
     55 	for _, url := range urls {
     56 		cutted := cutSchemeAndHost(url)
     57 		fmt.Println(cutted)
     58 		if !strings.HasPrefix(cutted, "/api") {
     59 			t.FailNow()
     60 		}
     61 	}
     62 }
     63 
     64 func TestWalleeMac(t *testing.T) {
     65 
     66 	// https://app-wallee.com/en-us/doc/api/web-service#_java
     67 	// assuming the java example on the website of wallee is correct
     68 	// the following parameters should result to the given expected
     69 	// result using my Golang implementation.
     70 
     71 	// authStr := "1|100000|1715454671|GET|/api/transaction/read?spaceId=10000&id=200000000"
     72 	secret := "OWOMg2gnaSx1nukAM6SN2vxedfY1yLPONvcTKbhDv7I="
     73 	expected := "PNqpGIkv+4jVcdIYqp5Pp2tKGWSjO1bNdEAIPgllWb7A6BDRvQQ/I2fnZF20roAIJrP22pe1LvHH8lWpIzJbWg=="
     74 
     75 	calculated, err := calculateWalleeAuthToken(100000, int64(1715454671), "GET", "https://some.domain/api/transaction/read?spaceId=10000&id=200000000", secret)
     76 	if err != nil {
     77 		t.Error(err)
     78 		t.FailNow()
     79 	}
     80 
     81 	fmt.Println("expected:", expected)
     82 	fmt.Println("calcultd:", calculated)
     83 
     84 	if expected != calculated {
     85 		t.Error(errors.New("calculated auth token not equal to expected token"))
     86 		t.FailNow()
     87 	}
     88 }
     89 
     90 func TestTransactionSearchIntegration(t *testing.T) {
     91 
     92 	if !ENABLE_WALLEE_INTEGRATION_TEST {
     93 		fmt.Println("info: integration test disabled")
     94 		return
     95 	}
     96 
     97 	filter := WalleeSearchFilter{
     98 		FieldName: "merchantReference",
     99 		Operator:  EQUALS,
    100 		Type:      LEAF,
    101 		Value:     "TTZQFA2QQ14AARC82F7Z2Q9JCH40ZHXCE3BMXJV1FG87BP2GA3P0",
    102 	}
    103 
    104 	req := WalleeTransactionSearchRequest{
    105 		Filter:           filter,
    106 		Language:         "en",
    107 		NumberOfEntities: 1,
    108 		StartingEntity:   0,
    109 	}
    110 
    111 	api := "https://app-wallee.com/api/transaction/search"
    112 	api = internal_utils.FormatUrl(api, map[string]string{}, map[string]string{"spaceId": strconv.Itoa(INT_TEST_SPACE_ID)})
    113 
    114 	hdrs, err := prepareWalleeHeaders(api, "POST", INT_TEST_USER_ID, INT_TEST_ACCESS_TOKEN)
    115 	if err != nil {
    116 		fmt.Println("Error preparing headers (req1): ", err.Error())
    117 		t.FailNow()
    118 	}
    119 
    120 	for k, v := range hdrs {
    121 		fmt.Println("req1", k, v)
    122 	}
    123 
    124 	p, s, err := internal_utils.HttpPost(
    125 		api,
    126 		hdrs,
    127 		&req,
    128 		internal_utils.NewJsonCodec[WalleeTransactionSearchRequest](),
    129 		internal_utils.NewJsonCodec[[]*WalleeTransaction](),
    130 	)
    131 	if err != nil {
    132 		fmt.Println("Error executing request: ", err.Error())
    133 		fmt.Println("Status: ", s)
    134 	} else {
    135 		fmt.Println("wallee response status: ", s)
    136 		fmt.Println("wallee response: ", p)
    137 	}
    138 }
    139 
    140 func TestRefundIntegration(t *testing.T) {
    141 
    142 	if !ENABLE_WALLEE_INTEGRATION_TEST {
    143 		fmt.Println("info: integration test disabled")
    144 		return
    145 	}
    146 
    147 	url := "https://app-wallee.com/api/refund/refund"
    148 	url = internal_utils.FormatUrl(url, map[string]string{}, map[string]string{"spaceId": strconv.Itoa(INT_TEST_SPACE_ID)})
    149 
    150 	hdrs, err := prepareWalleeHeaders(url, "POST", INT_TEST_USER_ID, INT_TEST_ACCESS_TOKEN)
    151 	if err != nil {
    152 		fmt.Println("Error preparing headers: ", err.Error())
    153 		t.FailNow()
    154 	}
    155 
    156 	for k, v := range hdrs {
    157 		fmt.Println("req", k, v)
    158 	}
    159 
    160 	refund := &WalleeRefund{
    161 		Amount:            INT_TEST_REFUND_AMOUNT,
    162 		ExternalID:        INT_TEST_REFUND_EXT_ID,
    163 		MerchantReference: INT_TEST_REFUND_MERCHANT_REFERENCE,
    164 		Transaction: WalleeRefundTransaction{
    165 			Id: INT_TEST_REFUND_TRANSACTION_ID,
    166 		},
    167 		Type: "MERCHANT_INITIATED_ONLINE", // this type will refund the transaction using the responsible processor (e.g. VISA, MasterCard, TWINT, etc.)
    168 	}
    169 
    170 	_, status, err := internal_utils.HttpPost[WalleeRefund, any](url, hdrs, refund, internal_utils.NewJsonCodec[WalleeRefund](), nil)
    171 	if err != nil {
    172 		fmt.Println("Error sending refund request:", err)
    173 		t.FailNow()
    174 	}
    175 	if status != internal_utils.HTTP_OK {
    176 		fmt.Println("Received unsuccessful status code:", status)
    177 		t.FailNow()
    178 	}
    179 }