Skip to content

Commit

Permalink
Merge branch 'init-v4' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
smakani authored Feb 10, 2024
2 parents 1d08c8c + 8034047 commit 415bbea
Show file tree
Hide file tree
Showing 43 changed files with 1,366 additions and 73 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/rspec_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby

name: Tests

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.7', '3.0', '3.2']

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run tests
run: bundle exec rspec
3 changes: 2 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--colour --format documentation
--colour
--format progress
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
### Unreleased

- [#693](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/693) gemspec: add change,issue,source_code URL by [@mtmail](https://github.com/mtmail)
- [#700](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/700) update the balance API to respond with instant_available by [@iamnader](https://github.com/iamnader)
- [#687](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/687) Add PaymentIntent Webhooks by [@klaustopher](https://github.com/klaustopher)
- [#708](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/708) Implement Stripe::Checkout::Session.retrieve by [@coorasse](https://github.com/coorasse)
- [#711](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/711) Adding account link mock by [@amenon](https://github.com/amenon)
- [#715](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/715) Added application_fee_amount to mock charge object by [@espen](https://github.com/espen)
- [#694](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/694) Introduce ideal and sepa_debit types for PaymentMethod by [@mnin](https://github.com/mnin)
- [#720](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/720) Add additional parameters by [@rpietraszko](https://github.com/rpietraszko)
- [#695](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/695) Add `has_more` attribute to all ListObject instances by [@gbp](https://github.com/gbp)
- [#727](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/727) Fix List initialize error with deleted records by [@jmulieri](https://github.com/jmulieri)
- [#739](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/739) Add data to account links by [@dudyn5ky1](https://github.com/dudyn5ky1)
- [#756](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/756) Added #715 PR to changelog by [@espen](https://github.com/espen)
- [#759](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/759) Adding express dashboard login link mock by [@rohitbegani](https://github.com/rohitbegani)
- [#747](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/747) Fix ruby 2.7 deprecation warnings by [@coding-chimp](https://github.com/coding-chimp)
- [#762](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/762) Support Stripe Connect by adding stripe_account header namespace for customers by [@csalvato](https://github.com/csalvato)
- [#758](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/758) Create price by [@jamesprior](https://github.com/jamesprior)
- [#730](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/730) support on price api by [@hidenba](https://github.com/hidenba)
- [#764](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/764) Fixes erroneous error message when fetching upcoming invoices. by [@csalvato](https://github.com/csalvato)
- [#765](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/765) Properly set the status of a trialing subscription by [@csalvato](https://github.com/csalvato)
- [#755](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/755) Add allowed params to subscriptions by [@dominikdarnel](https://github.com/dominikdarnel)
- [#709](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/709) Remove unnecessary check on customer's currency by [@coorasse](https://github.com/coorasse)

- [#806](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/806) - Remove `payment_method_types` from required arguments for `Stripe::Checkout::Session`
- [#806](https://github.com/stripe-ruby-mock/stripe-ruby-mock/pull/806) - Raise more helpful exception when Stripe::Price cannot be found within a `Stripe::Checkout::Session` `line_items` argument.

Expand Down
5 changes: 0 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
source 'https://rubygems.org'

platforms :ruby_19 do
gem 'mime-types', '~> 2.6'
gem 'rest-client', '~> 1.8'
end

group :test do
gem 'rake'
gem 'dotenv'
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# stripe-ruby-mock [![Build Status](https://travis-ci.org/stripe-ruby-mock/stripe-ruby-mock.png?branch=master)](https://travis-ci.org/stripe-ruby-mock/stripe-ruby-mock) [![Gitter chat](https://badges.gitter.im/rebelidealist/stripe-ruby-mock.png)](https://gitter.im/rebelidealist/stripe-ruby-mock)
# stripe-ruby-mock [![Tests](https://github.com/stripe-ruby-mock/stripe-ruby-mock/actions/workflows/rspec_tests.yml/badge.svg)](https://github.com/stripe-ruby-mock/stripe-ruby-mock/actions/workflows/rspec_tests.yml)

* Homepage: https://github.com/stripe-ruby-mock/stripe-ruby-mock
* Issues: https://github.com/stripe-ruby-mock/stripe-ruby-mock/issues
Expand All @@ -12,7 +12,7 @@ This gem has unexpectedly grown in popularity and I've gotten pretty busy, so I'

In your gemfile:

gem 'stripe-ruby-mock', '~> 3.0.1', :require => 'stripe_mock'
gem 'stripe-ruby-mock', '~> 3.1.0', :require => 'stripe_mock'

## !!! Important

Expand All @@ -29,7 +29,7 @@ version `3.0.0` has [breaking changes](https://github.com/stripe-ruby-mock/strip

### Requirements

* ruby >= 2.4.0
* ruby >= 2.7.0
* stripe > 5 & < 11

### Specifications
Expand Down Expand Up @@ -406,6 +406,8 @@ Patches are welcome and greatly appreciated! If you're contributing to fix a pro
be sure to write tests that illustrate the problem being fixed.
This will help ensure that the problem remains fixed in future updates.

Note: You may need to `ulimit -n 4048` before running the test suite to get all tests to pass.

## Copyright

Copyright (c) 2013 Gilbert
Expand Down
2 changes: 2 additions & 0 deletions lib/stripe_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
require 'stripe_mock/request_handlers/helpers/card_helpers.rb'
require 'stripe_mock/request_handlers/helpers/charge_helpers.rb'
require 'stripe_mock/request_handlers/helpers/coupon_helpers.rb'
require 'stripe_mock/request_handlers/helpers/search_helpers.rb'
require 'stripe_mock/request_handlers/helpers/subscription_helpers.rb'
require 'stripe_mock/request_handlers/helpers/token_helpers.rb'

Expand All @@ -65,6 +66,7 @@
require 'stripe_mock/request_handlers/orders.rb'
require 'stripe_mock/request_handlers/plans.rb'
require 'stripe_mock/request_handlers/prices.rb'
require 'stripe_mock/request_handlers/promotion_codes.rb'
require 'stripe_mock/request_handlers/recipients.rb'
require 'stripe_mock/request_handlers/refunds.rb'
require 'stripe_mock/request_handlers/transfers.rb'
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe_mock/api/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def self.event_list
'payment_link.created',
'payment_link.updated',
'payment_method.attached',
'payment_method.detached',
'payout.created',
'payout.paid',
'payout.updated',
Expand All @@ -118,6 +119,8 @@ def self.event_list
'subscription_schedule.created',
'subscription_schedule.released',
'subscription_schedule.updated',
'tax_rate.created',
'tax_rate.updated',
'transfer.created',
'transfer.failed',
'transfer.paid',
Expand Down
46 changes: 43 additions & 3 deletions lib/stripe_mock/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ def self.mock_invoice(lines, params={})
next_payment_attempt: 1349825350,
charge: nil,
discount: nil,
subscription: nil
subscription: nil,
number: "6C41730-0001"
}.merge(params)
if invoice[:discount]
invoice[:total] = [0, invoice[:subtotal] - invoice[:discount][:coupon][:amount_off]].max if invoice[:discount][:coupon][:amount_off]
Expand Down Expand Up @@ -657,6 +658,44 @@ def self.mock_product(params={})
}.merge(params)
end

def self.mock_promotion_code(params={})
{
id: "mock_promo_abc123",
object: "promotion_code",
active: true,
code: "TESTCODE",
coupon: {
id: "mock_coupon_abc123",
object: "coupon",
amount_off: nil,
created: 1665773498,
currency: "usd",
duration: "repeating",
duration_in_months: 1,
livemode: false,
max_redemptions: nil,
metadata: {},
name: "Mock Coupon",
percent_off: 10.0,
redeem_by: nil,
times_redeemed: 0,
valid: true
},
created: 1665773499,
customer: nil,
expires_at: nil,
livemode: false,
max_redemptions: nil,
metadata: {},
restrictions: {
first_time_transaction: false,
minimum_amount: nil,
minimum_amount_currency: nil
},
times_redeemed: 0
}.merge(params)
end

def self.mock_recipient(cards, params={})
rp_id = params[:id] || "test_rp_default"
cards.each {|card| card[:recipient] = rp_id}
Expand Down Expand Up @@ -1259,9 +1298,10 @@ def self.mock_payment_method(params = {})
payment_method_id = params[:id] || 'pm_1ExEuFL2DI6wht39WNJgbybl'

type = params[:type].to_sym
last4 = params.dig(:card, :number)
data = {
card: {
brand: case params.dig(:card, :number)&.to_s
brand: case last4&.to_s
when /^4/, nil
'visa'
when /^5[1-5]/
Expand All @@ -1280,7 +1320,7 @@ def self.mock_payment_method(params = {})
fingerprint: 'Hr3Ly5z5IYxsokWA',
funding: 'credit',
generated_from: nil,
last4: params.dig(:card, :number)&.[](-4..) || '3155',
last4: last4.nil? ? '3155' : last4.to_s[-4..],
three_d_secure_usage: { supported: true },
wallet: nil
},
Expand Down
7 changes: 5 additions & 2 deletions lib/stripe_mock/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.handler_for_method_url(method_url)
include StripeMock::RequestHandlers::Plans
include StripeMock::RequestHandlers::Prices
include StripeMock::RequestHandlers::Products
include StripeMock::RequestHandlers::PromotionCodes
include StripeMock::RequestHandlers::Refunds
include StripeMock::RequestHandlers::Recipients
include StripeMock::RequestHandlers::Transfers
Expand All @@ -57,8 +58,9 @@ def self.handler_for_method_url(method_url)

attr_reader :accounts, :balance, :balance_transactions, :bank_tokens, :charges, :coupons, :customers,
:disputes, :events, :invoices, :invoice_items, :orders, :payment_intents, :payment_methods,
:setup_intents, :plans, :prices, :recipients, :refunds, :transfers, :payouts, :subscriptions, :country_spec,
:subscriptions_items, :products, :tax_rates, :checkout_sessions, :checkout_session_line_items
:setup_intents, :plans, :prices, :promotion_codes, :recipients, :refunds, :transfers, :payouts,
:subscriptions, :country_spec, :subscriptions_items, :products, :tax_rates, :checkout_sessions,
:checkout_session_line_items

attr_accessor :error_queue, :debug, :conversion_rate, :account_balance

Expand All @@ -83,6 +85,7 @@ def initialize
@plans = {}
@prices = {}
@products = {}
@promotion_codes = {}
@recipients = {}
@refunds = {}
@transfers = {}
Expand Down
23 changes: 17 additions & 6 deletions lib/stripe_mock/request_handlers/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ module Accounts
VALID_START_YEAR = 2009

def Accounts.included(klass)
klass.add_handler 'post /v1/accounts', :new_account
klass.add_handler 'get /v1/account', :get_account
klass.add_handler 'get /v1/accounts/(.*)', :get_account
klass.add_handler 'post /v1/accounts/(.*)', :update_account
klass.add_handler 'get /v1/accounts', :list_accounts
klass.add_handler 'post /oauth/deauthorize',:deauthorize
klass.add_handler 'post /v1/accounts', :new_account
klass.add_handler 'get /v1/account', :get_account
klass.add_handler 'get /v1/accounts/(.*)', :get_account
klass.add_handler 'post /v1/accounts/(.*)', :update_account
klass.add_handler 'get /v1/accounts', :list_accounts
klass.add_handler 'post /oauth/deauthorize', :deauthorize
klass.add_handler 'delete /v1/accounts/(.*)', :delete_account
end

def new_account(route, method_url, params, headers)
Expand Down Expand Up @@ -48,6 +49,16 @@ def deauthorize(route, method_url, params, headers)
Stripe::StripeObject.construct_from(:stripe_user_id => params[:stripe_user_id])
end

def delete_account(route, method_url, params, headers)
init_account
route =~ method_url
assert_existence :account, $1, accounts[$1]
accounts[$1] = {
id: accounts[$1][:id],
deleted: true
}
end

private

def init_account
Expand Down
21 changes: 20 additions & 1 deletion lib/stripe_mock/request_handlers/charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Charges
def Charges.included(klass)
klass.add_handler 'post /v1/charges', :new_charge
klass.add_handler 'get /v1/charges', :get_charges
klass.add_handler 'get /v1/charges/(.*)', :get_charge
klass.add_handler 'get /v1/charges/search', :search_charges
klass.add_handler 'get /v1/charges/((?!search).*)', :get_charge
klass.add_handler 'post /v1/charges/(.*)/capture', :capture_charge
klass.add_handler 'post /v1/charges/(.*)/refund', :refund_charge
klass.add_handler 'post /v1/charges/(.*)/refunds', :refund_charge
Expand Down Expand Up @@ -90,6 +91,24 @@ def get_charges(route, method_url, params, headers)
Data.mock_list_object(clone.values, params)
end

SEARCH_FIELDS = [
"amount",
"currency",
"customer",
"payment_method_details.card.brand",
"payment_method_details.card.exp_month",
"payment_method_details.card.exp_year",
"payment_method_details.card.fingerprint",
"payment_method_details.card.last4",
"status",
].freeze
def search_charges(route, method_url, params, headers)
require_param(:query) unless params[:query]

results = search_results(charges.values, params[:query], fields: SEARCH_FIELDS, resource_name: "charges")
Data.mock_list_object(results, params)
end

def get_charge(route, method_url, params, headers)
route =~ method_url
charge_id = $1 || params[:charge]
Expand Down
13 changes: 12 additions & 1 deletion lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ module Customers
def Customers.included(klass)
klass.add_handler 'post /v1/customers', :new_customer
klass.add_handler 'post /v1/customers/([^/]*)', :update_customer
klass.add_handler 'get /v1/customers/([^/]*)', :get_customer
klass.add_handler 'get /v1/customers/((?!search)[^/]*)', :get_customer
klass.add_handler 'delete /v1/customers/([^/]*)', :delete_customer
klass.add_handler 'get /v1/customers', :list_customers
klass.add_handler 'get /v1/customers/search', :search_customers
klass.add_handler 'delete /v1/customers/([^/]*)/discount', :delete_customer_discount
end

Expand Down Expand Up @@ -140,6 +141,16 @@ def list_customers(route, method_url, params, headers)
Data.mock_list_object(customers[stripe_account]&.values, params)
end

SEARCH_FIELDS = ["email", "name", "phone"].freeze
def search_customers(route, method_url, params, headers)
require_param(:query) unless params[:query]

stripe_account = headers && headers[:stripe_account] || Stripe.api_key
all_customers = customers[stripe_account]&.values
results = search_results(all_customers, params[:query], fields: SEARCH_FIELDS, resource_name: "customers")
Data.mock_list_object(results, params)
end

def delete_customer_discount(route, method_url, params, headers)
stripe_account = headers && headers[:stripe_account] || Stripe.api_key
route =~ method_url
Expand Down
1 change: 1 addition & 0 deletions lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def add_coupon_to_object(object, coupon)
attrs[:coupon] = coupon
attrs[:start] = Time.now.to_i
attrs[:end] = (DateTime.now >> coupon[:duration_in_months].to_i).to_time.to_i if coupon[:duration] == 'repeating'
attrs[:id] = new_id("di")
end

object[:discount] = Stripe::Discount.construct_from(discount_attrs)
Expand Down
Loading

0 comments on commit 415bbea

Please sign in to comment.