-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
267 lines (217 loc) · 7.4 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/price"
"github.com/stripe/stripe-go/v72/sub"
)
const corrilyAPI = "https://client.corrily.com/v1/prices"
// Structs for the Corrily request and response
type CorrilyRequestPayload struct {
UserID string `json:"user_id"`
IP string `json:"ip"`
Country string `json:"country"`
Products []string `json:"products"`
}
type CorrilyProduct struct {
Price float64 `json:"price"`
Integrations Integrations `json:"integrations"`
}
type Integrations struct {
Stripe StripeDetail `json:"stripe"`
}
type StripeDetail struct {
Currency string `json:"currency"`
Amount int64 `json:"amount"`
}
type UserDetails struct {
UserID string
IP string
Country string
}
func main() {
// Set Stripe API key from environment variable
stripe.Key = os.Getenv("STRIPE_SECRET_KEY")
// Get corrily api key from environment variable
corrilyAPIKey := os.Getenv("CORRILY_API_KEY")
userDetails := getUserDetails()
// Get pricing from Corrily
payload := getCorrilyRequestPayload(userDetails)
products, err := getCorrilyPrice(corrilyAPIKey, payload)
if err != nil {
log.Fatalf("Failed to fetch pricing from Corrily: %v", err)
}
selectedProductCorrilyID := getUserProductSelection(products)
selectedProductStripeID, productInterval, err := getStripeProductID(selectedProductCorrilyID)
if err != nil {
log.Fatalf("No product selected or available for subscription")
}
selectedProduct := products[selectedProductCorrilyID]
currency := selectedProduct.Integrations.Stripe.Currency
unitAmount := selectedProduct.Integrations.Stripe.Amount
stripePriceID, err := createOrGetPrice(selectedProductStripeID, unitAmount, currency, productInterval)
if err != nil {
log.Printf("Failed to create or get stripe price for product %s: %v", selectedProductStripeID, err)
}
stripeCustomerID, err := getStripeCustomerID(userDetails.UserID)
if err != nil {
log.Printf("Error retrieving Stripe Customer ID: %v", err)
return
}
if err := createStripeSubscription(stripeCustomerID, stripePriceID); err != nil {
log.Printf("Failed to create stripe subscription for product %s: %v", selectedProductStripeID, err)
}
}
func getCorrilyRequestPayload(userDetails UserDetails) CorrilyRequestPayload {
// For now, these product IDs are hardcoded
return CorrilyRequestPayload{
UserID: userDetails.UserID,
IP: userDetails.IP,
Country: userDetails.Country,
Products: []string{
"corrily_product_id1",
"corrily_product_id2",
},
}
}
func getCorrilyPrice(apiKey string, payload CorrilyRequestPayload) (map[string]CorrilyProduct, error) {
data, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("Error marshalling Corrily payload: %v", err)
}
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, corrilyAPI, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Error creating new request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", apiKey) // Set the x-api-key header using the provided apiKey parameter
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP request to Corrily failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Corrily responded with status code: %d", resp.StatusCode)
}
var response struct {
Products map[string]CorrilyProduct `json:"products"`
}
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("Error decoding Corrily response: %v", err)
}
return response.Products, nil
}
func createOrGetPrice(productID string, amount int64, currency string, productInterval string) (string, error) {
existingPriceID, err := checkPriceExists(productID, amount, currency)
if err != nil {
return "", err
}
if existingPriceID != "" {
return existingPriceID, nil
}
newPriceID, err := createPrice(productID, amount, currency, productInterval)
if err != nil {
return "", err
}
return newPriceID, nil
}
func checkPriceExists(productID string, amount int64, currency string) (string, error) {
priceListParams := &stripe.PriceListParams{Product: stripe.String(productID)}
pricesIter := price.List(priceListParams)
stripeCurrency := getStripeCurrency(currency)
for pricesIter.Next() {
p := pricesIter.Price()
if p.UnitAmount == amount && p.Currency == stripeCurrency {
return p.ID, nil
}
}
if err := pricesIter.Err(); err != nil {
return "", fmt.Errorf("Error listing prices: %v", err)
}
return "", nil
}
func createPrice(productID string, amount int64, currency string, productInterval string) (string, error) {
newPriceParams := &stripe.PriceParams{
Product: &productID,
UnitAmount: stripe.Int64(amount),
Currency: stripe.String(currency),
Recurring: &stripe.PriceRecurringParams{
Interval: stripe.String(productInterval),
},
}
newPrice, err := price.New(newPriceParams)
if err != nil {
return "", fmt.Errorf("Error creating new price: %v", err)
}
return newPrice.ID, nil
}
func createStripeSubscription(customerID string, priceID string) error {
params := &stripe.SubscriptionParams{
Customer: stripe.String(customerID),
Items: []*stripe.SubscriptionItemsParams{
{Price: stripe.String(priceID)},
},
// This is just for demo purpose
// In real integration, this will be using actual payment id based on user
// payment method selection
CollectionMethod: stripe.String("send_invoice"),
DaysUntilDue: stripe.Int64(1),
}
if _, err := sub.New(params); err != nil {
return fmt.Errorf("Error creating Stripe subscription: %v", err)
}
return nil
}
func getStripeCurrency(currencyCode string) stripe.Currency {
return stripe.Currency(strings.ToUpper(currencyCode))
}
func getUserProductSelection(products map[string]CorrilyProduct) string {
// in real world this will be the product user chooses to subscribe to
// for eg purpose, we are returning the first product emulating user action
for productID := range products {
return productID
}
return ""
}
func getUserDetails() UserDetails {
// Example user details; in a real-world scenario, this will be fetched dynamically
return UserDetails{
UserID: "internal_user_id_passed_to_corrily",
IP: "192.168.1.1",
Country: "GB",
}
}
func getStripeProductID(corrilyProductID string) (string, string, error) {
// Hardcoded mapping of Corrily product IDs to Stripe product IDs for demo purposes
productMapping := map[string]struct {
StripeID string
Interval string
}{
"corrily_product_id1": {"stripe_product_id", "year"},
"corrily_product_id2": {"stripe_product_id", "month"},
}
productData, exists := productMapping[corrilyProductID]
if !exists {
return "", "", fmt.Errorf("No Stripe product ID found for Corrily product ID: %s", corrilyProductID)
}
return productData.StripeID, productData.Interval, nil
}
func getStripeCustomerID(userID string) (string, error) {
// Hardcoded mapping of internal user IDs to Stripe customer IDs.
// In a real application, replace this with the appropriate logic to retrieve your users' Stripe customer IDs.
customerMapping := map[string]string{
"internal_user_id_passed_to_corrily": "stripe_customer_id",
}
stripeCustomerID, exists := customerMapping[userID]
if !exists {
return "", fmt.Errorf("No Stripe customer ID found for internal user ID: %s", userID)
}
return stripeCustomerID, nil
}