-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_payment.go
77 lines (64 loc) · 2.1 KB
/
last_payment.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
package sendios
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/http"
)
type (
LastPaymentRequest struct {
UserID int64 `json:"user_id"` // User id in sendios
Payment
}
Payment struct {
StartDate string `json:"start_date"` // Payment date or subscription start date
ExpireDate string `json:"expire_date,omitempty"` // Subscription end date (optional, default false)
TotalCount string `json:"total_count,omitempty"` // Pay count (optional, default false)
PaymentType string `json:"payment_type,omitempty"` // Pay type (optional, default false)
Amount string `json:"amount,omitempty"` // Pay amount (optional, default false)
}
LastPaymentResponse struct {
Meta `json:"_meta"`
Data struct {
Message string `json:"message"`
Date string `json:"date"`
Status bool `json:"status"`
} `json:"data"`
}
)
func (c *Client) SendLastPayment(req LastPaymentRequest) (*LastPaymentResponse, error) {
data, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "marshal payload")
}
statusCode, body, err := c.makeRequest(http.MethodPost, "https://api.sendios.io/v1/lastpayment", bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "send last payment")
}
if statusCode == http.StatusNotFound {
var resp ErrorResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map last payment error")
}
return nil, fmt.Errorf("last payment error: %s", resp.Data.Error)
}
var resp LastPaymentResponse
if err := json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map last payment response")
}
return &resp, nil
}
func (c *Client) SendLastPaymentByEmail(email string, p Payment) (*LastPaymentResponse, error) {
user, err := c.GetUserInfo(email)
if err != nil {
return nil, errors.Wrap(err, "get user")
}
payment, err := c.SendLastPayment(LastPaymentRequest{UserID: user.Data.User.ID, Payment: p})
if err != nil {
return nil, errors.Wrap(err, "send last payment")
}
return payment, nil
}