File size: 3,243 Bytes
67fe275
9a274ef
67fe275
6647a73
9a274ef
6647a73
86da52f
67fe275
 
e2d80d5
67fe275
 
7ad2495
67fe275
55d075c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a274ef
67fe275
 
 
e2d80d5
67fe275
7ad2495
e2d80d5
 
9ebc709
e2d80d5
16b9fb9
e2d80d5
 
7ad2495
f97ef52
9d32818
 
85f7624
67fe275
 
 
 
 
6117b36
 
 
 
15a58fb
 
 
 
 
6117b36
 
 
 
4488955
67fe275
 
 
9a274ef
67fe275
 
 
 
f3d4dfe
 
67fe275
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import stripe
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel

router = APIRouter()

stripe.api_key = "sk_test_51N6K5JB9VMe0qzbOjlJvMEsfdQyrFgV49vRaeErtmhrzHV3Cu3f5jMDJmrhKdI5uqvpHubjkmwDQgMOtCEmz19t800AouH7W6g"
stripe.api_version = "2023-10-16"

### **1️⃣ CREATE CONNECTED ACCOUNT** ###
class AccountRequest(BaseModel):
    email: str
    name: str  # Name of the individual account owner

class CreateCustomerRequest(BaseModel):
    email: str
    phone: str  # Tel do usuário
    name: str   # Nome do usuário

@router.post("/create_customer")
def create_customer(data: CreateCustomerRequest):
    try:
        # Criar o cliente no Stripe com os dados fornecidos
        customer = stripe.Customer.create(
            email=data.email,
            phone=data.phone,
            name=data.name
        )

        # Retornar o ID do cliente (customer_id) criado
        return {"customer_id": customer.id}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error creating customer: {str(e)}")
        
@router.post("/create_account")
def create_account(account_data: AccountRequest):
    try:
        account = stripe.Account.create(
            type="express",  # Express account type for individual accounts
            email=account_data.email,
            country="BR",  # Brazil
            business_type="individual",  # Ensuring the account is individual, not business
            business_profile={
                "mcc": "7298",
                "name": account_data.name,  # Using the name from the request body
                "product_description": "We connect stylists with clients through subscription services.",
                "support_email": "[email protected]"
            },
            default_currency="brl",  # Set currency to BRL (Brazilian Real)
            capabilities={
                "transfers": {"requested": True},
                "card_payments": {"requested": True}
            }
        )
        return {"account_id": account.id}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/check_onboarding/{account_id}")
def check_onboarding(account_id: str):
    try:
        account = stripe.Account.retrieve(account_id)
        
        if account.requirements.currently_due:
            return {"status": "incomplete", "pending_requirements": account.requirements.currently_due}
        
        return {"status": "complete"}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

### **2️⃣ CREATE ACCOUNT LINK FOR ONBOARDING** ###
class AccountLinkRequest(BaseModel):
    account_id: str

@router.post("/account_link")
def create_account_link(link_data: AccountLinkRequest):
    try:
        account_link = stripe.AccountLink.create(
            account=link_data.account_id,
            return_url="https://ameddes.com/onboarding?success=true",  # Updated return URL
            refresh_url="https://ameddes.com/onboarding?success=false",  # Updated refresh URL
            type="account_onboarding",
        )
        return {"url": account_link.url}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))