0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 18:01:48 +08:00
discourse/plugins/discourse-patreon/spec/fixtures/v1/pledge.json
Rafael dos Santos Silva 8c0a4bb8f9
DEV: Upgrade Patreon plugin to API v2 with v1 backward compatibility (#38871)
## Summary

Patreon API v1 has been deprecated for years and the Patreon team has
requested we migrate to v2 (see
https://meta.discourse.org/t/upgrade-patreon-discourse-plugin-to-api-v2/386701).

Since hundreds of existing customers have v1 OAuth clients, this PR
supports **both versions simultaneously** via an adapter pattern,
controlled by a new `patreon_api_version` site setting (defaults to
`"1"` so no one breaks on upgrade).

### What changed

- **Adapter pattern**: `Patreon::ApiVersion::V1` and `ApiVersion::V2`
modules with identical interfaces for endpoints, response parsing, and
OAuth config. `ApiVersion.current` routes based on the site setting.
- **v2 API support**: campaigns endpoint returns tiers (not rewards),
members fetched separately with cursor-based pagination, v2 identity
endpoint with explicit field selection, v2 OAuth scopes
- **Webhooks accept both formats**: payload version detected from
`data.type` (`"pledge"` → v1, `"member"` → v2), regardless of the site
setting
- **Admin deprecation notice**: `ProblemCheck::PatreonApiV1Deprecated`
warns on the dashboard when still using v1
- **Shared logic unchanged**: rate limiting, PluginStore, group sync,
admin UI all version-agnostic
- Also fixes pre-existing flaky seed/campaign spec failures

### Migration path for existing users

1. Admin sees deprecation warning on dashboard
2. Create a new v2 OAuth client at
https://www.patreon.com/portal/registration/register-clients
3. Update credentials in site settings
4. Switch `patreon_api_version` to `"2"`
5. Creator re-authenticates via Patreon login to get v2-scoped tokens
6. Trigger a manual data sync from the Patreon admin panel

### Future v1 removal

When ready to drop v1: delete `lib/api_version/v1.rb`, the problem
check, the site setting, v1 fixtures, and v1 test contexts — one clean
cut.

## Test plan

- [x] All 41 plugin specs pass (both v1 and v2 contexts)
- [x] Lint passes on all changed files
- [x] Zeitwerk reload passes (`bin/rails runner
"Rails.application.reloader.reload!"`)
- [x] Manual testing with a real Patreon v2 OAuth client
2026-03-30 14:43:34 -03:00

107 lines
2.5 KiB
JSON
Vendored

{
"data": {
"attributes": {
"amount_cents": 250,
"created_at": "2015-05-18T23:50:42+00:00",
"declined_since": null,
"patron_pays_fees": false,
"pledge_cap_cents": null
},
"id": "1",
"relationships": {
"address": {
"data": null
},
"card": {
"data": null
},
"creator": {
"data": {
"id": "3024102",
"type": "user"
},
"links": {
"related": "https://www.patreon.com/api/user/3024102"
}
},
"patron": {
"data": {
"id": "32187",
"type": "user"
},
"links": {
"related": "https://www.patreon.com/api/user/32187"
}
},
"reward": {
"data": {
"id": "999999",
"type": "reward"
},
"links": {
"related": "https://www.patreon.com/api/rewards/599336"
}
}
},
"type": "pledge"
},
"included": [{
"attributes": {
"about": "sample text about",
"created": "2015-01-15T07:25:51+00:00",
"email": "roo@aar.com",
"facebook": null,
"first_name": "Roo",
"full_name": "Roo Aar",
"gender": 1,
"image_url": "",
"is_email_verified": true,
"last_name": "Aar",
"social_connections": {
"deviantart": null,
"discord": null,
"facebook": null,
"spotify": null,
"twitch": null,
"twitter": null,
"youtube": null
},
"thumb_url": "",
"twitch": null,
"twitter": "roo",
"url": "https://www.patreon.com/foo",
"vanity": "roo",
"youtube": null
},
"id": "32187",
"type": "user"
},
{
"attributes": {
"amount": 1,
"amount_cents": 1,
"created_at": null,
"description": "Patrons Only",
"id": "999999",
"remaining": 0,
"requires_shipping": false,
"type": "reward",
"url": null,
"user_limit": null
},
"id": "999999",
"relationships": {
"creator": {
"data": {
"id": "1111111",
"type": "user"
},
"links": {
"related": "https://www.patreon.com/api/user/1111111"
}
}
},
"type": "reward"
}
]
}