client

package module
v0.0.0-...-c8557f9 Latest Latest
Warning

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

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

README

Satispay golang client

Build Status GoDoc Go Report Card

This is the implementation of a client library (written in golang) in order to interact with Satispay online API.

Usage

In order to include this client in your project run:

go get github.com/charliemaiors/satispay-client

And use it in your code:

import client "github.com/charliemaiors/satispay-client"

func main(){
        client, err := client.NewClient("your-satispay-token", false)
        valid := client.CheckBearer()
        user, err := client.CreateUser("user-phone-number")
        ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Charge

type Charge struct {
	ID                   string             `json:"id"`
	Description          string             `json:"description"`
	Currency             string             `json:"currency"`
	Amount               int64              `json:"amount"`
	Status               ChargeStatus       `json:"status"`
	StatusDetail         ChargeStatusDetail `json:"status_detail"`
	UserID               string             `json:"user_id"`
	UserShortName        string             `json:"user_short_name"`
	Metadata             map[string]string  `json:"metadata"`
	RequiredSuccessEmail bool               `json:"required_success_email"`
	ExpireDate           time.Time          `json:"expire_date"`
	CallbackURL          string             `json:"callback_url"`
}

Charge represent a Satispay charge

func (Charge) String

func (charge Charge) String() string

type ChargeRequest

type ChargeRequest struct {
	UserID               string            `json:"user_id"`
	Description          string            `json:"description"`
	Currency             string            `json:"currency"`
	CallBackURL          string            `json:"callback_url"`
	Amount               int64             `json:"amount"`
	Metdata              map[string]string `json:"metadata"`
	RequiredSuccessEmail bool              `json:"required_success_email"`
	ExpireIn             int               `json:"expire_in"`
}

ChargeRequest represent a Satispay charge request for a target user identified by its id

func NewChargeRequest

func NewChargeRequest(userID, description, currency, callbackURL string, metadata map[string]string, requiredSuccessEmail bool, amount int64, expireIn int) (*ChargeRequest, error)

NewChargeRequest create a new ChargeRequest performing validations on parameters as specified in Satispay API documentation https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#create-a-charge

func (*ChargeRequest) String

func (request *ChargeRequest) String() string

String is the implementation of Stringer interface for ChargeRequest

type ChargeStatus

type ChargeStatus int

ChargeStatus represent the status of requested Charge

const (
	//Required Charge sent to a user waitng for acceptance
	Required ChargeStatus = iota

	//Success Charge accepted by the user
	Success

	//Failure Charge failed, more details can be found on ChargeStatusDetail
	Failure
)

func (ChargeStatus) MarshalJSON

func (r ChargeStatus) MarshalJSON() ([]byte, error)

MarshalJSON is generated so ChargeStatus satisfies json.Marshaler.

func (ChargeStatus) String

func (i ChargeStatus) String() string

func (*ChargeStatus) UnmarshalJSON

func (r *ChargeStatus) UnmarshalJSON(data []byte) error

UnmarshalJSON is generated so ChargeStatus satisfies json.Unmarshaler.

type ChargeStatusDetail

type ChargeStatusDetail int

ChargeStatusDetail represent the detail regarding a failure of Charge operation

const (
	//DeclinedByPayer user declined the Charge
	DeclinedByPayer ChargeStatusDetail = iota

	//DeclinedByPayerNotRequired user declined the Charge because he did not request it
	DeclinedByPayerNotRequired

	//CancelByNewCharge same Charge sent to the same user, the second will override the first
	CancelByNewCharge

	//InternalFailure generic error
	InternalFailure

	//Expired the Charge has expired
	Expired
)

func (ChargeStatusDetail) MarshalJSON

func (r ChargeStatusDetail) MarshalJSON() ([]byte, error)

MarshalJSON is generated so ChargeStatusDetail satisfies json.Marshaler.

func (ChargeStatusDetail) String

func (i ChargeStatusDetail) String() string

func (*ChargeStatusDetail) UnmarshalJSON

func (r *ChargeStatusDetail) UnmarshalJSON(data []byte) error

UnmarshalJSON is generated so ChargeStatusDetail satisfies json.Unmarshaler.

type Checkout

type Checkout struct {
	CheckoutRequest
	ExpireIn    int32  `json:"expire_in"`
	ID          string `json:"id"`
	CreatedAt   int64  `json:"created_at"`
	CheckoutURL string `json:"checkout_url"`
}

Checkout is the resulting object after submission of a new CheckoutRequest

type CheckoutRequest

type CheckoutRequest struct {
	PhoneNumber string `json:"phone_number"`
	RedirectURL string `json:"redirect_url"`
	Description string `json:"description"`
	CallbackURL string `json:"callback_url"`
	AmountUnit  int64  `json:"amount_unit"`
	Currency    string `json:"currency"`
}

CheckoutRequest represent the request for a checkout, please be sure that you have created a charge before

func NewCheckoutRequest

func NewCheckoutRequest(phoneNumber, redirectURL, description, callbackURL, currency string, amount int64) (*CheckoutRequest, error)

NewCheckoutRequest create a new request for checkout

func (*CheckoutRequest) String

func (request *CheckoutRequest) String() string

type Client

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

Client is the main structure of this library, represent the main client in order to interact with Satispay platform

func NewClient

func NewClient(token string, isProduction bool) (*Client, error)

NewClient define a new instance of Client struct, at creation time it will ONLY check if token is not equals to an empty string. If is empty the function will return an error otherwise a pointer to the built client

func (*Client) CheckBearer

func (client *Client) CheckBearer() bool

CheckBearer refers to https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#api-check-bearer api, this method will check through satispay api if the provided token is valid, is highly recommended to use this method after client creation at least the first time

func (*Client) CreateCharge

func (client *Client) CreateCharge(chargeRequest *ChargeRequest, idempotencyKey string) (Charge, error)

CreateCharge create a Charge having a user id.

func (*Client) CreateCheckout

func (client *Client) CreateCheckout(checkoutRequest *CheckoutRequest, idempotencyKey string) (checkout Checkout, err error)

CreateCheckout creates a new checkout on Satispay Platform

func (*Client) CreateRefund

func (client *Client) CreateRefund(refundRequest *RefundRequest, idempotencyKey string) (Refund, error)

CreateRefund create a refund, you must specify the Charge to create it on.

func (*Client) CreateUser

func (client *Client) CreateUser(phoneNumber, idempotencyKey string) (user User, err error)

CreateUser create a user you want to send Charge request to. The user must be subscribed to satispay service. Once you create a user you do not need to create it again but it is enough create a Charge with the user id used previously. But don’t worry, if you do not store user id you can call again the Create a user and, for the same phone number, it will always return the same user id.

func (*Client) GetCharge

func (client *Client) GetCharge(chargeID string) (Charge, error)

GetCharge get a Charge by id

func (*Client) GetChargeList

func (client *Client) GetChargeList(limit int, startingAfter, endingBefore string, startingAfterDate time.Time) ([]Charge, error)

GetChargeList get a list of Charge ordered by creation. To get element staring after or ending before (excludes) a Charge passed populate starting_after or ending_before with the Charge id. If both starting_after and ending before elements are passed return element ending before the id passed. Limit value indicate the number of elements returned. You could also pass a starting_after_timestamp query param with a UNIX timestamp in mills, will be returned the Charges after that date.

func (*Client) GetRefund

func (client *Client) GetRefund(refundID string) (ref Refund, err error)

GetRefund get a refund by id

func (*Client) GetRefundList

func (client *Client) GetRefundList(limit int, startingAfter, endingBefore, chargeID string) ([]Refund, error)

GetRefundList returns a list of refund objects issued in a predetermined time window

func (*Client) GetTotalAmount

func (client *Client) GetTotalAmount(startingDate, endingDate time.Time) (amount TotalAmount, err error)

GetTotalAmount calculate the total amount of Charges in SUCCESS status and the total amount of Refunds within a specific timeframe.

func (*Client) GetUser

func (client *Client) GetUser(userID string) (User, error)

GetUser return a user data from a given user ID

func (*Client) String

func (client *Client) String() string

func (*Client) UpdateCharge

func (client *Client) UpdateCharge(chargeID, description string, metadata map[string]string, changeState bool) (Charge, error)

UpdateCharge update a Charge, only metadata, description and state can be updated. About parameters: Metadata - Object key-value - not mandatory - Object with max 20 keys. Key length 45 characters and values 500 characters. New keys will be added, the existing keys will be updated and keys set with null value will be removed. Description - string - not mandatory - a Charge description ChargeState - bool - not mandatory - if set the target Charge gets canceled; the staus will be set to FAILURE and the status_detail will be set to DECLINED_BY_PAYER.

func (*Client) UpdateRefund

func (client *Client) UpdateRefund(refundID string, metadata map[string]string) (ref Refund, err error)

UpdateRefund updates the specified refund by setting the values of the parameters passed. Any parameters not provided will be left unchanged.

func (*Client) UserList

func (client *Client) UserList(limit int, startingAfter, endingBefore string) ([]User, error)

UserList get the list of Shop Users of a Online shop.

type Refund

type Refund struct {
	ID string `json:"id"`
	*RefundRequest
}

Refund is the rapresentation of refund object in Satispay Platform, this object will be defined (and returned) after definition of a RefundRequest and submission to platform

type RefundReason

type RefundReason int

RefundReason indicating the reason for the refund.

const (
	//Duplicate means a charge paid twice for some reason
	Duplicate RefundReason = iota

	//Fraudulent means that a charge is fraudolent
	Fraudulent

	//RequestedByCustomer for other reason requested by customer
	RequestedByCustomer
)

func (RefundReason) MarshalJSON

func (r RefundReason) MarshalJSON() ([]byte, error)

MarshalJSON is generated so RefundReason satisfies json.Marshaler.

func (RefundReason) String

func (i RefundReason) String() string

func (*RefundReason) UnmarshalJSON

func (r *RefundReason) UnmarshalJSON(data []byte) error

UnmarshalJSON is generated so RefundReason satisfies json.Unmarshaler.

type RefundRequest

type RefundRequest struct {
	ChargeID    string            `json:"charge_id"`
	Description string            `json:"description"`
	Amount      int64             `json:"amount"`
	Currency    string            `json:"currency"`
	Reason      RefundReason      `json:"reason"`
	Metadata    map[string]string `json:"metadata"`
}

RefundRequest represent the request for refund in Satispay Platform

func NewRefundRequest

func NewRefundRequest(chargeID, description, currency string, amount int64, reason RefundReason, metadata map[string]string) (*RefundRequest, error)

NewRefundRequest create a new refund request structure performing validation described in Satispay API https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#create-a-refund

func (*RefundRequest) String

func (request *RefundRequest) String() string

String is the implementation of Stringer interface for RefundRequest

type SatispayError

type SatispayError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

SatispayError represent satispay error message

func (SatispayError) String

func (satisErr SatispayError) String() string

type TotalAmount

type TotalAmount struct {
	TotalChargeAmountUnit int64  `json:"total_charge_amount_unit"`
	TotalRefundAmountUnit int64  `json:"total_refund_amount_unit"`
	Currency              string `json:"currency"`
}

TotalAmount represent the total charge requested and refunds using Satispay Platform

type User

type User struct {
	ID          string `json:"id"`
	PhoneNumber string `json:"phone_number"`
}

User represent a Satispay user resource

Jump to

Keyboard shortcuts

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