This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class StripeAccount(ProviderAccount):
def to_str(self):
default = super(StripeAccount, self).to_str()
return self.account.extra_data.get("business_name", default)
class StripeProvider(OAuth2Provider):
id = "stripe"
name = "Stripe"
account_class = StripeAccount
def extract_uid(self, data):
return data["id"]
def extract_common_fields(self, data):
return dict(name=data.get("display_name"), email=data.get("email"))
def get_default_scope(self):
return ["read_only"]
provider_classes = [StripeProvider]

View File

@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import StripeProvider
class StripeTests(OAuth2TestsMixin, TestCase):
provider_id = StripeProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""{
"id": "acct_sometestid",
"object": "account",
"business_logo": null,
"business_name": null,
"business_url": "example.com",
"charges_enabled": true,
"country": "SE",
"currencies_supported": [
"usd",
"eur",
"sek"
],
"default_currency": "eur",
"details_submitted": true,
"display_name": "Test",
"email": "test@example.com",
"managed": false,
"metadata": {},
"statement_descriptor": "TEST.COM",
"support_phone": "+460123456789",
"timezone": "Europe/Stockholm",
"transfers_enabled": true
}""",
)
def get_login_response_json(self, with_refresh_token=True):
rt = ""
if with_refresh_token:
rt = ',"refresh_token": "testrf"'
return (
"""{
"uid":"weibo",
"access_token":"testac",
"livemode": false,
"token_type": "bearer",
"stripe_publishable_key": "pk_test_someteskey",
"stripe_user_id": "acct_sometestid",
"scope": "read_write"
%s }"""
% rt
)

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import StripeProvider
urlpatterns = default_urlpatterns(StripeProvider)

View File

@@ -0,0 +1,28 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import StripeProvider
class StripeOAuth2Adapter(OAuth2Adapter):
provider_id = StripeProvider.id
access_token_url = "https://connect.stripe.com/oauth/token"
authorize_url = "https://connect.stripe.com/oauth/authorize"
profile_url = "https://api.stripe.com/v1/accounts/%s"
def complete_login(self, request, app, token, response, **kwargs):
headers = {"Authorization": "Bearer {0}".format(token.token)}
resp = requests.get(
self.profile_url % response.get("stripe_user_id"), headers=headers
)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(StripeOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(StripeOAuth2Adapter)