92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStripeProviderFlow(t *testing.T) {
|
|
var seen []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = r.ParseForm()
|
|
seen = append(seen, r.Method+" "+r.URL.Path)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch {
|
|
case r.URL.Path == "/v1/payment_intents":
|
|
if r.Form.Get("capture_method") != "manual" || r.Form.Get("confirm") != "true" {
|
|
t.Errorf("authorize missing manual-capture/confirm: %v", r.Form)
|
|
}
|
|
if r.Form.Get("amount") != "25000" || r.Form.Get("currency") != "sek" {
|
|
t.Errorf("authorize bad amount/currency: %v", r.Form)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": "pi_123", "status": "requires_capture"})
|
|
case strings.HasSuffix(r.URL.Path, "/capture"):
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": "pi_123", "status": "succeeded"})
|
|
case r.URL.Path == "/v1/refunds":
|
|
if r.Form.Get("payment_intent") != "pi_123" {
|
|
t.Errorf("refund missing payment_intent: %v", r.Form)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": "re_1"})
|
|
case strings.HasSuffix(r.URL.Path, "/cancel"):
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"id": "pi_123", "status": "canceled"})
|
|
default:
|
|
http.Error(w, "unexpected", http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewStripeProvider("sk_test_x", srv.URL, "pm_card_visa")
|
|
ctx := context.Background()
|
|
|
|
auth, err := p.Authorize(ctx, "ord-1", 25000, "SEK")
|
|
if err != nil {
|
|
t.Fatalf("authorize: %v", err)
|
|
}
|
|
if auth.Reference != "pi_123" || auth.Provider != "stripe" {
|
|
t.Fatalf("unexpected authorization %+v", auth)
|
|
}
|
|
if _, err := p.Capture(ctx, auth.Reference, 25000); err != nil {
|
|
t.Fatalf("capture: %v", err)
|
|
}
|
|
ref, err := p.Refund(ctx, auth.Reference, 1000)
|
|
if err != nil {
|
|
t.Fatalf("refund: %v", err)
|
|
}
|
|
if ref.Reference != "re_1" {
|
|
t.Fatalf("refund ref = %q", ref.Reference)
|
|
}
|
|
if err := p.Void(ctx, auth.Reference); err != nil {
|
|
t.Fatalf("void: %v", err)
|
|
}
|
|
|
|
want := []string{
|
|
"POST /v1/payment_intents",
|
|
"POST /v1/payment_intents/pi_123/capture",
|
|
"POST /v1/refunds",
|
|
"POST /v1/payment_intents/pi_123/cancel",
|
|
}
|
|
if strings.Join(seen, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("endpoints called = %v, want %v", seen, want)
|
|
}
|
|
}
|
|
|
|
func TestStripeErrorSurfaced(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"error": map[string]any{"message": "Your card was declined."},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewStripeProvider("sk_test_x", srv.URL, "pm_card_chargeDeclined")
|
|
_, err := p.Authorize(context.Background(), "ord-2", 100, "SEK")
|
|
if err == nil || !strings.Contains(err.Error(), "declined") {
|
|
t.Fatalf("expected declined error, got %v", err)
|
|
}
|
|
}
|