cashless2ecash

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

commit 48288ad5329c5ff7e77888832a5dc65f0633969b
parent 8103cce7a0467906d8a3e2f07667d3eb5e609578
Author: Joel-Haeberli <haebu@rubigen.ch>
Date:   Thu, 30 May 2024 15:12:34 +0200

fix: refund integration test

Diffstat:
Mc2ec/http-util.go | 4+++-
Mc2ec/wallee-client.go | 10++++++++--
Mc2ec/wallee-client_test.go | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
3 files changed, 69 insertions(+), 9 deletions(-)

diff --git a/c2ec/http-util.go b/c2ec/http-util.go @@ -182,7 +182,9 @@ func HttpPost[T any, R any]( for k, v := range headers { req.Header.Add(k, v) } - req.Header.Add("Accept", resCodec.HttpApplicationContentHeader()) + if resCodec != nil { + req.Header.Add("Accept", resCodec.HttpApplicationContentHeader()) + } req.Header.Add("Content-Type", reqCodec.HttpApplicationContentHeader()) res, err := http.DefaultClient.Do(req) diff --git a/c2ec/wallee-client.go b/c2ec/wallee-client.go @@ -171,7 +171,7 @@ func (w *WalleeClient) Refund(transactionId string) error { } url := FormatUrl(call, map[string]string{}, queryParams) - hdrs, err := prepareWalleeHeaders(url, HTTP_GET, w.credentials.UserId, w.credentials.ApplicationUserKey) + hdrs, err := prepareWalleeHeaders(url, HTTP_POST, w.credentials.UserId, w.credentials.ApplicationUserKey) if err != nil { return err } @@ -200,7 +200,13 @@ func (w *WalleeClient) Refund(transactionId string) error { Type: "MERCHANT_INITIATED_ONLINE", // this type will refund the transaction using the responsible processor (e.g. VISA, MasterCard, TWINT, etc.) } - _, status, err := HttpPost[WalleeRefund, any](url, hdrs, refund, NewJsonCodec[WalleeRefund](), nil) + _, status, err := HttpPost[WalleeRefund, any]( + url, + hdrs, + refund, + NewJsonCodec[WalleeRefund](), + nil, + ) if err != nil { LogError("wallee-client", err) return err diff --git a/c2ec/wallee-client_test.go b/c2ec/wallee-client_test.go @@ -8,11 +8,21 @@ import ( "testing" ) +// integration tests shall be executed manually +// because of needed stuff credentials. const ENABLE_WALLEE_INTEGRATION_TEST = false -const TEST_SPACE_ID = 0 -const TEST_USER_ID = 0 -const TEST_ACCESS_TOKEN = "" -const TEST_TRANSACTION_ID = 0 + +// configure the INT_* constants to to run integration tests +// be aware that this can possibly trigger and tamper real +// transactions (in case of the refund). +const INT_TEST_SPACE_ID = 0 +const INT_TEST_USER_ID = 0 +const INT_TEST_ACCESS_TOKEN = "" + +const INT_TEST_REFUND_AMOUNT = 0 +const INT_TEST_REFUND_EXT_ID = "" // can be anything -> idempotency +const INT_TEST_REFUND_MERCHANT_REFERENCE = "" +const INT_TEST_REFUND_TRANSACTION_ID = 0 func TestCutSchemeAndHost(t *testing.T) { @@ -61,6 +71,7 @@ func TestWalleeMac(t *testing.T) { func TestTransactionSearchIntegration(t *testing.T) { if !ENABLE_WALLEE_INTEGRATION_TEST { + fmt.Println("info: integration test disabled") return } @@ -79,9 +90,9 @@ func TestTransactionSearchIntegration(t *testing.T) { } api := "https://app-wallee.com/api/transaction/search" - api = FormatUrl(api, map[string]string{}, map[string]string{"spaceId": strconv.Itoa(TEST_SPACE_ID)}) + api = FormatUrl(api, map[string]string{}, map[string]string{"spaceId": strconv.Itoa(INT_TEST_SPACE_ID)}) - hdrs, err := prepareWalleeHeaders(api, "POST", TEST_USER_ID, TEST_ACCESS_TOKEN) + hdrs, err := prepareWalleeHeaders(api, "POST", INT_TEST_USER_ID, INT_TEST_ACCESS_TOKEN) if err != nil { fmt.Println("Error preparing headers (req1): ", err.Error()) t.FailNow() @@ -106,3 +117,44 @@ func TestTransactionSearchIntegration(t *testing.T) { fmt.Println("wallee response: ", p) } } + +func TestRefundIntegration(t *testing.T) { + + if !ENABLE_WALLEE_INTEGRATION_TEST { + fmt.Println("info: integration test disabled") + return + } + + url := "https://app-wallee.com/api/refund/refund" + url = FormatUrl(url, map[string]string{}, map[string]string{"spaceId": strconv.Itoa(INT_TEST_SPACE_ID)}) + + hdrs, err := prepareWalleeHeaders(url, "POST", INT_TEST_USER_ID, INT_TEST_ACCESS_TOKEN) + if err != nil { + fmt.Println("Error preparing headers: ", err.Error()) + t.FailNow() + } + + for k, v := range hdrs { + fmt.Println("req", k, v) + } + + refund := &WalleeRefund{ + Amount: INT_TEST_REFUND_AMOUNT, + ExternalID: INT_TEST_REFUND_EXT_ID, + MerchantReference: INT_TEST_REFUND_MERCHANT_REFERENCE, + Transaction: WalleeRefundTransaction{ + Id: INT_TEST_REFUND_TRANSACTION_ID, + }, + Type: "MERCHANT_INITIATED_ONLINE", // this type will refund the transaction using the responsible processor (e.g. VISA, MasterCard, TWINT, etc.) + } + + _, status, err := HttpPost[WalleeRefund, any](url, hdrs, refund, NewJsonCodec[WalleeRefund](), nil) + if err != nil { + fmt.Println("Error sending refund request:", err) + t.FailNow() + } + if status != HTTP_OK { + fmt.Println("Received unsuccessful status code:", status) + t.FailNow() + } +}