Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform): Implement top-up flow for PAYG System #9050

Draft
wants to merge 20 commits into
base: dev
Choose a base branch
from

Conversation

kcze
Copy link
Contributor

@kcze kcze commented Dec 18, 2024

WIP

Changes 🏗️

Checklist 📋

For code changes:

  • I have clearly listed my changes in the PR description
  • I have made a test plan
  • I have tested my changes according to the test plan:
    • ...
Example test plan
  • Create from scratch and execute an agent with at least 3 blocks
  • Import an agent from file upload, and confirm it executes correctly
  • Upload agent to marketplace
  • Import an agent from marketplace and confirm it executes correctly
  • Edit an agent from monitor, and confirm it executes correctly

For configuration changes:

  • .env.example is updated or already compatible with my changes
  • docker-compose.yml is updated or already compatible with my changes
  • I have included a list of my configuration changes in the PR description (under Changes)
Examples of configuration changes
  • Changing ports
  • Adding new services that need to communicate with each other
  • Secrets or environment variable changes
  • New or infrastructure changes such as databases

@github-actions github-actions bot added platform/frontend AutoGPT Platform - Front end platform/backend AutoGPT Platform - Back end size/l labels Dec 18, 2024
Copy link

netlify bot commented Dec 18, 2024

Deploy Preview for auto-gpt-docs-dev canceled.

Name Link
🔨 Latest commit 7958143
🔍 Latest deploy log https://app.netlify.com/sites/auto-gpt-docs-dev/deploys/677801924a58160008b8f052

Copy link

netlify bot commented Dec 18, 2024

Deploy Preview for auto-gpt-docs canceled.

Name Link
🔨 Latest commit 7958143
🔍 Latest deploy log https://app.netlify.com/sites/auto-gpt-docs/deploys/67780192bc5f9e00086fe6d7

Comment on lines +241 to +244
await User.prisma().update(
where={"id": user_id}, data={"stripeCustomerId": customer.id}
)
user.stripeCustomerId = customer.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await User.prisma().update(
where={"id": user_id}, data={"stripeCustomerId": customer.id}
)
user.stripeCustomerId = customer.id
user = await User.prisma().update(
where={"id": user_id}, data={"stripeCustomerId": customer.id}
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even better: avoid calling Prisma here and add set_stripe_customer_id in model class on user.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left the original code - trying to change it or move to the function cascades into multiple errors and requires additional checks.

}
],
mode="payment",
success_url="",# TODO kcze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these will be webhook URLs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are redirect urls after checkout


if (!stripe) {
console.error("Stripe failed to load");
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw an error instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed console.error altogether, this should never happen; stripePromise should never be null and loadStripe throws when we don't provide API key.

@kcze kcze changed the title feat(backend): Implement top-up flow for PAYG System feat(platform): Implement top-up flow for PAYG System Dec 27, 2024
@github-actions github-actions bot added the conflicts Automatically applied to PRs with merge conflicts label Dec 28, 2024
Copy link
Contributor

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.


# Check the Checkout Session's payment_status property
# to determine if fulfillment should be peformed
if checkout_session.payment_status != "unpaid":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this riskier than just match 'paid' & 'no_payment_required' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not now, there are only three options and this is from Stripe example but just to be sure I updated it anyway!

return {"checkout_url": checkout_url}


@v1_router.post(path="/credits/stripe_webhook", tags=["credits"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, Is this a non-authenticated request ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess no but it requires HTTPS on live data and API key to read correctly.

# Invalid signature
raise HTTPException(status_code=400)

print(event)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: make this logging ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

event['type'] == 'checkout.session.completed'
or event['type'] == 'checkout.session.async_payment_succeeded'
):
await _user_credit_model.fulfill_checkout(event['data']['object']['id'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nested check: uses get() with fallback for safety?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we log/warn the non matching events ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nested check: uses get() with fallback for safety?

What do you mean? I don't think using event.get(..) would change anything.

Also can we log/warn the non matching events ?

Do you think this is necessary on our side? Stripe logs everything and each event has tons of data and only 4 out of hundreds are enabled for this webhook. I could disabled 2, so only the 2 matching will be sent.

# Check the Checkout Session's payment_status property
# to determine if fulfillment should be peformed
if checkout_session.payment_status != "unpaid":
print("Payment status is not unpaid!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: make this and the one above logging with more information ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, that was only for testing.

const response = await api.requestTopUp(amount);

router.push(response.checkout_url);
}, [api]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won' this warn about router not part of the deplist ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added router

@@ -442,7 +446,7 @@ export default class BackendAPI {
await new Promise((resolve) => setTimeout(resolve, 100 * retryCount));
}
}
console.log("Request: ", method, path, "from: ", page);
console.log("Request: ", method, this.baseUrl, path, "from: ", page);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: console.debug ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed but this needs a refactor imo

Copy link
Contributor

github-actions bot commented Jan 3, 2025

Conflicts have been resolved! 🎉 A maintainer will review the pull request shortly.

@github-actions github-actions bot removed the conflicts Automatically applied to PRs with merge conflicts label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform/backend AutoGPT Platform - Back end platform/frontend AutoGPT Platform - Front end size/l
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants