Skip to content

Commit

Permalink
chore: organise functions in files
Browse files Browse the repository at this point in the history
  • Loading branch information
pilinux committed Jan 3, 2023
1 parent 949ac8a commit 9460358
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ go mod tidy
### Transfer

- [x] transfer balance to a connected Stripe account

### Transaction

- [x] get details of a balance transaction

## Usage
Expand Down
15 changes: 15 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package stripego

import (
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/balancetransaction"
)

// 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
}
39 changes: 39 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package stripego_test

import (
"encoding/json"
"os"
"strings"
"testing"

"github.com/pilinux/stripego"
"github.com/stripe/stripe-go/v74"
)

var BTxID string

func TestGetBalanceTx(t *testing.T) {
StripeSK = strings.TrimSpace(os.Getenv("STRIPE_SK"))
Currency = strings.TrimSpace(os.Getenv("CURRENCY"))
BTxID = strings.TrimSpace(os.Getenv("BALANCE_TRANSACTION_ID"))

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))
}
}
10 changes: 0 additions & 10 deletions transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stripego

import (
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/balancetransaction"
transfer "github.com/stripe/stripe-go/v74/transfer"
)

Expand All @@ -14,12 +13,3 @@ 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
}
24 changes: 0 additions & 24 deletions transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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"))
Expand All @@ -35,7 +34,6 @@ 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)
Expand All @@ -62,25 +60,3 @@ 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))
}
}

0 comments on commit 9460358

Please sign in to comment.