From 949ac8af91cfdebe7874d7990131eb15056113c2 Mon Sep 17 00:00:00 2001 From: pilinux Date: Tue, 3 Jan 2023 13:46:51 +0100 Subject: [PATCH] feat: function to retrieve a transaction details --- README.md | 1 + transfer.go | 10 ++++++++++ transfer_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/README.md b/README.md index f5bd033..dfdddf9 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ go mod tidy ### Transfer - [x] transfer balance to a connected Stripe account +- [x] get details of a balance transaction ## Usage diff --git a/transfer.go b/transfer.go index dbc3e8f..a0ada3c 100644 --- a/transfer.go +++ b/transfer.go @@ -2,6 +2,7 @@ package stripego import ( "github.com/stripe/stripe-go/v74" + "github.com/stripe/stripe-go/v74/balancetransaction" transfer "github.com/stripe/stripe-go/v74/transfer" ) @@ -13,3 +14,12 @@ func TransferBalance(sk string, tp *stripe.TransferParams) (tRes *stripe.Transfe tRes, err = transfer.New(tp) return } + +// GetBalanceTx - get details of a balance transaction in Stripe +func GetBalanceTx(sk, bTxID string) (txn *stripe.BalanceTransaction, err error) { + // stripe secret key + stripe.Key = sk + + txn, err = balancetransaction.Get(bTxID, nil) + return +} diff --git a/transfer_test.go b/transfer_test.go index 702e65f..8ad55cd 100644 --- a/transfer_test.go +++ b/transfer_test.go @@ -13,6 +13,7 @@ import ( var Destination string var TransferObject string var TransferAmount int64 +var BTxID string func TestTransferBalance(t *testing.T) { StripeSK = strings.TrimSpace(os.Getenv("STRIPE_SK")) @@ -34,6 +35,7 @@ func TestTransferBalance(t *testing.T) { t.Errorf("got error when initiating balance transfer: %v", err) return } + BTxID = tRes.BalanceTransaction.ID res := &stripe.Transfer{} err = json.Unmarshal(tRes.LastResponse.RawJSON, &res) @@ -60,3 +62,25 @@ func TestTransferBalance(t *testing.T) { t.Errorf("destination IDs do not match") } } + +func TestGetBalanceTx(t *testing.T) { + txn, err := stripego.GetBalanceTx(StripeSK, BTxID) + if err != nil { + t.Errorf("got error when retrieving a transaction details: %v", err) + return + } + + res := &stripe.BalanceTransaction{} + err = json.Unmarshal(txn.LastResponse.RawJSON, &res) + if err != nil { + t.Errorf("got error when unmarshalling transaction details: %v", err) + return + } + + if res.ID != BTxID { + t.Errorf("balance transaction IDs do not match") + } + if res.Currency != stripe.Currency(Currency) { + t.Errorf("got: %v, want: %v", res.Currency, stripe.Currency(Currency)) + } +}