【发布时间】:2018-09-19 15:14:05
【问题描述】:
我正在使用 Stripe 处理订阅付款。我有一个PaymentsController,当用户输入他们的信用卡信息时处理。 Stripe 创建一个用户并将订阅附加到该用户。 Stripe gem 处理这些网络请求。但是,如果 Stripe 在请求创建用户或附加订阅期间的任何时候遇到错误,我想呈现一个 json 错误。有没有办法通过 gems 处理来自网络请求的错误?
payments_controller.rb
class Api::V1::PaymentsController < ApplicationController
before_action :authenticate_user!
def create
Stripe.api_key = ENV['STRIPE_SECRET_KEY_TEST']
// render an error if there is an issue creating a customer
customer = Stripe::Customer.create({
email: current_user.email,
source: request.params[:id]
})
stripe_plan = ENV['STRIPE_PLAN_ID_TEST']
// render an error if there is an issue creating a subscription
subscription = Stripe::Subscription.create({
customer: customer.id,
items: [{ plan: stripe_plan }],
})
current_user.subscription_plan = 1
current_user.save
if current_user.save
render json: { 'success': true }, status: 200
else
render json: { 'error': 'Some error with saving user here' }, status: 500
end
end
end
【问题讨论】:
标签: ruby-on-rails