ID为" customer_id"的客户未找到

时间:2017-08-07 08:35:47

标签: ruby-on-rails ruby ruby-on-rails-4 braintree

我使用以下Braintree代码通过customer_id查找客户,但收到此错误customer with id "customer_id" not found。为了完成这项工作,我需要更换字符串' customer_id'使用一个变量来表示我的Braintree Vault中客户的客户ID。

我有点坚持变量,需要一些帮助!!

User.rb

customer = Braintree::Customer.find(customer_id)# this string here need to be replaced
customer_card = customer.payment_methods[0].token
payment_method = Braintree::PaymentMethod.find(customer_card)
sub = payment_method.subscriptions[0]
sub.status

create方法位于transactions_controller.rb

def create
    nonce_from_the_client = params['payment_method_nonce']
    @result = Braintree::Customer.create(
    first_name: params[:first_name],
    last_name: params[:last_name],
    :payment_method_nonce => nonce_from_the_client
)

  if @result.success?
    puts @result.customer.id
    puts @result.customer.payment_methods[0].token
    redirect_to showcase_index_path, notice: 'Subscribed'

  else
    redirect_back( fallback_location: (request.referer || root_path),
                 notice: "Something went wrong while processing your transaction. Please try again!")
  end
end

2 个答案:

答案 0 :(得分:0)

你将customer_id放到Braintree :: Customer.find的哪个位置?您是否在查找之前成功创建了客户可能会抛出异常,因为Braintree无法在其系统上找到提供的客户。您可以登录Braintree并导出客户列表以检查提供的ID是否存在。

答案 1 :(得分:0)

在您的transactions_controller.rb创建方法中,在调用Braintree :: Customer.create时,您可以提供:id参数将您的用户ID作为客户ID。 (因此,在这种情况下,您可以提供用户表的ID)

如果您不提供:id参数,那么Braintree将为该客户创建自己的ID。