Skip to content

Commit

Permalink
Allow overwriting of the default currency
Browse files Browse the repository at this point in the history
In my application, when we create customers, we aren't specifying the
currency. Stripe's documentation for "Create a customer" doesn't state
that you can specify a currency, so I'm guessing that it is somehow
inferred.

As my application does not use USD, the check for matching currencies
that was added in 0bb51d1 broke my
tests.

Allowing to specify a default currency other than usd seemed like the
most straightforward approach to get this working.

I couldn't figure out a good approach to get it to work for the server
spec, as I can't easily overwrite the default for the server (as the
server is started once before all the specs). It should still work
though even if you are using the server. Due to my change having little
chance of introducing a regression, I decided to only make a spec for
the instance version.
  • Loading branch information
pwim committed May 1, 2018
1 parent c7508b1 commit 44cc06e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/stripe_mock.rb
Expand Up @@ -74,11 +74,13 @@

module StripeMock

@default_currency = 'usd'
lib_dir = File.expand_path(File.dirname(__FILE__), '../..')
@webhook_fixture_path = './spec/fixtures/stripe_webhooks/'
@webhook_fixture_fallback_path = File.join(lib_dir, 'stripe_mock/webhook_fixtures')

class << self
attr_accessor :default_currency
attr_accessor :webhook_fixture_path
end
end
31 changes: 16 additions & 15 deletions lib/stripe_mock/data.rb
Expand Up @@ -3,7 +3,7 @@ module Data

def self.mock_account(params = {})
id = params[:id] || 'acct_103ED82ePvKYlo2C'
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: id,
email: "bob@example.com",
Expand Down Expand Up @@ -103,7 +103,8 @@ def self.mock_account(params = {})

def self.mock_customer(sources, params)
cus_id = params[:id] || "test_cus_default"
currency = params[:currency] || 'usd'
puts @default_currency
currency = params[:currency] || StripeMock.default_currency
sources.each {|source| source[:customer] = cus_id}
{
email: 'stripe_mock@example.com',
Expand Down Expand Up @@ -134,7 +135,7 @@ def self.mock_customer(sources, params)

def self.mock_charge(params={})
charge_id = params[:id] || "ch_1fD6uiR9FAA2zc"
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: charge_id,
object: "charge",
Expand Down Expand Up @@ -196,7 +197,7 @@ def self.mock_charge(params={})
end

def self.mock_refund(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: "re_4fWhgUh5si7InF",
amount: 1,
Expand Down Expand Up @@ -248,7 +249,7 @@ def self.mock_card(params={})
end

def self.mock_bank_account(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: "test_ba_default",
object: "bank_account",
Expand Down Expand Up @@ -306,7 +307,7 @@ def self.mock_subscription(params={})
plan: {
amount: 999,
created: 1504035972,
currency: 'usd'
currency: StripeMock.default_currency
},
quantity: 1
}]
Expand All @@ -328,7 +329,7 @@ def self.mock_subscription(params={})

def self.mock_invoice(lines, params={})
in_id = params[:id] || "test_in_default"
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
lines << Data.mock_line_item() if lines.empty?
invoice = {
id: 'in_test_invoice',
Expand Down Expand Up @@ -379,7 +380,7 @@ def self.mock_invoice(lines, params={})
end

def self.mock_line_item(params = {})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: "ii_test",
object: "line_item",
Expand All @@ -402,7 +403,7 @@ def self.mock_line_item(params = {})
end

def self.mock_invoice_item(params = {})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: "test_ii",
object: "invoiceitem",
Expand Down Expand Up @@ -490,7 +491,7 @@ def self.mock_order_item(params={})
end

def self.mock_plan(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: "2",
object: "plan",
Expand Down Expand Up @@ -591,7 +592,7 @@ def self.mock_bank_account_token(params={})
end

def self.mock_transfer(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
id = params[:id] || 'tr_test_transfer'
{
:status => 'pending',
Expand Down Expand Up @@ -623,7 +624,7 @@ def self.mock_transfer(params={})
end

def self.mock_payout(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
id = params[:id] || 'po_test_payout'
{
:amount => 100,
Expand All @@ -647,7 +648,7 @@ def self.mock_disputes(ids=[])

def self.mock_dispute(params={})
@timestamp ||= Time.now.to_i
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
id = params[:id] || "dp_test_dispute"
{
:id => id,
Expand Down Expand Up @@ -967,7 +968,7 @@ def self.mock_balance_transactions(ids=[])
end

def self.mock_balance_transaction(params = {})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
bt_id = params[:id] || 'test_txn_default'
source = params[:source] || 'ch_test_charge'
{
Expand Down Expand Up @@ -1015,7 +1016,7 @@ def self.mock_subscription_item(params = {})
object: 'plan',
amount: 1337,
created: 1504716177,
currency: 'usd',
currency: StripeMock.default_currency,
interval: 'month',
interval_count: 1,
livemode: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/stripe_mock/request_handlers/invoices.rb
Expand Up @@ -166,9 +166,9 @@ def get_mock_subscription_line_item(subscription)
#anonymous charge
def invoice_charge(invoice)
begin
new_charge(nil, nil, {customer: invoice[:customer]["id"], amount: invoice[:amount_due], currency: 'usd'}, nil)
new_charge(nil, nil, {customer: invoice[:customer]["id"], amount: invoice[:amount_due], currency: StripeMock.default_currency}, nil)
rescue Stripe::InvalidRequestError
new_charge(nil, nil, {source: generate_card_token, amount: invoice[:amount_due], currency: 'usd'}, nil)
new_charge(nil, nil, {source: generate_card_token, amount: invoice[:amount_due], currency: StripeMock.default_currency}, nil)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/stripe_mock/test_strategies/base.rb
Expand Up @@ -3,7 +3,7 @@ module TestStrategies
class Base

def create_plan_params(params={})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
:id => 'stripe_mock_default_plan_id',
:name => 'StripeMock Default Plan ID',
Expand All @@ -23,7 +23,7 @@ def generate_card_token(card_params={})
end

def generate_bank_token(bank_account_params={})
currency = bank_account_params[:currency] || 'usd'
currency = bank_account_params[:currency] || StripeMock.default_currency
bank_account = {
:country => "US",
:currency => currency,
Expand All @@ -39,7 +39,7 @@ def generate_bank_token(bank_account_params={})
end

def create_coupon_params(params = {})
currency = params[:currency] || 'usd'
currency = params[:currency] || StripeMock.default_currency
{
id: '10BUCKS',
amount_off: 1000,
Expand Down
14 changes: 14 additions & 0 deletions spec/instance_spec.rb
Expand Up @@ -52,4 +52,18 @@ def test_data_source(type); StripeMock.instance.send(type); end
StripeMock.set_conversion_rate(1.25)
expect(StripeMock.instance.conversion_rate).to eq(1.25)
end

it "allows non-usd default currency" do
old_default_currency = StripeMock.default_currency
customer = begin
StripeMock.default_currency = "jpy"
Stripe::Customer.create({
email: 'johnny@appleseed.com',
source: stripe_helper.generate_card_token
})
ensure
StripeMock.default_currency = old_default_currency
end
expect(customer.currency).to eq("jpy")
end
end

0 comments on commit 44cc06e

Please sign in to comment.