billing

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

akouendy-billing-sdk

Create billing order

Create Gorm billing transaction table
db.AutoMigrate(billing.MigrateModels()...)
Configure and send the request
	// create order on billing platform
	billingConfig := billing.Config{Debug: true, Env: "sandbox"}
	billingConfig.AppBaseUrl = viper.GetString("baseurl")
	billing.Init(billingConfig)
	client, err := billing.NewClient(billing.WithRequestBefore(func(ctx context.Context, req *http.Request) error {
		req.Header.Add("X-Billing-Requestid", uuid.New().String())
		req.Header.Add("Authorization", request.HeaderParameter("Authorization"))
		return nil
	}))
	if err == nil {
		request := billing.OrderRequest{}
		request.AppID = viper.GetString("billing-app.id")
		request.PriceID = viper.GetString("billing-app.priceId")
		request.BillingProvider = "orange-money-sn"
		request.CustomerID = userId
		request.CustomerEmail = ""
		request.CustomerFullName = ""
		ctx := context.Background()
		var billingTrx billing.BillingTransaction
		response, billingTrx, err = client.CreateOrder(ctx, settingsId, request)

		// save transactionId and billing ids
		if err == nil {
			repo := billing.BillingRepository{}
			repo.CreateBillingOrder(&billingTrx, utils.GetInstance().GetDB())
		}
	}

Documentation

Index

Constants

View Source
const (
	SUCCESS PaymentStatus = "SUCCESS"
	SANDBOX Environment   = "sandbox"
	PROD    Environment   = "prod"
)

Variables

This section is empty.

Functions

func Init

func Init(config Config)

Init to configure the sdk

func MigrateModels

func MigrateModels() (models []interface{})

Migrate return models for gorm

func ValidatePaymentWebhook

func ValidatePaymentWebhook(webhook PaymentWebhook) bool

Types

type BillingRepository

type BillingRepository struct{}

func (BillingRepository) CreateBillingOrder

func (r BillingRepository) CreateBillingOrder(m *BillingTransaction, db *gorm.DB) (err error)

func (BillingRepository) GetBillingOrderByPaymentToken

func (r BillingRepository) GetBillingOrderByPaymentToken(paymentToken string, db *gorm.DB) (model BillingTransaction, err error)

func (BillingRepository) GetBillingOrderByPaymentTokenAndTable added in v1.0.5

func (r BillingRepository) GetBillingOrderByPaymentTokenAndTable(table string, paymentToken string, db *gorm.DB) (model BillingTransaction, err error)

func (BillingRepository) GetBillingOrderByTransactionId added in v1.0.5

func (r BillingRepository) GetBillingOrderByTransactionId(transactionID string, db *gorm.DB) (model BillingTransaction, err error)

type BillingTransaction

type BillingTransaction struct {
	utils.Model
	OrderID       string
	PaymentToken  string
	AppTrxId      string
	CountryAlpha3 string `gorm:"default:SEN"`
	PaymentID     string
}

type Client

type Client struct {
	// The endpoint of the server conforming to this interface, with scheme,
	Endpoint string

	// Doer for performing requests, typically a *http.Client with any
	// customized settings, such as certificate chains.
	Client HttpRequestDoer

	// A callback for modifying requests which are generated before sending over
	// the network.
	RequestBefore RequestBeforeFn

	// A callback for modifying response which are generated before sending over
	// the network.
	ResponseAfter ResponseAfterFn

	// The user agent header identifies your application, its version number, and the platform and programming language you are using.
	// You must include a user agent header in each request submitted to the sales partner API.
	UserAgent string
}

func NewClient

func NewClient(opts ...ClientOption) (*Client, error)

Creates a new Client, with reasonable defaults

func (*Client) CreateOrder

func (c *Client) CreateOrder(ctx context.Context, transactionId string, body OrderRequest) (orderResponse OrderResponse, billingTrx BillingTransaction, err error)

func (*Client) CreatePayment added in v1.1.0

func (c *Client) CreatePayment(ctx context.Context, body PaymentRequest) (paymentResponse PaymentResponse, billingTrx BillingTransaction, err error)

func (*Client) GetOrderStatus

func (c *Client) GetOrderStatus(ctx context.Context, body OrderSubsRequest) (orderSubsResponse OrderSubsReponse, err error)

func (*Client) GetPaymentStatus added in v1.1.0

func (c *Client) GetPaymentStatus(ctx context.Context, paymentToken string) (paymentStatusResponse PaymentStatusResponse, err error)

type ClientInterface

type ClientInterface interface {
	CreateOrder(ctx context.Context, transactionId string, body OrderRequest) (orderResponse OrderResponse, billingTrx BillingTransaction, err error)
	GetOrderStatus(ctx context.Context, body OrderSubsRequest) (orderSubsResponse OrderSubsReponse, err error)
	CreatePayment(ctx context.Context, transactionId string, body PaymentRequest) (paymentResponse PaymentResponse, billingTrx BillingTransaction, err error)
	GetPaymentStatus(ctx context.Context, paymentToken string) (paymentStatusResponse PaymentStatusResponse, err error)
}

The interface specification for the client above.

type ClientOption

type ClientOption func(*Client) error

ClientOption allows setting custom parameters during construction

func WithRequestBefore

func WithRequestBefore(fn RequestBeforeFn) ClientOption

WithRequestBefore allows setting up a callback function, which will be called right before sending the request. This can be used to mutate the request.

func WithResponseAfter

func WithResponseAfter(fn ResponseAfterFn) ClientOption

WithResponseAfter allows setting up a callback function, which will be called right after get response the request. This can be used to log.

func WithUserAgent

func WithUserAgent(userAgent string) ClientOption

WithUserAgent set up useragent add user agent to every request automatically

type Config

type Config struct {
	AppBaseUrl string
	Debug      bool
	Env        string

	AppToken string
	// contains filtered or unexported fields
}

type Environment added in v1.1.0

type Environment string

type HttpRequestDoer

type HttpRequestDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer performs HTTP requests.

The standard http.Client implements this interface.

type OrderRequest

type OrderRequest struct {
	CustomerEmail    string
	CustomerFullName string
	CustomerID       string `json:"CustomerId"`
	BillingProvider  string
	CountryAlpha3    string
	PriceID          string `json:"PriceId"`
	AppID            string `json:"AppId"`
	Webhook          string
	ReturnUrl        string
	OwnedBy          string
}

type OrderResponse

type OrderResponse struct {
	OrderID      string `json:"OrderId"`
	PaymentUrl   string
	PriceID      string `json:"PriceId"`
	AppID        string `json:"AppId"`
	PaymentToken string `json:"PaymentToken"`
	Description  string
	Code         string
}

type OrderSubsReponse

type OrderSubsReponse struct {
	CustomerID  string `json:"CustomerId"`
	PriceID     string `json:"PriceId"`
	IsSubscribe bool
	ExpireDate  time.Time
}

type OrderSubsRequest

type OrderSubsRequest struct {
	CustomerID string `json:"CustomerId"`
	PriceID    string `json:"PriceId"`
}

type PaymentRequest added in v1.1.0

type PaymentRequest struct {
	AppId         string
	TransactionId string
	TotalAmount   int
	Hash          string
	Description   string
	ReturnUrl     string
	Webhook       string
	Env           Environment
	Email         string
	FullName      string
	SuccessMsg    string
	FailedMsg     string
	ClientId      string
}

type PaymentResponse added in v1.1.0

type PaymentResponse struct {
	Code       string
	Text       string
	Status     PaymentStatus
	Token      string
	PaymentUrl string
}

type PaymentStatus

type PaymentStatus string

type PaymentStatusResponse added in v1.1.0

type PaymentStatusResponse struct {
	Status           PaymentStatus
	TotalPayedAmount int
	Email            string
}

type PaymentWebhook

type PaymentWebhook struct {
	Hash          string `json:"Hash"`
	Status        string `json:"Status"`
	TransactionID string `json:"TransactionID"`
}

type RequestBeforeFn

type RequestBeforeFn func(ctx context.Context, req *http.Request) error

RequestBeforeFn is the function signature for the RequestBefore callback function

type ResponseAfterFn

type ResponseAfterFn func(ctx context.Context, rsp *http.Response) error

ResponseAfterFn is the function signature for the ResponseAfter callback function

type SubscriptionResponse added in v1.0.1

type SubscriptionResponse struct {
	Data        interface{}
	RedirectUrl string
	Status      SubscriptionStatus
}

type SubscriptionStatus added in v1.0.1

type SubscriptionStatus string
const (
	REDIRECT  SubscriptionStatus = "REDIRECT"
	SUBSCRIBE SubscriptionStatus = "SUBSCRIBE"
)

Jump to

Keyboard shortcuts

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