Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

possível conflito com nome de métodos??? Moip2::NotFoundError in OrdersController#create #82

Open
oscarlaf03 opened this issue Jun 23, 2018 · 0 comments

Comments

@oscarlaf03
Copy link

oscarlaf03 commented Jun 23, 2018

Erro

Não sei porque estou recebendo esta tela de erro
Al parcer é que como se não conseguisse chamar ao metodo create do @api.order.create

Alguma ideia do que pode estar acontecendo?

Print da mensagem do erró no terminal

Moip2::NotFoundError: Resource not found
from /home/oscar/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/moip-sdk-ruby-ae4f111a1c34/lib/moip2/client.rb:80:in `create_response'
[19] pry(main)> 

image

No meu order controller

app/controllers/orders_controller.rb

class OrdersController < ApplicationController
  #[...]
  def new
    @order = Order.new
    @order.build_payment
  end

  def create
    @order = Order.new(set_params)
    @order.bookings.each {|booking| booking.event = @event}
    if @order.save
      set_buyer_guest
      create_moip_customer
      create_moip_order
      if create_moip_customer[:errors].present?
        flash[:alert] = create_moip_customer[:errors].first[:description]
      elsif create_moip_order[:errors].present?
        flash[:alert] = create_moip_order[:errros].first[:description]
      else
      flash[:notice] = "Resserva feita com sucesso"
      end
      redirect_to activity_path(@activity)
    else
      if @order.errors.any?
        flash[:alert] = @order.errors.messages.first.second.first
      end
      @order = Order.new
      @order.build_payment
      render :new
    end
  end
  #[...]
  private
  def create_moip_order
    new_moip_order(@order)
  end

  def create_moip_customer
    new_moip_customer(@order)
  end
  #[...]
  def set_buyer_guest
    guest = Guest.new(first_name: @order.payment.name.split(' ').first,
      last_name: @order.payment.name.split(' ').last, email: @order.payment.email,
      buyer: true)
    @order.bookings.create(guest: guest, event: @event)
  end
  
  def new_moip_customer(sales_order)
    payment = sales_order.payment
    customer = @api.customer.create(
      ownId: sales_order.buyer.api_other_id,
      fullname: sales_order.buyer.full_name,
      email: sales_order.buyer.email,
      phone: {
        areaCode: payment.phone_area_code,
        number: payment.phone_number,
      },
      birthDate: "1990-10-22",
      taxDocument: {
        type: "CPF",
        number: payment.cpf,
      },
      shippingAddress: {
        street: payment.street,
        streetNumber: payment.street_number,
        complement: "",
        district: payment.district,
        city: payment.city,
        state: payment.state,
        country: "BRA",
        zipCode: payment.postal_code,
      },
    )
    sales_order.buyer.update(moip_id: customer[:id]) unless customer[:errors].present?
    customer
  end

  def new_moip_order(sales_order)
    moip_order = @api.order.create(
      ownId: sales_order.id,
      amount: {
        currency: 'BRL',
        subtotals: {
          shipping: 0
        }
      },
      items: [
        {
          product: sales_order.events.first.activity.name,
          category: 'OTHER_CATEGORIES',
          quantity: sales_order.bookings.count,
          detail: sales_order.events.first.activity.description,
          price: sales_order.events.first.price.to_i
        }
      ],
      customer: {
        id: sales_order.buyer.moip_id
      },
      receivers: [
        {
          type: 'SECONDARY',
          feePayor: false,
          moipAccount: {
            id: sales_order.events.first.activity.tour_store.moip_id
          },
          amount: {
            percetual: 90
          }
        },
        {
          type: 'PRIMARY',
          feePayor: true,
          moipAccount: {
            id: "APP-ECV6553RNHOR"
          },
          amount: {
            percetual: 10
          }
        }
      ]
    )
    sales_order.update(moip_id: moip_order[:id], status: moip_order[:status]) unless moip_order[:errors].present?
    moip_order
  end
end

Instanciando o API

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  #[...]
  def set_moip_api
    @api = Moip.new.call
  end
end

Configurando o gem

config/initializers/moip.rb

class Moip

  attr_reader :token, :key

  def initialize(token = nil, key = nil)
    @token = token || get_token
    @key = key || get_key
  end

  def call
    authentication
  end

  def puts_hello
    'hello world'
  end


  private

  def get_token
    ENV['MOIP_SANDBOX_TOKEN']
  end

  def get_key
    ENV['MOIP_SANDBOX_KEY']
  end

  def credentials
    Rails.application.credentials[Rails.env.to_sym][:moip]
  end

  def authentication
    Moip2::Api.new(moip_client)
  end

  def moip_client
    Moip2::Client.new(:sandbox, moip_auth)
  end

  def moip_auth
    auth = Moip2::Auth::OAuth.new(ENV['MOIP_SANDBOX_ACCESS_TOKEN'])
  end
end

Nos meus models

app/models/guest.rb

class Guest < ApplicationRecord
  #[...]
  def full_name
    "#{first_name} #{last_name}"
  end

  def api_other_id
    "#{id.to_s}-xxx*-teste"
  end
end

app/models/order.rb

class Order < ApplicationRecord
  has_many :bookings, inverse_of: :order
  has_many :events,through: :bookings
  has_many :guests, through: :bookings, class_name: 'Guest'
  has_many :activities, through: :events
  has_one :payment
  accepts_nested_attributes_for :bookings, allow_destroy: true
  accepts_nested_attributes_for :guests
  accepts_nested_attributes_for :payment
  validates_associated :bookings, :guests

  def total
    get_order_total
  end

  def buyer
    guests.find_by(buyer: true)
  end

end
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant