Ask A Question

Notifications

You’re not receiving notifications from this thread.

Braintree Error In Production Mode

Lee Terng Gio asked in Rails
Hi, I have implemented Braintree subscription payment in rails app which followed your tutorial. Everything works well in development, however when I switched to production (I have registered with Braintree and got a real account, and I change all the key in environment)

I have tried to submit an invalid card information to test the app, the page keeps showing error.

I look at the application logs and it said 
NoMethodError (undefined method `customer' for #<Braintree::ErrorResult:0x007f6ed80f1d80>):

Here's my create method, I follow your tutorial and it works fine in development
def create
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
      
          customer = result.customer
          current_user.update(braintree_id: customer.id)

        end

    result = Braintree::Subscription.create(
      payment_method_token: customer.payment_methods.find{ |pm| pm.default? }.token,
      plan_id: params[:plan_id]
    )
    if result.success?
    result.subscription.transactions.each do |transaction|
      current_user.transactions.create(braintree_transaction_id: transaction.id,
        plan_name: params[:plan_name],
        price: transaction.amount.to_f,
        start_date: transaction.subscription_details.billing_period_start_date,
        end_date: transaction.subscription_details.billing_period_end_date,
        subscription_id: result.subscription.id
        )
        
          
    end
  
    
    current_user.update(braintree_subscription_id: result.subscription.id, next_billing_date: result.subscription.next_billing_date,
    billing_period_start_date: result.subscription.billing_period_start_date,
    billing_period_end_date: result.subscription.billing_period_end_date,
    status: result.subscription.status,
    next_billing_period_amount: result.subscription.next_billing_period_amount,
    paid_through_date: result.subscription.paid_through_date,
    plan_id: params[:plan_id],
    plan_name: params[:plan_name])
    
    flash[:info] = "You've been subscribed successfully"
    redirect_to @current_user
    else
          flash[:warning] = "Invalid card information"
          render 'new'
    end
    end

The weird thing is it doesn't render the flash warning of unsuccessful result and redirect to the original new_subscription_path, instead the website url redirect to this
https://herokuappname.herokuapp.com/subscription.1
and the page error shows
This page isn’t working herokuappname.herokuapp.com is currently unable to handle this request.
HTTP ERROR 500

So, I want to know whether it is the customer method error (which I don't think so because it doesn't have any problem in development mode) or any other problem such as why the page url so weird?

I looked at the Braintree control panel, and the reason that the subscription failed was because the bank declined the transactions due to incorrect card information, which I entered incorrect card in order to test it, if it is invalid card info, why didn't it display the flash notice and redirect back to the new_subscription_path, instead it redirects to the subscription.1 url which I have mentioned above?
Reply
Hey Lee,

So for Braintree, you have to check _every_ time if the result is a success or not. In your case, it looks like the create of a customer may have failed.

          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
      
          customer = result.customer


This is the only line that calls `customer` on an object and can be the only line your original error came from. 

So you'll need to also handle the case where Braintree::Customer.create fails. You've got it handling the create Subscription failure, but not this one.

I'm not sure as to the redirect problem though.
Reply
I tried to put validation in create customer, which is
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
          if result.success?
            customer = result.customer
            current_user.update(braintree_id: customer.id)
          else
            flash[:info] = "Opps wrong"
            render 'new'
          end
        end

Now the error shows
2018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):
2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'


I really have no idea what causes this issue because everything works fine in development mode.
Reply
If your new.html.erb template uses the @plans variable, you'll have to also set it before you render it in create.

I would print out the error in the logs so you can view them and see what's going wrong in production. Right now I don't see anywhere that you'd be logging the error, so you'll have an impossible time figuring out what's up.
Reply

I have already set it
def new
@plans = Braintree::Plan.all
end

But right now the issue is
018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):

2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'

Reply
I have already solved it. Thanks!
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.