-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverifiable.go
109 lines (91 loc) · 4.01 KB
/
verifiable.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
98
99
100
101
102
103
104
105
106
107
108
109
package ravepay
// Verifiable is an abstract representation of any rave resources that can be verified
// verified here
type Verifiable interface {
VerifyStatus() error
VerifyCurrency(string) error
VerifyAmount(int) error
VerifyChargeResponseValue() error
VerifyReference(string) error
}
// TxnVerificationChecklist encapsulates the payment verification process
// It includes details of the payment to verify
// and the verification response
type TxnVerificationChecklist struct {
Amount int `json:"-"`
FlwRef string `json:"flw_ref,omitempty"` // for some weird reason, this just had to be different from below
Flwref string `json:"flwref,omitempty"` // for some weird reason, this just had to be different from above
LastAttempt string `json:"last_attempt,omitempty"`
Normalize string `json:"normalize"`
OnlySuccessful string `json:"only_successful,omitempty"`
VerificationURL string `json:"-"`
SECKEY string `json:"SECKEY"`
TransactionCurrency string `json:"-"`
TxRef string `json:"tx_ref,omitempty"` // for some weird reason, this just had to be different from above
Txref string `json:"txref,omitempty"` // for some weird reason, this just had to be different from below
// Done tracks whether verification has been attempted. It starts out false for new objects and changes to true after #verify is called on the object
Done bool `json:"-"`
}
// VerifyTransaction sends a rave Transaction verfication request and then verfies the response
// It validates that verification checklist contains all the required information for making the request
// http://flw-pms-dev.eu-west-1.elasticbeanstalk.com/flwv3-pug/getpaidx/api/verify
// it also marks the verification as done
// If the verification URL is not set, it set's it to the default from config
func (tvc *TxnVerificationChecklist) VerifyTransaction() (*TxnVerificationResponse, []error) {
// TODO: Validate checklist???
// Make request to endpoint
resp := &TxnVerificationResponse{}
if tvc.VerificationURL == "" {
tvc.VerificationURL = buildURL(txnVerificationURL)
}
err := sendRequestAndParseResponse("POST", tvc.VerificationURL, tvc, resp)
tvc.Done = true
if err != nil {
return resp, []error{err}
}
errs := tvc.Verify(resp)
return resp, errs
}
// VerifyXRequeryTransaction sends a rave XRequery transaction verification request and then verifies the response
// It validates that verification checklist contains all the required information for making the request
// it also marks the verification as done
// https://flutterwavedevelopers.readme.io/v1.0/reference#xrequery-transaction-verification
// If the verification URL is not set, it set's it to the default from config
func (tvc *TxnVerificationChecklist) VerifyXRequeryTransaction() (*XRQTxnVerificationResponse, []error) {
// TODO: Validate checklist???
// TODO: XRQT could return a data array depending on the query args. Handle that possibility
resp := &XRQTxnVerificationResponse{}
if tvc.VerificationURL == "" {
tvc.VerificationURL = buildURL(txnVerificationRequeryURL)
}
err := sendRequestAndParseResponse("POST", tvc.VerificationURL, tvc, resp)
tvc.Done = true
if err != nil {
return resp, []error{err}
}
errs := tvc.Verify(resp)
return resp, errs
}
// Verify performs the rave's recommended verification check on the verifiable resource
// It returns an array of error for verfications that fail (if any)
// and marks the verification as Done
func (tvc *TxnVerificationChecklist) Verify(v Verifiable) []error {
errs := []error{}
if err := v.VerifyCurrency(tvc.TransactionCurrency); err != nil {
errs = append(errs, err)
}
if err := v.VerifyAmount(tvc.Amount); err != nil {
errs = append(errs, err)
}
// Todo solve Flwref vs FlwRef
if err := v.VerifyReference(tvc.FlwRef); err != nil {
errs = append(errs, err)
}
if err := v.VerifyStatus(); err != nil {
errs = append(errs, err)
}
if err := v.VerifyChargeResponseValue(); err != nil {
errs = append(errs, err)
}
return errs
}