API GET 402 response.

New Member
3 0 0

Dear community,

 

I have a plugin for Shopify. I am trying to process card payments and get this error response when I try to run line 18. In line 18 I am trying to find an Order.

rb(main):027:0> order = ShopifyAPI::Order.find(checkout_id)
ActiveResource::ClientError: Failed.  Response code = 402.  Response message = Payment Required.
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:154:in `handle_response’
    from /app/vendor/bundle/ruby/2.1.0/gems/shopify_api-3.2.1/lib/active_resource/connection_ext.rb:9:in `handle_response_with_response_capture’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:123:in `request’
    from /app/vendor/bundle/ruby/2.1.0/gems/shopify_api-3.2.1/lib/active_resource/connection_ext.rb:13:in `request_with_detailed_log_subscriber’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:82:in `block in get’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:216:in `with_auth’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:82:in `get’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/base.rb:990:in `find_single’
    from /app/vendor/bundle/ruby/2.1.0/gems/activeresource-4.0.0/lib/active_resource/base.rb:877:in `find’
    from (irb):27
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start’
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start’
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from /app/bin/rails:4:in `require’
    from /app/bin/rails:4:in `<main>'

This is the code I am trying to run.

	1.	class OrderController < ApplicationController
	2.	  include OrderHelper
	3.	  
	4.	  def show
	5.	    cebollita_public_keys = params[:cebollita_public_keys].to_s.split('-')
	6.	
	7.	    checkout_id = parse_order_id(params[:order_id]).to_i
	8.	    shop = Shop.where(cebollita_public_key: { '$in'=> cebollita_public_keys }, :token.exists => true, :token.ne => "").first
	9.	
	10.	    puts "checkout_id: #{checkout_id}"
	11.	
	12.	    if shop
	13.	      sess = ShopifyAPI::Session.new(shop.domain, shop.token)
	14.	      ShopifyAPI::Base.activate_session(sess)
	15.	
	16.	      #try first looking up by order_id
	17.	      begin
	18.	        order = ShopifyAPI::Order.find(checkout_id)
	19.	        
	20.	        # map order fields from order_helper
	21.	        order_params = JSON.parse(order.attributes.to_json)
	22.	        json = get_order_hash_params(order_params).to_json
	23.	
	24.	        render json: json
	25.	
	26.	      rescue ActiveResource::ResourceNotFound
	27.	        since_id = checkout_id - 1
	28.	        checkout = ShopifyAPI::Checkout.where(since_id: since_id).first
	29.	
	30.	        if checkout and checkout.id == checkout_id
	31.	          # map order fields from order_helper
	32.	          order_params = JSON.parse(checkout.attributes.to_json)
	33.	          json = get_order_hash_params(order_params).to_json
	34.	        else
	35.	          #.where(created_at_min: Time.now.advance(:minutes=>-2).strftime('%Y-%m-%d %H:%M:%S')) 
	36.	          order = ShopifyAPI::Order.where(created_at_min: Time.now.advance(:days=>-1).strftime('%Y-%m-%d %H:%M:%S'), status: 'any').to_ary.select{|order| order.checkout_id == checkout_id}.first
	37.	          if order
	38.	            # map order fields from order_helper
	39.	            order_params = JSON.parse(order.attributes.to_json)
	40.	            json = get_order_hash_params(order_params).to_json
	41.	          else
	42.	            puts "**********************Order not found: #{params[:order_id]}****************************"
	43.	            json = {error: "not found"}
	44.	          end
	45.	        end
	46.	
	47.	        puts "json returned for #{shop.domain}: #{json}"
	48.	
	49.	        render json: json
	50.	      end
	51.	    else
	52.	      puts "**************************Shop does not exist: #{params[:+cebollita_public_key]}********************************"
	53.	      render json: {error: "not found"}
	54.	    end
	55.	  end

I have already tried to run locally and using different merchants environments but continue to get the same response. I already check the accounts and none of them are frozen. 

 

The  versions I am running are:

# gem 'debugger', group: [:development, :test]
gem 'shopify_app', '5.0.0'
gem 'therubyracer', platforms: :ruby
gem 'unicorn'
gem 'mongoid', github: 'mongoid/mongoid'

gem 'bson_ext'
gem 'sidekiq'
gem 'sidekiq-status'
gem 'sidekiq-cron'
gem 'redis'
gem 'sinatra', require: false
gem 'slim'
gem 'newrelic_rpm'
gem 'rake'
group :production do
  gem 'pg', '~> 0.14.1'
end
group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails'
  gem 'byebug', '9.0.0'
end

Does anyone have an idea why I am getting that response? Any help or insight will be truly appreciate it.

Thanks for your time.

0 Likes
Shopify Staff
Shopify Staff
1546 77 231

If you provide me with a shop ID or request ID (given in response headers by us) with time frames then I can take a closer look.

 

Cheers.

1 Like
New Member
3 0 0

11230311481434 <- Checkout id

0 Likes