Confirmation URL for Shopify RecurringApplicationCharge is nil - ruby-on-rails

Im trying to add a recurring charge to my shopify app. I followed the Shopify-Tutorial but wrote it slightly different. My root route goes to:
root 'mycontroller#charging'
the controller action is:
def charging
if #shop_domain != #myshop
#shop_charging_status = #shop.charging
unless ShopifyAPI::RecurringApplicationCharge.current
recurring_charge = ShopifyAPI::RecurringApplicationCharge.new(
name: "My App",
price: "1.99",
return_url: "https:\/\/appurl\/activated",
trial_days: 7,
terms: "$1.99 per month")
if recurring_charge.save
#tokens[:confirmation_url] = recurring_charge.confirmation_url
#shop_charging_status = true
#shop.save
redirect recurring_charge.confirmation_url
end
end
else
redirect_to myindex_path
end
When I try to start the app, i get an error: NoMethodError (undefined method `[]=' for nil:NilClass). It concerns the #token line. This line already confused me when i wrote the code, because the variable #token is only used in this method. But nevertheless, why is it nil?
What am I missing?

When I try to start the app, i get an error: NoMethodError (undefined method `[]=' for nil:NilClass). It concerns the #token line.
I assume you mean #tokens?
I think you're missing the first part of the tutorial here in which they set #tokens = {} in the initialize method and then store the access tokens for each shop in there.

Related

I'm having trouble understanding how to use webmock with a test for fetching a message from an api

I'm currently trying to write a test for this method:
#fetch message from api
def message_instance
project_id = ENV['SIGNALWIRE_PROJECT_KEY']
project_tkn = ENV['SIGNALWIRE_TOKEN']
host_url = ENV['SIGNALWIRE_HOST']
#client = Signalwire::REST::Client.new project_id, project_tkn, signalwire_space_url: host_url
message = #client.messages(sid).fetch
end
and am using FactoryBot to simulate a received message
#message = build :message, status: :received
But I can't get my head around how to test every line of the method. Hoping someone can break down how I can properly stub a request for this?
Edit: I so far I've tried this:
describe 'message_instance' do
it 'returns message instance' do
allow(ENV).to receive(:[]).with('SIGNALWIRE_PROJECT_KEY').and_return('AC123')
#message = build :message, status: 'received', sid: '123456789'
#message.message_instance.should eq #client
end
end
which returns this error:
"BUNDLER_ORIG_RUBYLIB"=>"BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL", "BUNDLER_ORIG_RUBYOPT"=>"BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL", "BUNDLE_GEMFILE"=>"/vagrant/oyetext-backend/Gemfile", "BUNDLE_BIN_PATH"=>"/usr/share/rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle", "BUNDLER_VERSION"=>"2.1.4", "RUBYOPT"=>"-r/usr/share/rvm/rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/setup", "RUBYLIB"=>"", "RAILS_ENV"=>"test", "SIGNALWIRE_PROJECT_KEY"=>"test", "SIGNALWIRE_TOKEN"=>"test", "SIGNALWIRE_NUMBER"=>"+19999999999"} received :[] with unexpected arguments
expected: ("SIGNALWIRE_PROJECT_KEY")
got: ("SIGNALWIRE_TOKEN")
Please stub a default value first if message might be received with other args as well.
I don't really think stubbing ENV is a good idea, as you could see that is used for way more stuff than your code logic, like bundler or ruby itself.
Instead of calling allow(ENV).to..., I'd try just with:
ENV['SIGNALWIRE_PROJECT_KEY'] = 'AC123'

NoMethodError (undefined method `each' for nil:NilClass):

class Api::SurveyAnswersController < ApplicationController
def create
# #survey_answer = SurveyAnswer.new(survey_answer_params)
survey_answers = []
survey_id = params[:survey_id]
params[:questions].each do |q|
answer = {survey_id: survey_id, option_ids: [], question_id: q[:id],
title: q[:answer]}
if q[:options].present?
selected_options = q[:answer].split(',')
selected_options.each do |selected_option|
q[:options].each do |option|
if option[:title]== selected_option
answer[:option_ids] << option[:id]
#<todo add break when in this condition
end
end
end
survey_answers << answer
end
end
puts survey_answers
# #survey_answers = SurveyAnswer.create(survey_answers)
if SurveyAnswer.create(survey_answers)
render json: survey_answers
end
end
end
I have a survey model which has some questions. Each question contains answers. When I try to hit post request through postman to insert answers, it gives 505 internal server error with message "undefined method each for nil:nilclass". Can anybody tell what the problem is?
You are trying to run the .each loop an empty object.
Make sure that both
params[:questions]
and
q[:options]
are not empty (not equal to nil).
NoMethodError sometimes sounds very unrepresentative, especially if you're just starting off with Ruby.
Try to browse Stackoverflow next time, because this has been answered here.

undefined method `+' for nil:NilClass spree

I am running a spree app.
I am getting below error when I try to add any product in the cart.
undefined method `+' for nil:NilClass
This error comes only when I add option types and variants of the same product.
I am not sure what's exactly going wrong here, because I am not doing any changes in the code or something.
This is the extracted source it shows.
if quantity.between?(1, 2_147_483_647)
begin
order.contents.add(variant, quantity, options)
rescue ActiveRecord::RecordInvalid => e
error = e.record.errors.full_messages.join(", ")
end
Here's my order controller's populate function.
# Adds a new item to the order (creating a new order if none already exists)
def populate
order = current_order(create_order_if_necessary: true)
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i
options = params[:options] || {}
# 2,147,483,647 is crazy. See issue #2695.
if quantity.between?(1, 2_147_483_647)
begin
order.contents.add(variant, quantity, options)
rescue ActiveRecord::RecordInvalid => e
error = e.record.errors.full_messages.join(", ")
end
else
error = Spree.t(:please_enter_reasonable_quantity)
end
if error
flash[:error] = error
redirect_back_or_default(spree.root_path)
else
respond_with(order) do |format|
format.html { redirect_to cart_path }
end
end
end
Please help me out here.
You need to ensure the values of variant, quantity and options before sending them to spree.
The fact that you get this error could be considered as a bug on their side, since you'd expect a nice error message saying "variant is nil" or the like.
To fix your problem though, I'd check that these values are valid ones before sending them to spree.
For future views about this issue.
Check if the Variant cost_currency attribute is the same that is configured in Spree. You can check it in a rails console doing:
Spree::Config.get(:currency)
Sometimes it happens when spree is initialized with some currency by default and then the default currency is changed.

Rails: first_or_create not working

Very strange issue with first_or_create. Consider the following method:
def self.store(session)
shop = self.first_or_create(shopify_domain: session.url, shopify_token: session.token)
binding.pry
shop.save!
shop.shopify_domain
end
When I pry into this method, I can call session.url to get domain2.myshopify.com and session.token to get 22222
But when I call shop, I get a shop where shopify_domain: domain1.myshopify.com and shopify_token: 11111.
Any idea why this would happen? It seems bizarre.
shop = self.where(shopify_domain: session.url, shopify_token: session.token).first_or_create(shopify_domain: session.url, shopify_token: session.token)
You are just getting the first one in general. Like calling .all.first

Facing issue NoMethodError (undefined method `include' for "56":String):

I am facing issue while writing this code
def sim_state
sim_employees = SimEmployee.find(params[:id].include(:employees))
respond_to do |format|
format.js {
render :layout => false,
:locals => {
:sim_employees => sim_employee
}
}
end
end
and in my sim_states.js.erb
$('#simState').text('<%= sim_employee.employees.app_state%>');
So it gives me this error
NoMethodError (undefined method `include' for "56":String):
when i use it with includes then it gives
undefined method `includes'
Please guide me how to solve this.
The reason is simple
params[:id]
is return you a String value which is "56" and includes works with ActiveRecord::Relation and not string. So you are not able to fire the query that ways.
SimEmployee.find(params[:id])
This will return again a result and not relation. Try using
SimEmployee.where(id: params[:id]).includes(:employees).first
This should help you.
PS : Using includes for one record is same as firing two independent queries i.e.
#sim_employee = SimEmployee.find(params[:id])
#emplyess = #sim_employee.employees
There is a simple solution for this:
SimEmployee.includes(:employees).find params[:id]
SimEmployee.find(params[:id].include(:employees))
This line is causing the issue, you are calling include(:employees) on params[:id] which would be a number.
And you can't actually do .find(params[:id].include(:employees) because find method returns an instance of Model class, in this case, an instance of SimEmployee class, and you can't run method include on it.
Edit:
SimEmployee.includes(:employees).where()
You can pass a hash in where(). This works if your SimEmployee model has has_many relation to Employee model.

Resources