flow

package module
v0.0.0-...-8941b6a Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: MIT Imports: 13 Imported by: 0

README

go-flow

Docs

A simple pure Go implementation of the Flow payment API, with handlers for the callbacks and easy access to payment creation and validation.

Example

package main  
  
import (  
   "fmt"  
   "github.com/CamiloHernandez/go-flow"
   "net/http"
 )  
  
func main() {  
   c := flow.NewClient("your api key", "your secret key")  
   c.SetSandbox()  
  
   result, err := c.CreateOrder(flow.OrderRequest{  
	CommerceOrder:   "123123",  
	Subject:         "Test Order",  
	Amount:          1000,  
	PayerEmail:      "example@example.com",  
	ConfirmationURL: "http://example.com/confirmation",  
	ReturnURL:       "http://example.com/return",  
   })  
   if err != nil {  
      panic(err)  
   }  
  
   fmt.Println("ID:", result.FlowID)  
   fmt.Println("Token:", result.Token)  
   fmt.Println("URL:", result.GetPaymentURL())  
  
   http.HandleFunc("/confirm", c.HTTPOrderConfirmationCallback(func(o *flow.Order) {  
      fmt.Println("Order", o.CommerceOrder, "confirmed with status", o.Status)  
   }))  
  
   http.HandleFunc("/return", func(w http.ResponseWriter, r *http.Request) {  
      _, _ = w.Write([]byte("Order completed!"))  
   })  
  
   panic(http.ListenAndServe(":80", nil))  
}

Documentation

Index

Constants

View Source
const (
	ProductionURL = "https://www.flow.cl/api"
	SandboxURL    = "https://sandbox.flow.cl/api"
)
View Source
const (
	// OrderStatusAwaitingPayment indicates that the order's payment is still pending.
	OrderStatusAwaitingPayment = iota + 1

	// OrderStatusPayed indicates that the order has been successfully payed and should be accepted.
	OrderStatusPayed

	// OrderStatusRejected indicates that the payment failed because it was rejected.
	OrderStatusRejected

	// OrderStatusCanceled indicates that some party canceled the order before it was fulfilled.
	OrderStatusCanceled
)
View Source
const (
	// RefundStatusCreated is a refund created and awaiting processing.
	RefundStatusCreated = "created"

	// RefundStatusAccepted is a refund accepted and ready to be transferred.
	RefundStatusAccepted = "accepted"

	// RefundStatusRejected is a refund that was not accepted.
	RefundStatusRejected = "rejected"

	// RefundStatusRefunded is a refund that was accepted and transferred.
	RefundStatusRefunded = "refunded"

	// RefundStatusCanceled is a refund that was canceled by one of the parties.
	RefundStatusCanceled = "canceled"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// APIKey is the access key provided by Flow.
	APIKey string

	// SecretKey is uses to sign the API requests.
	SecretKey string

	// URL is the base URL to be used in the requests. Can be ProductionURL or SandboxURL.
	URL string
}

Client contains the authentication data and the canonical URL used for requests to the Flow API/

func NewClient

func NewClient(apiKey, secretKey string) *Client

NewClient creates a *Client with the given keys. By default it's set to sandbox mode.

func (Client) CancelRefund

func (c Client) CancelRefund(token string) (*RefundStatus, error)

CancelRefund cancels a refund request.

func (Client) CreateEmailOrder

func (c Client) CreateEmailOrder(or OrderRequest) (orderID int, token string, err error)

CreateEmailOrder creates an Order to be sent by email and returns its ID and token.

func (Client) CreateOrder

func (c Client) CreateOrder(or OrderRequest) (*OrderResponse, error)

CreateOrder creates a new order and returns its ID and token.

func (Client) CreateRefund

func (c Client) CreateRefund(r Refund) (*RefundStatus, error)

CreateRefund starts a new refund request.

func (Client) GetOrder

func (c Client) GetOrder(token string) (*Order, error)

GetOrder fetches the an Order based on the provided order token.

func (Client) GetOrderByCommerceID

func (c Client) GetOrderByCommerceID(commerceID string) (*Order, error)

GetOrderByCommerceID fetches the an Order based on the provided commerce identifier.

func (Client) GetOrderByFlowID

func (c Client) GetOrderByFlowID(flowOrderID int) (*Order, error)

GetOrderByFlowID fetches the an Order based on the provided Flow identifier.

func (Client) GetRefundStatus

func (c Client) GetRefundStatus(token string) (*RefundStatus, error)

GetRefundStatus fetches the status of a refund request.

func (*Client) GinOrderConfirmationCallback

func (c *Client) GinOrderConfirmationCallback(onAccepted func(*Order)) gin.HandlerFunc

GinOrderConfirmationCallback returns a gin.HandlerFunc that processes the Flow callback request. It validates the provided token, and if the token matches a accepted order, the onAccepted function gets called.

func (*Client) GinRefundConfirmationCallback

func (c *Client) GinRefundConfirmationCallback(onAccepted func(*RefundStatus)) gin.HandlerFunc

GinRefundConfirmationCallback returns a gin.HandlerFunc that processes the Flow callback request. It validates the provided token, and if the token matches an accepted refund, the onAccepted function gets called.

func (*Client) HTTPOrderConfirmationCallback

func (c *Client) HTTPOrderConfirmationCallback(onAccepted func(*Order)) http.HandlerFunc

HTTPOrderConfirmationCallback returns a http.HandlerFunc that processes the Flow callback request. It validates the provided token, and if the token matches a accepted order, the onAccepted function gets called.

func (*Client) HTTPRefundConfirmationCallback

func (c *Client) HTTPRefundConfirmationCallback(onAccepted func(*RefundStatus)) http.HandlerFunc

HTTPRefundConfirmationCallback returns a http.HandlerFunc that processes the Flow callback request. It validates the provided token, and if the token matches an accepted refund, the onAccepted function gets called.

func (*Client) SetProduction

func (c *Client) SetProduction()

SetProduction sets the Client's URL to the production base URL.

func (*Client) SetSandbox

func (c *Client) SetSandbox()

SetProduction sets the Client's URL to the sandbox base URL.

type Optional

type Optional struct {
	// RUT or Rol Unico Tributario is a unique national identifier
	RUT string `json:"RUT,omitempty"`

	// ID is the ID of the transaction.
	ID string `json:"ID,omitempty"`
}

Optional contains optional fields like RUT and ID.

type Order

type Order struct {
	// FlowOrder is the ID provided by Flow.
	FlowOrder int `json:"flowOrder,omitempty"`

	// CommerceOrder is an optional ID created by the commerce.
	CommerceOrder string `json:"commerceOrder,omitempty"`

	// RequestDate is the date the order was created. If none is set, the current date is used. It follows the format
	// yyyy-mm-dd hh:mm:ss
	RequestDate string `json:"requestDate,omitempty"`

	// Status is the current status of an order it might be one of the following:
	//  1 Awaiting payment - OrderStatusAwaitingPayment
	//  2 Payed 		   - OrderStatusPayed
	//  3 Rejected		   - OrderStatusRejected
	//  4 Canceled         - OrderStatusCanceled
	Status int `json:"status,omitempty"`

	// Subject is the reason for the payment. It might be the items or service being bought.
	Subject string `json:"subject,omitempty"`

	// Currency is optionally set to define the currency of the transaction.
	Currency string `json:"currency,omitempty"`

	// Amount represents the amount of money being charged.
	Amount string `json:"amount,omitempty"`

	// PayerEmail is the email of the payer.
	PayerEmail string `json:"payer,omitempty"`

	// Optional contains optional fields like RUT and ID.
	Optional Optional `json:"optional,omitempty"`

	// PendingInfo reports if a media is still waiting on the payment, and the date since it's waiting.
	PendingInfo PendingInfo `json:"pending_info,omitempty"`

	// PaymentData contains additional information about the payment.
	PaymentData PaymentData `json:"paymentData,omitempty"`

	// MerchantID will be set if a merchant is being a middleman in the payment.
	MerchantID string `json:"merchantId,omitempty"`
}

Order represents a payment order.

type OrderRequest

type OrderRequest struct {
	// CommerceOrder is an optional ID created by the commerce.
	CommerceOrder string `structs:"commerceOrder"`

	// Subject is the reason for the payment. It might be the items or service being bought.
	Subject string `structs:"subject"`

	// Currency is optionally set to define the currency of the transaction.
	Currency string `structs:"currency,omitempty"`

	// Amount represents the amount of money being charged.
	Amount uint64 `structs:"amount"`

	// PayerEmail is the email of the payer.
	PayerEmail string `structs:"email"`

	// PaymentMethod can be set to define the allowed payment methods. It default to 9: all payment methods.
	PaymentMethod int `structs:"paymentMethod,omitempty"`

	// ConfirmationURL is the URL to which Flow will send the order details after the payment is made. This URL must
	// implement the confirmation logic. See GinOrderConfirmationCallback and HTTPOrderConfirmationCallback.
	ConfirmationURL string `structs:"urlConfirmation,omitempty"`

	// ReturnURL is the URL to which the user will be sent after the payment is made and confirmed. Usually on this
	// URL a boucher or confirmation message will be showed.
	ReturnURL string `structs:"urlReturn,omitempty"`

	// TimeoutSeconds is the number of seconds the order should stay active for.
	TimeoutSeconds uint64 `structs:"timeout,omitempty"`

	// MerchantID will be set if a merchant is being a middleman in the payment.
	MerchantID string `structs:"merchantId,omitempty"`

	// PaymentCurrency is optionally set to force the payer to pay in an specific currency.
	PaymentCurrency string `structs:"payment_currency,omitempty"`
}

OrderRequest is the data needed to start a payment order.

type OrderResponse

type OrderResponse struct {
	// FlowID is the Flow identifier for the order.
	FlowID int `json:"flowOrder"`

	// Token is the token used to buildPOST the payment URL.
	Token string `json:"token"`

	// URL is the base URL to redirect for payment.
	URL string `json:"url"`
}

OrderResponse is the values sent by Flow if an order is successfully created.

func (OrderResponse) GetPaymentURL

func (or OrderResponse) GetPaymentURL() string

GetPaymentURL parses the payment URL of an order

type PaymentData

type PaymentData struct {
	// Date is the date of the payment.
	Date string `json:"date,omitempty"`

	// Media refers to a payment entity.
	Media string `json:"media,omitempty"`

	// ConversionDate is the date of the conversion between currencies if more than one was involved in the payment.
	// The format is yyyy-mm-dd hh:mm:ss
	ConversionDate string `json:"conversionDate,omitempty"`

	// ConversionRate is the rate used to convert between currencies if more than one was involved in the payment.
	ConversionRate float64 `json:"conversionRate,omitempty"`

	// Amount is the payed amount.
	Amount string `json:"amount,omitempty"`

	// Currency is the payment in which the payment was made.
	Currency string `json:"currency,omitempty"`

	// Fee is the fee applied to the transaction.
	Fee string `json:"fee,omitempty"`

	// Balance is the Amount minus the Fee
	Balance int64 `json:"balance,omitempty"`

	// TransferDate is the date in which the transfer was made. It format the format yyyy-mm-dd hh:mm:ss
	TransferDate string `json:"transferDate,omitempty"`
}

PaymentData contains additional information about the payment.

type PendingInfo

type PendingInfo struct {
	// Media refers to a payment entity.
	Media string `json:"media,omitempty"`

	// Date is the date since the payment is pending. It format the format yyyy-mm-dd hh:mm:ss
	Date string `json:"date,omitempty"`
}

PendingInfo reports if a media is still waiting on the payment, and the date since it's waiting.

type Refund

type Refund struct {
	// OrderID is the ID of the order being refunded.
	OrderID string `structs:"refundCommerceOrder"`

	// ReceiverEmail is the email of the refunded payer.
	ReceiverEmail string `structs:"receiverEmail"`

	// Amount is the amount of money the refund is for.
	Amount uint64 `structs:"amount"`

	// CallbackURL is where Flow will notify the server about the refund status.
	CallbackURL string `structs:"urlCallBack"`
}

Refund represents a request for a refund.

type RefundStatus

type RefundStatus struct {
	// Token is an identifier for the refund.
	Token string `json:"token"`

	// RefundOrder is the OrderID of the order being refunded.
	RefundOrder string `json:"flowRefundOrder"`

	// Date is the date on which the refund request was created. It format the format yyyy-mm-dd hh:mm:ss
	Date string `json:"date"`

	// Status is the status of the refund. It might be one of:
	// created, accepted, rejected, refunded, canceled
	Status string `json:"status"`

	// Amount is the amount of money being refunded.
	Amount uint64 `json:"amount"`

	// Fee is the fee being charged for the refund.
	Fee uint64 `json:"fee"`
}

RefundStatus is the data related to a refund.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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