Skip to content

Commit

Permalink
worked on docs & wip: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mujhtech committed Mar 7, 2024
1 parent 23d1b5b commit 571cc37
Show file tree
Hide file tree
Showing 17 changed files with 459 additions and 241 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 25 additions & 12 deletions business.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
// Package swervpay provides a set of APIs to interact with the Swervpay service.
package swervpay

import (
"context"
"net/http"
)

// Business represents a business entity in the Swervpay system.
// It includes various properties like address, name, country, etc.
type Business struct {
Address string `json:"address"`
Name string `json:"name"`
Country string `json:"country"`
CreatedAt string `json:"created_at"`
Email string `json:"email"`
ID string `json:"id"`
Logo string `json:"logo"`
Slug string `json:"slug"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
Address string `json:"address"` // Address of the business
Name string `json:"name"` // Name of the business
Country string `json:"country"` // Country where the business is located
CreatedAt string `json:"created_at"` // Time when the business was created
Email string `json:"email"` // Email of the business
ID string `json:"id"` // Unique identifier of the business
Logo string `json:"logo"` // Logo of the business
Slug string `json:"slug"` // Slug of the business
Type string `json:"type"` // Type of the business
UpdatedAt string `json:"updated_at"` // Time when the business was last updated
}

// BusinessInt is an interface that defines the methods a Business must have.
type BusinessInt interface {
// Get retrieves the Business information from the Swervpay system.
Get(ctx context.Context) (*Business, error)
}

// BusinessIntImpl is a concrete implementation of the BusinessInt interface.
type BusinessIntImpl struct {
client *SwervpayClient
client *SwervpayClient // Client used to make requests to the Swervpay system
}

// Ensure BusinessIntImpl implements BusinessInt interface
var _ BusinessInt = &BusinessIntImpl{}

// Get is a method on BusinessIntImpl that retrieves the Business information from the Swervpay system.
// https://docs.swervpay.co/api-reference/business/get
func (b *BusinessIntImpl) Get(ctx context.Context) (*Business, error) {
// Prepare request

// Create a new request to get the business information
req, err := b.client.NewRequest(ctx, http.MethodGet, "business", nil)
if err != nil {
return nil, err
}

// Response will hold the business information
response := new(Business)

// Perform the request and populate the response
_, err = b.client.Perform(req, response)

if err != nil {
return nil, err
}

// Return the business information
return response, nil
}
30 changes: 0 additions & 30 deletions business_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,9 @@ import (
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)

var (
mux *http.ServeMux
client *SwervpayClient
server *httptest.Server
)

func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = NewSwervpayClient(&SwervpayClientOption{
BusinessID: "",
SecretKey: "",
BaseURL: "",
})
url, _ := url.Parse(server.URL)
client.BaseURL = url
}

func teardown() {
server.Close()
}

func TestGetBusiness(t *testing.T) {
setup()
defer teardown()
Expand All @@ -58,9 +34,3 @@ func TestGetBusiness(t *testing.T) {
}
assert.Equal(t, resp.ID, "bus_123456789")
}

func testMethod(t *testing.T, r *http.Request, expected string) {
if expected != r.Method {
t.Errorf("Request method = %v, expected %v", r.Method, expected)
}
}
145 changes: 84 additions & 61 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,100 @@ import (
"net/http"
)

// Card represents a card with its details.
type Card struct {
AddressCity string `json:"address_city"`
AddressCountry string `json:"address_country"`
AddressPostalCode string `json:"address_postal_code"`
AddressState string `json:"address_state"`
AddressStreet string `json:"address_street"`
Balance int64 `json:"balance"`
CardNumber string `json:"card_number"`
CreatedAt string `json:"created_at"`
Currency string `json:"currency"`
Cvv string `json:"cvv"`
Expiry string `json:"expiry"`
Freeze bool `json:"freeze"`
ID string `json:"id"`
Issuer string `json:"issuer"`
MaskedPan string `json:"masked_pan"`
NameOnCard string `json:"name_on_card"`
Status string `json:"status"`
TotalFunded int64 `json:"total_funded"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
AddressCity string `json:"address_city"` // City of the card holder's address.
AddressCountry string `json:"address_country"` // Country of the card holder's address.
AddressPostalCode string `json:"address_postal_code"` // Postal code of the card holder's address.
AddressState string `json:"address_state"` // State of the card holder's address.
AddressStreet string `json:"address_street"` // Street of the card holder's address.
Balance int64 `json:"balance"` // Balance on the card.
CardNumber string `json:"card_number"` // Card number.
CreatedAt string `json:"created_at"` // Creation date of the card.
Currency string `json:"currency"` // Currency of the card.
Cvv string `json:"cvv"` // CVV of the card.
Expiry string `json:"expiry"` // Expiry date of the card.
Freeze bool `json:"freeze"` // Freeze status of the card.
ID string `json:"id"` // ID of the card.
Issuer string `json:"issuer"` // Issuer of the card.
MaskedPan string `json:"masked_pan"` // Masked PAN of the card.
NameOnCard string `json:"name_on_card"` // Name on the card.
Status string `json:"status"` // Status of the card.
TotalFunded int64 `json:"total_funded"` // Total funded amount on the card.
Type string `json:"type"` // Type of the card.
UpdatedAt string `json:"updated_at"` // Last update date of the card.
}

// CardTransactionHistory represents a card's transaction history.
type CardTransactionHistory struct {
Amount int64 `json:"amount"`
Category string `json:"category"`
Charges int64 `json:"charges"`
CreatedAt string `json:"created_at"`
Currency string `json:"currency"`
ID string `json:"id"`
MerchantCity string `json:"merchant_city"`
MerchantCountry string `json:"merchant_country"`
MerchantMcc string `json:"merchant_mcc"`
MerchantMid string `json:"merchant_mid"`
MerchantName string `json:"merchant_name"`
MerchantPostalCode string `json:"merchant_postal_code"`
MerchantState string `json:"merchant_state"`
Reference string `json:"reference"`
Report bool `json:"report"`
ReportMessage string `json:"report_message"`
Status string `json:"status"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
Amount int64 `json:"amount"` // Transaction amount.
Category string `json:"category"` // Category of the transaction.
Charges int64 `json:"charges"` // Charges of the transaction.
CreatedAt string `json:"created_at"` // Creation date of the transaction.
Currency string `json:"currency"` // Currency of the transaction.
ID string `json:"id"` // ID of the transaction.
MerchantCity string `json:"merchant_city"` // City of the merchant.
MerchantCountry string `json:"merchant_country"` // Country of the merchant.
MerchantMcc string `json:"merchant_mcc"` // MCC of the merchant.
MerchantMid string `json:"merchant_mid"` // MID of the merchant.
MerchantName string `json:"merchant_name"` // Name of the merchant.
MerchantPostalCode string `json:"merchant_postal_code"` // Postal code of the merchant.
MerchantState string `json:"merchant_state"` // State of the merchant.
Reference string `json:"reference"` // Reference of the transaction.
Report bool `json:"report"` // Report status of the transaction.
ReportMessage string `json:"report_message"` // Report message of the transaction.
Status string `json:"status"` // Status of the transaction.
Type string `json:"type"` // Type of the transaction.
UpdatedAt string `json:"updated_at"` // Last update date of the transaction.
}

// CreateCardBody represents the body of a card creation request.
type CreateCardBody struct {
Amount float64 `json:"amount"`
CustomerId string `json:"customer_id"`
Provider string `json:"provider"`
NameOnCard string `json:"name_on_card"`
Currency string `json:"currency"`
Type string `json:"type"`
Amount float64 `json:"amount"` // Amount to be loaded on the card.
CustomerId string `json:"customer_id"` // ID of the customer.
Provider string `json:"provider"` // Provider of the card.
NameOnCard string `json:"name_on_card"` // Name to be printed on the card.
Currency string `json:"currency"` // Currency of the card.
Type string `json:"type"` // Type of the card.
}

// CardCreationResponse represents the response of a card creation request.
type CardCreationResponse struct {
CardID string `json:"card_id"`
Message string `json:"message"`
CardID string `json:"card_id"` // ID of the created card.
Message string `json:"message"` // Message of the response.
}

// FundOrWithdrawCardBody represents the body of a fund or withdraw request.
type FundOrWithdrawCardBody struct {
Amount float64 `json:"amount"`
Amount float64 `json:"amount"` // Amount to be funded or withdrawn.
}

// CardInt is the interface for card operations.
type CardInt interface {
Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Card, error)
Get(ctx context.Context, id string) (*Card, error)
Create(ctx context.Context, body *CreateCardBody) (*CardCreationResponse, error)
Fund(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error)
Withdraw(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error)
Terminate(ctx context.Context, id string) (*DefaultResponse, error)
Freeze(ctx context.Context, id string) (*DefaultResponse, error)
Transactions(ctx context.Context, id string, query *PageAndLimitQuery) (*[]CardTransactionHistory, error)
Transaction(ctx context.Context, id string, transactionId string) (*CardTransactionHistory, error)
Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Card, error) // Gets multiple cards.
Get(ctx context.Context, id string) (*Card, error) // Gets a single card.
Create(ctx context.Context, body *CreateCardBody) (*CardCreationResponse, error) // Creates a card.
Fund(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error) // Funds a card.
Withdraw(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error) // Withdraws from a card.
Terminate(ctx context.Context, id string) (*DefaultResponse, error) // Terminates a card.
Freeze(ctx context.Context, id string) (*DefaultResponse, error) // Freezes a card.
Transactions(ctx context.Context, id string, query *PageAndLimitQuery) (*[]CardTransactionHistory, error) // Gets multiple transactions of a card.
Transaction(ctx context.Context, id string, transactionId string) (*CardTransactionHistory, error) // Gets a single transaction of a card.
}

// CardIntImpl is the implementation of the CardInt interface.
type CardIntImpl struct {
client *SwervpayClient
client *SwervpayClient // Client to perform the requests.
}

// Verify that CardIntImpl implements CardInt.
var _ CardInt = &CardIntImpl{}

// Gets gets multiple cards.
// https://docs.swervpay.co/api-reference/cards/get-all-cards
func (c CardIntImpl) Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Card, error) {
path := GenerateURLPath("cards", query)

// Prepare request
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
Expand All @@ -106,6 +115,8 @@ func (c CardIntImpl) Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Car
return response, nil
}

// Get gets a single card.
// https://docs.swervpay.co/api-reference/cards/get
func (c CardIntImpl) Get(ctx context.Context, id string) (*Card, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, "cards/"+id, nil)
if err != nil {
Expand All @@ -123,6 +134,8 @@ func (c CardIntImpl) Get(ctx context.Context, id string) (*Card, error) {
return response, nil
}

// Create creates a card.
// https://docs.swervpay.co/api-reference/cards/create
func (c CardIntImpl) Create(ctx context.Context, body *CreateCardBody) (*CardCreationResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "cards", body)
if err != nil {
Expand All @@ -140,6 +153,8 @@ func (c CardIntImpl) Create(ctx context.Context, body *CreateCardBody) (*CardCre
return response, nil
}

// Fund funds a card.
// https://docs.swervpay.co/api-reference/cards/fund-card
func (c CardIntImpl) Fund(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "cards/"+id+"/fund", body)
if err != nil {
Expand All @@ -157,6 +172,8 @@ func (c CardIntImpl) Fund(ctx context.Context, id string, body *FundOrWithdrawCa
return response, nil
}

// Withdraw withdraws from a card.
// https://docs.swervpay.co/api-reference/cards/withdraw-from-card
func (c CardIntImpl) Withdraw(ctx context.Context, id string, body *FundOrWithdrawCardBody) (*DefaultResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "cards/"+id+"/withdraw", body)
if err != nil {
Expand All @@ -174,6 +191,8 @@ func (c CardIntImpl) Withdraw(ctx context.Context, id string, body *FundOrWithdr
return response, nil
}

// Terminate terminates a card.
// https://docs.swervpay.co/api-reference/cards/terminate
func (c CardIntImpl) Terminate(ctx context.Context, id string) (*DefaultResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "cards/"+id+"/terminate", nil)
if err != nil {
Expand All @@ -191,6 +210,8 @@ func (c CardIntImpl) Terminate(ctx context.Context, id string) (*DefaultResponse
return response, nil
}

// Freeze freezes a card.
// https://docs.swervpay.co/api-reference/cards/freeze
func (c CardIntImpl) Freeze(ctx context.Context, id string) (*DefaultResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "cards/"+id+"/freeze", nil)
if err != nil {
Expand All @@ -208,10 +229,11 @@ func (c CardIntImpl) Freeze(ctx context.Context, id string) (*DefaultResponse, e
return response, nil
}

// Transactions gets multiple transactions of a card.
// https://docs.swervpay.co/api-reference/cards/transactions
func (c CardIntImpl) Transactions(ctx context.Context, id string, query *PageAndLimitQuery) (*[]CardTransactionHistory, error) {
path := GenerateURLPath("cards/"+id+"/transactions", query)

// Prepare request
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
Expand All @@ -228,9 +250,10 @@ func (c CardIntImpl) Transactions(ctx context.Context, id string, query *PageAnd
return response, nil
}

// Transaction get transactions of a card.
// https://docs.swervpay.co/api-reference/cards/get-transaction
func (c CardIntImpl) Transaction(ctx context.Context, id string, transactionId string) (*CardTransactionHistory, error) {

// Prepare request
req, err := c.client.NewRequest(ctx, http.MethodGet, "cards/"+id+"/transactions/"+transactionId, nil)
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func TestFundCard(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/cards/card_12345678/fund", func(w http.ResponseWriter, r *http.Request) {
cardId := "card_12345678"

mux.HandleFunc("/cards/"+cardId+"/fund", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand All @@ -30,7 +32,7 @@ func TestFundCard(t *testing.T) {
Amount: 1000,
}

resp, err := client.Card.Fund(context.Background(), "card_12345678", req)
resp, err := client.Card.Fund(context.Background(), cardId, req)
if err != nil {
t.Errorf("Unable to fund card: %v", err)
}
Expand Down
Loading

0 comments on commit 571cc37

Please sign in to comment.