forked from mohammadv184/gopayment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopayment.go
88 lines (73 loc) · 2.13 KB
/
gopayment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package gopayment multi gateway payment package for Golang
package gopayment
import (
"github.com/mohammadv184/gopayment/gateway"
"github.com/mohammadv184/gopayment/invoice"
httpClient "github.com/mohammadv184/gopayment/pkg/http"
)
// Version is the version of gopayment
const Version = "v1.9.0"
// Payment is the payment main struct of gopayment
type Payment struct {
driver gateway.Driver
invoice *invoice.Invoice
}
// Amount set the amount of payment invoice
func (p *Payment) Amount(amount int) *Payment {
p.invoice.SetAmount(uint32(amount))
return p.returnThis()
}
// Purchase send the purchase request to the payment gateway
func (p *Payment) Purchase() error {
transID, err := p.driver.Purchase(p.invoice)
if err != nil {
return err
}
p.invoice.SetTransactionID(transID)
return nil
}
// PayURL return the payment URL
func (p *Payment) PayURL() string {
return p.driver.PayURL(p.invoice)
}
// PayMethod returns the Request Method to be used to pay the invoice.
func (p *Payment) PayMethod() string {
return p.driver.PayMethod()
}
// Client sets the driver http client.
func (p *Payment) Client(client httpClient.Client) *Payment {
p.driver.SetClient(client)
return p.returnThis()
}
// GetInvoice return the payment invoice
func (p *Payment) GetInvoice() *invoice.Invoice {
return p.invoice
}
// GetTransactionID return the payment transaction id
func (p *Payment) GetTransactionID() string {
return p.invoice.GetTransactionID()
}
// Description set the payment description
func (p *Payment) Description(description string) *Payment {
p.invoice.SetDescription(description)
return p.returnThis()
}
// Detail set the payment detail
func (p *Payment) Detail(key, value string) *Payment {
p.invoice.Detail(key, value)
return p.returnThis()
}
// RenderRedirectForm renders the html form for redirect to payment page.
func (p *Payment) RenderRedirectForm() (string, error) {
return p.driver.RenderRedirectForm(p.invoice)
}
func (p *Payment) returnThis() *Payment {
return p
}
// NewPayment create a new payment
func NewPayment(driver gateway.Driver) *Payment {
return &Payment{
driver: driver,
invoice: invoice.NewInvoice(),
}
}