This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
charge.go
97 lines (81 loc) · 2.92 KB
/
charge.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
89
90
91
92
93
94
95
96
97
package pay
import (
"fmt"
"time"
)
// ChargeService service user by charge
type ChargeService service
type Charge struct {
ID string `json:"id"`
Addresses map[string]string `json:"addresses"`
Pricing map[string]Pricing `json:"pricing"`
Payments []Payment `json:"payments"`
CreatedAt time.Time `json:"created_at"`
Code string `json:"code"`
HostedURL string `json:"hosted_url"`
RedirectURL string `json:"redirect_url"`
CancelURL string `json:"cancel_url"`
ExpiresAt time.Time `json:"expires_at"`
ConfirmedAt time.Time `json:"confirmed_at"`
Timeline []ChargeStatus `json:"timelines"`
Meta map[string]interface{} `json:"metadata"`
}
// Pricing ...
type Pricing struct {
ID string `json:"-" db:"id"`
ChargeID string `json:"-" db:"charge_id"`
CreatedAt time.Time `json:"-" db:"created_at"`
UpdatedAt time.Time `json:"-" db:"updated_at"`
Currency string `json:"currency" db:"currency"`
Value float64 `json:"value,string" db:"value"`
Rate float64 `json:"rate,string" db:"rate"`
}
// ChargeStatus ...
type ChargeStatus struct {
CreatedAt time.Time `json:"time"`
Status string `json:"status"`
Context string `json:"context"`
}
type Payment struct {
CreatedAt time.Time `json:"created_at"`
Network string `json:"network"`
TransactionID string `json:"transaction_id"`
TransactionHash string `json:"transaction_hash"`
Status string `json:"status"`
DetectedAt time.Time `json:"detected_at"`
Traded bool `json:"traded"`
Value Balance `json:"value" bson:"value"`
}
type Amount struct {
Amount float64 `json:"amount,string" bson:"amount"`
Currency string `json:"currency" bson:"currency"`
}
type Balance struct {
Local *Amount `json:"local" bson:"local"`
Crypto *Amount `json:"crypto,omitempty"`
}
// List lists the available charges a business has. page and limit are used to paginate the response.
// page is the page to get from, while limit i
func (c *ChargeService) List(page, limit int) ([]Charge, error) {
var cc = []Charge{}
err := c.client.Call("GET", fmt.Sprintf("/charges?page=%d&limit=%d", page, limit), nil, &cc)
return cc, err
}
// Get gets a single charge with the given id
func (c *ChargeService) Get(id string) (Charge, error) {
var ch Charge
err := c.client.Call("GET", fmt.Sprintf("/charges/%s", id), nil, &ch)
return ch, err
}
// Create creates a new charge
func (c *ChargeService) Create(cc ChargeCreateReq) (Charge, error) {
var ch Charge
err := c.client.Call("POST", fmt.Sprintf("/charges"), cc, &ch)
return ch, err
}
// Cancel cancels a charge
func (c *ChargeService) Cancel(id string) (Charge, error) {
var ch Charge
err := c.client.Call("POST", fmt.Sprintf("/charges/%s/cancel", id), nil, &ch)
return ch, err
}