KeyError at /webhook/ 'HTTP_STRIPE_SIGNATURE'
Ниже приведен мой код для webhook с приложением Django
@csrf_exempt
def webhook(request):
webhook_secret = STRIPE_WEBHOOK_SECRET
payload = request.body.decode('utf-8')
signature = request.META["HTTP_STRIPE_SIGNATURE"]
try:
event = stripe.Webhook.construct_event(
payload=payload, sig_header=signature, secret=webhook_secret)
data = event['data']
except Exception as e:
return e
event_type = event['type']
data_object = data['object']
if event_type == 'invoice.paid':
webhook_object = data["object"]
stripe_customer_id = webhook_object["customer"]
stripe_sub = stripe.Subscription.retrieve(webhook_object["subscription"])
stripe_price_id = stripe_sub["plan"]["id"]
current_period_end = stripe_sub["current_period_end"]
current_period_end = datetime.datetime.fromtimestamp(current_period_end, tz=None)
pricing = Pricing.objects.get(stripe_price_id=stripe_price_id)
user = User.objects.get(stripe_customer_id=stripe_customer_id)
subscription = Subscription.objects.get(user=user)
subscription.status = stripe_sub["status"]
subscription.stripe_subscription_id = webhook_object["subscription"]
subscription.pricing = pricing
subscription.current_period_end = current_period_end
subscription.save()
if event_type == 'customer.subscription.deleted':
webhook_object = data["object"]
stripe_customer_id = webhook_object["customer"]
stripe_sub = stripe.Subscription.retrieve(webhook_object["id"])
user = User.objects.get(stripe_customer_id=stripe_customer_id)
subscription = Subscription.objects.get(user=user)
subscription.status = stripe_sub["status"]
subscription.save()
return HttpResponse()
и url является
path('webhook/', webhook, name='webhook')
если я проверяю путь https://example.com/webhook/, я получаю ошибку
Exception Type: KeyError at /webhook/
Exception Value: 'HTTP_STRIPE_SIGNATURE'
и в учетной записи strpe я получаю 500 ошибку
Он принимает POST запрос, когда бы вы ни ввели URL, он покажет вам эту ошибку из-за GET запроса.
выполните эти команды:
$ stripe login
$ stripe listen --forward-to localhost:8000/webhook
за сказанным можно проследить по самой полосе.