swift

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 17 Imported by: 0

README

SWIFT SDK in GO

This package is written to cover low level stuff for integration with SWIFT Payments. All code is written according to specification from Developers API, checkout the link to discover more. References for the moment package implements:

INSTALL

go get github.com/alifcapital/swift

TESTS

// provide `tests.json` file (example: `tests.json.example`) and run
go test -cover

// to see coverage details
go test . -coverprofile tests.cover && go tool cover -html tests.cover && rm tests.cover

Contributing

Pull requests are welcome. For any changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

Documentation

Overview

Usage

Create API by NewAPI and call it's methods. See more about usage in NewAPI's docs.

You have two ways of using API. First is by calling API's method:

swiftAPI := swift.NewAPI(...)
swiftAPI.GetBicDetails(ctx, bic)

This way is more convenient and less verbose. In most cases that's enough, but by using this approach you can provide only required arguments in SWIFT API. If you need to provide optional arguments you can use second way:

swiftAPI := swift.NewAPI(...)
optionalArgs := swiftref.BicsApiGetBicDetailsV2Opts{APIStatus: optional.NewString("APIStatus")}
swiftAPI.Reference.BicsApi.GetBicDetailsV2(swiftAPI.WithContext(ctx, swiftref.ContextOAuth2), bic, &optionalArgs)

More verbose, but now you can specify optional arguments to SWIFT API. See more about this approach in API.WithContext docs.

Index

Examples

Constants

View Source
const (
	// Sandbox is a testing environment, but can be used in production for some services
	// from docs:  ... KYC Registry API, Banking Analytics API, Banking Premium API and Compliance Analytics API, Sandbox CAN be used for UAT.
	Sandbox = env(iota)
	// Production is a production environment
	Production
)

Variables

View Source
var (
	ErrInvalidAnyBic                   = errors.New("bic number is not valid")
	ErrCreditorAccountLengthOutOfRange = errors.New("credit account length out of 1 to 34 range")
	ErrCreditorNameLengthOutOfRange    = errors.New("credit name length out of 1 to 140 range")
	ErrInvalidCountryCode              = errors.New("county code must contain only 2 uppercase symbols")
)

Functions

func NewErrWithHTTPResponse

func NewErrWithHTTPResponse(HTTPResponse *http.Response, err error) error

func NotFound

func NotFound(err error) bool

func UnAuthorized added in v0.3.0

func UnAuthorized(err error) bool

func ValidateBic

func ValidateBic(anyBic string) error

ValidateBic - validates BIC format BIC - code allocated to a financial or non-financial institution by the ISO 9362 Registration Authority

func ValidateBicFi

func ValidateBicFi(bicFi string) error

ValidateBicFi - same as `ValidateBic`

func ValidateCountryCode

func ValidateCountryCode(countryCode string) error

ValidateCountryCode - ISO 3166, Alpha-2 code

func ValidateCreditorAccount

func ValidateCreditorAccount(creditorAccount string) error

ValidateCreditorAccount - strings, between 1 and 34 chars

func ValidateCreditorName

func ValidateCreditorName(creditorAccount string) error

ValidateCreditorName - strings, between 1 and 140 chars

Types

type API

type API struct {
	Reference     *swiftref.APIClient
	Analytics     *analytics.APIClient
	PreValidation *prevalidation.APIClient
	// contains filtered or unexported fields
}

API aggregates swiftref, bank-analytics APIs and deals with authentication (gets access token as needed)

func NewAPI

func NewAPI(environment env, credentials AppCredentials, options ...Option) *API

NewAPI return new API based on given args and options

Example
package main

import (
	"context"
	"github.com/alifcapital/swift"
	"log"
	"net/http"
)

func main() {
	swiftAPI := swift.NewAPI(swift.Sandbox, swift.AppCredentials{
		BasicAuthUser: "some_user",
		BasicAuthPass: "some_pass",
		Username:      "some_username",
		Password:      "some_password",
	}, swift.HTTPClient(http.DefaultClient))

	bicDetails, err := swiftAPI.GetBicDetails(context.Background(), "DEUTDEFF")
	if err != nil {
		switch {
		case swift.NotFound(err):
			log.Println("no such bic")
		case swift.UnAuthorized(err):
			log.Println("unauthorized")
		default:
			log.Println("getting bic details:", err)
		}
		return
	}
	_ = bicDetails
}
Output:

func (*API) BankingAnalytics

func (api *API) BankingAnalytics(ctx context.Context, code analytics.Market1Code, reportingPeriod string) (*analytics.InlineResponse200, error)

func (*API) GetAccountNumberValidity

func (api *API) GetAccountNumberValidity(ctx context.Context, accountNumber string) (*swiftref.InlineResponse20021, error)

func (*API) GetAuthToken

func (api *API) GetAuthToken(ctx context.Context) (*AuthenticationToken, error)

GetAuthToken fetches from SWIFT api new `AuthenticationToken`. https://developer.swift.com/oauth-reference#section/Authentication/clientAuth

CAUTION: getting new access token invalidates previous one, so DO NOT use it when you are using API methods for SWIFT-API. Getting new access tokens by need is managed automatically by API.

func (*API) GetBicDetails

func (api *API) GetBicDetails(ctx context.Context, BIC string) (*swiftref.InlineResponse2005, error)

func (*API) GetBicFromIban

func (api *API) GetBicFromIban(ctx context.Context, iban string) (*swiftref.InlineResponse2001, error)

func (*API) GetBicFromIbanNationalId

func (api *API) GetBicFromIbanNationalId(ctx context.Context, ibanNationalID, countryCode string) (*swiftref.InlineResponse2003, error)

func (*API) GetBicFromLei

func (api *API) GetBicFromLei(ctx context.Context, lei string) (*swiftref.InlineResponse2008, error)

func (*API) GetBicValidity

func (api *API) GetBicValidity(ctx context.Context, BIC string) (*swiftref.InlineResponse2006, error)

func (*API) GetBicsFromNationalId

func (api *API) GetBicsFromNationalId(ctx context.Context, nationalID string) (*swiftref.InlineResponse20014, error)

func (*API) GetCountryCodeDetails

func (api *API) GetCountryCodeDetails(ctx context.Context, countryCode string) (*swiftref.InlineResponse20016, error)

func (*API) GetCountryCodeValidity

func (api *API) GetCountryCodeValidity(ctx context.Context, countryCode string) (*swiftref.InlineResponse20015, error)

func (*API) GetCurrencyCodeDetails

func (api *API) GetCurrencyCodeDetails(ctx context.Context, countryCode string) (*swiftref.InlineResponse20018, error)

func (*API) GetCurrencyCodeValidity

func (api *API) GetCurrencyCodeValidity(ctx context.Context, currencyCode string) (*swiftref.InlineResponse20017, error)

func (*API) GetIbanDetails

func (api *API) GetIbanDetails(ctx context.Context, iban string) (*swiftref.InlineResponse2002, error)

func (*API) GetIbanFromBban

func (api *API) GetIbanFromBban(ctx context.Context, BBAN, countryCode string) (*swiftref.InlineResponse2004, error)

func (*API) GetIbanValidity

func (api *API) GetIbanValidity(ctx context.Context, iban string) (*swiftref.InlineResponse200, error)

func (*API) GetLeiDetails

func (api *API) GetLeiDetails(ctx context.Context, lei string) (*swiftref.InlineResponse20020, error)

func (*API) GetLeiFromBic

func (api *API) GetLeiFromBic(ctx context.Context, BIC string) (*swiftref.InlineResponse2007, error)

func (*API) GetLeiValidity

func (api *API) GetLeiValidity(ctx context.Context, lei string) (*swiftref.InlineResponse20019, error)

func (*API) GetNationalIdDetails

func (api *API) GetNationalIdDetails(ctx context.Context, nationalID string) (*swiftref.InlineResponse20012, error)

func (*API) GetNationalIdValidity

func (api *API) GetNationalIdValidity(ctx context.Context, nationalID string) (*swiftref.InlineResponse20013, error)

func (*API) GetNationalIdsFromBic

func (api *API) GetNationalIdsFromBic(ctx context.Context, BIC string) (*swiftref.InlineResponse2009, error)

func (*API) GetSepaReachabilityFromBic

func (api *API) GetSepaReachabilityFromBic(ctx context.Context, BIC, sepaScheme string) (*swiftref.InlineResponse20010, error)

func (*API) GetSsisFromBic

func (api *API) GetSsisFromBic(ctx context.Context, BIC, currencyCode string) (*swiftref.InlineResponse20011, error)

func (*API) RefreshAuthToken

func (api *API) RefreshAuthToken(ctx context.Context) (*AuthenticationToken, error)

RefreshAuthToken fetches from SWIFT api new `AuthenticationToken`. https://developer.swift.com/oauth-reference#section/Authentication/clientAuth

func (*API) RevokeAuthToken

func (api *API) RevokeAuthToken(ctx context.Context) error

RevokeAuthToken removes/disposes authentication tokens. https://developer.swift.com/oauth-reference#operation/revokeAccessToken

func (*API) Token

func (api *API) Token() (*oauth2.Token, error)

Token gets new access token. You SHOULD NOT call it.

func (*API) WithContext

func (api *API) WithContext(ctx context.Context, key interface{}) context.Context

WithContext wraps ctx with authentication information.

Example
package main

import (
	"context"
	"github.com/alifcapital/swift"
	"github.com/alifcapital/swift/swagger/swift/analytics"
	"github.com/alifcapital/swift/swagger/swift/swiftref"
	"github.com/antihax/optional"
)

func main() {
	// swiftref:
	swiftAPI := swift.NewAPI(swift.Sandbox, swift.AppCredentials{
		BasicAuthUser: "some_user",
		BasicAuthPass: "some_pass",
		Username:      "some_username",
		Password:      "some_password",
	})
	ctx := context.Background()
	bicDetails, httpResponse, err := swiftAPI.Reference.BicsApi.
		GetBicDetailsV2(swiftAPI.WithContext(ctx, swiftref.ContextOAuth2), "DEUTDEFF", nil)
	if err != nil {
		// ...
	}
	_, _ = bicDetails, httpResponse

	// bank analytics:
	bankingAnalyticsOpts := analytics.BankingAnalyticsApiBankingAnalyticsOpts{
		Limit:  optional.NewInterface(10),
		Offset: optional.NewInterface(0),
	}

	bankingAnalytics, httpResponse, err := swiftAPI.Analytics.BankingAnalyticsApi.
		BankingAnalytics(swiftAPI.WithContext(ctx, analytics.ContextOAuth2), "PMTS_payments", "2018-08", &bankingAnalyticsOpts)
	if err != nil {
		// ...
	}
	_, _ = bankingAnalytics, httpResponse
}
Output:

type AppCredentials

type AppCredentials struct {
	BasicAuthUser string // consumer_key
	BasicAuthPass string // consumer_secret
	Username      string // license_id
	Password      string // license_key
}

AppCredentials is a credentials to make OAuth2 request

type AuthenticationToken

type AuthenticationToken struct {
	AccessToken           string `json:"access_token"`
	ExpiresIn             string `json:"expires_in"`
	RefreshToken          string `json:"refresh_token"`
	RefreshTokenExpiresIn string `json:"refresh_token_expires_in"`
	TokenType             string `json:"token_type"`
}

AuthenticationToken returned after successful authentication `AccessToken` used in http Authorization headers `ExpiresIn` by default is 1800-1 seconds (30min) `RefreshToken` used for requiring new AuthenticationToken and is alive for 86400-1 seconds (1 day) `TokenType` is a part of Authorization header: ex. req.Header.Set("Authorization", "Bearer "+t.AuthenticationToken

It is strongly recommended that your application dispose tokens that are no longer needed. SWIFT will invalidate the tokens from further use if you do. Once invalidated, they can no longer be used to access SWIFT APIs.

type ErrWithHTTPResponse

type ErrWithHTTPResponse struct {
	HTTPResponse *http.Response
	Err          error
}

func (*ErrWithHTTPResponse) Error

func (e *ErrWithHTTPResponse) Error() string

func (*ErrWithHTTPResponse) NotFound

func (e *ErrWithHTTPResponse) NotFound() bool

func (*ErrWithHTTPResponse) UnAuthorized added in v0.3.0

func (e *ErrWithHTTPResponse) UnAuthorized() bool

func (*ErrWithHTTPResponse) Unwrap

func (e *ErrWithHTTPResponse) Unwrap() error

type Option

type Option func(api *API)

Option is a function that sets some option on API.

func HTTPClient

func HTTPClient(client *http.Client) Option

HTTPClient creates new option for using custom http.Client

Directories

Path Synopsis
swagger
swift/analytics
* SWIFT Banking Analytics * * SWIFT Watch Banking Analytics API enables institutions to retrieve their own SWIFT traffic data and the SWIFT totals, extending to the level of value and currency per market.
* SWIFT Banking Analytics * * SWIFT Watch Banking Analytics API enables institutions to retrieve their own SWIFT traffic data and the SWIFT totals, extending to the level of value and currency per market.
swift/prevalidation
* Payment Pre-validation API * * New version of the Bank Account Verification Service (the BAV), BAV v2, for Users and Data Providers.
* Payment Pre-validation API * * New version of the Bank Account Verification Service (the BAV), BAV v2, for Users and Data Providers.
swift/swiftref
* SWIFTRef API * * SWIFTRef API * * API version: 1.6.0 * Contact: developer-support@swift.com * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
* SWIFTRef API * * SWIFTRef API * * API version: 1.6.0 * Contact: developer-support@swift.com * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL