Permalink
Browse files

Add a public /settings endpoint

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 2ca0b1053c05a3993577809246551b60691eda38
Showing with 34 additions and 10 deletions.
  1. +1 −1 api/api.go
  2. +16 −7 api/settings.go
  3. +16 −2 calculator/calculator.go
  4. +1 −0 conf/configuration.go
@@ -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)
})
@@ -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
}
@@ -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.
@@ -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 {

0 comments on commit 2ca0b10

Please sign in to comment.