This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe.go
85 lines (77 loc) · 2.07 KB
/
stripe.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
package conveyearthgo
import (
"aletheiaware.com/authgo"
"fmt"
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/account"
"log"
"strings"
"time"
)
func FormatStripeAmount(amount float64, currency stripe.Currency) string {
switch currency {
case stripe.CurrencyBGN:
// Format as decimal with lev size
price := fmt.Sprintf("%.2f", amount/100.0)
// Remove trailing zeros
price = strings.TrimRight(price, "0")
// Remove trailing point
price = strings.TrimRight(price, ".")
return price + " лв."
case stripe.CurrencyGBP:
// Format as decimal with pound size
price := fmt.Sprintf("£%.2f", amount/100.0)
// Remove trailing zeros
price = strings.TrimRight(price, "0")
// Remove trailing point
price = strings.TrimRight(price, ".")
return price
case stripe.CurrencyUSD:
// Format as decimal with dollar size
price := fmt.Sprintf("$%.2f", amount/100.0)
// Remove trailing zeros
price = strings.TrimRight(price, "0")
// Remove trailing point
price = strings.TrimRight(price, ".")
return price
default:
log.Println("Unhandled currency:", currency)
}
return ""
}
type StripeDatabase interface {
CreateStripeAccount(int64, string, time.Time) (int64, error)
SelectStripeAccount(int64) (string, time.Time, error)
}
type StripeManager interface {
NewStripeAccount(*authgo.Account, *stripe.Account) error
StripeAccount(*authgo.Account) (*stripe.Account, error)
}
func NewStripeManager(db StripeDatabase) StripeManager {
return &stripeManager{
database: db,
}
}
type stripeManager struct {
database StripeDatabase
}
func (m *stripeManager) NewStripeAccount(a *authgo.Account, s *stripe.Account) error {
created := time.Now()
sa, err := m.database.CreateStripeAccount(a.ID, s.ID, created)
if err != nil {
return err
}
log.Println("Created Stripe Account", sa)
return nil
}
func (m *stripeManager) StripeAccount(a *authgo.Account) (*stripe.Account, error) {
id, _, err := m.database.SelectStripeAccount(a.ID)
if err != nil {
return nil, err
}
s, err := account.GetByID(id, nil)
if err != nil {
return nil, err
}
return s, nil
}