swish

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2020 License: MIT Imports: 12 Imported by: 0

README

Kansuler/payment-swish

License Version Codacy Badge

A package to simplify integrations against the Swedish payment solution Swish.

It is recommended to read through the integration guide thoroughly to understand the the process, and what responses that can occur.

API and detailed documentation can be found at https://godoc.org/github.com/Kansuler/payment-swish

This library implements version 2 of create payment request and create refund.

Installation

go get github.com/Kansuler/payment-swish

Usage

package main

import (
	"context"
	swish "github.com/Kansuler/payment-swish"
	"github.com/satori/go.uuid"
	"io/ioutil"
	"log"
)

func main() {
	cert, err := ioutil.ReadFile("certificates/Swish_Merchant_TestCertificate_1234679304.p12")
	if err != nil {
		log.Fatalf("could not load test certificate: %s", err.Error())
	}

	s, err := swish.New(swish.Options{
		Passphrase:     "swish",
		CA:             swish.TestCertificate,
		SSLCertificate: cert,
		Test:           true,
		Timeout:        5,
	})
	if err != nil {
		log.Fatalf("could not create swish instance: %s", err.Error())
	}

	paymentRequest := swish.CreatePaymentRequestOptions{
		InstructionUUID:       uuid.NewV4().String(),
		CallbackURL:           "https://localhost:8080/callback",
		PayeeAlias:            "1234679304",
		Amount:                "100.01",
		Currency:              "SEK",
		PayeePaymentReference: "",
		PayerAlias:            "",
		PayerSSN:              "",
		PayerAgeLimit:         "",
		Message:               "",
	}

	payment, err := s.CreatePaymentRequest(context.Background(), paymentRequest)
	if err != nil {
		log.Fatalf("could not create payment paymentRequest: %s", err.Error())
	}

	_, err = s.Status(context.Background(), payment.Location)
	if err != nil {
		log.Fatalf("could not get status of payment: %s", err.Error())
	}

	refundRequest := swish.CreateRefundOptions{
		InstructionUUID:          uuid.NewV4().String(),
		OriginalPaymentReference: paymentRequest.InstructionUUID,
		CallbackURL:              paymentRequest.CallbackURL,
		PayerAlias:               paymentRequest.PayeeAlias,
		Amount:                   paymentRequest.Amount,
		Currency:                 paymentRequest.Currency,
		PayerPaymentReference:    "123",
		Message:                  "Refund",
	}

	refund, err := s.CreateRefund(context.Background(), refundRequest)
	if err != nil {
		log.Fatalf("could not create refund: %s", err.Error())
	}

	_, err = s.Status(context.Background(), refund.Location)
	if err != nil {
		log.Fatalf("could not get status of refund: %s", err.Error())
	}
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Documentation

Index

Constants

View Source
const (
	TestCertificate = "" /* 1816-byte string literal not displayed */
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CreatePaymentRequestOptions

type CreatePaymentRequestOptions struct {
	// Required: The identifier of the payment request to be saved. Example 11A86BE70EA346E4B1C39C874173F088 or d2eb91f4-f3a7-4088-970f-a108b58bf8d9
	// The endpoint will format the string to fit Swish specification.
	InstructionUUID string `json:"-"`

	// Required: The endpoint Swish will call on with payment status updates, you need to receive data on this endpoint
	CallbackURL string `json:"callbackUrl"`

	// Required: The phone number that will receive the payment. Format E.164 except the plus ("+") symbol.
	PayeeAlias string `json:"payeeAlias"`

	// Required: The amount that is charged with a float value. Example "100.01"
	Amount string `json:"amount"`

	// Required: Currency code according to ISO 4217
	Currency string `json:"currency"`

	// Optional: Payment reference of the payee, which is the merchant that receives the payment. This reference could
	// be order id or similar. Allowed characters are a-z A-Z 0-9 -_.+*/ and length must be between 1 and 36 characters.
	PayeePaymentReference string `json:"payeePaymentReference,omitempty"`

	// Optional: The registered cellphone number of the person that makes the payment. It can only contain numbers and
	// has to be at least 8 and at most 15 numbers. It also needs to match the following format in order to be found in
	// Swish: country code + cellphone number (without leading zero). E.g.: 46712345678
	PayerAlias string `json:"payerAlias,omitempty"`

	// Optional: The social security number of the individual making the payment, should match the registered value for
	// payerAlias or the payment will not be accepted. The value should be a proper Swedish social security number
	// (personnummer or sammordningsnummer).
	PayerSSN string `json:"payerSSN,omitempty"`

	// Optional: 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.
	PayerAgeLimit string `json:"payerAgeLimit,omitempty"`

	// Optional: Merchant supplied message about the payment/order. Max 50 chars. Allowed characters are the letters
	// a-ö, A-Ö, the numbers 0-9 and the special characters :;.,?!()”.
	Message string `json:"message,omitempty"`
}

CreatePaymentRequestOptions for the create payment request

type CreateRefundOptions

type CreateRefundOptions struct {
	// Required: InstructionUUID The ID for this refund, should be different from create payment request InstructionUUID
	InstructionUUID string `json:"-"`

	// Required: OriginalPaymentReference Reference of the original payment that this refund is for.
	OriginalPaymentReference string `json:"originalPaymentReference"`

	// Required: CallbackURL URL that Swish will use to notify caller about the outcome of the refund. The URL has to
	// use HTTPS.
	CallbackURL string `json:"callbackUrl"`

	// Required: PayerAlias The Swish number of the merchant that makes the refund payment.
	PayerAlias string `json:"payerAlias"`

	// Required: Amount The amount of money to refund. The amount cannot be less than 0.01 SEK and not more than
	// 999999999999.99 SEK. Moreover, the amount cannot exceed the remaining amount of the original payment that the
	// refund is for.
	Amount string `json:"amount"`

	// Required: Currency The currency to use. The only currently supported value is SEK.
	Currency string `json:"currency"`

	// Optional: PayerPaymentReference Payment reference supplied by the merchant. This could be order id or similar.
	PayerPaymentReference string `json:"payerPaymentReference"`

	// Optional: Merchant supplied message about the refund. Max 50 chars. Allowed characters are the letters a-ö, A-Ö,
	// the numbers 0-9 and the special characters :;.,?!()”.
	Message string `json:"message"`
}

CreateRefundOptions for create refund

type Options

type Options struct {
	// Passphrase is the password for the p12 encoded SSL certificate
	Passphrase string

	// SSLCertificate is a byte encoded array with the SSL certificate content
	SSLCertificate []byte

	// Test indicates whether the http client will use the test environment endpoint and CA certificate
	Test bool // enable test environment

	// CA is base64 encoded string with your certificate authority
	CA string

	// Timeout in seconds for the http client
	Timeout int // Client timeout in seconds
}

Options are settings that is used by the http client

type Swish

type Swish struct {

	// URL is the endpoint which we use to talk with BankID and can be replaced.
	URL string
	// contains filtered or unexported fields
}

Swish holds settings for this session

func New

func New(opts Options) (*Swish, error)

New creates a new client

func (*Swish) CreatePaymentRequest

func (s *Swish) CreatePaymentRequest(ctx context.Context, opts CreatePaymentRequestOptions) (result createPaymentRequestResponse, err error)

CreatePaymentRequest sends a v2 payment request to Swish to create a payment

func (*Swish) CreateRefund

func (s *Swish) CreateRefund(ctx context.Context, opts CreateRefundOptions) (result createRefundResponse, err error)

CreateRefund A merchant that has received a Swish payment can refund the whole or part of the original transaction amount to the consumer.

func (*Swish) Status

func (s *Swish) Status(ctx context.Context, Location string) (result statusResponse, err error)

Status use the location header from other endpoints to get status from Swish

Jump to

Keyboard shortcuts

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