Skip to content
This repository
tree: 8cf7d9cf9d
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 105 lines (83 sloc) 3.016 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
module FakeBraintree
  class Subscription
    include Helpers

    def initialize(subscription_hash_from_params, options)
      @subscription_hash = subscription_hash_from_params.merge("merchant_id" => options[:merchant_id],
                                                               "id" => options[:id])
    end

    def create
      hash = subscription_hash
      create_subscription_with(hash)

      response_for_created_subscription(hash)
    end

    def update
      if subscription_exists_in_registry?
        updated_subscription = update_subscription!(subscription_hash)
        response_for_created_subscription(updated_subscription)
      else
        response_for_subscription_not_found
      end
    end

    def subscription_hash
      @subscription_hash["id"] ||= generate_new_subscription_id
      @subscription_hash["transactions"] = []
      @subscription_hash["add_ons"] = added_add_ons
      @subscription_hash["discounts"] = added_discounts
      @subscription_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now)
      @subscription_hash["status"] ||= active_status

      @subscription_hash
    end

    private

    def update_subscription!(updates)
      updated_subscription = subscription_from_registry.merge(updates)
      FakeBraintree.registry.subscriptions[subscription_id] = updated_subscription
    end

    def create_subscription_with(new_subscription_hash)
      FakeBraintree.registry.subscriptions[new_subscription_hash["id"]] = new_subscription_hash
    end

    def subscription_from_registry
      FakeBraintree.registry.subscriptions[subscription_id]
    end

    def subscription_exists_in_registry?
      FakeBraintree.registry.subscriptions.key?(subscription_id)
    end

    def braintree_formatted_date(date)
      date.strftime('%Y-%m-%d')
    end

    def added_add_ons
      if @subscription_hash["add_ons"].is_a?(Hash) && @subscription_hash["add_ons"]["add"]
        @subscription_hash["add_ons"]["add"].map { |add_on| { "id" => add_on["inherited_from_id"] } }
      else
        []
      end
    end

    def added_discounts
      if @subscription_hash["discounts"].is_a?(Hash) && @subscription_hash["discounts"]["add"]
        @subscription_hash["discounts"]["add"].map { |discount| { "id" => discount["inherited_from_id"] } }
      else
        []
      end
    end

    def subscription_id
      subscription_hash["id"]
    end

    def generate_new_subscription_id
      md5("#{payment_method_token}#{Time.now.to_f}")[0,6]
    end

    def payment_method_token
      @subscription_hash["payment_method_token"]
    end

    def active_status
      Braintree::Subscription::Status::Active
    end

    def response_for_created_subscription(hash)
      gzipped_response(201, hash.to_xml(:root => "subscription"))
    end

    def response_for_subscription_not_found
      gzipped_response(404, {})
    end

    def response_for_created_subscription(hash)
      gzipped_response(201, hash.to_xml(:root => "subscription"))
    end
  end
end
Something went wrong with that request. Please try again.