Class: Killbill::Plugin::ActiveMerchant::PaymentPlugin
- Inherits:
-
Payment
- Object
- PluginBase
- Notification
- Payment
- Killbill::Plugin::ActiveMerchant::PaymentPlugin
- Includes:
- ActiveRecordHelper, PropertiesHelper
- Defined in:
- lib/killbill/helpers/active_merchant/payment_plugin.rb
Instance Attribute Summary
Attributes inherited from PluginBase
#active, #clock, #conf_dir, #kb_apis, #logger, #plugin_name, #root
Instance Method Summary collapse
- #account_currency(kb_account_id, kb_tenant_id) ⇒ Object
- #add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object
-
#after_gateway(response, transaction, gw_response, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0).
-
#after_gateways(response, transaction, gw_response, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0).
- #after_request ⇒ Object
- #authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
-
#before_gateway(gateway, kb_transaction, last_transaction, payment_source, amount_in_cents, currency, options, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0).
-
#before_gateways(kb_transaction, last_transaction, payment_source, amount_in_cents, currency, options, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0).
- #build_am_credit_card(cc_number, attributes, pm = nil) ⇒ Object
- #build_am_token(token, attributes) ⇒ Object
- #build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object
- #capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
- #config(kb_tenant_id = nil) ⇒ Object
- #credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
- #delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
-
#dispatch_to_gateways(operation, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, linked_transaction_proc = nil, extra_params = {}) ⇒ Object
TODO Split settlements is partially implemented.
- #get_active_merchant_module ⇒ Object
- #get_kb_transaction(kb_payment_id, kb_payment_transaction_id, kb_tenant_id) ⇒ Object
- #get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object
- #get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
-
#get_payment_methods(kb_account_id, refresh_from_gateway = false, properties, context) ⇒ Object
No default implementation def set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) end.
- #get_payment_source(kb_payment_method_id, properties, options, context) ⇒ Object
-
#initialize(gateway_builder, identifier, payment_method_model, transaction_model, response_model) ⇒ PaymentPlugin
constructor
A new instance of PaymentPlugin.
- #lookup_gateway(payment_processor_account_id = :default, kb_tenant_id = nil) ⇒ Object
- #merge_transaction_info_plugins(payment_processor_account_ids, responses, transactions) ⇒ Object
- #on_event(event) ⇒ Object
- #process_notification(notification, properties, context) {|gw_notification, service| ... } ⇒ Object
- #purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
- #refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
- #reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object
- #save_response_and_transaction(gw_response, api_call, kb_account_id, kb_tenant_id, payment_processor_account_id, kb_payment_id = nil, kb_payment_transaction_id = nil, transaction_type = nil, amount_in_cents = 0, currency = nil, extra_params = {}) ⇒ Object
- #search_payment_methods(search_key, offset = 0, limit = 100, properties, context) ⇒ Object
- #search_payments(search_key, offset = 0, limit = 100, properties, context) ⇒ Object
- #start_plugin ⇒ Object
- #to_cents(amount, currency) ⇒ Object
- #void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context, extra_params = {}) ⇒ Object
Methods included from PropertiesHelper
#build_property, #find_value_from_properties, #hash_to_properties, #merge_properties, #properties_to_hash
Methods included from ActiveRecordHelper
Methods inherited from Payment
Methods inherited from PluginBase
Constructor Details
#initialize(gateway_builder, identifier, payment_method_model, transaction_model, response_model) ⇒ PaymentPlugin
Returns a new instance of PaymentPlugin.
14 15 16 17 18 19 20 21 22 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 14 def initialize(gateway_builder, identifier, payment_method_model, transaction_model, response_model) super() @gateway_builder = gateway_builder @identifier = identifier @payment_method_model = payment_method_model @transaction_model = transaction_model @response_model = response_model end |
Instance Method Details
#account_currency(kb_account_id, kb_tenant_id) ⇒ Object
567 568 569 570 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 567 def account_currency(kb_account_id, kb_tenant_id) account = @kb_apis.account_user_api.get_account_by_id(kb_account_id, @kb_apis.create_context(kb_tenant_id)) account.currency end |
#add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 141 def add_payment_method(kb_account_id, kb_payment_method_id, payment_method_props, set_default, properties, context) all_properties = (payment_method_props.nil? || payment_method_props.properties.nil? ? [] : payment_method_props.properties) + properties = properties_to_hash(properties) [:set_default] ||= set_default [:order_id] ||= kb_payment_method_id should_skip_gw = Utils.normalized(, :skip_gw) # Registering a card or a token if should_skip_gw # If nothing is passed, that's fine - we probably just want a placeholder row in the plugin payment_source = get_payment_source(nil, all_properties, , context) rescue nil else payment_source = get_payment_source(nil, all_properties, , context) end # Go to the gateway payment_processor_account_id = Utils.normalized(, :payment_processor_account_id) || :default gateway = lookup_gateway(payment_processor_account_id, context.tenant_id) gw_response = gateway.store(payment_source, ) response, transaction = save_response_and_transaction(gw_response, :add_payment_method, kb_account_id, context.tenant_id, payment_processor_account_id) if response.success # If we have skipped the call to the gateway, we still need to store the payment method (either a token or the full credit card) if should_skip_gw cc_or_token = payment_source else # response.authorization may be a String combination separated by ; - don't split it! Some plugins expect it as-is (they split it themselves) cc_or_token = response. end attributes = properties_to_hash(all_properties) # Note: keep the same keys as in build_am_credit_card below extra_params = { :cc_first_name => Utils.normalized(attributes, :cc_first_name), :cc_last_name => Utils.normalized(attributes, :cc_last_name), :cc_type => Utils.normalized(attributes, :cc_type), :cc_exp_month => Utils.normalized(attributes, :cc_expiration_month), :cc_exp_year => Utils.normalized(attributes, :cc_expiration_year), :cc_last_4 => Utils.normalized(attributes, :cc_last_4) } payment_method = @payment_method_model.from_response(kb_account_id, kb_payment_method_id, context.tenant_id, cc_or_token, gw_response, , extra_params, @payment_method_model) payment_method.save! payment_method else raise response. end end |
#after_gateway(response, transaction, gw_response, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0)
460 461 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 460 def after_gateway(response, transaction, gw_response, context = nil) end |
#after_gateways(response, transaction, gw_response, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0)
450 451 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 450 def after_gateways(response, transaction, gw_response, context = nil) end |
#after_request ⇒ Object
40 41 42 43 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 40 def after_request # return DB connections to the Pool if required close_connection(@logger) end |
#authorize_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 52 def (kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | gateway.(amount_in_cents, payment_source, ) end dispatch_to_gateways(:authorize, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, nil, extra_params) end |
#before_gateway(gateway, kb_transaction, last_transaction, payment_source, amount_in_cents, currency, options, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0)
454 455 456 457 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 454 def before_gateway(gateway, kb_transaction, last_transaction, payment_source, amount_in_cents, currency, , context = nil) # Can be used to implement idempotency for example: lookup the payment in the gateway # and pass options[:skip_gw] if the payment has already been through end |
#before_gateways(kb_transaction, last_transaction, payment_source, amount_in_cents, currency, options, context = nil) ⇒ Object
Default nil value for context only for backward compatibility (Kill Bill 0.14.0)
446 447 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 446 def before_gateways(kb_transaction, last_transaction, payment_source, amount_in_cents, currency, , context = nil) end |
#build_am_credit_card(cc_number, attributes, pm = nil) ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 522 def build_am_credit_card(cc_number, attributes, pm=nil) card_attributes = { :number => cc_number, :brand => Utils.normalized(attributes, :cc_type) || (pm.nil? ? nil : pm.cc_type), :month => Utils.normalized(attributes, :cc_expiration_month) || (pm.nil? ? nil : pm.cc_exp_month), :year => Utils.normalized(attributes, :cc_expiration_year) || (pm.nil? ? nil : pm.cc_exp_year), :verification_value => Utils.normalized(attributes, :cc_verification_value) || (pm.nil? ? nil : pm.cc_verification_value), :first_name => Utils.normalized(attributes, :cc_first_name) || (pm.nil? ? nil : pm.cc_first_name), :last_name => Utils.normalized(attributes, :cc_last_name) || (pm.nil? ? nil : pm.cc_last_name) } tokenization_attributes = { :eci => Utils.normalized(attributes, :eci), :payment_cryptogram => Utils.normalized(attributes, :payment_cryptogram), :transaction_id => Utils.normalized(attributes, :transaction_id) } if tokenization_attributes[:eci].nil? && tokenization_attributes[:payment_cryptogram].nil? && tokenization_attributes[:transaction_id].nil? ::ActiveMerchant::Billing::CreditCard.new(card_attributes) else # NetworkTokenizationCreditCard is exactly like a credit card but with EMV/3DS standard payment network tokenization data ::ActiveMerchant::Billing::NetworkTokenizationCreditCard.new(card_attributes.merge(tokenization_attributes)) end end |
#build_am_token(token, attributes) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 548 def build_am_token(token, attributes) token_attributes = { :payment_instrument_name => Utils.normalized(attributes, :payment_instrument_name), :payment_network => Utils.normalized(attributes, :payment_network), :transaction_identifier => Utils.normalized(attributes, :transaction_identifier) } if token_attributes[:payment_instrument_name].nil? && token_attributes[:payment_network].nil? && token_attributes[:transaction_identifier].nil? token else # Use ActiveSupport since ActiveMerchant does the same payment_data = ::ActiveSupport::JSON.decode(token) rescue token # PaymentToken is meant for modeling proprietary payment token structures (like stripe and apple_pay) ::ActiveMerchant::Billing::ApplePayPaymentToken.new(payment_data, token_attributes) end end |
#build_form_descriptor(kb_account_id, descriptor_fields, properties, context) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 273 def build_form_descriptor(kb_account_id, descriptor_fields, properties, context) = properties_to_hash(descriptor_fields) order = .delete(:order_id) account = .delete(:account_id) = { :amount => .delete(:amount), :currency => .delete(:currency), :test => .delete(:test), :credential2 => .delete(:credential2), :credential3 => .delete(:credential3), :credential4 => .delete(:credential4), :country => .delete(:country), :account_name => .delete(:account_name), :transaction_type => .delete(:transaction_type), :authcode => .delete(:authcode), :notify_url => .delete(:notify_url), :return_url => .delete(:return_url), :redirect_param => .delete(:redirect_param), :forward_url => .delete(:forward_url) } # Retrieve the ActiveMerchant integration integration_module = get_active_merchant_module service_class = integration_module.const_get('Helper') service = service_class.new(order, account, ) # Add the specified fields .each do |field, value| mapping = service_class.mappings[field] next if mapping.nil? case mapping when Array mapping.each { |field2| service.add_field(field2, value) } when Hash = value.is_a?(Hash) ? value : {} mapping.each { |key, field2| service.add_field(field2, [key]) } else service.add_field(mapping, value) end end form_fields = {} service.form_fields.each do |field, value| form_fields[field] = value end service.raw_html_fields.each do |field, value| form_fields[field] = value end # Build the response object descriptor = ::Killbill::Plugin::Model::HostedPaymentPageFormDescriptor.new descriptor.kb_account_id = kb_account_id descriptor.form_method = service.form_method || 'POST' descriptor.form_url = service.respond_to?(:credential_based_url) ? service.credential_based_url : integration_module.service_url descriptor.form_fields = hash_to_properties(form_fields) # Any other custom property descriptor.properties = hash_to_properties({}) descriptor end |
#capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 60 def capture_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | gateway.capture(amount_in_cents, linked_transaction.txn_id, ) end linked_transaction_proc = Proc.new do |amount_in_cents, | # TODO We use the last transaction at the moment, is it good enough? = @transaction_model.(kb_payment_id, context.tenant_id).last raise "Unable to retrieve last authorization for operation=capture, kb_payment_id=#{kb_payment_id}, kb_payment_transaction_id=#{kb_payment_transaction_id}, kb_payment_method_id=#{kb_payment_method_id}" if .nil? end dispatch_to_gateways(:capture, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, linked_transaction_proc, extra_params) end |
#config(kb_tenant_id = nil) ⇒ Object
588 589 590 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 588 def config(kb_tenant_id=nil) ::Killbill::Plugin::ActiveMerchant.config(kb_tenant_id) end |
#credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 105 def credit_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | gateway.credit(amount_in_cents, payment_source, ) end dispatch_to_gateways(:credit, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, nil, extra_params) end |
#delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 190 def delete_payment_method(kb_account_id, kb_payment_method_id, properties, context) = properties_to_hash(properties) pm = @payment_method_model.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id) # Delete the card payment_processor_account_id = Utils.normalized(, :payment_processor_account_id) || :default gateway = lookup_gateway(payment_processor_account_id, context.tenant_id) customer_id = Utils.normalized(, :customer_id) if customer_id gw_response = gateway.unstore(customer_id, pm.token, ) else gw_response = gateway.unstore(pm.token, ) end response, transaction = save_response_and_transaction(gw_response, :delete_payment_method, kb_account_id, context.tenant_id, payment_processor_account_id) if response.success @payment_method_model.mark_as_deleted! kb_payment_method_id, context.tenant_id else raise response. end end |
#dispatch_to_gateways(operation, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, linked_transaction_proc = nil, extra_params = {}) ⇒ Object
TODO Split settlements is partially implemented. Left to be done:
-
payment_source should probably be retrieved per gateway
-
amount per gateway should be retrieved from the options
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 369 def dispatch_to_gateways(operation, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, linked_transaction_proc=nil, extra_params={}) kb_transaction = Utils::LazyEvaluator.new { get_kb_transaction(kb_payment_id, kb_payment_transaction_id, context.tenant_id) } amount_in_cents = amount.nil? ? nil : to_cents(amount, currency) # Setup options for ActiveMerchant = properties_to_hash(properties) [:order_id] ||= (Utils.normalized(, :external_key_as_order_id) ? kb_transaction.external_key : kb_payment_transaction_id) [:currency] ||= currency.to_s.upcase unless currency.nil? [:description] ||= "Kill Bill #{operation.to_s} for #{kb_payment_transaction_id}" # Retrieve the payment method payment_source = get_payment_source(kb_payment_method_id, properties, , context) # Sanity checks if [:authorize, :purchase, :credit].include?(operation) raise "Unable to retrieve payment source for operation=#{operation}, kb_payment_id=#{kb_payment_id}, kb_payment_transaction_id=#{kb_payment_transaction_id}, kb_payment_method_id=#{kb_payment_method_id}" if payment_source.nil? end # Retrieve the previous transaction for the same operation and payment id - this is useful to detect dups for example last_transaction = Utils::LazyEvaluator.new { @transaction_model.send("#{operation.to_s}s_from_kb_payment_id", kb_payment_id, context.tenant_id).last } # Retrieve the linked transaction (authorization to capture, purchase to refund, etc.) linked_transaction = nil unless linked_transaction_proc.nil? linked_transaction = linked_transaction_proc.call(amount_in_cents, ) [:payment_processor_account_id] ||= linked_transaction.payment_processor_account_id end # Filter before all gateways call before_gateways(kb_transaction, last_transaction, payment_source, amount_in_cents, currency, , context) # Dispatch to the gateways. In most cases (non split settlements), we only dispatch to a single gateway account gw_responses = [] responses = [] transactions = [] payment_processor_account_ids = Utils.normalized(, :payment_processor_account_ids) if !payment_processor_account_ids payment_processor_account_ids = [Utils.normalized(, :payment_processor_account_id) || :default] else payment_processor_account_ids = payment_processor_account_ids.split(',') end payment_processor_account_ids.each do |payment_processor_account_id| # Find the gateway gateway = lookup_gateway(payment_processor_account_id, context.tenant_id) # Filter before each gateway call before_gateway(gateway, kb_transaction, last_transaction, payment_source, amount_in_cents, currency, , context) # Perform the operation in the gateway gw_response = gateway_call_proc.call(gateway, linked_transaction, payment_source, amount_in_cents, ) response, transaction = save_response_and_transaction(gw_response, operation, kb_account_id, context.tenant_id, payment_processor_account_id, kb_payment_id, kb_payment_transaction_id, operation.upcase, amount_in_cents, currency, extra_params) # Filter after each gateway call after_gateway(response, transaction, gw_response, context) gw_responses << gw_response responses << response transactions << transaction end # Filter after all gateways call after_gateways(responses, transactions, gw_responses, context) # Merge data merge_transaction_info_plugins(payment_processor_account_ids, responses, transactions) end |
#get_active_merchant_module ⇒ Object
592 593 594 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 592 def get_active_merchant_module ::OffsitePayments.integration(@identifier.to_s.camelize) end |
#get_kb_transaction(kb_payment_id, kb_payment_transaction_id, kb_tenant_id) ⇒ Object
437 438 439 440 441 442 443 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 437 def get_kb_transaction(kb_payment_id, kb_payment_transaction_id, kb_tenant_id) kb_payment = @kb_apis.payment_api.get_payment(kb_payment_id, false, false, [], @kb_apis.create_context(kb_tenant_id)) kb_transaction = kb_payment.transactions.find { |t| t.id == kb_payment_transaction_id } # This should never happen... raise ArgumentError.new("Unable to find Kill Bill transaction for id #{kb_payment_transaction_id}") if kb_transaction.nil? kb_transaction end |
#get_payment_info(kb_account_id, kb_payment_id, properties, context) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 129 def get_payment_info(kb_account_id, kb_payment_id, properties, context) # We assume the payment is immutable in the Gateway and only look at our tables @response_model.from_kb_payment_id(@transaction_model, kb_payment_id, context.tenant_id).collect do |response| response.to_transaction_info_plugin(response.send("#{@identifier}_transaction")) end end |
#get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) ⇒ Object
214 215 216 217 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 214 def get_payment_method_detail(kb_account_id, kb_payment_method_id, properties, context) = properties_to_hash(properties) @payment_method_model.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id).to_payment_method_plugin end |
#get_payment_methods(kb_account_id, refresh_from_gateway = false, properties, context) ⇒ Object
No default implementation def set_default_payment_method(kb_account_id, kb_payment_method_id, properties, context) end
223 224 225 226 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 223 def get_payment_methods(kb_account_id, refresh_from_gateway = false, properties, context) = properties_to_hash(properties) @payment_method_model.from_kb_account_id(kb_account_id, context.tenant_id).collect { |pm| pm.to_payment_method_info_plugin } end |
#get_payment_source(kb_payment_method_id, properties, options, context) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 468 def get_payment_source(kb_payment_method_id, properties, , context) attributes = properties_to_hash(properties, ) # Use ccNumber for: # * the real number # * in-house token (e.g. proxy tokenization) # * token from a token service provider (e.g. ApplePay) # If not specified, the rest of the card details will be retrieved from the locally stored payment method (if available) cc_number = Utils.normalized(attributes, :cc_number) # Use token for the token stored in an external vault. The token itself should be enough to process payments. token = Utils.normalized(attributes, :token) || Utils.normalized(attributes, :card_id) || Utils.normalized(attributes, :payment_data) if token.blank? pm = nil begin pm = @payment_method_model.from_kb_payment_method_id(kb_payment_method_id, context.tenant_id) rescue => e raise e if cc_number.blank? end unless kb_payment_method_id.nil? if cc_number.blank? && !pm.nil? # Lookup existing token if pm.token.blank? # Real credit card cc_or_token = build_am_credit_card(pm.cc_number, attributes, pm) else # Tokenized card cc_or_token = pm.token end else # Real credit card or network tokenization cc_or_token = build_am_credit_card(cc_number, attributes, pm) end else # Use specified token cc_or_token = build_am_token(token, attributes) end [:billing_address] ||= { :email => Utils.normalized(attributes, :email), :address1 => Utils.normalized(attributes, :address1) || (pm.nil? ? nil : pm.address1), :address2 => Utils.normalized(attributes, :address2) || (pm.nil? ? nil : pm.address2), :city => Utils.normalized(attributes, :city) || (pm.nil? ? nil : pm.city), :zip => Utils.normalized(attributes, :zip) || (pm.nil? ? nil : pm.zip), :state => Utils.normalized(attributes, :state) || (pm.nil? ? nil : pm.state), :country => Utils.normalized(attributes, :country) || (pm.nil? ? nil : pm.country) } # To make various gateway implementations happy... [:billing_address].each { |k, v| [k] ||= v } cc_or_token end |
#lookup_gateway(payment_processor_account_id = :default, kb_tenant_id = nil) ⇒ Object
582 583 584 585 586 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 582 def lookup_gateway(payment_processor_account_id=:default, kb_tenant_id=nil) gateway = ::Killbill::Plugin::ActiveMerchant.gateways(kb_tenant_id)[payment_processor_account_id.to_sym] raise "Unable to lookup gateway for payment_processor_account_id #{payment_processor_account_id}, kb_tenant_id = #{kb_tenant_id}, gateways: #{::Killbill::Plugin::ActiveMerchant.gateways(kb_tenant_id)}" if gateway.nil? gateway end |
#merge_transaction_info_plugins(payment_processor_account_ids, responses, transactions) ⇒ Object
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 596 def merge_transaction_info_plugins(payment_processor_account_ids, responses, transactions) result = Killbill::Plugin::Model::PaymentTransactionInfoPlugin.new result.amount = nil result.properties = [] result.status = :PROCESSED # Nothing meaningful we can set here result.first_payment_reference_id = nil result.second_payment_reference_id = nil responses.each_with_index do |response, idx| t_info_plugin = response.to_transaction_info_plugin(transactions[idx]) if responses.size == 1 # We're done return t_info_plugin end # Unique values [:kb_payment_id, :kb_transaction_payment_id, :transaction_type, :currency].each do |element| result_element = result.send(element) t_info_plugin_element = t_info_plugin.send(element) if result_element.nil? result.send("#{element}=", t_info_plugin_element) elsif result_element != t_info_plugin_element raise "#{element.to_s} mismatch, #{result_element} != #{t_info_plugin_element}" end end # Arbitrary values [:created_date, :effective_date].each do |element| if result.send(element).nil? result.send("#{element}=", t_info_plugin.send(element)) end end t_info_plugin.properties.each do |property| prop = Killbill::Plugin::Model::PluginProperty.new prop.key = "#{property.key}_#{payment_processor_account_ids[idx]}" prop.value = property.value result.properties << prop end if result.amount.nil? result.amount = t_info_plugin.amount elsif !t_info_plugin.nil? # TODO Adding decimals - are we losing precision? result.amount = result.amount + t_info_plugin.amount end # We set an error status if we have at least one error # TODO Does this work well with retries? if t_info_plugin.status == :ERROR result.status = :ERROR # Return the first error result.gateway_error = t_info_plugin.gateway_error if result.gateway_error.nil? result.gateway_error_code = t_info_plugin.gateway_error_code if result.gateway_error_code.nil? end end result end |
#on_event(event) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 45 def on_event(event) if (event.event_type == :TENANT_CONFIG_CHANGE || event.event_type == :TENANT_CONFIG_DELETION) && event..to_sym == @config_key_name ::Killbill::Plugin::ActiveMerchant.invalidate_tenant_config!(event.tenant_id) end end |
#process_notification(notification, properties, context) {|gw_notification, service| ... } ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 334 def process_notification(notification, properties, context, &proc) = properties_to_hash(properties) # Retrieve the ActiveMerchant integration integration_module = get_active_merchant_module service_class = integration_module.const_get('Notification') # notification is either a body or a query string service = service_class.new(notification, ) if service.respond_to? :acknowledge service.acknowledge end gw_notification = ::Killbill::Plugin::Model::GatewayNotification.new gw_notification.kb_payment_id = nil gw_notification.status = service.status == 'Completed' ? 200 : 400 gw_notification.headers = {} gw_notification.properties = [] if service.respond_to? :success_response gw_notification.entity = service.success_response(properties_to_hash(properties)) else gw_notification.entity = '' end yield(gw_notification, service) if block_given? gw_notification end |
#purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 75 def purchase_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | gateway.purchase(amount_in_cents, payment_source, ) end dispatch_to_gateways(:purchase, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, nil, extra_params) end |
#refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 113 def refund_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | gateway.refund(amount_in_cents, linked_transaction.txn_id, ) end linked_transaction_proc = Proc.new do |amount_in_cents, | linked_transaction_type = find_value_from_properties(properties, :linked_transaction_type) transaction = @transaction_model.find_candidate_transaction_for_refund(kb_payment_id, context.tenant_id, linked_transaction_type) # This should never happen raise "Unable to retrieve transaction to refund for operation=refund, kb_payment_id=#{kb_payment_id}, kb_payment_transaction_id=#{kb_payment_transaction_id}, kb_payment_method_id=#{kb_payment_method_id}" if transaction.nil? transaction end dispatch_to_gateways(:refund, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, amount, currency, properties, context, gateway_call_proc, linked_transaction_proc, extra_params) end |
#reset_payment_methods(kb_account_id, payment_methods, properties, context) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 233 def reset_payment_methods(kb_account_id, payment_methods, properties, context) return if payment_methods.nil? = properties_to_hash(properties) pms = @payment_method_model.from_kb_account_id(kb_account_id, context.tenant_id) payment_methods.delete_if do |payment_method_info_plugin| should_be_deleted = false pms.each do |pm| # Do pm and payment_method_info_plugin represent the same payment method? if pm.external_payment_method_id == payment_method_info_plugin.external_payment_method_id # Do we already have a kb_payment_method_id? if pm.kb_payment_method_id == payment_method_info_plugin.payment_method_id should_be_deleted = true break elsif pm.kb_payment_method_id.nil? # We didn't have the kb_payment_method_id - update it pm.kb_payment_method_id = payment_method_info_plugin.payment_method_id should_be_deleted = pm.save break # Otherwise the same token points to 2 different kb_payment_method_id. This should never happen! end end end should_be_deleted end # The remaining elements in payment_methods are not in our table (this should never happen?!) payment_methods.each do |payment_method_info_plugin| pm = @payment_method_model.create(:kb_account_id => kb_account_id, :kb_payment_method_id => payment_method_info_plugin.payment_method_id, :kb_tenant_id => context.tenant_id, :token => payment_method_info_plugin.external_payment_method_id, :created_at => Time.now.utc, :updated_at => Time.now.utc) end end |
#save_response_and_transaction(gw_response, api_call, kb_account_id, kb_tenant_id, payment_processor_account_id, kb_payment_id = nil, kb_payment_transaction_id = nil, transaction_type = nil, amount_in_cents = 0, currency = nil, extra_params = {}) ⇒ Object
572 573 574 575 576 577 578 579 580 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 572 def save_response_and_transaction(gw_response, api_call, kb_account_id, kb_tenant_id, payment_processor_account_id, kb_payment_id=nil, kb_payment_transaction_id=nil, transaction_type=nil, amount_in_cents=0, currency=nil, extra_params={}) @logger.warn "Unsuccessful #{api_call}: #{gw_response.}" unless gw_response.success? response, transaction = @response_model.create_response_and_transaction(@identifier, @transaction_model, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, gw_response, amount_in_cents, currency, extra_params, @response_model) @logger.debug { "Recorded transaction: #{transaction.inspect}" } unless transaction.nil? return response, transaction end |
#search_payment_methods(search_key, offset = 0, limit = 100, properties, context) ⇒ Object
228 229 230 231 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 228 def search_payment_methods(search_key, offset = 0, limit = 100, properties, context) = properties_to_hash(properties) @payment_method_model.search(search_key, context.tenant_id, offset, limit) end |
#search_payments(search_key, offset = 0, limit = 100, properties, context) ⇒ Object
136 137 138 139 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 136 def search_payments(search_key, offset = 0, limit = 100, properties, context) = properties_to_hash(properties) @response_model.search(search_key, context.tenant_id, offset, limit) end |
#start_plugin ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 24 def start_plugin @logger.progname = "#{@identifier.to_s}-plugin" @config_key_name = "PLUGIN_CONFIG_#{@plugin_name}".to_sym ::Killbill::Plugin::ActiveMerchant.initialize! @gateway_builder, @identifier.to_sym, @logger, @config_key_name, "#{@conf_dir}/#{@identifier.to_s}.yml", @kb_apis super @logger.info "#{@identifier} payment plugin started" end |
#to_cents(amount, currency) ⇒ Object
463 464 465 466 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 463 def to_cents(amount, currency) # Use Money to compute the amount in cents, as it depends on the currency (1 cent of BTC is 1 Satoshi, not 0.01 BTC) ::Monetize.from_numeric(amount, currency).cents.to_i end |
#void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context, extra_params = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/killbill/helpers/active_merchant/payment_plugin.rb', line 83 def void_payment(kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, properties, context, extra_params = {}) gateway_call_proc = Proc.new do |gateway, linked_transaction, payment_source, amount_in_cents, | = linked_transaction.txn_id # Go to the gateway - while some gateways implementations are smart and have void support 'auth_reversal' and 'void' (e.g. Litle), # others (e.g. CyberSource) implement different methods if linked_transaction.transaction_type == 'AUTHORIZE' && gateway.respond_to?(:auth_reversal) [:currency] ||= linked_transaction.currency gateway.auth_reversal(linked_transaction.amount_in_cents, , ) else gateway.void(, ) end end linked_transaction_proc = Proc.new do |amount_in_cents, | linked_transaction_type = find_value_from_properties(properties, :linked_transaction_type) @transaction_model.find_candidate_transaction_for_void(kb_payment_id, context.tenant_id, linked_transaction_type) end dispatch_to_gateways(:void, kb_account_id, kb_payment_id, kb_payment_transaction_id, kb_payment_method_id, nil, nil, properties, context, gateway_call_proc, linked_transaction_proc, extra_params) end |