let the Bazaar verify URL double as the OAuth redirect URI

The Bazaar account owner registered /api/payment/bazaar/verify as the
redirect URI. POST stays the app's purchase check; GET now takes Bazaar's
?code=, exchanges it for a refresh token and stores it in the new
BazaarCredential row, so connecting needs no SSH or .env edit.

The GET is unauthenticated, so it only acts inside a window opened from
the admin (or bazaar_authorize_link): 72 h by default, closed by the first
successful exchange, state checked when Bazaar echoes it. Authorize/token
URL shapes follow the known devapi v2 flow; not checked against Bazaar's
current docs, which did not load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-26 21:30:44 +03:30
parent 5d490edaf9
commit 8763ffd7bf
9 changed files with 414 additions and 9 deletions

View File

@@ -69,3 +69,29 @@ class Transaction(models.Model):
def __str__(self):
return f"txn-{self.pk}-order{self.order_id}"
class BazaarCredential(models.Model):
"""
refresh tokenِ API بازار، که با اولین «اجازه دادن» در پنل بازار ساخته
می‌شود. در دیتابیس است نه `.env` تا گرفتنش کسی را مجبور به SSH نکند:
بازار کاربر را با `?code=` به همان `/api/payment/bazaar/verify`
برمی‌گرداند (Redirect URIای که در پنل ثبت شده) و ویو خودش code را عوض
می‌کند. یک ردیف بیشتر ندارد (pk=1).
`pending_state`/`pending_since` پنجره‌ی اتصال است: GETِ بی‌پنجره هیچ
کاری نمی‌کند، تا کسی نتواند با codeِ حسابِ بازارِ خودش توکنِ ما را
عوض کند. `BAZAAR_REFRESH_TOKEN` در `.env`، اگر باشد، بر این ردیف مقدم است.
"""
refresh_token = models.TextField(blank=True)
authorized_at = models.DateTimeField(null=True, blank=True)
pending_state = models.CharField(max_length=64, blank=True)
pending_since = models.DateTimeField(null=True, blank=True)
class Meta:
verbose_name = 'اتصال به API بازار'
verbose_name_plural = 'اتصال به API بازار'
def __str__(self):
return 'متصل' if self.refresh_token else 'متصل نیست'