forked from leavengood/donation_tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paypal.go
135 lines (106 loc) · 2.9 KB
/
paypal.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
const PayPalDateFormat = "2006-01-02T15:04:05Z"
type PayPalTxn struct {
Timestamp time.Time
Type string
Email string
Name string
TransactionID string `json:"transaction_id"`
Status string
Amt float32
FeeAmt float32 `json:"fee_amt"`
NetAmt float32 `json:"net_amt"`
CurrencyCode string `json:"currency_code"`
}
func NewPayPalTxn(tran map[string]string) *PayPalTxn {
result := new(PayPalTxn)
elem := reflect.ValueOf(result).Elem()
for name, value := range tran {
field := elem.FieldByNameFunc(func(n string) bool {
return name == strings.ToUpper(n)
})
if field.IsValid() && field.CanSet() {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Float32:
f, _ := strconv.ParseFloat(value, 32)
field.SetFloat(f)
default:
// its the Timestamp field
timestamp, _ := time.Parse(PayPalDateFormat, value)
field.Set(reflect.ValueOf(timestamp))
}
}
}
return result
}
func (p *PayPalTxn) IsSubscription() bool {
return p.Amt > 0 && p.Type == "Payment"
}
func (p *PayPalTxn) IsDonation() bool {
return p.Amt > 0 && p.Type == "Donation"
}
func (p *PayPalTxn) String() string {
return fmt.Sprintf("%s %s <%s> %s, %s %0.02f (%0.02f fee) = %0.02f, %s", p.Timestamp, p.Name, p.Email,
p.Type, p.CurrencyCode, p.Amt, p.FeeAmt, p.NetAmt, p.Status)
}
type PayPalTxns []*PayPalTxn
func PayPalTxnsFromNvp(nvp *NvpResult) PayPalTxns {
result := make(PayPalTxns, len(nvp.List))
for i, item := range nvp.List {
result[i] = NewPayPalTxn(item)
// fmt.Println(result[i])
}
return result
}
// Sorting
func (p PayPalTxns) Len() int { return len(p) }
func (p PayPalTxns) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type ByDate struct{ PayPalTxns }
func (s ByDate) Less(i, j int) bool {
return s.PayPalTxns[i].Timestamp.Before(s.PayPalTxns[j].Timestamp)
}
func (p PayPalTxns) Sort() {
sort.Sort(ByDate{p})
}
func (p PayPalTxns) TotalByCurrency() CurrencyAmounts {
result := make(CurrencyAmounts)
for _, txn := range p {
result[txn.CurrencyCode] += txn.Amt
}
return result
}
func (p PayPalTxns) FilterDonations() (PayPalTxns, PayPalTxns) {
donations := make(PayPalTxns, 0, len(p))
other := make(PayPalTxns, 0, len(p))
for _, item := range p {
if item.IsDonation() || item.IsSubscription() {
donations = append(donations, item)
} else {
other = append(other, item)
}
}
return donations, other
}
func (p PayPalTxns) Summarize() MonthlySummaries {
result := make(MonthlySummaries)
for _, item := range p {
_, month, _ := item.Timestamp.Date()
summary := result.ForMonth(month)
if item.IsDonation() {
summary.AddOneTime(item.Amt, item.FeeAmt, item.CurrencyCode)
} else if item.IsSubscription() {
summary.AddSubscription(item.Amt, item.FeeAmt, item.CurrencyCode)
}
}
return result
}