swish

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2021 License: MIT Imports: 14 Imported by: 0

README

Swish library

Golang library for the Swish Payment and Refund Request API.

Build Status Reviewdog Go Report Card GoDoc

Check out the API Documentation http://godoc.org/github.com/NicklasWallgren/swish

Installation

The library can be installed through go get

go get github.com/NicklasWallgren/swish

Supported versions

We support the two major Go versions, which are 1.14 and 1.15 at the moment.

Features

  • Create payment request
  • Retrieve payment result
  • Create refund request
  • Retrieve refund result

Examples

Initiate payment request

import (
    "context"
    "fmt"
    "io/ioutil"
    "github.com/NicklasWallgren/swish"
)

certificate, err := ioutil.ReadFile("path/to/environment.p12")
if err != nil {
    panic(err)
}

configuration := swish.NewConfiguration(
    &swish.TestEnvironment,
    &swish.Pkcs12{Content: certificate, Password: "p12 password"},
)

instance := swish.New(configuration)

payload := swish.PaymentPayload{PayeePaymentReference: "0123456789", CallbackUrl: "https://myfakehost.se/swishcallback.cfm", PayeeAlias: "9871065216", PayerAlias: "1231181189", Amount: "100", Currency: "SEK"}

paymentResponse, err := instance.Payment(context.Background(), &payload)
if err != nil {
    fmt.Println(err)

    return
}

paymentResult, err := instance.PaymentResult(context.Background(), paymentResponse.Id)
if err != nil {
    fmt.Println(err)

    return
}

fmt.Println(paymentResult)

Unit tests

go test -v -race $(go list ./... | grep -v vendor)
Code Guide

We use GitHub Actions to make sure the codebase is consistent (golangci-lint run) and continuously tested (go test -v -race $(go list ./... | grep -v vendor)). We try to keep comments at a maximum of 120 characters of length and code at 120.

Contributing

If you find any problems or have suggestions about this library, please submit an issue. Moreover, any pull request, code review and feedback are welcome.

Contributors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TestEnvironment contains the environment specific fields for the test environment.
	TestEnvironment = Environment{BaseURL: "https://mss.cpc.getswish.net/swish-cpcapi/api/v1", Certificate: certificate}
	// ProductionEnvironment contains the environment specific fields for the production environment.
	ProductionEnvironment = Environment{BaseURL: "https://cpc.getswish.net/swish-cpcapi/api/v1", Certificate: certificate}
)

Functions

This section is empty.

Types

type Client

type Client interface {
	// contains filtered or unexported methods
}

Client is the interface implemented by types that can invoke the BankID REST API.

type ClientOption

type ClientOption func(*client)

ClientOption definition.

type Configuration

type Configuration struct {
	Environment *Environment
	Pkcs12      *Pkcs12
}

Configuration contains the configuration specific fields.

func NewConfiguration

func NewConfiguration(environment *Environment, pkcs12 *Pkcs12, options ...ConfigurationOption) *Configuration

NewConfiguration creates a new configuration.

type ConfigurationOption

type ConfigurationOption func(*Configuration)

ConfigurationOption definition.

type Environment

type Environment struct {
	BaseURL     string
	Certificate string
}

Environment contains the environment specific fields.

func NewEnvironment

func NewEnvironment(baseURL string, certificate string) *Environment

NewEnvironment creates a new environment.

type Error

type Error struct {
	Code                  string `json:"ErrorCode"`
	Message               string `json:"ErrorMessage"`
	AdditionalInformation string `json:"additionalInformation"`
}

Error contains fields specific for the API error.

type ErrorResponse

type ErrorResponse []Error

ErrorResponse contains fields for the error response.

func (*ErrorResponse) Decode

func (e *ErrorResponse) Decode(response *http.Response, swish *Swish) (Response, error)

Decode reads the JSON-encoded value and stories it in a error response struct.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

type PaymentPayload

type PaymentPayload struct {

	// Payment reference supplied by theMerchant. This is not used by Swish but is included in responses back to the
	// client. This reference could for example be an order id or similar. If set the value must not exceed 35 characters
	// and only the following characters are allowed: [a-ö, A-Ö, 0-9, -]
	PayeePaymentReference string `json:"payeePaymentReference,omitempty"` // validate
	// URL that Swish will use to notify caller about the result of the payment request. The URL has to use HTTPS.
	CallbackURL string `json:"callbackUrl"` // validate
	// The registered Cell phone number of the person that makes the payment. It can only contain numbers and has to be
	// at least 8 and at most 15 digits. It also needs to match the following format in order to be found in
	// Swish: country code + cell phone number (without leading zero). E.g.: 46712345678
	// If set, request is handled as E-Commerce payment.
	// If not set, request is handled as M- Commerce payment.
	PayerAlias string `json:"payerAlias,omitempty"` // validate
	// The social security number of the individual making the payment,
	// should match the registered value for payerAlias or the payment will not be accepted.
	PayerSSN string `json:"payerSSN,omitempty"`
	// Minimum age (in years) that the individual connected to the payerAlias has to be in order for the payment to
	// be accepted. Value has to be in the range of 1 to 99.
	AgeLimit string `json:"ageLimit,omitempty"` // validate
	// The Swish number of the payee. It needs to match with Merchant Swish number.
	PayeeAlias string `json:"payeeAlias"` // validate
	// The amount of money to pay. The amount cannot be less than 1 SEK and not more than
	// 999999999999.99 SEK. Valid value has to
	// be all digits or with 2 digit decimal separated with a period.
	Amount string `json:"amount"` // validate
	// The currency to use. Currently the only supported value is SEK.
	Currency string `json:"currency"` // validate
	// Merchant supplied message about the payment/order. Max 50 characters.
	// Allowed characters are the letters a-ö, A-Ö, the numbers 0-9 and any of the special characters :;.,?!()-”.
	Message string `json:"message,omitempty"` // validate`
	// contains filtered or unexported fields
}

PaymentPayload holds the required and optional fields of the payment request.

type PaymentResponse

type PaymentResponse struct {
	// Used as reference to this order when the client is started automatically.
	ID string `json:"id"`
	// Used to collect the status of the order.
	URL string `json:"url"`
	// Payment request token
	Token string
	// contains filtered or unexported fields
}

PaymentResponse holds the information of a initiated payment request.

func (*PaymentResponse) Decode

func (p *PaymentResponse) Decode(response *http.Response, swish *Swish) (Response, error)

Decode reads the http response and stories it in a payment response struct.

func (*PaymentResponse) String

func (p *PaymentResponse) String() string

type PaymentResultResponse

type PaymentResultResponse struct {
	ID                    string  `json:"id"`
	PayeePaymentReference string  `json:"payeePaymentReference"`
	PaymentReference      string  `json:"paymentReference"`
	CallbackURL           string  `json:"callbackUrl"`
	PayerAlias            string  `json:"payerAlias"`
	PayeeAlias            string  `json:"payeeAlias"`
	Amount                float32 `json:"amount"`
	Currency              string  `json:"currency"`
	Message               string  `json:"message"`
	Status                string  `json:"status"`
	DateCreated           string  `json:"dateCreated"`
	DatePaid              string  `json:"datePaid"`
	ErrorCode             string  `json:"errorCode"`
	ErrorMessage          string  `json:"errorMessage"`
}

PaymentResultResponse holds the information of a initiated payment result request.

func (*PaymentResultResponse) Decode

func (p *PaymentResultResponse) Decode(response *http.Response, swish *Swish) (Response, error)

Decode reads the JSON-encoded response and stories it in a payment result response struct.

func (*PaymentResultResponse) String

func (p *PaymentResultResponse) String() string

type Pkcs12

type Pkcs12 struct {
	Content  []byte
	Password string
}

Pkcs12 contains the PKCS12 specific fields.

type RefundPayload

type RefundPayload struct {

	// Payment reference supplied by the Merchant. This is not used by Swish but is included in responses back to the client.
	PayerPaymentReference string `json:"payerPaymentReference,omitempty"`
	// Payment reference to the original payment that this refund is for.
	OriginalPaymentReference string `json:"originalPaymentReference"` // validate
	// URL that Swish will use to notify caller about the outcome of the refund. The URL has to use HTTPS.
	CallbackURL string `json:"callbackUrl"` // validate
	// The Swish number of the Merchant that makes the refund payment.
	PayerAlias string `json:"payerAlias"` // validate
	// The Cell phone number of the person that receives the refund payment.
	PayeeAlias string `json:"payeeAlias,omitempty"` // validate
	// The amount of money to refund.
	// The amount cannot be less than 1 SEK and not more than
	// Moreover, the amount cannot exceed the remaining amount of the original payment that the refund is for.
	Amount string `json:"amount"` // validate
	// The currency to use.
	Currency string `json:"currency"` // validate
	// Merchant supplied message about the refund. Max 50 chars.
	// Allowed characters are the letters a-ö, A-Ö, the numbers 0-9 and any of the special characters :;.,?!()-”.
	Message string `json:"message,omitempty"` // validate
	// contains filtered or unexported fields
}

RefundPayload holds the required and optional fields of the refund request.

type RefundResponse

type RefundResponse struct {
	// Used as reference to this order when the client is started automatically.
	ID string `json:"id"`
	// Used to collect the status of the refund.
	URL string `json:"url"`
	// contains filtered or unexported fields
}

RefundResponse contains fields specific for the refund response.

func (*RefundResponse) Decode

func (r *RefundResponse) Decode(response *http.Response, swish *Swish) (Response, error)

Decode reads the JSON-encoded value and stories it in a refund response struct.

func (RefundResponse) String

func (r RefundResponse) String() string

type RefundResultResponse

type RefundResultResponse struct {
	ID                       string  `json:"id"`
	PaymentReference         string  `json:"paymentReference"`
	PayerPaymentReference    string  `json:"payerPaymentReference"`
	OriginalPaymentReference string  `json:"originalPaymentReference"`
	CallbackURL              string  `json:"callbackUrl"`
	PayerAlias               string  `json:"payerAlias"`
	PayeeAlias               string  `json:"payeeAlias"`
	Amount                   float32 `json:"amount"`
	Currency                 string  `json:"currency"`
	Message                  string  `json:"message"`
	Status                   string  `json:"status"`
	DateCreated              string  `json:"dateCreated"`
	DatePaid                 string  `json:"datePaid"`
	ErrorMessage             string  `json:"errorMessage"`
	AdditionalInformation    string  `json:"additionalInformation"`
	ErrorCode                string  `json:"errorCode"`
}

RefundResultResponse contains fields specific for the refund result response.

func (*RefundResultResponse) Decode

func (r *RefundResultResponse) Decode(response *http.Response, swish *Swish) (Response, error)

Decode reads the JSON-encoded value and stories it in a refund result response struct.

func (RefundResultResponse) String

func (r RefundResultResponse) String() string

type Request

type Request interface {
	Method() string
	URI() string
	Payload() payload
	Response() Response
}

Request is the interface implemented by types that holds the request context fields.

type Response

type Response interface {
	Decode(response *http.Response, swish *Swish) (Response, error)
}

Response is the interface implemented by types that holds the response context fields.

type Swish

type Swish struct {
	// contains filtered or unexported fields
}

Swish contains the validator and configuration context.

func New

func New(configuration *Configuration) Swish

New returns a new instance of 'Swish'.

func (Swish) Payment

func (s Swish) Payment(ctx context.Context, payload *PaymentPayload) (*PaymentResponse, error)

Payment - Initiates a payment request

Merchants and Technical Suppliers can send payment requests for both E-Commerce and M- Commerce to MSS.

Once MSS receives a “payment request” call, there are two answers that will be returned from MSS (unless error situation). The first answer is synchronous, the second one is asynchronous.

func (Swish) PaymentResult

func (s Swish) PaymentResult(ctx context.Context, token string) (*PaymentResultResponse, error)

PaymentResult - Retrieves the payment result

The client can retrieve payment result information of an initiated payment request (successful or failed) If the previous create payment request call simulated a delayed error the response of the GET operation will have a status property with value ERROR and properties errorCode and errorMessage will be set accordingly

MSS stores the necessary information about each incoming “PaymentRequest request” in a cache which automatically expires every 24 hours or when the MSS server is restarted.

func (Swish) Refund

func (s Swish) Refund(ctx context.Context, payload *RefundPayload) (*RefundResponse, error)

Refund - Initiates a refund request.

Merchants and Technical Suppliers can send refund request to MSS.

When MSS receives a “Refund request” there are three answers that will be returned from MSS (unless error situation). The first answer is synchronous, the second and third responses are asynchronous.

func (Swish) RefundResult

func (s Swish) RefundResult(ctx context.Context, token string) (*RefundResultResponse, error)

RefundResult - Retrieves the refund result

The client can retrieve refund result information of an initiated refund request (successful or failed).

If the previous create refund request call simulated a delayed error the response of the GET operation will have a status property with value ERROR and properties errorCode and errorMessage will be set accordingly

MSS stores the necessary information about each incoming “Refund request” in a cache which automatically expires every 24 hours or when the MSS server is restarted.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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