gpt4 book ai didi

python - Stripe创建客户一次性收费

转载 作者:行者123 更新时间:2023-12-01 06:39:37 27 4
gpt4 key购买 nike

我正在使用 stripe 在 python Django 中构建捐赠表单。有些捐款是一次性的,有些则是经常性的。我想要做的是创建一个包含两种类型费用的客户对象,这样我们就可以收集每次捐赠的邮寄地址、电子邮件地址等。

我遇到的问题是我可以使用 Stripe token 处理一次性付款。但我不知道如何与客户一起做。这是我迄今为止所尝试过的。我收到的错误是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')

最佳答案

您似乎在此处使用付款方式,因此在创建一次性交易或订阅时,除了客户之外,您还需要显式指定付款方式。

您可以get Payment Methods使用 stripe.PaymentMethod.list

附加到客户

具体来说,对于通过付款方式支付的一次性费用,您必须使用 Payment Intent API创建 Charge 而不是 Charge API。您可以associate与客户的付款意向。

对于您的订阅电话:当 creating a Subscription使用 stripe.Subscription.create,您需要指定附加到您希望订阅收费的客户的 customerdefault_ payment_method (pm_xxxyyyzz) 。

关于python - Stripe创建客户一次性收费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59515502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com