gopayment

package module
Version: v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2022 License: MIT Imports: 3 Imported by: 1

README

GoPayment

Go Reference GitHub license GitHub issues codecov Go Report Card Build Status

Multi Gateway Payment Package for Golang.

List of contents

List of available drivers

Installation

go get -u github.com/mohammadv184/gopayment

How to use

Purchase

In order to pay the invoice, we need the payment transactionId. We purchase the invoice to retrieve transaction id:

// Configure the Gateway Driver
gateway:=&payping.Driver{
Callback:    "http://example.test/callback",
Token:       "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}
// Create new Payment.
payment := gopayment.NewPayment(gateway)
// Set Invoice Amount.
payment.Amount(amountInt)
// Set Invoice Description.
payment.Description("description")
// Purchase the invoice.
err = payment.Purchase()
if err != nil {
    fmt.Println(err)
}
// Get Transaction ID
transactionID := payment.GetTransactionID()
Pay

After purchasing the invoice, we can redirect the user to the bank payment page:

func pay() gin.HandlerFunc {
    return func(c *gin.Context) {
        // Configure the Gateway Driver
        gateway:=&payping.Driver{
        Callback:    "http://example.test/callback",
        Token:       "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        }
        // Create new Payment.
        payment := gopayment.NewPayment(gateway)
        // Set Invoice Amount.
		payment.Amount(amountInt)
		// Set Invoice Description.
		payment.Description("description")
		// Purchase the invoice.
        err = payment.Purchase()
        if err != nil {
            fmt.Println(err)
        }
        // Get Transaction ID And Save it to the database.
        transactionID := payment.GetTransactionID()
        // Redirect the user to the bank payment page.
        payUrl := payment.PayURL()
		c.Redirect(http.StatusFound, payUrl)
	}
}

Verify payment

When user has completed the payment, the bank redirects them to your website, then you need to verify your payment to make sure that the payment is valid.:

// Configure the Gateway Driver
gateway:=&payping.Driver{
Callback:    "http://example.test/callback",
Token:       "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}

refId := c.Query("refId")
VerifyRequest:=&payping.VerifyRequest{
Amount: "100",
RefID:  refID,
}

if receipt, err := gateway.Verify(VerifyRequest); err == nil {

c.JSON(200, gin.H{
"status": "success",
"data":   receipt.GetReferenceID(),
"date":   receipt.GetDate().String(),
"card":   receipt.GetDetail("cardNumber"),
"gateway": receipt.GetDriver(),
})
} else {
c.JSON(200, gin.H{
"message": "error " + err.Error(),
})
}
Working with invoices

When you make a payment, the invoice is automatically generated within the payment

In your code, use it like the below:

// Create new Payment.
payment := gopayment.NewPayment(gateway)
// Get the invoice.
invoice:=payment.GetInvoice()
// Set Invoice Amount.
invoice.SetAmount(1000)
// Set Invoice Description.
invoice.SetDescription("description")
// Set Invoice Deatils.
invoice.Detail("phone","0912345678")
invoice.Detail("email","example@example.com")

Available methods:

  • SetUuid: set the invoice unique id
  • GetUuid: retrieve the invoice current unique id
  • Detail: attach some custom details into invoice
  • GetDetail: retrieve the invoice detail
  • GetDetails: retrieve all custom details
  • SetAmount: set the invoice amount
  • GetAmount: retrieve invoice amount
  • SetTransactionID: set invoice payment transaction id
  • GetTransactionID: retrieve payment transaction id
  • SetDescription: set invoice Description
  • GetDescription: retrieve payment Description
Working with receipts

When you verify a payment, the receipt is automatically generated.

In your code, use it like the below:

// Verify the Payment.
receipt := gateway.verify(...)
// Get the Payment Reference ID.
refId := receipt.GetReferenceID()
// Get the payment date .
paymentDate:=receipt.GetDate()
// Get the payment driver name.
paymentDriver:=receipt.GetDriver()
// Get payment Deatils.
cardNum:=receipt.GetDetail("cardNumber")
cardHNum:=receipt.GetDetail("HashedCardNumber")


Available methods:

  • GetReferenceID: retrieve the payment reference id
  • GetDriver: retrieve the payment driver name
  • Detail: attach some custom details into invoice
  • GetDate: retrieve payment date
  • GetDetail: retrieve the invoice detail
  • GetDetails: retrieve all custom details
Example

There is an example project using GoPayment you can find at GoPayment-Example Repo It contains a payment implementation.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email mohammadv184@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Documentation

Overview

Package gopayment multi gateway payment package for Golang

Index

Constants

View Source
const Version = "v1.9.0"

Version is the version of gopayment

Variables

This section is empty.

Functions

This section is empty.

Types

type Payment

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

Payment is the payment main struct of gopayment

func NewPayment

func NewPayment(driver gateway.Driver) *Payment

NewPayment create a new payment

func (*Payment) Amount

func (p *Payment) Amount(amount int) *Payment

Amount set the amount of payment invoice

func (*Payment) Client added in v1.5.0

func (p *Payment) Client(client httpClient.Client) *Payment

Client sets the driver http client.

func (*Payment) Description added in v1.5.0

func (p *Payment) Description(description string) *Payment

Description set the payment description

func (*Payment) Detail added in v1.5.0

func (p *Payment) Detail(key string, value string) *Payment

Detail set the payment detail

func (*Payment) GetInvoice

func (p *Payment) GetInvoice() *invoice.Invoice

GetInvoice return the payment invoice

func (*Payment) GetTransactionID added in v1.0.0

func (p *Payment) GetTransactionID() string

GetTransactionID return the payment transaction id

func (*Payment) PayMethod added in v1.1.0

func (p *Payment) PayMethod() string

PayMethod returns the Request Method to be used to pay the invoice.

func (*Payment) PayURL added in v1.2.0

func (p *Payment) PayURL() string

PayURL return the payment URL

func (*Payment) Purchase

func (p *Payment) Purchase() error

Purchase send the purchase request to the payment gateway

func (*Payment) RenderRedirectForm added in v1.7.0

func (p *Payment) RenderRedirectForm() (string, error)

RenderRedirectForm renders the html form for redirect to payment page.

Source Files

Directories

Path Synopsis
pkg
test

Jump to

Keyboard shortcuts

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