我正在使用 stripe 在 swift 2.2 中编写一个应用程序。我的后端运行 ruby 脚本
post '/charge' do
# Get the credit card details submitted by the form
Stripe.api_key = params[:stripeAPIKey]
token = params[:stripeToken]
# Create the charge on Stripe's servers - this will charge the user's card
begin
charge = Stripe::Charge.create(
:amount => params[:amount], # this number should be in cents
:currency => "eur",
:card => token,
:description => params[:description],
:receipt_email => params[:email],
:statement_descriptor => params[:statement]
)
#rescue Stripe::CardError => e
# Since it's a decline, Stripe::CardError will be caught
end
status 200
return "Succès"
end
但我不知道如何在出现错误时获取 Stripe 返回的 JSON 消息。
在我的应用程序中,我使用 Alamofire
Alamofire.request(.POST, requestString, parameters: (params as! [String: AnyObject]))
.responseJSON { response in
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
最佳答案
您似乎正在使用 Sinatra。要返回 JSON,只需添加 json
gem 并返回它。
post '/charge' do
# ...
begin
charge = Stripe::Charge.create(
# ...
)
rescue Stripe::CardError => e
content_type :json
return { error: true, field: 'value' }.to_json
end
# ...
end
但是,对于成功和错误这两种情况,最好坚持使用一种内容类型。
post '/charge' do
content_type :json
result = {}
# ...
begin
charge = Stripe::Charge.create(
# ...
)
status 200
{ status: 'success', field: 'value'}.to_json
rescue Stripe::CardError => e
status 500
{ error: true, field: 'value' }.to_json
end
end
关于ios - Stripe iOS ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466189/