stripe-ruby-mock 2.4.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +6 -4
- data/README.md +10 -4
- data/lib/stripe_mock/api/client.rb +3 -3
- data/lib/stripe_mock/api/errors.rb +25 -14
- data/lib/stripe_mock/api/instance.rb +4 -4
- data/lib/stripe_mock/api/webhooks.rb +1 -1
- data/lib/stripe_mock/client.rb +2 -2
- data/lib/stripe_mock/data.rb +51 -26
- data/lib/stripe_mock/instance.rb +7 -3
- data/lib/stripe_mock/request_handlers/charges.rb +6 -6
- data/lib/stripe_mock/request_handlers/coupons.rb +3 -2
- data/lib/stripe_mock/request_handlers/customers.rb +16 -7
- data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +2 -2
- data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +6 -1
- data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +5 -5
- data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -2
- data/lib/stripe_mock/request_handlers/invoices.rb +63 -11
- data/lib/stripe_mock/request_handlers/orders.rb +4 -4
- data/lib/stripe_mock/request_handlers/refunds.rb +3 -3
- data/lib/stripe_mock/request_handlers/subscriptions.rb +8 -8
- data/lib/stripe_mock/request_handlers/tokens.rb +1 -1
- data/lib/stripe_mock/request_handlers/transfers.rb +1 -1
- data/lib/stripe_mock/server.rb +1 -1
- data/lib/stripe_mock/version.rb +1 -1
- data/spec/instance_spec.rb +4 -4
- data/spec/integration_examples/prepare_error_examples.rb +6 -6
- data/spec/readme_spec.rb +2 -0
- data/spec/server_spec.rb +2 -2
- data/spec/shared_stripe_examples/card_examples.rb +3 -3
- data/spec/shared_stripe_examples/coupon_examples.rb +6 -0
- data/spec/shared_stripe_examples/customer_examples.rb +31 -3
- data/spec/shared_stripe_examples/dispute_examples.rb +9 -8
- data/spec/shared_stripe_examples/error_mock_examples.rb +3 -3
- data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
- data/spec/shared_stripe_examples/invoice_examples.rb +232 -48
- data/spec/shared_stripe_examples/recipient_examples.rb +7 -7
- data/spec/shared_stripe_examples/subscription_examples.rb +52 -16
- data/spec/shared_stripe_examples/transfer_examples.rb +8 -6
- data/spec/shared_stripe_examples/webhook_event_examples.rb +3 -3
- data/spec/spec_helper.rb +6 -5
- data/spec/stripe_mock_spec.rb +3 -3
- data/stripe-ruby-mock.gemspec +1 -1
- metadata +4 -10
| @@ -8,10 +8,10 @@ module StripeMock | |
| 8 8 | 
             
                    if card.nil?
         | 
| 9 9 | 
             
                      if class_name == 'Recipient'
         | 
| 10 10 | 
             
                        msg = "#{class_name} #{object[:id]} does not have a card with ID #{card_id}"
         | 
| 11 | 
            -
                        raise Stripe::InvalidRequestError.new(msg, 'card', 404)
         | 
| 11 | 
            +
                        raise Stripe::InvalidRequestError.new(msg, 'card', http_status: 404)
         | 
| 12 12 | 
             
                      else
         | 
| 13 13 | 
             
                        msg = "There is no source with ID #{card_id}"
         | 
| 14 | 
            -
                        raise Stripe::InvalidRequestError.new(msg, 'id', 404)
         | 
| 14 | 
            +
                        raise Stripe::InvalidRequestError.new(msg, 'id', http_status: 404)
         | 
| 15 15 | 
             
                      end
         | 
| 16 16 | 
             
                    end
         | 
| 17 17 | 
             
                    card
         | 
| @@ -3,7 +3,12 @@ module StripeMock | |
| 3 3 | 
             
                module Helpers
         | 
| 4 4 |  | 
| 5 5 | 
             
                  def add_coupon_to_customer(customer, coupon)
         | 
| 6 | 
            -
                    customer[:discount] = { | 
| 6 | 
            +
                    customer[:discount] = {
         | 
| 7 | 
            +
                        coupon: coupon,
         | 
| 8 | 
            +
                        customer: customer[:id],
         | 
| 9 | 
            +
                        start: Time.now.to_i,
         | 
| 10 | 
            +
                    }
         | 
| 11 | 
            +
                    customer[:discount][:end] = (DateTime.now >> coupon[:duration_in_months]).to_time.to_i  if coupon[:duration].to_sym == :repeating && coupon[:duration_in_months]
         | 
| 7 12 |  | 
| 8 13 | 
             
                    customer
         | 
| 9 14 | 
             
                  end
         | 
| @@ -14,7 +14,7 @@ module StripeMock | |
| 14 14 | 
             
                    params.merge! options.select {|k,v| k =~ /application_fee_percent|quantity|metadata|tax_percent/}
         | 
| 15 15 | 
             
                    # TODO: Implement coupon logic
         | 
| 16 16 |  | 
| 17 | 
            -
                    if (plan[:trial_period_days] | 
| 17 | 
            +
                    if ((plan[:trial_period_days]||0) == 0 && options[:trial_end].nil?) || options[:trial_end] == "now"
         | 
| 18 18 | 
             
                      end_time = get_ending_time(start_time, plan)
         | 
| 19 19 | 
             
                      params.merge!({status: 'active', current_period_end: end_time, trial_start: nil, trial_end: nil})
         | 
| 20 20 | 
             
                    else
         | 
| @@ -34,7 +34,7 @@ module StripeMock | |
| 34 34 | 
             
                    if cus[:currency].nil?
         | 
| 35 35 | 
             
                      cus[:currency] = sub[:plan][:currency]
         | 
| 36 36 | 
             
                    elsif cus[:currency] != sub[:plan][:currency]
         | 
| 37 | 
            -
                      raise Stripe::InvalidRequestError.new( "Can't combine currencies on a single customer. This customer has had a subscription, coupon, or invoice item with currency #{cus[:currency]}", 'currency', 400)
         | 
| 37 | 
            +
                      raise Stripe::InvalidRequestError.new( "Can't combine currencies on a single customer. This customer has had a subscription, coupon, or invoice item with currency #{cus[:currency]}", 'currency', http_status: 400)
         | 
| 38 38 | 
             
                    end
         | 
| 39 39 | 
             
                    cus[:subscriptions][:total_count] = (cus[:subscriptions][:total_count] || 0) + 1
         | 
| 40 40 | 
             
                    cus[:subscriptions][:data].unshift sub
         | 
| @@ -65,11 +65,11 @@ module StripeMock | |
| 65 65 | 
             
                  def verify_trial_end(trial_end)
         | 
| 66 66 | 
             
                    if trial_end != "now"
         | 
| 67 67 | 
             
                      if !trial_end.is_a? Integer
         | 
| 68 | 
            -
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer', nil, 400)
         | 
| 68 | 
            +
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer', nil, http_status: 400)
         | 
| 69 69 | 
             
                      elsif trial_end < Time.now.utc.to_i
         | 
| 70 | 
            -
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer Unix timestamp in the future', nil, 400)
         | 
| 70 | 
            +
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer Unix timestamp in the future', nil, http_status: 400)
         | 
| 71 71 | 
             
                      elsif trial_end > Time.now.utc.to_i + 31557600*5 # five years
         | 
| 72 | 
            -
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: can be no more than five years in the future', nil, 400)
         | 
| 72 | 
            +
                        raise Stripe::InvalidRequestError.new('Invalid timestamp: can be no more than five years in the future', nil, http_status: 400)
         | 
| 73 73 | 
             
                      end
         | 
| 74 74 | 
             
                    end
         | 
| 75 75 | 
             
                  end
         | 
| @@ -28,7 +28,7 @@ module StripeMock | |
| 28 28 | 
             
                    if token.nil? || @card_tokens[token].nil?
         | 
| 29 29 | 
             
                      # TODO: Make this strict
         | 
| 30 30 | 
             
                      msg = "Invalid token id: #{token}"
         | 
| 31 | 
            -
                      raise Stripe::InvalidRequestError.new(msg, 'tok', 404)
         | 
| 31 | 
            +
                      raise Stripe::InvalidRequestError.new(msg, 'tok', http_status: 404)
         | 
| 32 32 | 
             
                    else
         | 
| 33 33 | 
             
                      @card_tokens.delete(token)
         | 
| 34 34 | 
             
                    end
         | 
| @@ -36,7 +36,7 @@ module StripeMock | |
| 36 36 |  | 
| 37 37 | 
             
                  def get_card_or_bank_by_token(token)
         | 
| 38 38 | 
             
                    token_id = token['id'] || token
         | 
| 39 | 
            -
                     @card_tokens[token_id] || @bank_tokens[token_id] || raise(Stripe::InvalidRequestError.new("Invalid token id: #{token_id}", 'tok', 404))
         | 
| 39 | 
            +
                     @card_tokens[token_id] || @bank_tokens[token_id] || raise(Stripe::InvalidRequestError.new("Invalid token id: #{token_id}", 'tok', http_status: 404))
         | 
| 40 40 | 
             
                  end
         | 
| 41 41 |  | 
| 42 42 | 
             
                end
         | 
| @@ -58,24 +58,76 @@ module StripeMock | |
| 58 58 |  | 
| 59 59 | 
             
                  def upcoming_invoice(route, method_url, params, headers)
         | 
| 60 60 | 
             
                    route =~ method_url
         | 
| 61 | 
            -
                    raise Stripe::InvalidRequestError.new('Missing required param: customer', nil, 400) if params[:customer].nil?
         | 
| 61 | 
            +
                    raise Stripe::InvalidRequestError.new('Missing required param: customer', nil, http_status: 400) if params[:customer].nil?
         | 
| 62 | 
            +
                    raise Stripe::InvalidRequestError.new('When previewing changes to a subscription, you must specify either `subscription` or `subscription_items`', nil, http_status: 400) if !params[:subscription_proration_date].nil? && params[:subscription].nil? && params[:subscription_plan].nil?
         | 
| 63 | 
            +
                    raise Stripe::InvalidRequestError.new('Cannot specify proration date without specifying a subscription', nil, http_status: 400) if !params[:subscription_proration_date].nil? && params[:subscription].nil?
         | 
| 62 64 |  | 
| 63 65 | 
             
                    customer = customers[params[:customer]]
         | 
| 64 66 | 
             
                    assert_existence :customer, params[:customer], customer
         | 
| 65 67 |  | 
| 66 | 
            -
                    raise Stripe::InvalidRequestError.new("No upcoming invoices for customer: #{customer[:id]}", nil, 404) if customer[:subscriptions][:data].length == 0
         | 
| 68 | 
            +
                    raise Stripe::InvalidRequestError.new("No upcoming invoices for customer: #{customer[:id]}", nil, http_status: 404) if customer[:subscriptions][:data].length == 0
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    subscription =
         | 
| 71 | 
            +
                      if params[:subscription]
         | 
| 72 | 
            +
                        customer[:subscriptions][:data].select{|s|s[:id] == params[:subscription]}.first
         | 
| 73 | 
            +
                      else
         | 
| 74 | 
            +
                        customer[:subscriptions][:data].min_by { |sub| sub[:current_period_end] }
         | 
| 75 | 
            +
                      end
         | 
| 76 | 
            +
                    
         | 
| 77 | 
            +
                    if params[:subscription_proration_date] && !((subscription[:current_period_start]..subscription[:current_period_end]) === params[:subscription_proration_date])
         | 
| 78 | 
            +
                      raise Stripe::InvalidRequestError.new('Cannot specify proration date outside of current subscription period', nil, http_status: 400)
         | 
| 79 | 
            +
                    end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                    prorating = false
         | 
| 82 | 
            +
                    subscription_proration_date = nil
         | 
| 83 | 
            +
                    subscription_plan_id = params[:subscription_plan] || subscription[:plan][:id]
         | 
| 84 | 
            +
                    subscription_quantity = params[:subscription_quantity] || subscription[:quantity]
         | 
| 85 | 
            +
                    if subscription_plan_id != subscription[:plan][:id] || subscription_quantity != subscription[:quantity]
         | 
| 86 | 
            +
                      prorating = true
         | 
| 87 | 
            +
                      invoice_date = Time.now.to_i
         | 
| 88 | 
            +
                      subscription_plan = assert_existence :plan, subscription_plan_id, plans[subscription_plan_id.to_s]
         | 
| 89 | 
            +
                      preview_subscription = Data.mock_subscription
         | 
| 90 | 
            +
                      preview_subscription.merge!(custom_subscription_params(subscription_plan, customer, { trial_end: params[:subscription_trial_end] }))
         | 
| 91 | 
            +
                      preview_subscription[:id] = subscription[:id]
         | 
| 92 | 
            +
                      preview_subscription[:quantity] = subscription_quantity
         | 
| 93 | 
            +
                      subscription_proration_date = params[:subscription_proration_date] || Time.now
         | 
| 94 | 
            +
                    else
         | 
| 95 | 
            +
                      preview_subscription = subscription
         | 
| 96 | 
            +
                      invoice_date = subscription[:current_period_end]
         | 
| 97 | 
            +
                    end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                    invoice_lines = []
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                    if prorating
         | 
| 102 | 
            +
                      unused_amount = subscription[:plan][:amount] * subscription[:quantity] * (subscription[:current_period_end] - subscription_proration_date.to_i) / (subscription[:current_period_end] - subscription[:current_period_start])
         | 
| 103 | 
            +
                      invoice_lines << Data.mock_line_item(
         | 
| 104 | 
            +
                                               id: new_id('ii'),
         | 
| 105 | 
            +
                                               amount: -unused_amount,
         | 
| 106 | 
            +
                                               description: 'Unused time',
         | 
| 107 | 
            +
                                               plan: subscription[:plan],
         | 
| 108 | 
            +
                                               period: {
         | 
| 109 | 
            +
                                                   start: subscription_proration_date.to_i,
         | 
| 110 | 
            +
                                                   end: subscription[:current_period_end]
         | 
| 111 | 
            +
                                               },
         | 
| 112 | 
            +
                                               quantity: subscription[:quantity],
         | 
| 113 | 
            +
                                               proration: true
         | 
| 114 | 
            +
                      )
         | 
| 115 | 
            +
                    end
         | 
| 67 116 |  | 
| 68 | 
            -
                     | 
| 69 | 
            -
                     | 
| 117 | 
            +
                    subscription_line = get_mock_subscription_line_item(preview_subscription)
         | 
| 118 | 
            +
                    invoice_lines << subscription_line
         | 
| 70 119 |  | 
| 71 120 | 
             
                    id = new_id('in')
         | 
| 72 | 
            -
                    invoices[id] = Data.mock_invoice( | 
| 121 | 
            +
                    invoices[id] = Data.mock_invoice(invoice_lines,
         | 
| 73 122 | 
             
                      id: id,
         | 
| 74 123 | 
             
                      customer: customer[:id],
         | 
| 75 | 
            -
                       | 
| 76 | 
            -
                       | 
| 77 | 
            -
                       | 
| 78 | 
            -
                       | 
| 124 | 
            +
                      discount: customer[:discount],
         | 
| 125 | 
            +
                      date: invoice_date,
         | 
| 126 | 
            +
                      starting_balance: customer[:account_balance],
         | 
| 127 | 
            +
                      subscription: preview_subscription[:id],
         | 
| 128 | 
            +
                      period_start: prorating ? invoice_date : preview_subscription[:current_period_start],
         | 
| 129 | 
            +
                      period_end: prorating ? invoice_date : preview_subscription[:current_period_end],
         | 
| 130 | 
            +
                      next_payment_attempt: preview_subscription[:current_period_end] + 3600 )
         | 
| 79 131 | 
             
                  end
         | 
| 80 132 |  | 
| 81 133 | 
             
                  private
         | 
| @@ -85,9 +137,9 @@ module StripeMock | |
| 85 137 | 
             
                      id: subscription[:id],
         | 
| 86 138 | 
             
                      type: "subscription",
         | 
| 87 139 | 
             
                      plan: subscription[:plan],
         | 
| 88 | 
            -
                      amount: subscription[:plan][:amount],
         | 
| 140 | 
            +
                      amount: subscription[:status] == 'trialing' ? 0 : subscription[:plan][:amount] * subscription[:quantity],
         | 
| 89 141 | 
             
                      discountable: true,
         | 
| 90 | 
            -
                      quantity:  | 
| 142 | 
            +
                      quantity: subscription[:quantity],
         | 
| 91 143 | 
             
                      period: {
         | 
| 92 144 | 
             
                        start: subscription[:current_period_end],
         | 
| 93 145 | 
             
                        end: get_ending_time(subscription[:current_period_start], subscription[:plan], 2)
         | 
| @@ -15,16 +15,16 @@ module StripeMock | |
| 15 15 | 
             
                    order_items = []
         | 
| 16 16 |  | 
| 17 17 | 
             
                    unless params[:currency].to_s.size == 3
         | 
| 18 | 
            -
                      raise Stripe::InvalidRequestError.new('You must supply a currency', nil, 400)
         | 
| 18 | 
            +
                      raise Stripe::InvalidRequestError.new('You must supply a currency', nil, http_status: 400)
         | 
| 19 19 | 
             
                    end
         | 
| 20 20 |  | 
| 21 21 | 
             
                    if params[:items]
         | 
| 22 22 | 
             
                      unless params[:items].is_a? Array
         | 
| 23 | 
            -
                        raise Stripe::InvalidRequestError.new('You must supply a list of items', nil, 400)
         | 
| 23 | 
            +
                        raise Stripe::InvalidRequestError.new('You must supply a list of items', nil, http_status: 400)
         | 
| 24 24 | 
             
                      end
         | 
| 25 25 |  | 
| 26 26 | 
             
                      unless params[:items].first.is_a? Hash
         | 
| 27 | 
            -
                        raise Stripe::InvalidRequestError.new('You must supply an item', nil, 400)
         | 
| 27 | 
            +
                        raise Stripe::InvalidRequestError.new('You must supply an item', nil, http_status: 400)
         | 
| 28 28 | 
             
                      end
         | 
| 29 29 | 
             
                    end
         | 
| 30 30 |  | 
| @@ -61,7 +61,7 @@ module StripeMock | |
| 61 61 | 
             
                    order = assert_existence :order, $1, orders[$1]
         | 
| 62 62 |  | 
| 63 63 | 
             
                    if params[:source].blank? && params[:customer].blank?
         | 
| 64 | 
            -
                      raise Stripe::InvalidRequestError.new('You must supply a source or customer', nil, 400)
         | 
| 64 | 
            +
                      raise Stripe::InvalidRequestError.new('You must supply a source or customer', nil, http_status: 400)
         | 
| 65 65 | 
             
                    end
         | 
| 66 66 |  | 
| 67 67 | 
             
                    charge_id = new_id('ch')
         | 
| @@ -47,7 +47,7 @@ module StripeMock | |
| 47 47 | 
             
                    allowed = allowed_refund_params(params)
         | 
| 48 48 | 
             
                    disallowed = params.keys - allowed
         | 
| 49 49 | 
             
                    if disallowed.count > 0
         | 
| 50 | 
            -
                      raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', 400)
         | 
| 50 | 
            +
                      raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', http_status: 400)
         | 
| 51 51 | 
             
                    end
         | 
| 52 52 |  | 
| 53 53 | 
             
                    refunds[id] = Util.rmerge(refund, params)
         | 
| @@ -72,9 +72,9 @@ module StripeMock | |
| 72 72 |  | 
| 73 73 | 
             
                  def ensure_refund_required_params(params)
         | 
| 74 74 | 
             
                    if non_integer_charge_amount?(params)
         | 
| 75 | 
            -
                      raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', 400)
         | 
| 75 | 
            +
                      raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
         | 
| 76 76 | 
             
                    elsif non_positive_charge_amount?(params)
         | 
| 77 | 
            -
                      raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', 400)
         | 
| 77 | 
            +
                      raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
         | 
| 78 78 | 
             
                    elsif params[:charge].nil?
         | 
| 79 79 | 
             
                      raise Stripe::InvalidRequestError.new('Must provide the identifier of the charge to refund.', nil)
         | 
| 80 80 | 
             
                    end
         | 
| @@ -63,7 +63,7 @@ module StripeMock | |
| 63 63 | 
             
                      if coupon
         | 
| 64 64 | 
             
                        subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
         | 
| 65 65 | 
             
                      else
         | 
| 66 | 
            -
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
         | 
| 66 | 
            +
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
         | 
| 67 67 | 
             
                      end
         | 
| 68 68 | 
             
                    end
         | 
| 69 69 |  | 
| @@ -89,10 +89,10 @@ module StripeMock | |
| 89 89 | 
             
                      customer[:default_source] = new_card[:id]
         | 
| 90 90 | 
             
                    end
         | 
| 91 91 |  | 
| 92 | 
            -
                    allowed_params = %w(customer application_fee_percent coupon items metadata plan quantity source tax_percent trial_end trial_period_days)
         | 
| 92 | 
            +
                    allowed_params = %w(customer application_fee_percent coupon items metadata plan quantity source tax_percent trial_end trial_period_days current_period_start)
         | 
| 93 93 | 
             
                    unknown_params = params.keys - allowed_params.map(&:to_sym)
         | 
| 94 94 | 
             
                    if unknown_params.length > 0
         | 
| 95 | 
            -
                      raise Stripe::InvalidRequestError.new("Received unknown parameter: #{unknown_params.join}", unknown_params.first.to_s, 400)
         | 
| 95 | 
            +
                      raise Stripe::InvalidRequestError.new("Received unknown parameter: #{unknown_params.join}", unknown_params.first.to_s, http_status: 400)
         | 
| 96 96 | 
             
                    end
         | 
| 97 97 |  | 
| 98 98 | 
             
                    subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) })
         | 
| @@ -112,7 +112,7 @@ module StripeMock | |
| 112 112 | 
             
                      if coupon
         | 
| 113 113 | 
             
                        subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
         | 
| 114 114 | 
             
                      else
         | 
| 115 | 
            -
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
         | 
| 115 | 
            +
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
         | 
| 116 116 | 
             
                      end
         | 
| 117 117 | 
             
                    end
         | 
| 118 118 |  | 
| @@ -170,7 +170,7 @@ module StripeMock | |
| 170 170 | 
             
                      elsif coupon_id == ""
         | 
| 171 171 | 
             
                        subscription[:discount] = Stripe::Util.convert_to_stripe_object(nil, {})
         | 
| 172 172 | 
             
                      else
         | 
| 173 | 
            -
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400)
         | 
| 173 | 
            +
                        raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', http_status: 400)
         | 
| 174 174 | 
             
                      end
         | 
| 175 175 | 
             
                    end
         | 
| 176 176 |  | 
| @@ -224,8 +224,8 @@ module StripeMock | |
| 224 224 | 
             
                  private
         | 
| 225 225 |  | 
| 226 226 | 
             
                  def verify_card_present(customer, plan, subscription, params={})
         | 
| 227 | 
            -
                    if customer[:default_source].nil? && customer[:trial_end].nil? && plan[:trial_period_days] | 
| 228 | 
            -
                      raise Stripe::InvalidRequestError.new('You must supply a valid card xoxo', nil, 400)
         | 
| 227 | 
            +
                    if customer[:default_source].nil? && customer[:trial_end].nil? && (plan[:trial_period_days]||0)==0 && plan[:amount] != 0 && plan[:trial_end].nil? && params[:trial_end].nil? && (subscription.nil? || subscription[:trial_end].nil? || subscription[:trial_end] == 'now')
         | 
| 228 | 
            +
                      raise Stripe::InvalidRequestError.new('You must supply a valid card xoxo', nil, http_status: 400)
         | 
| 229 229 | 
             
                    end
         | 
| 230 230 | 
             
                  end
         | 
| 231 231 |  | 
| @@ -234,7 +234,7 @@ module StripeMock | |
| 234 234 |  | 
| 235 235 | 
             
                    if status == 'canceled'
         | 
| 236 236 | 
             
                      message = "No such subscription: #{id}"
         | 
| 237 | 
            -
                      raise Stripe::InvalidRequestError.new(message, 'subscription', 404)
         | 
| 237 | 
            +
                      raise Stripe::InvalidRequestError.new(message, 'subscription', http_status: 404)
         | 
| 238 238 | 
             
                    end
         | 
| 239 239 | 
             
                  end
         | 
| 240 240 | 
             
                end
         | 
| @@ -9,7 +9,7 @@ module StripeMock | |
| 9 9 |  | 
| 10 10 | 
             
                  def create_token(route, method_url, params, headers)
         | 
| 11 11 | 
             
                    if params[:customer].nil? && params[:card].nil? && params[:bank_account].nil?
         | 
| 12 | 
            -
                      raise Stripe::InvalidRequestError.new('You must supply either a card, customer, or bank account to create a token.', nil, 400)
         | 
| 12 | 
            +
                      raise Stripe::InvalidRequestError.new('You must supply either a card, customer, or bank account to create a token.', nil, http_status: 400)
         | 
| 13 13 | 
             
                    end
         | 
| 14 14 |  | 
| 15 15 | 
             
                    cus_id = params[:customer]
         | 
| @@ -36,7 +36,7 @@ module StripeMock | |
| 36 36 | 
             
                    end
         | 
| 37 37 |  | 
| 38 38 | 
             
                    unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
         | 
| 39 | 
            -
                      raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', 400)
         | 
| 39 | 
            +
                      raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
         | 
| 40 40 | 
             
                    end
         | 
| 41 41 |  | 
| 42 42 | 
             
                    transfers[id] = Data.mock_transfer(params.merge :id => id)
         | 
    
        data/lib/stripe_mock/server.rb
    CHANGED
    
    | @@ -23,7 +23,7 @@ module StripeMock | |
| 23 23 | 
             
                    {
         | 
| 24 24 | 
             
                      :error_raised => 'invalid_request',
         | 
| 25 25 | 
             
                      :error_params => [
         | 
| 26 | 
            -
                        e.message, e.param, e.http_status, e.http_body, e.json_body
         | 
| 26 | 
            +
                        e.message, e.param, { http_status: e.http_status, http_body: e.http_body, json_body: e.json_body}
         | 
| 27 27 | 
             
                      ]
         | 
| 28 28 | 
             
                    }
         | 
| 29 29 | 
             
                  end
         | 
    
        data/lib/stripe_mock/version.rb
    CHANGED
    
    
    
        data/spec/instance_spec.rb
    CHANGED
    
    | @@ -17,9 +17,9 @@ describe StripeMock::Instance do | |
| 17 17 | 
             
                  "id" => "str_abcde",
         | 
| 18 18 | 
             
                  :name => "String Plan"
         | 
| 19 19 | 
             
                )
         | 
| 20 | 
            -
                res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', 'api_key', string_params)
         | 
| 21 | 
            -
                expect(res[:id]).to eq('str_abcde')
         | 
| 22 | 
            -
                expect(res[:name]).to eq('String Plan')
         | 
| 20 | 
            +
                res, api_key = StripeMock.instance.mock_request('post', '/v1/plans', api_key: 'api_key', params: string_params)
         | 
| 21 | 
            +
                expect(res.data[:id]).to eq('str_abcde')
         | 
| 22 | 
            +
                expect(res.data[:name]).to eq('String Plan')
         | 
| 23 23 | 
             
              end
         | 
| 24 24 |  | 
| 25 25 | 
             
              it "exits gracefully on an unrecognized handler url" do
         | 
| @@ -28,7 +28,7 @@ describe StripeMock::Instance do | |
| 28 28 | 
             
                  "name" => "PLAN"
         | 
| 29 29 | 
             
                }
         | 
| 30 30 |  | 
| 31 | 
            -
                expect { res, api_key = StripeMock.instance.mock_request('post', '/v1/unrecongnized_method', 'api_key', dummy_params) }.to_not raise_error
         | 
| 31 | 
            +
                expect { res, api_key = StripeMock.instance.mock_request('post', '/v1/unrecongnized_method', api_key: 'api_key', params: dummy_params) }.to_not raise_error
         | 
| 32 32 | 
             
              end
         | 
| 33 33 |  | 
| 34 34 | 
             
              it "can toggle debug" do
         | 
| @@ -26,13 +26,13 @@ shared_examples 'Card Error Prep' do | |
| 26 26 | 
             
                  )
         | 
| 27 27 | 
             
                rescue Stripe::CardError => e
         | 
| 28 28 | 
             
                  body = e.json_body
         | 
| 29 | 
            -
                  err  = body[:error]
         | 
| 30 | 
            -
             | 
| 31 29 | 
             
                  expect(body).to be_a(Hash)
         | 
| 32 | 
            -
                   | 
| 33 | 
            -
             | 
| 34 | 
            -
                  expect( | 
| 35 | 
            -
                  expect( | 
| 30 | 
            +
                  error = body[:error]
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  expect(error[:type]).to eq 'card_error'
         | 
| 33 | 
            +
                  expect(error[:param]).to eq 'number'
         | 
| 34 | 
            +
                  expect(error[:code]).to eq 'invalid_number'
         | 
| 35 | 
            +
                  expect(error[:message]).to eq 'The card number is not a valid credit card number.'
         | 
| 36 36 | 
             
                end
         | 
| 37 37 | 
             
              end
         | 
| 38 38 | 
             
            end
         | 
    
        data/spec/readme_spec.rb
    CHANGED
    
    | @@ -1,3 +1,4 @@ | |
| 1 | 
            +
            require 'stripe_mock'
         | 
| 1 2 |  | 
| 2 3 | 
             
            describe 'README examples' do
         | 
| 3 4 | 
             
              let(:stripe_helper) { StripeMock.create_test_helper }
         | 
| @@ -24,6 +25,7 @@ describe 'README examples' do | |
| 24 25 | 
             
                  expect(e).to be_a Stripe::CardError
         | 
| 25 26 | 
             
                  expect(e.http_status).to eq(402)
         | 
| 26 27 | 
             
                  expect(e.code).to eq('card_declined')
         | 
| 28 | 
            +
                  expect(e.json_body[:error][:decline_code]).to eq('do_not_honor')
         | 
| 27 29 | 
             
                }
         | 
| 28 30 | 
             
              end
         | 
| 29 31 |  | 
    
        data/spec/server_spec.rb
    CHANGED
    
    | @@ -52,8 +52,8 @@ describe 'StripeMock Server', :mock_server => true do | |
| 52 52 |  | 
| 53 53 | 
             
              it "returns a response with symbolized hash keys" do
         | 
| 54 54 | 
             
                stripe_helper.create_plan(id: 'x')
         | 
| 55 | 
            -
                response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', 'xxx')
         | 
| 56 | 
            -
                response.keys.each {|k| expect(k).to be_a(Symbol) }
         | 
| 55 | 
            +
                response, api_key = StripeMock.redirect_to_mock_server('get', '/v1/plans/x', api_key: 'xxx')
         | 
| 56 | 
            +
                response.data.keys.each {|k| expect(k).to be_a(Symbol) }
         | 
| 57 57 | 
             
              end
         | 
| 58 58 |  | 
| 59 59 |  | 
| @@ -21,7 +21,7 @@ shared_examples 'Card API' do | |
| 21 21 | 
             
                expect(card.exp_year).to eq(2099)
         | 
| 22 22 | 
             
              end
         | 
| 23 23 |  | 
| 24 | 
            -
              it 'creates/returns a card when using recipient.cards.create given a card token' do
         | 
| 24 | 
            +
              it 'creates/returns a card when using recipient.cards.create given a card token', skip: 'Stripe has deprecated Recipients' do
         | 
| 25 25 | 
             
                params = {
         | 
| 26 26 | 
             
                  id: 'test_recipient_sub',
         | 
| 27 27 | 
             
                  name: 'MyRec',
         | 
| @@ -69,7 +69,7 @@ shared_examples 'Card API' do | |
| 69 69 | 
             
                expect(card.exp_year).to eq(3031)
         | 
| 70 70 | 
             
              end
         | 
| 71 71 |  | 
| 72 | 
            -
              it 'creates/returns a card when using recipient.cards.create given card params' do
         | 
| 72 | 
            +
              it 'creates/returns a card when using recipient.cards.create given card params', skip: 'Stripe has deprecated Recipients' do
         | 
| 73 73 | 
             
                params = {
         | 
| 74 74 | 
             
                  id: 'test_recipient_sub',
         | 
| 75 75 | 
             
                  name: 'MyRec',
         | 
| @@ -192,7 +192,7 @@ shared_examples 'Card API' do | |
| 192 192 | 
             
                end
         | 
| 193 193 | 
             
              end
         | 
| 194 194 |  | 
| 195 | 
            -
              describe "retrieval and deletion with recipients", :live => true do
         | 
| 195 | 
            +
              describe "retrieval and deletion with recipients", :live => true, skip: 'Stripe has deprecated Recipients' do
         | 
| 196 196 | 
             
                let!(:recipient) { Stripe::Recipient.create(name: 'Test Recipient', type: 'individual') }
         | 
| 197 197 | 
             
                let!(:card_token) { stripe_helper.generate_card_token(number: "4000056655665556") }
         | 
| 198 198 | 
             
                let!(:card) { recipient.cards.create(card: card_token) }
         | 
| @@ -27,6 +27,12 @@ shared_examples 'Coupon API' do | |
| 27 27 | 
             
                             expect(e.message).to match /duration/
         | 
| 28 28 | 
             
                         }
         | 
| 29 29 | 
             
                end
         | 
| 30 | 
            +
                it 'fails when a coupon is created without a currency when amount_off is specified' do
         | 
| 31 | 
            +
                  expect { Stripe::Coupon.create(id: '10OFF', duration: 'once', amount_off: 1000) }.to raise_error {|e|
         | 
| 32 | 
            +
                    expect(e).to be_a(Stripe::InvalidRequestError)
         | 
| 33 | 
            +
                    expect(e.message).to match /You must pass currency when passing amount_off/
         | 
| 34 | 
            +
                  }
         | 
| 35 | 
            +
                end
         | 
| 30 36 | 
             
              end
         | 
| 31 37 |  | 
| 32 38 | 
             
              context 'retrieve coupon', live: true do
         | 
| @@ -215,6 +215,21 @@ shared_examples 'Customer API' do | |
| 215 215 | 
             
                customer = Stripe::Customer.retrieve('test_cus_coupon')
         | 
| 216 216 | 
             
                expect(customer.discount).to_not be_nil
         | 
| 217 217 | 
             
                expect(customer.discount.coupon).to_not be_nil
         | 
| 218 | 
            +
                expect(customer.discount.customer).to eq customer.id
         | 
| 219 | 
            +
                expect(customer.discount.start).to be_within(1).of Time.now.to_i
         | 
| 220 | 
            +
              end
         | 
| 221 | 
            +
             | 
| 222 | 
            +
              describe 'repeating coupon with duration limit', live: true do
         | 
| 223 | 
            +
                let!(:coupon) { Stripe::Coupon.create(id: '10OFF', amount_off: 1000, currency: 'usd', duration: 'repeating', duration_in_months: 12) }
         | 
| 224 | 
            +
                let!(:customer) { Stripe::Customer.create(coupon: '10OFF') }
         | 
| 225 | 
            +
                it 'creates the discount with the end date', live: true do
         | 
| 226 | 
            +
                  discount = Stripe::Customer.retrieve(customer.id).discount
         | 
| 227 | 
            +
                  expect(discount).to_not be_nil
         | 
| 228 | 
            +
                  expect(discount.coupon).to_not be_nil
         | 
| 229 | 
            +
                  expect(discount.end).to be_within(1).of (Time.now + 365 * 24 * 3600).to_i
         | 
| 230 | 
            +
                end
         | 
| 231 | 
            +
                after { Stripe::Coupon.retrieve(coupon.id).delete }
         | 
| 232 | 
            +
                after { Stripe::Customer.retrieve(customer.id).delete }
         | 
| 218 233 | 
             
              end
         | 
| 219 234 |  | 
| 220 235 | 
             
              it 'cannot create a customer with a coupon that does not exist' do
         | 
| @@ -253,10 +268,23 @@ shared_examples 'Customer API' do | |
| 253 268 | 
             
                expect(customer.id).to eq(original.id)
         | 
| 254 269 | 
             
                expect(customer.email).to eq(original.email)
         | 
| 255 270 | 
             
                expect(customer.default_source).to eq(original.default_source)
         | 
| 271 | 
            +
                expect(customer.default_source).not_to be_a(Stripe::Card)
         | 
| 256 272 | 
             
                expect(customer.subscriptions.count).to eq(0)
         | 
| 257 273 | 
             
                expect(customer.subscriptions.data).to be_empty
         | 
| 258 274 | 
             
              end
         | 
| 259 275 |  | 
| 276 | 
            +
              it "can expand default_source" do
         | 
| 277 | 
            +
                original = Stripe::Customer.create({
         | 
| 278 | 
            +
                  email: 'johnny@appleseed.com',
         | 
| 279 | 
            +
                  source: gen_card_tk
         | 
| 280 | 
            +
                })
         | 
| 281 | 
            +
                customer = Stripe::Customer.retrieve(
         | 
| 282 | 
            +
                  id: original.id,
         | 
| 283 | 
            +
                  expand: ['default_source']
         | 
| 284 | 
            +
                )
         | 
| 285 | 
            +
                expect(customer.default_source).to be_a(Stripe::Card)
         | 
| 286 | 
            +
              end
         | 
| 287 | 
            +
             | 
| 260 288 | 
             
              it "cannot retrieve a customer that doesn't exist" do
         | 
| 261 289 | 
             
                expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
         | 
| 262 290 | 
             
                  expect(e).to be_a Stripe::InvalidRequestError
         | 
| @@ -378,7 +406,7 @@ shared_examples 'Customer API' do | |
| 378 406 | 
             
                customer = customer.delete
         | 
| 379 407 | 
             
                expect(customer.deleted).to eq(true)
         | 
| 380 408 | 
             
              end
         | 
| 381 | 
            -
             | 
| 409 | 
            +
             | 
| 382 410 | 
             
              it 'works with the update_subscription method' do
         | 
| 383 411 | 
             
                stripe_helper.create_plan(id: 'silver')
         | 
| 384 412 | 
             
                cus   = Stripe::Customer.create(source: gen_card_tk)
         | 
| @@ -386,7 +414,7 @@ shared_examples 'Customer API' do | |
| 386 414 | 
             
                  cus.update_subscription(plan: 'silver')
         | 
| 387 415 | 
             
                }.not_to raise_error
         | 
| 388 416 | 
             
              end
         | 
| 389 | 
            -
             | 
| 417 | 
            +
             | 
| 390 418 | 
             
              it "deletes a stripe customer discount" do
         | 
| 391 419 | 
             
                original = Stripe::Customer.create(id: 'test_customer_update')
         | 
| 392 420 |  | 
| @@ -395,7 +423,7 @@ shared_examples 'Customer API' do | |
| 395 423 | 
             
                original.save
         | 
| 396 424 |  | 
| 397 425 | 
             
                expect(original.discount.coupon).to be_a Stripe::Coupon
         | 
| 398 | 
            -
             | 
| 426 | 
            +
             | 
| 399 427 | 
             
                original.delete_discount
         | 
| 400 428 |  | 
| 401 429 | 
             
                customer = Stripe::Customer.retrieve("test_customer_update")
         |