Class: StripeMock::TestStrategies::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe_mock/test_strategies/base.rb

Direct Known Subclasses

Live, Mock

Instance Method Summary collapse

Instance Method Details

#complete_checkout_session(session, payment_method) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/stripe_mock/test_strategies/base.rb', line 129

def complete_checkout_session(session, payment_method)
  session = session.is_a?(Stripe::Checkout::Session) ? session : Stripe::Checkout::Session.retrieve(session)
  payment_method = payment_method.is_a?(Stripe::PaymentMethod) ? payment_method : Stripe::PaymentMethod.retrieve(payment_method)
  case session.mode
  when "payment"
    Stripe::PaymentIntent.retrieve(session.payment_intent).confirm(payment_method: payment_method.id)
  when "setup"
    Stripe::SetupIntent.update(session.setup_intent, {payment_method: payment_method.id})
  when "subscription"
    line_items = Stripe::Checkout::Session.list_line_items(session.id)
    Stripe::Subscription.create({
      customer: session.customer,
      items: line_items.map do |line_item|
        {
          price: line_item.price.id,
          quantity: line_item.quantity
        }
      end,
      default_payment_method: payment_method.id
    })
  end
end

#create_checkout_session(params = {}) ⇒ Object



111
112
113
# File 'lib/stripe_mock/test_strategies/base.rb', line 111

def create_checkout_session(params = {})
  Stripe::Checkout::Session.create create_checkout_session_params(params)
end

#create_checkout_session_params(params = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stripe_mock/test_strategies/base.rb', line 115

def create_checkout_session_params(params = {})
  {
    payment_method_types: ['card'],
    line_items: params[:mode] == "setup" ? nil : [{
      name: 'T-shirt',
      quantity: 1,
      amount: 500,
      currency: 'usd',
    }],
    cancel_url: "https://example.com/cancel",
    success_url: "https://example.com/success",
  }.merge(params)
end

#create_coupon(params = {}) ⇒ Object



152
153
154
# File 'lib/stripe_mock/test_strategies/base.rb', line 152

def create_coupon(params = {})
  Stripe::Coupon.create create_coupon_params(params)
end

#create_coupon_params(params = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/stripe_mock/test_strategies/base.rb', line 87

def create_coupon_params(params = {})
  currency = params[:currency] || StripeMock.default_currency
  {
    id: '10BUCKS',
    amount_off: 1000,
    currency: currency,
    max_redemptions: 100,
    metadata: {
      created_by: 'admin_acct_1'
    },
    duration: 'once'
  }.merge(params)
end

#create_coupon_percent_of_params(params = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/stripe_mock/test_strategies/base.rb', line 101

def create_coupon_percent_of_params(params = {})
  {
    id: '25PERCENT',
    percent_off: 25,
    redeem_by: nil,
    duration_in_months: 3,
    duration: :repeating
  }.merge(params)
end

#create_plan(params = {}) ⇒ Object



28
29
30
# File 'lib/stripe_mock/test_strategies/base.rb', line 28

def create_plan(params = {})
  Stripe::Plan.create create_plan_params(params)
end

#create_plan_params(params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/stripe_mock/test_strategies/base.rb', line 32

def create_plan_params(params = {})
  {
    :id => 'stripe_mock_default_plan_id',
    :interval => 'month',
    :currency => StripeMock.default_currency,
    :product => nil, # need to override yourself to pass validations
    :amount => 1337
  }.merge(params)
end

#create_price(params = {}) ⇒ Object



42
43
44
# File 'lib/stripe_mock/test_strategies/base.rb', line 42

def create_price(params = {})
  Stripe::Price.create create_price_params(params)
end

#create_price_params(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/stripe_mock/test_strategies/base.rb', line 46

def create_price_params(params = {})
  price_params = {
    currency: StripeMock.default_currency,
  }.merge(params)
  unless price_params.key?(:product) || price_params.key?(:product_data)
    price_params[:product_data] = {
      name: 'Product created for price'
    }
  end
  price_params
end

#create_product(params = {}) ⇒ Object



9
10
11
# File 'lib/stripe_mock/test_strategies/base.rb', line 9

def create_product(params = {})
  Stripe::Product.create create_product_params(params)
end

#create_product_params(params = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/stripe_mock/test_strategies/base.rb', line 13

def create_product_params(params = {})
  {
    :id => 'stripe_mock_default_product_id',
    :name => 'Default Product',
  }.merge(params)
end

#delete_all_couponsObject



156
157
158
159
# File 'lib/stripe_mock/test_strategies/base.rb', line 156

def delete_all_coupons
  coupons = Stripe::Coupon.list
  coupons.data.map(&:delete) if coupons.data.count > 0
end

#generate_bank_token(bank_account_params = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/stripe_mock/test_strategies/base.rb', line 71

def generate_bank_token( = {})
  currency = [:currency] || StripeMock.default_currency
   = {
    :country => "US",
    :currency => currency,
    :account_holder_name => "Jane Austen",
    :account_holder_type => "individual",
    :routing_number => "110000000",
    :account_number => "000123456789"
  }.merge()
  [:fingerprint] = StripeMock::Util.fingerprint([:account_number]) if StripeMock.state == 'local'

  stripe_token = Stripe::Token.create(:bank_account => )
  stripe_token.id
end

#generate_card_token(card_params = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/stripe_mock/test_strategies/base.rb', line 62

def generate_card_token(card_params = {})
  card_data = {:number => "4242424242424242", :exp_month => 9, :exp_year => (Time.now.year + 5), :cvc => "999", :tokenization_method => nil}
  card = StripeMock::Util.card_merge(card_data, card_params)
  card[:fingerprint] = StripeMock::Util.fingerprint(card[:number]) if StripeMock.state == 'local'

  stripe_token = Stripe::Token.create(:card => card)
  stripe_token.id
end

#list_plans(limit) ⇒ Object



24
25
26
# File 'lib/stripe_mock/test_strategies/base.rb', line 24

def list_plans(limit)
  Stripe::Plan.list(limit: limit)
end

#list_products(limit) ⇒ Object



5
6
7
# File 'lib/stripe_mock/test_strategies/base.rb', line 5

def list_products(limit)
  Stripe::Product.list(limit: limit)
end

#list_subscriptions(limit) ⇒ Object



58
59
60
# File 'lib/stripe_mock/test_strategies/base.rb', line 58

def list_subscriptions(limit)
  Stripe::Subscription.list(limit: limit)
end

#prepare_card_errorObject



161
162
163
# File 'lib/stripe_mock/test_strategies/base.rb', line 161

def prepare_card_error
  StripeMock.prepare_card_error(:card_error, :new_customer) if StripeMock.state == 'local'
end

#retrieve_product(product_id) ⇒ Object



20
21
22
# File 'lib/stripe_mock/test_strategies/base.rb', line 20

def retrieve_product(product_id)
  Stripe::Product.retrieve(product_id)
end