Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/stripe_mock/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ def self.mock_balance_transaction(params = {})
created: 1461880226,
currency: currency,
description: nil,
exchange_rate: nil,
fee: 320,
fee_details: [
{
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe_mock/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def initialize
@dispute_counter = 0
@conversion_rate = 1.0
@account_balance = 10000
@account_currency = "usd"

# This is basically a cache for ParamValidators
@base_strategy = TestStrategies::Base.new
Expand Down Expand Up @@ -229,7 +230,9 @@ def new_balance_transaction(prefix, params = {})
calculate_fees(params) unless params[:fee]
params[:net] = amount - params[:fee]
params[:amount] = amount * @conversion_rate
params[:currency] = @account_currency
end
params[:exchange_rate] = @conversion_rate if @conversion_rate != 1.0
@balance_transactions[id] = Data.mock_balance_transaction(params.merge(id: id))
id
end
Expand Down
8 changes: 8 additions & 0 deletions lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ def get_customer(route, method_url, params, headers)
end
end

if params[:expand].is_a?(Array) && params[:expand].include?('invoice_credit_balance')
customer[:invoice_credit_balance] = if customer[:balance_transactions]
customer[:balance_transactions].group_by { |bt| bt[:currency] }.transform_values { |v| v.sum { |t| t[:amount] } }
else
{}
end
end

customer
end

Expand Down
11 changes: 10 additions & 1 deletion lib/stripe_mock/request_handlers/payment_intents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def get_payment_intent(route, method_url, params, headers)
payment_intent = assert_existence :payment_intent, payment_intent_id, payment_intents[payment_intent_id]

payment_intent = payment_intent.clone

if params[:expand].is_a?(Array) && params[:expand].include?('latest_charge')
payment_intent[:latest_charge] = charges.values.first
end

payment_intent
end

Expand Down Expand Up @@ -180,7 +185,11 @@ def last_payment_error_generator(code: nil, message: nil, decline_code: nil)

def succeeded_payment_intent(payment_intent)
payment_intent[:status] = 'succeeded'
btxn = new_balance_transaction('txn', { source: payment_intent[:id] })
btxn = new_balance_transaction('txn', {
source: payment_intent[:id],
amount: payment_intent[:amount],
currency: payment_intent[:currency]
})

charge_id = new_id('ch')

Expand Down
19 changes: 19 additions & 0 deletions spec/shared_stripe_examples/customer_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ def gen_card_tk
expect(customer.default_source).to be_a(Stripe::Card)
end

it "can expand invoice_credit_balance" do
original = Stripe::Customer.create({
email: 'johnny@appleseed.com',
source: gen_card_tk
})

Stripe::Customer.create_balance_transaction(
original.id,
{ amount: -100, currency: 'usd' }
)

customer = Stripe::Customer.retrieve(
id: original.id,
expand: ['invoice_credit_balance']
)

expect(customer.invoice_credit_balance.to_h).to eq({ usd: -100 })
end

it "cannot retrieve a customer that doesn't exist" do
expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
expect(e).to be_a Stripe::InvalidRequestError
Expand Down
6 changes: 6 additions & 0 deletions spec/shared_stripe_examples/payment_intent_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
expect(payment_intent.metadata.to_hash).to eq(original.metadata.to_hash)
end

it "can expand latest_charge" do
original = Stripe::PaymentIntent.create(amount: 100, currency: "usd", confirm: true)
payment_intent = Stripe::PaymentIntent.retrieve({ id: original.id, expand: ["latest_charge"] })
expect(payment_intent.latest_charge.object).to eq("charge")
end

it "cannot retrieve a payment_intent that doesn't exist" do
expect { Stripe::PaymentIntent.retrieve('nope') }.to raise_error {|e|
expect(e).to be_a Stripe::InvalidRequestError
Expand Down