shopify application charge failing to save - ruby-on-rails

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.

Related

Allauth user_logged_in signal does not trigger in a TestCase

I'm using allauth for user management in my Django web app and I have functionality which finds session data stored when the user was not logged in and stores it when they next log in on the same session.
It is triggered on login via the standard allauth signal:
#receiver(user_logged_in)
def update_on_login(request, user, **kwargs):
print('Signal triggered')
# Do some stuff with session data here, e.g.:
for session_key in request.session.items():
...
This works perfectly when logging in via a web browser, but in TestCase functions the signal is not triggered at all when logging in with:
self.client = Client()
logged_in = self.client.login(username=self.username, password=self.password)
(I've also tried force_login and force_authenticate, but info on these indicates that they actually skip all real validation and just assume the user is logged in).
I think I've understood that client.login doesn't work because the test client doesn't really handle the request in the same way as a web browser would, but I can't say I really understand it.
I've also seen some indication that RequestFactory might be able to help in someway, but I haven't been able to trigger the signal with this either:
request = RequestFactory().post(reverse('account_login'), { 'username': self.username, 'password': self.password })
request.user = AnonymousUser()
response = login(request)
I've seen some comments about needing to call middleware as well, but pretty out of date and nothing clear enough for me to understand and try.
Any suggestions to point me in the right direction would be much appreciated.
Thanks in advance.

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.

How do I get the JSON response from Dialogflow with Rails?

I understand the whole process of dialogflow and I have a working deployed bot with 2 different intents. How do I actually get the response from the bot when a user answers questions? (I set the bot on fulfillment to go to my domain). Using rails 5 app and it's deployed with Heroku.
Thanks!
If you have already set the GOOGLE_APPLICATION_CREDENTIALS path to the jso file, now you can test using a ruby script.
Create a ruby file -> ex: chatbot.rb
Write the code bellow in the file.
project_id = "Your Google Cloud project ID"
session_id = "mysession"
texts = ["hello"]
language_code = "en-US"
require "google/cloud/dialogflow"
session_client = Google::Cloud::Dialogflow::Sessions.new
session = session_client.class.session_path project_id, session_id
puts "Session path: #{session}"
texts.each do |text|
query_input = { text: { text: text, language_code: language_code } }
response = session_client.detect_intent session, query_input
query_result = response.query_result
puts "Query text: #{query_result.query_text}"
puts "Intent detected: #{query_result.intent.display_name}"
puts "Intent confidence: #{query_result.intent_detection_confidence}"
puts "Fulfillment text: #{query_result.fulfillment_text}\n"
end
Insert your project_id. You can find this information on your agent on Dialogflow. Click on the gear on the right side of the Agent's name in the left menu.
Run the ruby file in the terminal or in whatever you using to run ruby files. Then you see the bot replying to the "hello" message you have sent.
Obs: Do not forget to install the google-cloud gem:
Not Entirely familiar with Dilogflow, but if you want to receive a response when an action occurs on another app this usually mean you need to receive web-hooks from them
A WebHook is an HTTP callback: an HTTP POST that occurs when something happens; a simple event-notification via HTTP POST. A web application implementing WebHooks will POST a message to a URL when certain things happen.
I would recommend checking their fulfillment documentation for an example. Hope this helps you out.

Gibbon API does not error, but does not subscribe

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...

Google Federated OAuth/OpenID with Tornado: why is it ignoring my scopes?

I'm trying to use Tornado's library for federated login to authenticate users and get access to their calendar, contacts, and mail. However, when I get the "mydomain.dyndns.info is asking for some information from your Google Account" message, the only bullet point listed is "Email Address". Subsequently, when I check the returned user object after I approve the request, the user object doesn't have an 'access_token' property.
Here's the code:
def get(self):
scope_list = ['https://mail.google.com/','http://www.google.com/m8/feeds/','http://www.google.com/calendar/feeds/']
...
self.authorize_redirect(scope_list, callback_uri=self._switch_command('auth_callback'), ax_attrs=["name","email"])
def _on_auth(self, user):
print 'in on auth'
if user:
self.set_the_user(user['email'])
session.set_data('usertoken_' + user['email'], user['access_token'])
self.redirect('/')
The uri that this spits out is:
https://www.google.com/accounts/o8/ud
?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0
&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select
&openid.return_to=http%3A%2F%2Fmydomain.dyndns.info%3A333%2Fauth%2Fauth_callback%3Fperms%3Dgmail%26perms%3Dcontacts%26perms%3Dcalendar
&openid.realm=http%3A%2F%2Fmydomain.dyndns.info%3A333%2F
&openid.mode=checkid_setup
&openid.ns.oauth=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Foauth%2F1.0
&openid.oauth.consumer=mydomain.dyndns.info
&openid.oauth.scope=https%3A%2F%2Fmail.google.com%2F+http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F+http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F
&openid.ns.ax=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0
&openid.ax.type.fullname=http%3A%2F%2Faxschema.org%2FnamePerson
&openid.ax.type.lastname=http%3A%2F%2Faxschema.org%2FnamePerson%2Flast
&openid.ax.type.firstname=http%3A%2F%2Faxschema.org%2FnamePerson%2Ffirst
&openid.ax.mode=fetch_request
&openid.ax.type.email=http%3A%2F%2Faxschema.org%2Fcontact%2Femail
&openid.ax.required=firstname%2Cfullname%2Clastname%2Cemail
Ideas: 1. maybe this has something to do with the fact I'm running on a local machine behind a dyndns forwarder? 2. Tornado's documentation says "No application registration is necessary to use Google for authentication or to access Google resources on behalf of a user" -- but maybe that's not true anymore?
If anyone has thoughts, I'd really appreciate it -- this is driving me a little batty!
Figured it out. You have to set the application properties google_consumer_key and google_consumer_secret.
application = tornado.web.Application(urlhandlers, cookie_secret=cookie_secret, google_consumer_key=google_consumer_key, google_consumer_secret=google_consumer_secret)
You get them by going here: https://www.google.com/accounts/ManageDomains

Resources