Skip to content

Commit 1b22a77

Browse files
committed
stripe integration working
1 parent 3ec7ed2 commit 1b22a77

4 files changed

Lines changed: 389 additions & 316 deletions

File tree

backend/apps/stripe_home/tests/test_integration.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def tearDown(self):
188188
logger.warning(f"Error archiving product: {e}")
189189

190190
# Clean up Django database records
191-
if hasattr(self, 'customer'):
191+
if hasattr(self, 'customer') and self.customer and self.customer.pk is not None:
192192
self.customer.delete()
193193
if hasattr(self, 'plan'):
194194
self.plan.delete()
@@ -489,7 +489,7 @@ def tearDown(self):
489489
logger.warning(f"Error archiving product: {e}")
490490

491491
# Clean up Django database records
492-
if hasattr(self, 'customer'):
492+
if hasattr(self, 'customer') and self.customer and self.customer.pk is not None:
493493
self.customer.delete()
494494

495495
# Clear cache
@@ -607,33 +607,58 @@ def test_missing_customer_in_subscription(self):
607607
# Delete the customer from our database (but not from Stripe)
608608
self.customer.delete()
609609

610-
# Create webhook payload
610+
# Create webhook payload that doesn't contain the full subscription object
611+
# to avoid potential serialization issues
611612
payload = json.dumps({
612613
"id": "evt_test",
613614
"object": "event",
614615
"type": "customer.subscription.updated",
615616
"data": {
616-
"object": subscription
617+
"object": {
618+
"id": subscription.id,
619+
"object": "subscription",
620+
"customer": self.stripe_customer.id,
621+
"items": {
622+
"data": [
623+
{"price": {"id": self.stripe_price.id}}
624+
]
625+
}
626+
}
617627
}
618628
})
619629

620-
# Create valid headers with timestamp and placeholder signature
621-
# (we'll bypass signature verification in the view)
622-
headers = {
623-
'HTTP_STRIPE_SIGNATURE': f't={int(datetime.now().timestamp())},v1=placeholder'
624-
}
630+
# For testing, we need to monkey patch the construct_event function to avoid signature verification
631+
original_construct_event = stripe.Webhook.construct_event
625632

626-
# Make request to webhook endpoint
627-
url = reverse('stripe:webhook')
628-
response = self.client.post(
629-
url,
630-
payload,
631-
content_type='application/json',
632-
**headers
633-
)
633+
def mock_construct_event(payload, sig_header, secret):
634+
return stripe.Event.construct_from(
635+
json.loads(payload),
636+
stripe.api_key
637+
)
634638

635-
# Response should indicate customer not found, but not crash
636-
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
639+
# Patch the construct_event method
640+
stripe.Webhook.construct_event = mock_construct_event
641+
642+
try:
643+
# Create headers with any value since we're bypassing verification
644+
headers = {
645+
'HTTP_STRIPE_SIGNATURE': 'bypass_verification'
646+
}
647+
648+
# Make request to webhook endpoint
649+
url = reverse('stripe:webhook')
650+
response = self.client.post(
651+
url,
652+
payload,
653+
content_type='application/json',
654+
**headers
655+
)
656+
657+
# Response should indicate customer not found, but not crash
658+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
659+
finally:
660+
# Restore the original method
661+
stripe.Webhook.construct_event = original_construct_event
637662

638663
# Clean up - delete subscription
639664
stripe.Subscription.delete(subscription.id)

0 commit comments

Comments
 (0)