我正在使用Stripe在python Django中构建捐赠表单。有些捐赠是一次性的,而另一些则是经常性的。我想做的是创建一个同时包含两种费用的客户对象,以便我们可以为每次捐赠收集邮件地址,电子邮件地址等。
我遇到的问题是,我可以使用条纹令牌处理一次性付款。但我不知道如何与客户合作。到目前为止,这是我尝试过的。我收到的错误是No such token: cus_asdfasdfasdf
。知道我在做什么错吗?
donate.html页面上的Javascript:
var stripe = Stripe('public_key');
var elements = stripe.elements();
var card = elements.create('card');
card.mount('#card-element');
// Create a token or display an error when the form is submitted.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createPaymentMethod({
type: 'card',
card: card,
billing_details: {
email: 'my_email@gmail.com',
},
}).then(function(result) {
stripePaymentMethodHandler(result.PaymentMethod)
});
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
function stripePaymentMethodHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var paymentMethodInput = document.createElement('input');
paymentMethodInput.setAttribute('type', 'hidden');
paymentMethodInput.setAttribute('name', 'paymentMethodToken');
paymentMethodInput.setAttribute('value', token);
form.appendChild(paymentMethodInput);
}
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
在Django中查看
from django.shortcuts import render
import stripe
from transcal.settings import STRIPE_SECRET_KEY
from .forms import DonateForm
stripe.api_key = STRIPE_SECRET_KEY
def donate(request):
if request.method == 'POST':
stripe.api_key = STRIPE_SECRET_KEY
# Get the payment token ID submitted by the form:
token = request.POST.get('stripeToken')
amount = request.POST.get('amount')
payment_method = request.POST.get('paymentMethodToken')
single_or_recurring = request.POST.get('single_or_recurring')
customer = stripe.Customer.create(
description="Customer for jenny.rosen@example.com",
name='Joe',
email='joe@gmail.com',
payment_method=payment_method
)
if single_or_recurring == 'recurring':
# charge subscription
stripe.Subscription.create(
customer=customer,
items=[
{
"plan": "my_plan_id",
},
],
expand=["latest_invoice.payment_intent"]
)
else:
# charge one time
stripe.Charge.create(
amount=amount,
currency='usd',
description='Example charge',
source=customer
)
return render(request, 'donate.html')
答案 0 :(得分:1)
您似乎在这里使用“付款方式”,并且在创建一次性交易或订阅时,除了“客户”以外,您还需要明确指定“付款方式”。
您可以get Payment Methods通过stripe.PaymentMethod.list
附加到客户
具体来说,对于使用“付款方式”进行的一次性费用,您必须使用Payment Intent API创建费用,而不是费用API。您可以associate与客户进行付款。
对于您的订阅电话:当creating a Subscription与stripe.Subscription.create
一起使用时,您将希望指定附加到希望订阅的客户的customer
和default_payment_method
(pm_xxxyyyzz)收费。