在 django 中使用 stripe API 获取计划的数量
Getting the amount of a plan using stripe API in django
我有以下 class 基础视图,它处理我的 django 应用程序和 stripe API 之间的 get / post:
class StripeApi(View):
@staticmethod
def get(request):
return render(request, 'index.html',
{
'stripe_pub_key': settings.STRIPE_PUBLISHABLE_KEY
})
@staticmethod
def post(request):
charge = stripe.Customer.create(
source=request.POST['stripeToken'],
email=request.POST['stripeEmail'],
plan=request.POST['plan'],
description='Charge for {}'.format(request.POST.get("stripeEmail", "")),
)
paym = stripe.Charge.retrieve(
source=request.POST['stripeToken'],
amount=request.POST.get("amount", "")
)
return render(request, 'stripe.html',
{
'charge_id': charge.id,
'created': charge.created,
'email': request.POST['stripeEmail'],
'plan': request.POST['plan'],
'amount': request.POST.get("amount", "")
})
以及以下发送必要参数的HTML:
<form action="stripe/" method="POST">
<input type="hidden" value="test12months" id="plan" name="plan">
<input type="hidden" value="stripeEmail" id="stripeEmail" name="stripeEmail">
<input type="hidden" value="amount" id="amount" name="amount">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_secret"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-name="12 Months"
data-description="12 Months Subscription (19.99$ per year)"
data-amount= "1999"
data-locale="auto">
</script>
</form>
问题是当我尝试添加 amount
:
时出现以下错误
Exception Value: retrieve() takes at least 2 arguments (1 given)
当我添加 amount
.
谁能帮我弄清楚如何检索特定创建计划的数量?
要检索计划的金额,您需要发出 plan retrieval 调用:
plan = stripe.Plan.retrieve(request.POST['plan'])
amount = plan.amount
您的 charge retrieval call is invalid: to retrieve a charge, you need to pass its ID, which in this case you don't have because you didn't directly create the charge -- rather, you created a subscription,Stripe 会自动为您创建费用。
我有以下 class 基础视图,它处理我的 django 应用程序和 stripe API 之间的 get / post:
class StripeApi(View):
@staticmethod
def get(request):
return render(request, 'index.html',
{
'stripe_pub_key': settings.STRIPE_PUBLISHABLE_KEY
})
@staticmethod
def post(request):
charge = stripe.Customer.create(
source=request.POST['stripeToken'],
email=request.POST['stripeEmail'],
plan=request.POST['plan'],
description='Charge for {}'.format(request.POST.get("stripeEmail", "")),
)
paym = stripe.Charge.retrieve(
source=request.POST['stripeToken'],
amount=request.POST.get("amount", "")
)
return render(request, 'stripe.html',
{
'charge_id': charge.id,
'created': charge.created,
'email': request.POST['stripeEmail'],
'plan': request.POST['plan'],
'amount': request.POST.get("amount", "")
})
以及以下发送必要参数的HTML:
<form action="stripe/" method="POST">
<input type="hidden" value="test12months" id="plan" name="plan">
<input type="hidden" value="stripeEmail" id="stripeEmail" name="stripeEmail">
<input type="hidden" value="amount" id="amount" name="amount">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_secret"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-name="12 Months"
data-description="12 Months Subscription (19.99$ per year)"
data-amount= "1999"
data-locale="auto">
</script>
</form>
问题是当我尝试添加 amount
:
Exception Value: retrieve() takes at least 2 arguments (1 given)
当我添加 amount
.
谁能帮我弄清楚如何检索特定创建计划的数量?
要检索计划的金额,您需要发出 plan retrieval 调用:
plan = stripe.Plan.retrieve(request.POST['plan'])
amount = plan.amount
您的 charge retrieval call is invalid: to retrieve a charge, you need to pass its ID, which in this case you don't have because you didn't directly create the charge -- rather, you created a subscription,Stripe 会自动为您创建费用。