|
| 1 | +package handlers |
| 2 | + |
| 3 | +// billing_notes_unmarshal_test.go — regression for the Razorpay `notes` |
| 4 | +// polymorphism bug found by the live failure-path test (2026-06-07): Razorpay |
| 5 | +// sends `notes` as an OBJECT when populated but an empty ARRAY ([]) when absent. |
| 6 | +// The old `Notes map[string]string` failed to unmarshal the [] form, so every |
| 7 | +// payment.failed webhook with no notes hit |
| 8 | +// "cannot unmarshal array into Go struct field …notes of type map[string]string" |
| 9 | +// and the immediate payment-failure handling was skipped. rzpNotes tolerates |
| 10 | +// object / array / null. |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/json" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRzpNotes_ToleratesArrayObjectAndNull(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + // payment.failed with no notes → Razorpay sends notes:[] (the bug trigger). |
| 21 | + t.Run("payment entity notes:[] parses to empty map", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + var p rzpPaymentEntity |
| 24 | + err := json.Unmarshal([]byte(`{"id":"pay_x","subscription_id":"sub_x","notes":[]}`), &p) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("notes:[] must not error, got %v", err) |
| 27 | + } |
| 28 | + if p.Notes == nil || len(p.Notes) != 0 { |
| 29 | + t.Fatalf("notes:[] must decode to an empty map, got %#v", p.Notes) |
| 30 | + } |
| 31 | + if p.SubscriptionID != "sub_x" { |
| 32 | + t.Fatalf("other fields must still decode; got sub=%q", p.SubscriptionID) |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + // subscription with team_id notes (the happy path that must keep working). |
| 37 | + t.Run("subscription entity notes:{team_id} decodes", func(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + var s rzpSubscriptionEntity |
| 40 | + err := json.Unmarshal([]byte(`{"id":"sub_y","plan_id":"plan_y","notes":{"team_id":"abc"}}`), &s) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("object notes must decode, got %v", err) |
| 43 | + } |
| 44 | + if s.Notes["team_id"] != "abc" { |
| 45 | + t.Fatalf("team_id must round-trip; got %#v", s.Notes) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + // A malformed object (non-string value) must surface the decode error rather |
| 50 | + // than silently swallow it — covers the json.Unmarshal error branch. |
| 51 | + t.Run("object with non-string value errors", func(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + var p rzpPaymentEntity |
| 54 | + err := json.Unmarshal([]byte(`{"id":"p","notes":{"k":123}}`), &p) |
| 55 | + if err == nil { |
| 56 | + t.Fatal("a notes object with a non-string value must return a decode error, not be swallowed") |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + // null and empty-array on the subscription entity → empty map, no error. |
| 61 | + for _, tc := range []struct{ name, body string }{ |
| 62 | + {"null", `{"id":"s","notes":null}`}, |
| 63 | + {"empty array", `{"id":"s","notes":[]}`}, |
| 64 | + } { |
| 65 | + tc := tc |
| 66 | + t.Run("subscription notes "+tc.name, func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + var s rzpSubscriptionEntity |
| 69 | + if err := json.Unmarshal([]byte(tc.body), &s); err != nil { |
| 70 | + t.Fatalf("%s notes must not error, got %v", tc.name, err) |
| 71 | + } |
| 72 | + if s.Notes == nil { |
| 73 | + t.Fatalf("%s notes must be a non-nil empty map", tc.name) |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments