Skip to content

Commit

Permalink
Remove deprecated payments through stripe charges (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Nov 4, 2019
1 parent bd36677 commit c853c76
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 92 deletions.
3 changes: 1 addition & 2 deletions api/payments.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -535,8 +535,7 @@ func createPaymentProviders(c *conf.Configuration) (map[string]payments.Provider
provs := map[string]payments.Provider{} provs := map[string]payments.Provider{}
if c.Payment.Stripe.Enabled { if c.Payment.Stripe.Enabled {
p, err := stripe.NewPaymentProvider(stripe.Config{ p, err := stripe.NewPaymentProvider(stripe.Config{
SecretKey: c.Payment.Stripe.SecretKey, SecretKey: c.Payment.Stripe.SecretKey,
UsePaymentIntents: c.Payment.Stripe.UsePaymentIntents,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
Expand Down
44 changes: 0 additions & 44 deletions api/payments_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -439,8 +439,6 @@ func TestPaymentCreate(t *testing.T) {
for name, card := range tests { for name, card := range tests {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
test := NewRouteTest(t) test := NewRouteTest(t)
test.Config.Payment.Stripe.UsePaymentIntents = true

callCount := 0 callCount := 0
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error { stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
switch path { switch path {
Expand Down Expand Up @@ -518,46 +516,6 @@ func TestPaymentCreate(t *testing.T) {
}) })
} }
}) })
t.Run("Charges", func(t *testing.T) {
test := NewRouteTest(t)

callCount := 0
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
switch path {
case "/v1/charges":
payload := params.GetParams()
assert.Equal(t, test.Data.firstOrder.ID, payload.Metadata["order_id"])
assert.Equal(t, "1", payload.Metadata["invoice_number"])
callCount++
return nil
default:
t.Fatalf("unknown Stripe API call to %s", path)
return &stripe.Error{Code: stripe.ErrorCodeURLInvalid}
}
}))
defer stripe.SetBackend(stripe.APIBackend, nil)

test.Data.firstOrder.PaymentState = models.PendingState
rsp := test.DB.Save(test.Data.firstOrder)
require.NoError(t, rsp.Error, "Failed to update order")

params := &stripePaymentParams{
Amount: test.Data.firstOrder.Total,
Currency: test.Data.firstOrder.Currency,
StripeToken: "123456",
Provider: payments.StripeProvider,
}

body, err := json.Marshal(params)
require.NoError(t, err)

recorder := test.TestEndpoint(http.MethodPost, "/orders/first-order/payments", bytes.NewBuffer(body), test.Data.testUserToken)

trans := models.Transaction{}
extractPayload(t, http.StatusOK, recorder, &trans)
assert.Equal(t, models.PaidState, trans.Status)
assert.Equal(t, 1, callCount)
})
}) })
} }


Expand All @@ -576,8 +534,6 @@ func TestPaymentConfirm(t *testing.T) {
for name, testParams := range tests { for name, testParams := range tests {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
test := NewRouteTest(t) test := NewRouteTest(t)
test.Config.Payment.Stripe.UsePaymentIntents = true

callCount := 0 callCount := 0
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error { stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
if path == fmt.Sprintf("/v1/payment_intents/%s/confirm", stripePaymentIntentID) { if path == fmt.Sprintf("/v1/payment_intents/%s/confirm", stripePaymentIntentID) {
Expand Down
2 changes: 0 additions & 2 deletions conf/configuration.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ type Configuration struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
PublicKey string `json:"public_key" split_words:"true"` PublicKey string `json:"public_key" split_words:"true"`
SecretKey string `json:"secret_key" split_words:"true"` SecretKey string `json:"secret_key" split_words:"true"`

UsePaymentIntents bool `json:"use_payment_intents" split_words:"true"`
} `json:"stripe"` } `json:"stripe"`
PayPal struct { PayPal struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
Expand Down
45 changes: 1 addition & 44 deletions payments/stripe/stripe.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (


type stripePaymentProvider struct { type stripePaymentProvider struct {
client *client.API client *client.API

usePaymentIntents bool
} }


type stripeBodyParams struct { type stripeBodyParams struct {
Expand All @@ -28,8 +26,7 @@ type stripeBodyParams struct {


// Config contains the Stripe-specific configuration for payment providers. // Config contains the Stripe-specific configuration for payment providers.
type Config struct { type Config struct {
SecretKey string `mapstructure:"secret_key" json:"secret_key"` SecretKey string `mapstructure:"secret_key" json:"secret_key"`
UsePaymentIntents bool `mapstructure:"use_payment_intents" json:"use_payment_intents"`
} }


// NewPaymentProvider creates a new Stripe payment provider using the provided configuration. // NewPaymentProvider creates a new Stripe payment provider using the provided configuration.
Expand All @@ -40,8 +37,6 @@ func NewPaymentProvider(config Config) (payments.Provider, error) {


s := stripePaymentProvider{ s := stripePaymentProvider{
client: &client.API{}, client: &client.API{},

usePaymentIntents: config.UsePaymentIntents,
} }
s.client.Init(config.SecretKey, nil) s.client.Init(config.SecretKey, nil)
return &s, nil return &s, nil
Expand All @@ -62,16 +57,6 @@ func (s *stripePaymentProvider) NewCharger(ctx context.Context, r *http.Request,
return nil, err return nil, err
} }


if !s.usePaymentIntents {
log.Warning(`Deprecation Warning: Payment Intents are not enabled. Stripe requires those after Sep 14th 2019. Enable by setting "payment.stripe.use_payment_intents" to true`)
if bp.StripeToken == "" {
return nil, errors.New("Stripe requires a stripe_token for creating a payment")
}
return func(amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
return s.chargeDeprecated(bp.StripeToken, amount, currency, order, invoiceNumber)
}, nil
}

if bp.StripePaymentMethodID == "" { if bp.StripePaymentMethodID == "" {
return nil, errors.New("Stripe requires a stripe_payment_method_id for creating a payment intent") return nil, errors.New("Stripe requires a stripe_payment_method_id for creating a payment intent")
} }
Expand All @@ -94,30 +79,6 @@ func prepareShippingAddress(addr models.Address) *stripe.ShippingDetailsParams {
} }
} }


func (s *stripePaymentProvider) chargeDeprecated(token string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
stripeAmount := int64(amount)
stripeDescription := fmt.Sprintf("Invoice No. %d", invoiceNumber)
ch, err := s.client.Charges.New(&stripe.ChargeParams{
Amount: &stripeAmount,
Source: &stripe.SourceParams{Token: &token},
Currency: &currency,
Description: &stripeDescription,
Shipping: prepareShippingAddress(order.ShippingAddress),
Params: stripe.Params{
Metadata: map[string]string{
"order_id": order.ID,
"invoice_number": fmt.Sprintf("%d", invoiceNumber),
},
},
})

if err != nil {
return "", err
}

return ch.ID, nil
}

func (s *stripePaymentProvider) chargePaymentIntent(paymentMethodID string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) { func (s *stripePaymentProvider) chargePaymentIntent(paymentMethodID string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
params := &stripe.PaymentIntentParams{ params := &stripe.PaymentIntentParams{
PaymentMethod: stripe.String(paymentMethodID), PaymentMethod: stripe.String(paymentMethodID),
Expand Down Expand Up @@ -176,10 +137,6 @@ func (s *stripePaymentProvider) NewPreauthorizer(ctx context.Context, r *http.Re
} }


func (s *stripePaymentProvider) NewConfirmer(ctx context.Context, r *http.Request, log logrus.FieldLogger) (payments.Confirmer, error) { func (s *stripePaymentProvider) NewConfirmer(ctx context.Context, r *http.Request, log logrus.FieldLogger) (payments.Confirmer, error) {
if !s.usePaymentIntents {
return nil, errors.New("Cannot confirm a payment if not using payment intents")
}

return s.confirm, nil return s.confirm, nil
} }


Expand Down

0 comments on commit c853c76

Please sign in to comment.