通过alamofire将付款金额从ios前端发布到条带后端
因此,基本上我添加了一些功能,允许我向后端发布请求,以创建条带支付意图,并验证和运行该功能。到目前为止,当我将后端ruby文件中的amount值硬编码为一个设置值时,一切都正常。例如,如果我将金额硬编码为1099(10.99),则请求有效,并通过我的stripe sandbox通过10.99收费;但是,我想将我自己的金额值发布到后端以收取费用。下面是我试图编辑的stripe的posting方法的后端代码示例。在这里,我写amount=payload[:sub],我过去有一个硬编码的amount值,所以它过去是amount=1099通过alamofire将付款金额从ios前端发布到条带后端,ios,swift,ruby,stripe-payments,Ios,Swift,Ruby,Stripe Payments,因此,基本上我添加了一些功能,允许我向后端发布请求,以创建条带支付意图,并验证和运行该功能。到目前为止,当我将后端ruby文件中的amount值硬编码为一个设置值时,一切都正常。例如,如果我将金额硬编码为1099(10.99),则请求有效,并通过我的stripe sandbox通过10.99收费;但是,我想将我自己的金额值发布到后端以收取费用。下面是我试图编辑的stripe的posting方法的后端代码示例。在这里,我写amount=payload[:sub],我过去有一个硬编码的amount值
post '/create_payment_intent' do
authenticate!
payload = params
if request.content_type != nil and request.content_type.include? 'application/json' and params.empty?
payload = Sinatra::IndifferentHash[JSON.parse(request.body.read)]
end
# Calculate how much to charge the customer
amount = payload[:sub].to_f // This used to be - amount = 1099
begin
payment_intent = Stripe::PaymentIntent.create(
:amount => amount,
:currency => currency_for_country(payload[:country]),
:customer => payload[:customer_id] || @customer.id,
:description => "Example PaymentIntent",
:capture_method => ENV['CAPTURE_METHOD'] == "manual" ? "manual" : "automatic",
payment_method_types: payment_methods_for_country(payload[:country]),
:metadata => {
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
}.merge(payload[:metadata] || {}),
)
rescue Stripe::StripeError => e
status 402
return log_info("Error creating PaymentIntent: #{e.message}")
end
log_info("PaymentIntent successfully created: #{payment_intent.id}")
status 200
return {
:intent => payment_intent.id,
:secret => payment_intent.client_secret,
:status => payment_intent.status
}.to_json
end
给出了这个后端代码,我试图通过编写这个方法来实现我的前端post,在这个方法中,我有一个参数,该参数是字符串形式的金额,例如“1099”,用于字符串,以便通过这个alamofire post请求将其发布到后端
func createPaymentIntent(sub: String? = nil, completion: @escaping STPJSONResponseCompletionBlock) {
var url = URL(string: backendURL)!
url.appendPathComponent("create_payment_intent")
Alamofire.request(url, method: .post, parameters: ["sub": sub])
.validate(statusCode: 200..<500)
.responseJSON { (response) in
switch (response.result) {
case .failure(let error):
completion(nil, error)
case .success(let json):
completion(json as? [String : Any], nil)
}
}
}
每当我这样做的时候,我会得到一个错误
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
我的Stripe Dashboard显示PaymentIntent从来不是由后端创建的,这意味着变量的发布一定不起作用
我该怎么做才能解决这个问题?如何将可变金额发布到后端进行处理
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))