I have implemented Braintree subscription payment in rails app. Everything works fine 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?
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
I'm not sure if you used the same invalid card number for testing in production as in sandbox, but I'll try to answer your questions with the information we have on hand:
By attempting to create a customer with a payment method with an invalid card, the result of that API call was an ErrorResult
object. ErrorResult
objects are either a validation error, processor decline, gateway rejection, or other exception messages, and do not contain a customer
method. Hence, the undefined method
error.
You should add some error handling around all of your Braintree API calls so that you can address any errors throughout the subscription process.