ruby-on-railspaymentspreestate-machine

Rails Spree Cannot transition from payment to complete


I am integrating a custom pay method into my rails app. But I cannot do that my order goes from payment stat to completed state. I am ensure that the payment registered at the order is at completed state, so I cannot figure out why the state machine are telling me "No payment found"

Here's my code.

  def perform_payment payment_id, state
    Rails.logger.info("PERFORM PAYMENT => payment_id: #{payment_id}, state: #{state}")
    payment = Spree::Payment.find payment_id
    return unless payment
    order = payment.order
    
    begin
      if state == "accepted"
        payment.started_processing!
        payment.capture!
        Rails.logger.info("PERFORM PAYMENT => order_id:#{order.id}, current_order_state: #{order.state}")
        Rails.logger.info("PERFORM PAYMENT => order_payments:#{order.payments.size}")
        order.next! unless order.state == "completed"
      elsif state == "rejected"
        payment.started_processing!
        payment.failure!
      end
    rescue Exception => e
      Rails.logger.error("Error al procesar pago orden #{order.number}: E -> #{e.message}")
      return false
    end
  end

Here's the error that I'm getting

StateMachines::InvalidTransition (Cannot transition state via :next from :payment (Reason(s): No payment found))


Solution

  • Actually, It was @Alex mentions. It's only neccesary the .netx! on the order. To do that. you need to ensure that you have a payment in "completed" state.