简体   繁体   English

Stripe 订阅取消:KeyError 'HTTP_STRIPE_SIGNATURE'

[英]Stripe subscription cancel: KeyError 'HTTP_STRIPE_SIGNATURE'

I'm trying to configure Django Stripe Subscriptions for WebApp.我正在尝试为 WebApp 配置 Django 条纹订阅。

I want to let Subscribed users cancel the subscription themselves.我想让订阅用户自己取消订阅。

The code below is to delete user's information from StripeAPI and Django StripeCustomer model.下面的代码是从 StripeAPI 和 Django StripeCustomer model 中删除用户信息。

Here is view.py这是view.py

import stripe
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User  
from django.http.response import JsonResponse, HttpResponse  
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import get_user_model
from subscriptions.models import StripeCustomer 

@login_required    
@csrf_exempt
def cancel_subscription(request):
    if request.user.is_authenticated:

        endpoint_secret = settings.STRIPE_ENDPOINT_SECRET
        payload = request.body
        event = None
        sig_header = request.META['HTTP_STRIPE_SIGNATURE']
       
        event = stripe.Webhook.construct_event(
        payload, sig_header, endpoint_secret
        )
        session = event['data']['object']

        stripe_customer = StripeCustomer.objects.get(user=request.user)
        stripe.api_key = settings.STRIPE_SECRET_KEY
        sub_id = stripe.Subscription.retrieve(stripe_customer.stripeSubscriptionId)
        client_reference_id = session.get('client_reference_id')
        user = get_user_model().objects.get(id=client_reference_id)

        try:
            #delete from stripeapi
            stripe.Subscription.delete(sub_id)

            #delete from StripeCustomer model
            StripeCustomer.objects.delete(
            user=user,
            stripeCustomerId=stripe_customer_id,
            stripeSubscriptionId=stripe_subscription_id,
            )
            print(user.username + ' unsubscribed.')
        except Exception as e:
            import traceback
            traceback.print_exc()
            return JsonResponse({'error': (e.args[0])}, status =403)
    
    return render(request, 'home.html')

When I excecute the code error occuerd at当我执行代码错误发生在

sig_header = request.META['HTTP_STRIPE_SIGNATURE'] sig_header = request.META['HTTP_STRIPE_SIGNATURE']

The error message is below错误信息如下

Exception Type: keyError异常类型:keyError

Exception Value: 'HTTP_STRIPE_SIGNATURE'异常值:“HTTP_STRIPE_SIGNATURE”

I don't understand why the error occurs at request.META['HTTP_STRIPE_SIGNATURE'],because other part of this view can execute this code.我不明白为什么request.META['HTTP_STRIPE_SIGNATURE']会出现错误,因为这个视图的其他部分可以执行这个代码。

I just mentioned the above settings in this question but still if more code is required then tell me I'll update my question with that information.我刚刚在这个问题中提到了上述设置,但如果需要更多代码,然后告诉我我会用这些信息更新我的问题。 Thank you谢谢

I think you're mixing up a webhook handler and a regular POST request route as part of your application here.我认为您在此处将 webhook 处理程序和常规 POST 请求路由作为您的应用程序的一部分混合在一起。 You either need one or the other, and I suspect you don't need the webhook stuff at all given what you're trying to to.您要么需要一个,要么需要另一个,而且我怀疑您根本不需要 webhook 的东西,因为您正在尝试这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM