payos

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: MIT Imports: 12 Imported by: 3

README

PayOS Go Package

Go Reference

Installation

go get github.com/payOSHQ/payos-lib-golang

Usage

package main

import (
    "fmt"
    "log"

    "github.com/payOSHQ/payos-lib-golang"
)

func main(){
    payos.Key(clientId, apiKey,checksumKey)
    // or with your partner code
    // payos.Key(clientId, apiKey,checksumKey, partnerCode)
    body := CheckoutRequestType{
		OrderCode:   12345,
		Amount:      2000,
		Description: "Thanh toán đơn hàng",
		CancelUrl:   "http://localhost:8080/cancel/",
		ReturnUrl:   "http://localhost:8080/success/",
	}

	data, err := CreatePaymentLink(body)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(data)
}
package main

import (
    "fmt"
    "log"

    "github.com/payOSHQ/payos-lib-golang"
)

func main(){
    payos.Key(clientId, apiKey,checksumKey)
	data, err := GetPaymentLinkInformation("12345")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(data)
}
package main

import (
    "fmt"
    "log"

    "github.com/payOSHQ/payos-lib-golang"
)

func main(){
    payos.Key(clientId, apiKey,checksumKey)
    cancelReason := "Khách hàng hủy đơn hàng"
    data, err := CancelPaymentLink("12345", &cancelReason)
}
Confirm Webhook
package main

import (
    "fmt"
    "log"

    "github.com/payOSHQ/payos-lib-golang"
)

func main(){
    payos.Key(clientId, apiKey,checksumKey)
    data, err := ConfirmWebhook("http://yourdomain.com/webhook/")
}
Verify Webhook
package main

import (
    "fmt"
    "log"

    "github.com/payOSHQ/payos-lib-golang"
)

func main(){
    payos.Key(clientId, apiKey,checksumKey)
    body := WebhookType{}
	data, err := VerifyPaymentWebhookData(body)
}

Development

  1. Clone the repository
  2. Install dependencies
go mod tidy
  1. Run tests
go test
  1. Commit and create new tag
git commit -m "Your message"
git tag v0.0.1
git push origin v0.0.1
  1. Publish to pkg.go.dev
GOPROXY=proxy.golang.org
go list -m github.com/payOSHQ/payos-lib-golang@v0.1.0

Documentation

Index

Constants

View Source
const (
	NoSignatureErrorMessage         = "No signature."
	NoDataErrorMessage              = "No data."
	InvalidSignatureErrorMessage    = "Invalid signature."
	DataNotIntegrityErrorMessage    = "The data is unreliable because the signature of the response does not match the signature of the data."
	WebhookURLInvalidErrorMessage   = "Webhook URL invalid."
	UnauthorizedErrorMessage        = "Unauthorized."
	InternalServerErrorErrorMessage = "Internal Server Error."
	InvalidParameterErrorMessage    = "Invalid Parameter."
	OrderCodeOuOfRange              = "orderCode is out of range."
)
View Source
const (
	InternalServerErrorErrorCode = "20"
	UnauthorizedErrorCode        = "401"
	InvalidParameterErrorCode    = "21"
	NoSignatureErrorCode         = "22"
	NoDataErrorCode              = "23"
	InvalidSignatureErrorCode    = "24"
	DataNotIntegrityErrorCode    = "25"
	WebhookURLInvalidErrorCode   = "26"
)
View Source
const PayOSBaseUrl = "https://api-merchant.payos.vn"

Variables

View Source
var PayOSApiKey string
View Source
var PayOSChecksumKey string
View Source
var PayOSClientId string
View Source
var PayOSPartnerCode string

Functions

func ConfirmWebhook added in v1.0.0

func ConfirmWebhook(webhookUrl string) (string, error)

Validate the Webhook URL of a payment channel and add or update the Webhook URL for that Payment Channel if successful

func CreateSignatureFromObj added in v1.0.0

func CreateSignatureFromObj(obj interface{}, key string) (string, error)

func CreateSignatureOfPaymentRequest added in v1.0.0

func CreateSignatureOfPaymentRequest(data CheckoutRequestType, key string) (string, error)

func Key added in v1.0.0

func Key(clientId string, apiKey string, checksumKey string, partnerCode ...string) error

Set ClientId, APIKey, ChecksumKey and PartnerCode(a string, optional)

func SortObjByKey added in v1.0.0

func SortObjByKey(obj interface{}) (string, error)

Types

type CancelPaymentLinkRequestType added in v1.0.0

type CancelPaymentLinkRequestType struct {
	CancellationReason *string `json:"cancellationReason"`
}

type CheckoutRequestType added in v1.0.0

type CheckoutRequestType struct {
	OrderCode    int64   `json:"orderCode"`
	Amount       int     `json:"amount"`
	Description  string  `json:"description"`
	CancelUrl    string  `json:"cancelUrl"`
	ReturnUrl    string  `json:"returnUrl"`
	Signature    *string `json:"signature"`
	Items        []Item  `json:"items"`
	BuyerName    *string `json:"buyerName"`
	BuyerEmail   *string `json:"buyerEmail"`
	BuyerPhone   *string `json:"buyerPhone"`
	BuyerAddress *string `json:"buyerAddress"`
	ExpiredAt    *int    `json:"expiredAt"`
}

type CheckoutResponseDataType added in v1.0.0

type CheckoutResponseDataType struct {
	Bin           string `json:"bin"`
	AccountNumber string `json:"accountNumber"`
	AccountName   string `json:"accountName"`
	Amount        int    `json:"amount"`
	Description   string `json:"description"`
	OrderCode     int64  `json:"orderCode"`
	Currency      string `json:"currency"`
	PaymentLinkId string `json:"paymentLinkId"`
	Status        string `json:"status"`
	CheckoutUrl   string `json:"checkoutUrl"`
	QRCode        string `json:"qrCode"`
	ExpiredAt     *int   `json:"expiredAt"`
}
func CreatePaymentLink(paymentData CheckoutRequestType) (*CheckoutResponseDataType, error)

Create a payment link for the order data passed in the parameter

type ConfirmWebhookRequestType added in v1.0.0

type ConfirmWebhookRequestType struct {
	WebhookUrl string `json:"webhookUrl"`
}

type Item added in v1.0.0

type Item struct {
	Name     string `json:"name"`
	Quantity int    `json:"quantity"`
	Price    int    `json:"price"`
}

type PayOSError added in v1.0.0

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

func NewPayOSError added in v1.0.0

func NewPayOSError(code, message string) *PayOSError

func (*PayOSError) Error added in v1.0.0

func (e *PayOSError) Error() string

type PayOSResponseType added in v1.0.0

type PayOSResponseType struct {
	Code      string      `json:"code"`
	Desc      string      `json:"desc"`
	Data      interface{} `json:"data"`
	Signature *string     `json:"signature"`
}

type PaymentLinkDataType added in v1.0.0

type PaymentLinkDataType struct {
	Id                 string            `json:"id"`
	OrderCode          int64             `json:"orderCode"`
	Amount             int               `json:"amount"`
	AmountPaid         int               `json:"amountPaid"`
	AmountRemaining    int               `json:"amountRemaining"`
	Status             string            `json:"status"`
	CreateAt           string            `json:"createAt"`
	Transactions       []TransactionType `json:"transactions"`
	CancellationReason *string           `json:"cancellationReason"`
	CancelAt           *string           `json:"cancelAt"`
}
func CancelPaymentLink(orderCode string, cancellationReason *string) (*PaymentLinkDataType, error)

Cancel the payment link of the order

func GetPaymentLinkInformation added in v1.0.0

func GetPaymentLinkInformation(orderCode string) (*PaymentLinkDataType, error)

Get payment information of an order that has created a payment link

type TransactionType added in v1.0.0

type TransactionType struct {
	Reference              string  `json:"reference"`
	Amount                 int     `json:"amount"`
	AccountNumber          string  `json:"accountNumber"`
	Description            string  `json:"description"`
	TransactionDateTime    string  `json:"transactionDateTime"`
	VirtualAccountName     *string `json:"virtualAccountName"`
	VirtualAccountNumber   *string `json:"virtualAccountNumber"`
	CounterAccountBankId   *string `json:"counterAccountBankId"`
	CounterAccountBankName *string `json:"counterAccountBankName"`
	CounterAccountName     *string `json:"counterAccountName"`
	CounterAccountNumber   *string `json:"counterAccountNumber"`
}

type WebhookDataType added in v1.0.0

type WebhookDataType struct {
	OrderCode              int64   `json:"orderCode"`
	Amount                 int     `json:"amount"`
	Description            string  `json:"description"`
	AccountNumber          string  `json:"accountNumber"`
	Reference              string  `json:"reference"`
	TransactionDateTime    string  `json:"transactionDateTime"`
	Currency               string  `json:"currency"`
	PaymentLinkId          string  `json:"paymentLinkId"`
	Code                   string  `json:"code"`
	Desc                   string  `json:"desc"`
	CounterAccountBankId   *string `json:"counterAccountBankId"`
	CounterAccountBankName *string `json:"counterAccountBankName"`
	CounterAccountName     *string `json:"counterAccountName"`
	CounterAccountNumber   *string `json:"counterAccountNumber"`
	VirtualAccountName     *string `json:"virtualAccountName"`
	VirtualAccountNumber   *string `json:"virtualAccountNumber"`
}

func VerifyPaymentWebhookData added in v1.0.0

func VerifyPaymentWebhookData(webhookBody WebhookType) (*WebhookDataType, error)

Verify data received via webhook after payment

type WebhookType added in v1.0.0

type WebhookType struct {
	Code      string           `json:"code"`
	Desc      string           `json:"desc"`
	Success   bool             `json:"success"`
	Data      *WebhookDataType `json:"data"`
	Signature string           `json:"signature"`
}

Jump to

Keyboard shortcuts

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