Gibbon API does not error, but does not subscribe - ruby-on-rails

This is a strange one. I've had a working MailChimp, Gibbon, RoR app going for a couple of years now, and I went to go use part of my app this week and realized that the integration was no longer working. I am not receiving any errors, and some basic testing shows that the exception section of the code is never called.
Here is the code I am using:
begin
gb = Gibbon::API.new(mailchimp_api_key)
gb.lists.subscribe( id: mailchimp_list_id, email: {email: email} )
rescue Gibbon::MailChimpError => e
logger.error "Mailchimp threw an error. The code is: #{e.code}, with message: #{e.message}"
end
Some code edited for readability, but assume that the variables are defined and no errors are thrown.
What I'm looking for is some debugging help. I can't seem to find a way to debug the integration to know if there is something silently failing or not. Does anyone have any tips for debugging this outside of trying to catch a raised exception?

I use the same code and when something wrong an exception is thrown.
You should check and print what subscribeis returning.
response = gb.lists.subscribe( id: mailchimp_list_id, email: {email: email} )
puts response
According to the mailchimp documentation it should return a JSON like this one :
{
"email": "example email",
"euid": "example euid",
"leid": "example leid"
}
https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

Thanks!
And yep, I do get a response back that matches what you suggested (note, I used a real email address):
{
"email"=>"my#email.com",
"euid"=>"3cb513752a",
"leid"=>"89681797"
}
Strangely enough, it does show up on the mailchimp side as pending subscription, but the subscription confirmation is not sending. That sounds like I have a MailChimp problem, not a gibbon problem. Does anyone know of a setting on the MailChimp side I am missing?
Will keep digging...

Related

Braintree Error In Production Mode

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:
NoMethodError (undefined method `customer' for #):
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.

shopify application charge failing to save

Below is my code for a Shopify one-time-application-charge in Ruby. I followed the shopify "add billing to your app" page (https://help.shopify.com/api/tutorials/adding-billing-to-your-app) for the code, except didn't need a recurring charge. I have also found someone else who posted their one-time-charge code which looks very similar to mine (https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/one-time-application-charge-example-for-shopify-rails-app-489347).
def create_application_charge
application_charge = ShopifyAPI::ApplicationCharge.new(
name: "MyApp",
price: 0.09,
return_url: "https:\/\/myapp.herokuapp.com\/activatecharge",
test: true)
save = application_charge.save
if save
redirect application_charge.confirmation_url
return
end
flash[:error] = "The save worked: #{save}"
end
The flash always responds as false. Is there a failure at authentication that would prevent this? Or something to get the store to accept an application charge? I'm at a loss as to why this does not work.
Any help would be greatly appreciated, thank you.
The primary issue appears to be that the minimum charge you can request is $0.50, for which I wasn't meeting with my choice of using $0.09 for my test.

Stub Rails UJS/Ajax responses status to test returning message in Rspec/Capybara feature test

I have AJAX calls initiated by Rails UJS that I would like to test. specifically, I have used Rails UJS ajax events to provide for cases of errors.
I would like to test them but I don't know how to tell rspec/capybara to "stub" and assume the error code
$("button").
on('ajax:error',function(event,xhr, status, error){
if(status == "timeout") {
var msg;
msg = Messenger().post({
message: "This is taking too long"
});
} else {
var msg;
msg = Messenger().post({
message: "it seems there is a bug. Please try again."
});
};
});
I would like to do something like the following:
describe "test returning error message", js: true do
it "should be successful" do
visit deal_page_path(deal)
first('a.button').click
stub(:xhr.status) = "timeout"
expect(page).to have_content('This is taking too long')
end
end
How to do this?
Note: the ajax requests are internal they don't go to third party API or services (such as facebook for ex).
When testing with Capybara (JS enabled drivers) it has no access to the request or response except through the changes it creates in the browser. You could build a test mode into your relevant controllers that could be turned on and off to allow it to output the errors you want, but the cleanest way to do this is probably to use a programmable proxy like puffing-billy which will allow you to selectively return whatever you'd like for any given request from the browser. One thing to realize is that this isn't testing that app correctly returns errors, it's just testing that your front-end handles errors the way you expect.

Paypal-express gem, working in dev but not in prod

So I have an issue I can't figure out alone.
I have run some test on my app and when launch in dev it's working perfectly but as soon as it's on Prod and so it uses the real identificator, it doesn't work anymore.
I got this error:
Paypal::Exception::APIError (PayPal API Error: 'Security error'):
/offers_controller.rb:218:in choose_step'
if Rails.env.production?
response = request.setup(
payment_request,
"http://www.workiz.com/recruteurs/paypal_callback/" + params[:app_id],
"http://www.workiz.com/recruteurs/offres",
paypal_options # Optional
)
else # Development ou Test
response = request.setup(
payment_request,
"http://localhost:3000/recruteurs/paypal_callback/" + params[:app_id],
"http://localhost:3000/recruteurs/offres",
paypal_options # Optional
)
end
That is the line that crash, so it's when I call request.setup
The request is created like that:
if Rails.env.development?
Paypal.sandbox!
Paypal::Express::Request.new(
username: ENV['PAYPAL_SANDBOX_USERNAME'],
password: ENV['PAYPAL_SANDBOX_CLI_ID'],
signature: ENV['PAYPAL_SANDBOX_SECRET']
)
elsif Rails.env.production?
Rails.logger.info "Paypal SETUP PRODUCTION"
Paypal::Express::Request.new(
username: ENV['PAYPAL_USERNAME'],
password: ENV['PAYPAL_CLI_ID'],
signature: ENV['PAYPAL_SECRET']
)
end
And yes the logger "Paypal SETUP PRODUCTION" appear and the value set are the good one from the ENV variables.
I had to put Paypal.sandbox! in the config/development.rb to make it work for the sandbox but I cannot find a way to make it work for the production...
Any help is welcome. Thank you very much.
I have display the error, it look like that:
ERROR IS: #<Paypal::Exception::APIError::Response:0x007fa61661e040
#raw={
:TIMESTAMP=>"2015-05-24T15:01:30Z",
:CORRELATIONID=>"f3067f049ad",
:ACK=>"Failure",
:VERSION=>"88.0",
:BUILD=>"1675131",
:L_ERRORCODE0=>"10002",
:L_SHORTMESSAGE0=>"Security error",
:L_LONGMESSAGE0=>"Security header is not valid",
:L_SEVERITYCODE0=>"Error"},
#ack="Failure", #build="16751317", #correlation_id="f3067f049a", #timestamp="2015-05-24T15:01:30Z", #version="88.0", #order_time=nil, #pending_reason=nil, #payment_status=nil, #payment_type=nil, #reason_code=nil, #transaction_type=nil,
#error_code="10002",
#severity_code="Error",
#long_message="Security header is not valid",
#short_message="Security error"
Have a detailed look at the exception you're getting.
According to https://github.com/nov/paypal-express/blob/master/lib/paypal/exception/api_error.rb the error should have more useful information from the API response.
Simply catch the exception, and print it's contents:
begin
response = request.setup...
rescue Paypal::Exception::APIError => error
puts error.inspect
raise error
end
You're probably missing some configuration in your PayPal account. The detailed error message and error code should point you in the right direction.
Ok I have finally found my mistake.
It appear, you shouldn't use the CLI ID, PWD and SIGNATURE from the "live page" on your application.
But instead:
Log in to PayPal.com
You must have a PayPal Business account to make calls to the live PayPal servers. Log in to your Business account on the following page: https://www.paypal.com.
Navigate to the API Access page
Click the Tools tab and navigate to Manage your business > API Access.
Here you go, those are the good one...
Very confusing !
Hope it helps
First, double check that the 2 sets of credentials are different. You need a different account for sandbox than production.
PAYPAL_SANDBOX_USERNAME != PAYPAL_USERNAME
PAYPAL_SANDBOX_CLI_ID != PAYPAL_CLI_ID
PAYPAL_SANDBOX_SECRET != PAYPAL_SECRET
Second, double check that the production credentials are correct, i.e. no extra characters or typos.

Facebook Graph Feed Post Not Including Message

I have the following snippet to post to a user's feed on Facebook:
require 'httparty'
token = "..."
message = "..."
url = URI.escape("https://graph.facebook.com/me/feed?access_token=#{token}")
response = HTTParty.post(url, body: { message: message })
This posts to the wall, but no message is included. Any ideas what's wrong?
Edit:
I tried changing out the message for a caption or description and both failed as well.
Solution is to change HTTParty from using body to query for posting form data:
require 'httparty'
token = "..."
message = "..."
url = URI.escape("https://graph.facebook.com/me/feed?access_token=#{token}")
response = HTTParty.post(url, query: { message: message })
Based on the link cited above, it appears message functionality has been completely removed from the feed connection since July 12.
This is a problem for my current app as it is specifically a public opinion site. Asking users to express their opinions authentically is an important part of our design and we'd like to give them the option to post that to their feeds on Facebook as well.
Per the Facebook terms of use IV.2, "You must not pre-fill any of the fields associated with the following products, unless the user manually generated the content earlier in the workflow." The new change appears to change the terms of service: in my use, I am specifically asking the user to generate the content earlier in the workflow, but I still can't use it to pre-fill the feed dialog.
Anyone have any ideas or insight?

Resources