Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:/sweeftdigital/accounts_G2 into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sandronadiradze committed Nov 4, 2024
2 parents f43a701 + 207fc96 commit 8ac131d
Show file tree
Hide file tree
Showing 10 changed files with 789 additions and 24 deletions.
3 changes: 3 additions & 0 deletions accounts/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,6 @@
stripe.api_key = os.getenv("STRIPE_SECRET_KEY")
STRIPE_PUBLISHABLE_KEY = os.getenv("STRIPE_PUBLISHABLE_KEY")
STRIPE_WEBHOOK_SECRET = os.getenv("STRIPE_WEBHOOK_SECRET")
STRIPE_WEBHOOK_CONNECT_SECRET = os.getenv("STRIPE_WEBHOOK_CONNECT_SECRET")
STRIPE_WEBHOOK_PAYOUT_SECRET = os.getenv("STRIPE_WEBHOOK_PAYOUT_SECRET")
STRIPE_WEBHOOK_TRANSFER_SECRET = os.getenv("STRIPE_WEBHOOK_TRANSFER_SECRET")
2 changes: 1 addition & 1 deletion apis/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class CustomUserAdmin(UserAdmin):
"zip_code",
"address_one",
"address_two",
"stripe_account_id",
]
},
),
Expand Down Expand Up @@ -173,7 +174,6 @@ class BalanceAdmin(admin.ModelAdmin):
model = Balance
list_display = ("user", "amount")
search_fields = ("user__email",)
readonly_fields = ("amount",)

actions = ["add_to_balance", "subtract_from_balance"]

Expand Down
18 changes: 18 additions & 0 deletions apis/migrations/0019_customuser_stripe_account_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-11-02 11:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("apis", "0018_customuser_two_factor_authentication_method"),
]

operations = [
migrations.AddField(
model_name="customuser",
name="stripe_account_id",
field=models.CharField(blank=True, max_length=255, null=True),
),
]
1 change: 1 addition & 0 deletions apis/models/custom_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class TwoFactorAuthenticationChoices(models.TextChoices):
choices=TutorialCompleteChoices.choices,
default=TutorialCompleteChoices.PENDING,
)
stripe_account_id = models.CharField(max_length=255, blank=True, null=True)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
Expand Down
30 changes: 30 additions & 0 deletions payments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from rest_framework import serializers

from apis.models import CustomUser
from payments.models import Payment


Expand Down Expand Up @@ -29,3 +30,32 @@ class DepositSerializer(serializers.Serializer):
amount = serializers.DecimalField(
min_value=0.01, max_digits=20, decimal_places=2, help_text="Amount to deposit."
)


class TransferFundsSerializer(serializers.Serializer):
seller_id = serializers.UUIDField()
amount = serializers.DecimalField(max_digits=10, decimal_places=2)

def validate_amount(self, value):
# Ensure the amount is positive
if value <= Decimal("0.00"):
raise serializers.ValidationError("Amount must be positive.")
return value

def validate_seller_id(self, value):
# Ensure the seller exists and is a seller user
seller = CustomUser.objects.filter(
id=value, user_type=CustomUser.UserType.SELLER
).first()
if not seller:
raise serializers.ValidationError("Invalid seller ID.")
return seller # Pass the seller instance forward for easier access in the view


class PayoutRequestSerializer(serializers.Serializer):
amount = serializers.DecimalField(max_digits=10, decimal_places=2)

def validate_amount(self, value):
if value <= Decimal("0.00"):
raise serializers.ValidationError("Amount must be greater than zero.")
return value
22 changes: 21 additions & 1 deletion payments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

from .views import (
BalanceView,
ConnectStripeAccountView,
CreateCheckoutSession,
DepositBalanceView,
SellerPayoutView,
TransferFundsView,
stripe_account_refresh,
stripe_account_return,
stripe_connect_webhook,
stripe_payout_webhook,
stripe_transfer_webhook,
stripe_webhook,
)

Expand All @@ -13,7 +21,19 @@
CreateCheckoutSession.as_view(),
name="create-checkout-session",
),
path("webhook/", stripe_webhook, name="stripe-webhook"),
path("webhook/", stripe_webhook, name="deposit_webhook"),
path("balance/<uuid:user_id>/", BalanceView.as_view(), name="balance-view"),
path("deposit/", DepositBalanceView.as_view(), name="deposit-balance"),
path(
"connect-stripe-account/",
ConnectStripeAccountView.as_view(),
name="connect_stripe_account",
),
path("stripe-return/", stripe_account_return, name="stripe_account_return"),
path("stripe-refresh/", stripe_account_refresh, name="stripe_account_refresh"),
path("connect/webhook/", stripe_connect_webhook, name="connect_webhook"),
path("transfer/", TransferFundsView.as_view(), name="transfer-funds"),
path("payout/", SellerPayoutView.as_view(), name="seller_payout"),
path("transfer/webhook/", stripe_transfer_webhook, name="transfer_webhook"),
path("payout/webhook/", stripe_payout_webhook, name="payout_webhook"),
]
Empty file added payments/utils/__init__.py
Empty file.
118 changes: 118 additions & 0 deletions payments/utils/connect_to_stripe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# import stripe
# import pycountry
# from django.shortcuts import get_object_or_404
#
# from django.urls import reverse
#
# from apis.models import Company, Individual
#
#
# def get_country_code(country_name):
# """Convert country name to ISO 3166-1 alpha-2 code"""
# try:
# # Try direct lookup first
# country = pycountry.countries.get(name=country_name)
#
# if country:
# return country.alpha_2
#
# # If direct lookup fails, try searching
# countries = pycountry.countries.search_fuzzy(country_name)
# if countries:
# return countries[0].alpha_2
#
# except (AttributeError, LookupError):
# return "US" # Default fallback
#
# return "US"
#
#
# def create_stripe_connect_account(user, request):
# country_code = get_country_code(user.country)
#
# print(country_code)
# print("* *" * 50)
#
# # Get the appropriate profile based on user type
# if user.user_profile_type == "Individual":
# profile = get_object_or_404(Individual, user=user)
#
# # Create Stripe Connect account for Individual
# account = stripe.Account.create(
# type="express",
# country=country_code,
# email=user.email,
# business_type="individual",
# individual={
# "first_name": profile.first_name,
# "last_name": profile.last_name,
# "email": user.email,
# "phone": str(user.phone_number) if user.phone_number else None,
# "dob": {
# "day": profile.date_of_birth.day if profile.date_of_birth else None,
# "month": profile.date_of_birth.month if profile.date_of_birth else None,
# "year": profile.date_of_birth.year if profile.date_of_birth else None,
# } if profile.date_of_birth else None,
# "address": {
# "line1": user.address_one,
# "line2": user.address_two,
# "city": user.city,
# "state": user.state,
# "postal_code": user.zip_code,
# "country": country_code,
# }
# },
# capabilities = {
# # Only request the capabilities you actually need
# "transfers": {"requested": True}, # Basic transfer capability
# # Remove other capabilities like:
# # "card_payments": {"requested": True},
# # "tax_reporting_us_1099_k": {"requested": True},
# },
# settings = {
# 'payouts': {
# 'schedule': {
# 'interval': 'manual' # Simplifies payout setup
# }
# }
# }
#
# )
# else: # Company
# profile = get_object_or_404(Company, user=user)
#
# # Create Stripe Connect account for Company
# account = stripe.Account.create(
# type="express",
# country=country_code,
# email=user.email,
# business_type="company",
# company={
# "name": profile.company_name,
# "tax_id": profile.company_code, # If company_code is a tax ID
# "phone": str(user.phone_number) if user.phone_number else None,
# },
# business_profile={
# "name": profile.company_name,
# "support_email": profile.contact_person or user.email,
# "support_phone": str(user.phone_number) if user.phone_number else None,
# "url": None, # Add website URL if you have it in your model
# },
# )
#
# user.stripe_account_id = account.id
# user.save()
#
# # Generate onboarding link
# return_url = request.build_absolute_uri(reverse("stripe_account_return"))
# refresh_url = request.build_absolute_uri(reverse("stripe_account_refresh"))
#
# account_link = stripe.AccountLink.create(
# account=account.id,
# refresh_url=refresh_url,
# return_url=return_url,
# type="account_onboarding",
# collect="eventually_due",
# )
#
# return account_link.url
Loading

0 comments on commit 8ac131d

Please sign in to comment.