Skip to content

Commit

Permalink
Merge pull request #2314 from shadrak98/payment_intent
Browse files Browse the repository at this point in the history
chore: capture payment intent id in webhook log
  • Loading branch information
shadrak98 authored Nov 29, 2024
2 parents 6901ed2 + f99f6ed commit a3face0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 11 additions & 3 deletions press/press/doctype/stripe_webhook_log/stripe_webhook_log.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"customer_id",
"invoice_id",
"stripe_payment_method",
"stripe_payment_intent_id",
"section_break_ecbt",
"payload"
],
Expand All @@ -39,7 +40,8 @@
"fieldname": "invoice",
"fieldtype": "Link",
"label": "Invoice",
"options": "Invoice"
"options": "Invoice",
"search_index": 1
},
{
"fieldname": "invoice_id",
Expand All @@ -50,7 +52,8 @@
"fieldname": "team",
"fieldtype": "Link",
"label": "Team",
"options": "Team"
"options": "Team",
"search_index": 1
},
{
"fieldname": "column_break_hywj",
Expand All @@ -67,10 +70,15 @@
{
"fieldname": "section_break_ecbt",
"fieldtype": "Section Break"
},
{
"fieldname": "stripe_payment_intent_id",
"fieldtype": "Data",
"label": "Stripe Payment Intent ID"
}
],
"links": [],
"modified": "2024-06-12 14:53:14.051698",
"modified": "2024-11-29 10:44:55.011202",
"modified_by": "Administrator",
"module": "Press",
"name": "Stripe Webhook Log",
Expand Down
19 changes: 18 additions & 1 deletion press/press/doctype/stripe_webhook_log/stripe_webhook_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class StripeWebhookLog(Document):
invoice: DF.Link | None
invoice_id: DF.Data | None
payload: DF.Code | None
stripe_payment_intent_id: DF.Data | None
stripe_payment_method: DF.Link | None
team: DF.Link | None
# end: auto-generated types
Expand All @@ -40,6 +41,7 @@ def before_insert(self):
self.event_type = payload.get("type")
customer_id = get_customer_id(payload)
invoice_id = get_invoice_id(payload)
self.stripe_payment_intent_id = get_intent_id(payload)
if customer_id:
self.customer_id = customer_id
self.team = frappe.db.get_value("Team", {"stripe_customer_id": customer_id}, "name")
Expand All @@ -61,7 +63,11 @@ def before_insert(self):
"name",
)

if self.event_type == "invoice.payment_failed" and self.invoice and payload.get("data", {}).get("object", {}).get("next_payment_attempt"):
if (
self.event_type == "invoice.payment_failed"
and self.invoice
and payload.get("data", {}).get("object", {}).get("next_payment_attempt")
):
next_payment_attempt_date = datetime.fromtimestamp(
payload.get("data", {}).get("object", {}).get("next_payment_attempt")
).strftime("%Y-%m-%d")
Expand Down Expand Up @@ -95,6 +101,17 @@ def stripe_webhook_handler():
raise


def get_intent_id(form_dict):
try:
form_dict_str = frappe.as_json(form_dict)
intent_id = re.search(r"pi_\w+", form_dict_str)
if intent_id:
return intent_id.group(0)
return None
except Exception:
frappe.log_error(title="Failed to capture intent id from stripe webhook log")


def get_customer_id(form_dict):
try:
form_dict_str = frappe.as_json(form_dict)
Expand Down

0 comments on commit a3face0

Please sign in to comment.