-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added idempotency + recovery checks in fulfillment #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,86 @@ | ||
| import os | ||
|
|
||
| import requests | ||
| import stripe | ||
| from lib.stripe import get_api_key | ||
| from lib.types import StripeCheckout | ||
|
|
||
| from lib.types import Order, OrderStatus, StripeCheckout | ||
| from lib.db import Database | ||
| from lib.printful import ( | ||
| PRINTFUL_STATUS_MAP, | ||
| PrintfulClient, | ||
| PrintfulItem, | ||
| PrintfulRecipient, | ||
| ) | ||
| from lib.logs import Logs | ||
|
|
||
| db = Database(url=os.getenv("DATABASE_URL")) | ||
| printful = PrintfulClient(api_key=os.getenv("PRINTFUL_API_KEY")) | ||
| log = Logs(log_group=os.environ["LOG_GROUP"]) | ||
| stripe.api_key = get_api_key() | ||
|
|
||
|
|
||
| def process_fulfillment(checkout: StripeCheckout): | ||
| """ | ||
| Fulfills a successful checkout session. | ||
| """ | ||
| # TODO: Make sure fulfillment hasn't already been | ||
| # performed for this checkout. | ||
|
|
||
| # Fetch the items purchased and the receipt link. | ||
| session = stripe.checkout.Session.retrieve( | ||
| checkout.id, | ||
| expand=["line_items"], | ||
| expand=["line_items", "payment_intent.latest_charge"], | ||
| ) | ||
|
|
||
| if session.payment_status == "unpaid": | ||
| return | ||
|
|
||
| # TODO: Perform fulfillment of the line items | ||
| # TODO: Record/save fulfillment status for this | ||
| pass | ||
| log.info(session.line_items.data) | ||
| order = Order( | ||
| email=session.customer_details.email, | ||
| stripe_id=checkout.id, | ||
| receipt_url=session.payment_intent.latest_charge.receipt_url, | ||
| price=session.amount_total, | ||
| items=[ | ||
| # TODO: Think about the items a little more. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can use a raw, hard-coded lookup if we correlate a Stripe product ID with our internal list?—there are only five pieces
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that should work. |
||
| ], | ||
| ) | ||
|
|
||
| order, created = db.create_order(order) | ||
| if not created and order.printful_id is not None: | ||
| # Order was already fully fulfilled on a previous attempt. | ||
| return | ||
|
|
||
| shipping = session.shipping_details | ||
| recipient = PrintfulRecipient( | ||
| name=shipping.name, | ||
| address1=shipping.address.line1, | ||
| address2=shipping.address.line2, | ||
| city=shipping.address.city, | ||
| state_code=shipping.address.state, | ||
| country_code=shipping.address.country, | ||
| zip=shipping.address.postal_code, | ||
| email=session.customer_details.email, | ||
| ) | ||
| items = [ | ||
| PrintfulItem(product_id=int(item.product_id), quantity=item.quantity) | ||
| for item in order.items | ||
| ] | ||
|
|
||
| # Perform a lookup so we avoid creating duplicate errors if we already processed | ||
| # this in a previous call. | ||
| try: | ||
| result = printful.get_order_by_external_id(order.id) | ||
| except requests.HTTPError as e: | ||
| if e.response.status_code == 404: | ||
| result = printful.create_order( | ||
| recipient=recipient, | ||
| items=items, | ||
| external_id=checkout.id, | ||
| ) | ||
| else: | ||
| raise e | ||
|
|
||
| printful_order = result["result"] | ||
| db.update_order( | ||
| order.id, | ||
| printful_id=str(printful_order["id"]), | ||
| status=PRINTFUL_STATUS_MAP.get(printful_order["status"], OrderStatus.pending), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.