<commit_before>module Spree::Fulfillment::Providers::Amazon
  class AmazonFulfillment < Spree::Fulfillment
    scope :refreshable, -> { joins(:shipment).where("spree_fulfillments.status in ('received','planning','processing') OR (spree_fulfillments.status = 'complete' AND spree_shipments.state in ('pending', 'ready'))") }    

    def handle_status
      case status
      when "processing"
        processing
      when "invalid", "unfulfillable"
        cannot_fulfill
      when "complete"
        complete
      when "complete_partialled"
        complete_partialled
      end
    end

    def refresh
      provider.refresh_fulfillment(self)
    end

    def cancellable?
      ["received", "planning"].include?(status)
    end
    
    def tracking_numbers
      @tracking_numbers ||= packages.map{|package| package[:tracking_number]}.compact.join(", ")
    end
    
    def sync_tracking_numbers
      if tracking_numbers
        shipment.tracking = tracking_numbers
        shipment.save
      end
    end

    protected

    def provider 
      Spree::FulfillmentConfig.amazon_provider
    end

    private

    def processing
      if fulfillment_data && fulfillment_data[:shipments] && fulfillment_data[:shipments].select{ |shipment| shipment[:status] == "shipped"}.any?
        capture_payments
        ship_shipment
      end
    end

    def capture_payments
      shipment.order.payments.each do |payment|
        payment.capture! if !["invalid", "failed", "completed"].include?(payment.state)
      end
    end

    def ship_shipment
      if shipment
        sync_tracking_numbers
        if shipment.state != "shipped"
          shipment.ready
          shipment.ship
        end
      end
    end

    def packages
      fulfillment_data[:shipments].map{|shipment| shipment[:packages]}.flatten.compact
    end

    def cannot_fulfill
      if !shipment.order.canceled?
        shipment.order.cancel
      end
    end

    def complete
      capture_payments
      ship_shipment
    end    

    def complete_partialled<commit_msg>Don't attempt to capture voided payments<commit_after>module Spree::Fulfillment::Providers::Amazon
  class AmazonFulfillment < Spree::Fulfillment
    scope :refreshable, -> { joins(:shipment).where("spree_fulfillments.status in ('received','planning','processing') OR (spree_fulfillments.status = 'complete' AND spree_shipments.state in ('pending', 'ready'))") }    

    def handle_status
      case status
      when "processing"
        processing
      when "invalid", "unfulfillable"
        cannot_fulfill
      when "complete"
        complete
      when "complete_partialled"
        complete_partialled
      end
    end

    def refresh
      provider.refresh_fulfillment(self)
    end

    def cancellable?
      ["received", "planning"].include?(status)
    end
    
    def tracking_numbers
      @tracking_numbers ||= packages.map{|package| package[:tracking_number]}.compact.join(", ")
    end
    
    def sync_tracking_numbers
      if tracking_numbers
        shipment.tracking = tracking_numbers
        shipment.save
      end
    end

    protected

    def provider 
      Spree::FulfillmentConfig.amazon_provider
    end

    private

    def processing
      if fulfillment_data && fulfillment_data[:shipments] && fulfillment_data[:shipments].select{ |shipment| shipment[:status] == "shipped"}.any?
        capture_payments
        ship_shipment
      end
    end

    def capture_payments
      shipment.order.payments.each do |payment|
        payment.capture! if !["invalid", "failed", "completed", "void"].include?(payment.state)
      end
    end

    def ship_shipment
      if shipment
        sync_tracking_numbers
        if shipment.state != "shipped"
          shipment.ready
          shipment.ship
        end
      end
    end

    def packages
      fulfillment_data[:shipments].map{|shipment| shipment[:packages]}.flatten.compact
    end

    def cannot_fulfill
      if !shipment.order.canceled?
        shipment.order.cancel
      end
    end

    def complete
      capture_payments
      ship_shipment
    end    

    def complete_partialled
{'fraction_non_alphanumeric': 0.08556925308194344, 'fraction_numerical': 0.0, 'mean_word_length': 2.5826839826839825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '375378', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1150, 'n_words': 253}