Skip to content

Commit

Permalink
Add a public /settings endpoint
Browse files Browse the repository at this point in the history
This allow client libraries easy access to public settings to detect
which payment methods are enabled and access the needed public keys/ids
  • Loading branch information
biilmann committed Jan 14, 2018
1 parent 2ea4def commit 2ca0b10
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
r.Get("/{coupon_code}", api.CouponView)
})

r.With(adminRequired).Get("/settings", api.ViewSettings)
r.Get("/settings", api.ViewSettings)

r.With(authRequired).Post("/claim", api.ClaimOrders)
})
Expand Down
23 changes: 16 additions & 7 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@ package api

import (
"fmt"
"io"
"net/http"

"github.com/netlify/gocommerce/calculator"
gcontext "github.com/netlify/gocommerce/context"
)

func (a *API) ViewSettings(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
config := gcontext.GetConfig(ctx)

resp, err := a.httpClient.Get(config.SettingsURL())
settings, err := a.loadSettings(ctx)
if err != nil {
return fmt.Errorf("Error loading site settings: %v", err)
}
defer resp.Body.Close()

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, err = io.Copy(w, resp.Body)
return err
pms := &calculator.PaymentMethods{}
if config.Payment.Stripe.Enabled {
pms.Stripe.Enabled = true
pms.Stripe.PublicKey = config.Payment.Stripe.PublicKey
}
if config.Payment.PayPal.Enabled {
pms.PayPal.Enabled = true
pms.PayPal.ClientID = config.Payment.PayPal.ClientID
pms.PayPal.Environment = config.Payment.PayPal.Env
}
settings.PaymentMethods = pms

sendJSON(w, 200, settings)
return nil
}
18 changes: 16 additions & 2 deletions calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,25 @@ type ItemPrice struct {
Total int64
}

// PaymentMethods settings
type PaymentMethods struct {
Stripe struct {
Enabled bool `json:"enabled"`
PublicKey string `json:"public_key,omitempty"`
} `json:"stripe"`
PayPal struct {
Enabled bool `json:"enabled"`
ClientID string `json:"client_id,omitempty"`
Environment string `json:"environment,omitempty"`
} `json:"paypal"`
}

// Settings represent the site-wide settings for price calculation.
type Settings struct {
PricesIncludeTaxes bool `json:"prices_include_taxes"`
Taxes []*Tax `json:"taxes"`
MemberDiscounts []*MemberDiscount `json:"member_discounts"`
Taxes []*Tax `json:"taxes,omitempty"`
MemberDiscounts []*MemberDiscount `json:"member_discounts,omitempty"`
PaymentMethods *PaymentMethods `json:"payment_methods,omitempty"`
}

// Tax represents a tax, potentially specific to countries and product types.
Expand Down
1 change: 1 addition & 0 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Configuration struct {
Payment struct {
Stripe struct {
Enabled bool `json:"enabled"`
PublicKey string `json:"public_key" split_words:"true"`
SecretKey string `json:"secret_key" split_words:"true"`
} `json:"stripe"`
PayPal struct {
Expand Down

0 comments on commit 2ca0b10

Please sign in to comment.