使用python创建条带对象的实例

2024-06-16 09:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

使用django和stripe创建签出。当我使用view.py中的python变量publishkey时,它在设置中引用了我的测试条带\u PUBLISHABLE\u密钥。我收到错误消息“api密钥无效”:

// Create a Stripe client.

const stripe = Stripe('{{publishkey}}');

,但当我直接粘贴STRIPE_PUBLISHABLE_键时,如下所示:

// Create a Stripe client.

const stripe = Stripe('pk_test_xxxx')

结帐工作!问题是我不想在javascript文件中公开可发布密钥,而是想使用一个变量

stripe-app.js

// Create a Stripe client.
const stripe = Stripe('{{publishkey}}');

// Create an instance of Elements.
let elements = stripe.elements();

签出/views.py

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
import stripe

stripe.api_key = settings.STRIPE_SECRET_KEY
# Create your views here.

@login_required
def checkout(request):
    publishkey = settings.STRIPE_PUBLISHABLE_KEY
    customer_id = request.user.userstripe.stripe_id
    if request.method == 'POST':
        token = request.POST['stripeToken']
        stripe.Charge.create(
            amount=499,
            currency='usd',
            description='Example charge',
            source=token,
            statement_descriptor='Custom descriptor',
        )
    context = {'publishkey': publishkey}
    template = 'checkout.html'
    return render(request, template, context)

Tags: djangofrompyimportclientsettingsrequestcreate