【发布时间】:2019-02-24 19:54:03
【问题描述】:
我在 python 中使用条纹。我跟着this guide创建了一个sepa_debit源对象。
据我了解,我需要先创建一个源对象,然后才能创建费用,因为费用需要源对象 ID。
此时我不关心客户,因为所有费用都应该是一次性的,并且源对象不可重复使用。
简而言之,在客户端我得到了来自 stripe 和 src_id 的肯定响应:
这是一个成功的响应还是我错了?它发生在这部分:
// Call `stripe.createSource` with the iban Element and additional options.
stripe.createSource(iban, sourceData).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
errorMessage.textContent = result.error.message;
$('#error-message').removeClass('display-none');
$('#book-room-container #book-room-form button[type="submit"]').text("Unterkunft kostenpflichtig buchen");
$('#book-room-container #book-room-form button[type="submit"]').attr("disabled", false);
//stopLoading();
} else {
// Send the Source to your server to create a charge.
$('#error-message').addClass('display-none');
console.log(result.source); // <-- THIS IS THE LOG OF THE IMAGE ABOVE
console.log(result.source.id);
console.log(result.source.type);
$('#book-room-container #stripe_source_id').val(result.source.id);
$('#book-room-container #stripe_source_type').val(result.source.type);
// create charge and return charge id
$.ajax({
url : '/process-stripe-payment',
type : "post",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify({'total_price': total_room_price, 'user_email': $.trim($('#book-room-container #sepa_email').val()),
'room_id': $('#book-room-container').data('roomid'), 'sepa_owner': $.trim($('#book-room-container #sepa_owner').val()),
'stripe_source_id': $.trim($('#book-room-container #stripe_source_id').val()), 'stripe_source_type': $.trim($('#book-room-container #stripe_source_type').val())}),
success : function(response) {
console.log("process-stripe-payment", response);
},
error : function(xhr) {
console.log("failed process-stripe-payment", xhr);
}
});
}
我有点怀疑,因为要创建这个源对象,公钥就足够了,但这就是文档所说的。他们的提示还表明,现在我应该可以使用 src_id 并创建费用了:
// 将源发送到您的服务器以创建费用。
现在我正在获取 src_id 和一些必要的数据,然后转到我要创建费用的服务器:
// create charge and return charge id
$.ajax({
url : '/process-stripe-payment',
type : "post",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify({'total_price': total_room_price, 'user_email': $.trim($('#book-room-container #sepa_email').val()),
'room_id': $('#book-room-container').data('roomid'), 'sepa_owner': $.trim($('#book-room-container #sepa_owner').val()),
'stripe_source_id': $.trim($('#book-room-container #stripe_source_id').val()), 'stripe_source_type': $.trim($('#book-room-container #stripe_source_type').val())}),
success : function(response) {
console.log("process-stripe-payment", response);
},
error : function(xhr) {
console.log("failed process-stripe-payment", xhr);
}
});
这是服务器代码:
@app.route('/process-stripe-payment', methods=["POST"])
def process_stripe_payment():
if request.method == "POST":
data_received = json.loads(request.data)
print (data_received, data_received['stripe_source_id'])
# FIRST TRY STRIPE
stripe_charge = stripe.Charge.create(
amount=int(float(data_received['total_price']) * 100),
currency="eur",
source=data_received['stripe_source_id'],
description=None,
#customer=user_id, # None if guest
capture=False, # if False the charge needs to be captured, otherwise it will expire in 7 days and is refunded
#receipt_email='email für den typer, der die rechnung kriegt, funktioniert nur im livemode',
metadata={
'user_email': data_received['user_email'],
'room_id': data_received['room_id']
}
)
print (stripe_charge)
return json.dumps({'status' : 'success'})
return redirect(url_for('index'))
问题是它不起作用。在条带日志中,我看到提出了一个请求但失败了(400)。错误:
{
"error": {
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such token: src_1DCRd12eZvKYlo2ClWoO3vFj",
"param": "source",
"type": "invalid_request_error"
}
}
【问题讨论】: