Class: ActivePayment::Gateway
- Inherits:
-
Object
- Object
- ActivePayment::Gateway
- Defined in:
- lib/active_payment/gateway.rb
Instance Attribute Summary collapse
-
#gateway ⇒ Object
Returns the value of attribute gateway.
-
#purchase_token ⇒ Object
Returns the value of attribute purchase_token.
-
#transactions ⇒ Object
Returns the value of attribute transactions.
Class Method Summary collapse
- .cancel_purchase_from_request(gateway:, request:) ⇒ Object
- .verify_purchase_from_request(gateway:, request:, data:) ⇒ Object
Instance Method Summary collapse
- #cancel_purchase(external_id, remote_ip) ⇒ Object
- #external_id_from_request(request) ⇒ Object
-
#initialize(name) ⇒ Gateway
constructor
A new instance of Gateway.
- #livemode? ⇒ Boolean
- #setup_purchase(sales, ip_address) ⇒ Object
- #verify_purchase(external_id, remote_ip, raw_data) ⇒ Object
Constructor Details
#initialize(name) ⇒ Gateway
Returns a new instance of Gateway.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_payment/gateway.rb', line 5 def initialize(name) @transactions = [] name_str = name.to_s.strip.downcase raise(ArgumentError, 'A gateway provider must be specified') if name_str.blank? ActiveMerchant::Billing::Base.mode = :test if ActivePayment.configuration.test begin @gateway = ActivePayment::Gateways.const_get("#{name_str}".camelize).new rescue SyntaxError, NameError raise ArgumentError, "The specified gateway is not valid (#{name_str})" end end |
Instance Attribute Details
#gateway ⇒ Object
Returns the value of attribute gateway.
3 4 5 |
# File 'lib/active_payment/gateway.rb', line 3 def gateway @gateway end |
#purchase_token ⇒ Object
Returns the value of attribute purchase_token.
3 4 5 |
# File 'lib/active_payment/gateway.rb', line 3 def purchase_token @purchase_token end |
#transactions ⇒ Object
Returns the value of attribute transactions.
3 4 5 |
# File 'lib/active_payment/gateway.rb', line 3 def transactions @transactions end |
Class Method Details
.cancel_purchase_from_request(gateway:, request:) ⇒ Object
70 71 72 73 74 |
# File 'lib/active_payment/gateway.rb', line 70 def self.cancel_purchase_from_request(gateway:, request:) payment_gateway = ActivePayment::Gateway.new(gateway) external_id = payment_gateway.external_id_from_request(request) payment_gateway.cancel_purchase(external_id, request.remote_ip) end |
.verify_purchase_from_request(gateway:, request:, data:) ⇒ Object
64 65 66 67 68 |
# File 'lib/active_payment/gateway.rb', line 64 def self.verify_purchase_from_request(gateway:, request:, data:) payment_gateway = ActivePayment::Gateway.new(gateway) external_id = payment_gateway.external_id_from_request(request) payment_gateway.verify_purchase(external_id, request.remote_ip, data) end |
Instance Method Details
#cancel_purchase(external_id, remote_ip) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/active_payment/gateway.rb', line 48 def cancel_purchase(external_id, remote_ip) @transactions = ActivePayment::Transaction.where(external_id: external_id) fail ActivePayment::NoTransactionError unless @transactions.size > 0 verify_ip_address(@transactions, remote_ip) transactions_cancel(@transactions) end |
#external_id_from_request(request) ⇒ Object
56 57 58 |
# File 'lib/active_payment/gateway.rb', line 56 def external_id_from_request(request) @gateway.external_id_from_request(request) end |
#livemode? ⇒ Boolean
60 61 62 |
# File 'lib/active_payment/gateway.rb', line 60 def livemode? @gateway.livemode? end |
#setup_purchase(sales, ip_address) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_payment/gateway.rb', line 20 def setup_purchase(sales, ip_address) amount = sales.amount_in_cents raise ActivePayment::InvalidAmountError unless amount >= ActivePayment.configuration.min_amount url = @gateway.setup_purchase(sales) @purchase_token = @gateway.purchase_token create_transactions(ip_address) url end |
#verify_purchase(external_id, remote_ip, raw_data) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_payment/gateway.rb', line 31 def verify_purchase(external_id, remote_ip, raw_data) @transactions = ActivePayment::Transaction.where(external_id: external_id) fail ActivePayment::NoTransactionError unless @transactions.size > 0 verify_ip_address(@transactions, remote_ip) if raw_data.is_a?(Hash) && raw_data[:amount].blank? raw_data[:amount] = @transactions.map(&:amount).inject(0, &:+) end if @gateway.verify_purchase(raw_data) transactions_success(@transactions) else transactions_error(@transactions) fail ActivePayment::InvalidGatewayResponseError end end |