我正在使用Stripe API在支付网关上工作。我已经将card元素分为三个元素,分别为:cardnumer,cvc和cardExpiry。我仅使用cardnumber元素创建了令牌,并且尝试在条带中创建customer。
我收到错误提示:
Request req_vL0Z2mkBIrhu7N: Invalid object
/home/nishant/Development/ecommerce/project/shop/views.py in order_view
source = request.POST['stripeToken']
…
▶ Local vars
/home/nishant/Development/venv/lib/python3.6/site-packages/stripe/api_resources/abstract/createable_api_resource.py in create
response, api_key = requestor.request("post", url, params, headers)
…
▶ Local vars
/home/nishant/Development/venv/lib/python3.6/site-packages/stripe/api_requestor.py in request
resp = self.interpret_response(rbody, rcode, rheaders)
…
▶ Local vars
/home/nishant/Development/venv/lib/python3.6/site-packages/stripe/api_requestor.py in interpret_response
self.handle_error_response(rbody, rcode, resp.data, rheaders)
…
▶ Local vars
/home/nishant/Development/venv/lib/python3.6/site-packages/stripe/api_requestor.py in handle_error_response
raise err
…
▶ Local vars
这里是我的payment.html
:
var stripe = Stripe('pk_test_nnGYKgpxl7AmicqNbcoGIMqQ008d6IpXgw');
// Create an instance of Elements.
var elements = stripe.elements();
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
**var cardnumber = elements.create('cardNumber', { **
classes : {
base : "form-control",
focus : "green",
invalid : "error",
},
style : style
});
** var cvc = elements.create('cardCvc', { **
classes : {
base : "form-control",
focus : "green",
invalid : "error",
},
style : style
});
** var exp = elements.create('cardExpiry',{ **
classes : {
base: "form-control",
invalid : "error",
},
style : style
});
// Add an instance of the card Element into the `card-element` <div>.
cardnumber.mount('#cardnumber'); **
cvc.mount('#cardcvc'); **
exp.mount('#cardexpiry'); **
// Handle real-time validation errors from the card Element.
cardnumber.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
cvc.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
exp.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(cardnumber).then(function(result) { ***
if (result.error) {
// Inform the user if 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);
}
});
});
// Submit the form with the token ID.
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();
}
我的views.py
正在创建客户为:
stripe.Customer.create(
name = request.POST['name'],
email = request.POST['email'],
address = request.POST['address'],
source = request.POST['stripeToken']
)
我是条带的新手。我该如何解决?
[尝试将地址作为字典而不是纯字符串[1]:]传递>
stripe.Customer.create( description="My First Test Customer (created for API docs)", address = {'line1': 'one two three'} )
希望有帮助!
[1] https://stripe.com/docs/api/customers/create?lang=python#create_customer-address