Class: Killbill::Stripe::PaymentPlugin

Inherits:
Plugin::ActiveMerchant::PaymentPlugin
  • Object
show all
Defined in:
lib/stripe/api.rb

Instance Method Summary collapse

Constructor Details

#initializePaymentPlugin

Returns a new instance of PaymentPlugin.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/stripe/api.rb', line 5

def initialize
  gateway_builder = Proc.new do |config|
    ::ActiveMerchant::Billing::StripeGateway.new :login => config[:api_secret_key]
  end

  super(gateway_builder,
        :stripe,
        ::Killbill::Stripe::StripePaymentMethod,
        ::Killbill::Stripe::StripeTransaction,
        ::Killbill::Stripe::StripeResponse)
end

Instance Method Details

#add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/stripe/api.rb', line 99

def add_payment_method(, kb_payment_method_id, payment_method_props, set_default, properties, context)
  # Do we have a customer for that account already?
  stripe_customer_id = find_value_from_properties(payment_method_props.properties, :customer) || StripePaymentMethod.(, context.tenant_id)
  email = find_value_from_properties(payment_method_props.properties, :email) || @kb_apis..(, @kb_apis.create_context(context.tenant_id)).email

  # Pass extra parameters for the gateway here
  options = {
      :email => email,
      # This will either update the current customer if present, or create a new one
      :customer => stripe_customer_id,
      # Magic field, see also private_api.rb (works only when creating an account)
      :description => 
  }

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, payment_method_props, set_default, properties, context)
end

#authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/stripe/api.rb', line 25

def authorize_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  pm = @payment_method_model.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id)

  options = {}
  populate_defaults(pm, amount, currency, properties, context, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/stripe/api.rb', line 174

def build_form_descriptor(, descriptor_fields, properties, context)
  # Pass extra parameters for the gateway here
  options = {}
  properties = merge_properties(properties, options)

  # Add your custom static hidden tags here
  options = {
      #:token => config[:stripe][:token]
  }
  descriptor_fields = merge_properties(descriptor_fields, options)

  super(, descriptor_fields, properties, context)
end

#capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/stripe/api.rb', line 35

def capture_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/stripe/api.rb', line 61

def credit_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stripe/api.rb', line 117

def delete_payment_method(, kb_payment_method_id, properties, context)
  pm = StripePaymentMethod.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id)

  # Pass extra parameters for the gateway here
  options = {
      :customer_id => pm.stripe_customer_id
  }

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/stripe/api.rb', line 83

def get_payment_info(, kb_payment_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_id, properties, context)
end

#get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/stripe/api.rb', line 129

def get_payment_method_detail(, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_method_id, properties, context)
end

#get_payment_methods(kb_account_id, refresh_from_gateway, properties, context) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/stripe/api.rb', line 154

def get_payment_methods(, refresh_from_gateway, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, refresh_from_gateway, properties, context)
end

#on_event(event) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/stripe/api.rb', line 17

def on_event(event)
  # Require to deal with per tenant configuration invalidation
  super(event)
  #
  # Custom event logic could be added below...
  #
end

#process_notification(notification_json, properties, context) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/stripe/api.rb', line 188

def process_notification(notification_json, properties, context)
  notification = JSON.parse(notification_json)
  gw_response = ::ActiveMerchant::Billing::Response.new(true,
                                                        nil,
                                                        notification,
                                                        :test => !notification['livemode'],
                                                        :authorization => notification['request'],
                                                        :avs_result => nil,
                                                        :cvv_result => nil,
                                                        :emv_authorization => nil,
                                                        :error_code => nil)
  save_response_and_transaction(gw_response, "webhook.#{notification['type']}".to_sym, nil, context.tenant_id, nil)

  gw_notification = ::Killbill::Plugin::Model::GatewayNotification.new
  gw_notification.kb_payment_id = nil
  gw_notification.status = 200
  gw_notification.headers = {}
  gw_notification.properties = []
  gw_notification
end

#purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/stripe/api.rb', line 43

def purchase_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  pm = @payment_method_model.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id)

  options = {}
  populate_defaults(pm, amount, currency, properties, context, options)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stripe/api.rb', line 69

def refund_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  reverse_transfer = find_value_from_properties(properties, :reverse_transfer)
  options[:reverse_transfer] = ::Killbill::Plugin::ActiveMerchant::Utils.normalize(reverse_transfer)

  refund_application_fee = find_value_from_properties(properties, :refund_application_fee)
  options[:refund_application_fee] = ::Killbill::Plugin::ActiveMerchant::Utils.normalize(refund_application_fee)

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context)
end

#reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object



170
171
172
# File 'lib/stripe/api.rb', line 170

def reset_payment_methods(, payment_methods, properties, context)
  super
end

#search_payment_methods(search_key, offset, limit, properties, context) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/stripe/api.rb', line 162

def search_payment_methods(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#search_payments(search_key, offset, limit, properties, context) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/stripe/api.rb', line 91

def search_payments(search_key, offset, limit, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(search_key, offset, limit, properties, context)
end

#set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/stripe/api.rb', line 137

def set_default_payment_method(, kb_payment_method_id, properties, context)
  pm                           = StripePaymentMethod.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id)

  # Update the default payment method on the customer object
  options                      = properties_to_hash(properties)
   = options[:payment_processor_account_id] || :default
  gateway                      = lookup_gateway(, context.tenant_id)
  stripe_response              = gateway.update_customer(pm.stripe_customer_id, :default_card => pm.token)
  response, transaction        = save_response_and_transaction(stripe_response, :set_default_payment_method, , context.tenant_id, )

  if response.success
    # TODO Update our records
  else
    raise response.message
  end
end

#verify_bank_account(stripe_customer_id, stripe_bank_account_id, amounts, kb_tenant_id) ⇒ Object



209
210
211
212
213
# File 'lib/stripe/api.rb', line 209

def (stripe_customer_id, , amounts, kb_tenant_id)
  gateway = lookup_gateway(:default, kb_tenant_id)
  url = "customers/#{CGI.escape(stripe_customer_id)}/sources/#{CGI.escape()}/verify?#{amounts_to_uri(amounts)}"
  gateway.api_request(:post, url)
end

#void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/stripe/api.rb', line 53

def void_payment(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
  # Pass extra parameters for the gateway here
  options = {}

  properties = merge_properties(properties, options)
  super(, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context)
end