corrected stripe sig header
This commit is contained in:
@@ -7,7 +7,7 @@ from django.template.loader import render_to_string
|
||||
from django.shortcuts import get_object_or_404
|
||||
from smtplib import SMTPException
|
||||
from accounts.models import IAmPrincipal, IAmPrincipalOtp, IAmPrincipalType
|
||||
from manage_referrals.models import GoodTimeCoins
|
||||
from manage_referrals.models import GoodTimeCoins, ReferralRecord
|
||||
from manage_subscriptions.models import PrincipalSubscription, Subscription
|
||||
from manage_wallets.models import TransactionStatus, Wallet, Transaction
|
||||
from goodtimes.utils import CapacityError, RandomGenerator
|
||||
@@ -191,6 +191,7 @@ class PaymentProcessingService:
|
||||
self.charge_data = webhook_data["data"]["object"]
|
||||
self.customer_id = self._get_customer_id()
|
||||
self.transaction = self._get_transaction_by_id()
|
||||
self.principal_id = self.transaction.principal_id
|
||||
|
||||
def _get_customer_id(self):
|
||||
# Access the customer ID from the charge object
|
||||
@@ -219,6 +220,38 @@ class PaymentProcessingService:
|
||||
self._create_principal_subscription()
|
||||
self._update_wallet()
|
||||
self._update_transaction_success()
|
||||
self._credit_referral_reward_if_applicable()
|
||||
|
||||
def _credit_referral_reward_if_applicable(self):
|
||||
# Step 1: Check for an existing, completed referral record
|
||||
referral_record = ReferralRecord.objects.filter(
|
||||
referred_principal_id=self.principal_id, is_completed=True
|
||||
).first()
|
||||
|
||||
if referral_record:
|
||||
# Step 2: Check for an active subscription of the referrer
|
||||
today = timezone.now().date()
|
||||
active_subscription = (
|
||||
PrincipalSubscription.objects.filter(
|
||||
principal=referral_record.referrer_principal,
|
||||
end_date__gte=today,
|
||||
cancelled=False,
|
||||
)
|
||||
.order_by("-end_date")
|
||||
.first()
|
||||
)
|
||||
|
||||
if active_subscription:
|
||||
# Step 3: Credit Good Time Coin
|
||||
# Assume a method exists to credit the coin, adjust as needed
|
||||
self._credit_good_time_coin(referral_record.referrer_principal)
|
||||
|
||||
def _credit_good_time_coin(self, referrer_principal):
|
||||
# Assuming functionality to credit coin exists, implement or call here.
|
||||
# This could involve updating a wallet model, creating a transaction, etc.
|
||||
print(
|
||||
f"Crediting a Good Time Coin to {referrer_principal}'s wallet"
|
||||
) # Placeholder
|
||||
|
||||
def _handle_failure(self):
|
||||
# Implement any necessary logic to handle a failed payment
|
||||
@@ -254,7 +287,9 @@ class PaymentProcessingService:
|
||||
.value_in_pound
|
||||
)
|
||||
|
||||
wallet, created = Wallet.objects.get_or_create(principal=self.transaction.principal)
|
||||
wallet, created = Wallet.objects.get_or_create(
|
||||
principal=self.transaction.principal
|
||||
)
|
||||
wallet.coins += 1
|
||||
wallet.save()
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ class ReferralRecordReward(BaseModel):
|
||||
subscription = models.ForeignKey(
|
||||
Subscription, on_delete=models.CASCADE, related_name="subscription_reward"
|
||||
)
|
||||
coins = models.PositiveBigIntegerField()
|
||||
coins = models.PositiveBigIntegerField(default=1)
|
||||
value = models.DecimalField(max_digits=14, decimal_places=2)
|
||||
|
||||
class Meta:
|
||||
|
||||
@@ -165,11 +165,11 @@ class StripeWebhookTest(APIView):
|
||||
def post(self, request):
|
||||
stripe.api_key = settings.STRIPE_SECRET_KEY
|
||||
payload = request.body
|
||||
sig_header = request.META.get("HTTP_STRIPE_SIGNATURE")
|
||||
sig_header = request.META["HTTP_STRIPE_SIGNATURE"]
|
||||
endpoint_secret = "whsec_ccf1f87295603cdd1733995ee2d3c0d6f74c7ceaf28916ea45114a54b7ce1d0f" # Make sure to retrieve this from your settings
|
||||
|
||||
event = None
|
||||
try:
|
||||
event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
|
||||
event = stripe.Event.construct_from(json.loads(payload), stripe.api_key)
|
||||
except ValueError as e:
|
||||
# Invalid payload
|
||||
return ApiResponse.error(
|
||||
|
||||
Reference in New Issue
Block a user