Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ HOSTED_PUSHER_API_URL=

TYPESENSE_HOST=
TYPESENSE_HOST_PORT=
TYPESENSE_API_KEY=
TYPESENSE_API_KEY=

STRIPE_API_KEY=
STRIPE_WEBHOOK_SECRET=
STRIPE_CONNECT_WEBHOOK_SECRET=
19 changes: 16 additions & 3 deletions backend/routers/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ async def stripe_webhook(request: Request, stripe_signature: str = Header(None))
# paid
paid_app(app_id, uid)

return {"status": "success"}


@router.post('/v1/stripe/connect/webhook', tags=['v1', 'stripe', 'webhook'])
async def stripe_webhook(request: Request, stripe_signature: str = Header(None)):
payload = await request.body()

try:
event = stripe_utils.parse_connect_event(payload, stripe_signature)
except ValueError as e:
raise HTTPException(status_code=400, detail="Invalid payload")
except stripe.error.SignatureVerificationError as e:
raise HTTPException(status_code=400, detail="Invalid signature")

if event['type'] == 'account.updated':
# this event occurs for the connected account, check if the account is fully onboarded to set default method
account = event['data']['object']
Expand All @@ -50,9 +64,8 @@ async def stripe_webhook(request: Request, stripe_signature: str = Header(None))
set_default_payment_method(uid, 'stripe')

# TODO: handle this event to link transfers?
if event['type'] == 'transfer.created':
transfer = event['data']['object']
print(transfer)
# if event['type'] == 'transfer.created':
# transfer = event['data']['object']

return {"status": "success"}

Expand Down
9 changes: 8 additions & 1 deletion backend/utils/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

stripe.api_key = os.getenv('STRIPE_API_KEY')
endpoint_secret = os.getenv('STRIPE_WEBHOOK_SECRET')
connect_secret = os.getenv('STRIPE_CONNECT_WEBHOOK_SECRET')


def create_product(name: str, description: str, image: str):
Expand Down Expand Up @@ -58,6 +59,13 @@ def parse_event(payload, sig_header):
)


def parse_connect_event(payload, sig_header):
"""Parse the Stripe Connect event."""
return stripe.Webhook.construct_event(
payload, sig_header, connect_secret
)


def create_connect_account(base_url: str, uid: str):
account = stripe.Account.create(
controller={
Expand Down Expand Up @@ -111,7 +119,6 @@ def refresh_connect_account_link(account_id: str, base_url: str):
}



def is_onboarding_complete(account_id: str):
account = stripe.Account.retrieve(account_id)
return account.charges_enabled and account.payouts_enabled and account.details_submitted