Class: AdaptivePay::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/adaptive_pay/interface.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_env = nil) ⇒ Interface

Initialize a new interface object, takes an optional rails_env parameter

The rails_env parameter decides which configuration to load can be:

nil -> use current Rails.env section in config/adaptive_pay.yml
false -> dont load config from config/adaptive_pay.yml
string/symbol -> use that section from config/adaptive_pay.yml


28
29
30
# File 'lib/adaptive_pay/interface.rb', line 28

def initialize(rails_env=nil)
  load(rails_env||Rails.env) unless rails_env == false
end

Instance Attribute Details

#application_idObject

Returns the value of attribute application_id.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def application_id
  @application_id
end

#base_page_urlObject

Returns the value of attribute base_page_url.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def base_page_url
  @base_page_url
end

#base_urlObject

Returns the value of attribute base_url.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def base_url
  @base_url
end

#environmentObject

Returns the value of attribute environment.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def environment
  @environment
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def password
  @password
end

#retain_requests_for_testObject

Returns the value of attribute retain_requests_for_test.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def retain_requests_for_test
  @retain_requests_for_test
end

#signatureObject

Returns the value of attribute signature.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def signature
  @signature
end

#test_responseObject

Returns the value of attribute test_response.



11
12
13
# File 'lib/adaptive_pay/interface.rb', line 11

def test_response
  @test_response
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/adaptive_pay/interface.rb', line 13

def username
  @username
end

Class Method Details

.requestsObject



6
7
8
# File 'lib/adaptive_pay/interface.rb', line 6

def self.requests
  @requests ||= []
end

.test_interface(response = nil) ⇒ Object



15
16
17
18
19
20
# File 'lib/adaptive_pay/interface.rb', line 15

def self.test_interface(response = nil)
  iface = new false
  iface.retain_requests_for_test = true
  iface.test_response = response
  iface
end

Instance Method Details

#load(rails_env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/adaptive_pay/interface.rb', line 32

def load(rails_env)
  config = YAML.load(ERB.new(File.read(File.join(Rails.root, "config/adaptive_pay.yml"))).result)[rails_env.to_s]
  if config["retain_requests_for_test"] == true
    @retain_requests_for_test = true
  else
    set_environment config.delete("environment")
    config.each do |k, v|
      send "#{k}=", v
    end
  end
end

#perform(request) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/adaptive_pay/interface.rb', line 83

def perform(request)
  if retain_requests_for_test?
    self.class.requests << request
    test_response || self.class.test_response
  else
    request.perform self
  end
end

#request_payment {|request| ... } ⇒ Object

Yields:

  • (request)


71
72
73
74
75
# File 'lib/adaptive_pay/interface.rb', line 71

def request_payment(&block)
  request = PaymentRequest.new
  yield request
  perform request
end

#request_preapproval {|request| ... } ⇒ Object

send a preapproved payment request to paypal

Yields:

  • (request)


65
66
67
68
69
# File 'lib/adaptive_pay/interface.rb', line 65

def request_preapproval(&block)
  request = PreapprovalRequest.new
  yield request
  perform request
end

#request_refund {|request| ... } ⇒ Object

Yields:

  • (request)


77
78
79
80
81
# File 'lib/adaptive_pay/interface.rb', line 77

def request_refund(&block)
  request = RefundRequest.new
  yield request
  perform request
end

#retain_requests_for_test?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/adaptive_pay/interface.rb', line 60

def retain_requests_for_test?
  !!@retain_requests_for_test
end

#set_environment(environment) ⇒ Object

Explicitly select a paypal environment to connect to environment parameter can be :production, :sandbox, :beta_sandbox



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adaptive_pay/interface.rb', line 46

def set_environment(environment)
  @environment = environment.to_sym
  @base_url = {
    :production => "https://svcs.paypal.com/AdaptivePayments/",
    :sandbox => "https://svcs.sandbox.paypal.com/AdaptivePayments/",
    :beta_sandbox => "https://svcs.beta-sandbox.paypal.com/AdaptivePayments/"
  }[@environment]
  @base_page_url = {
    :production => "https://www.paypal.com",
    :sandbox => "https://www.sandbox.paypal.com",
    :beta_sandbox => "https://www.beta-sandbox.paypal.com"
  }[@environment]
end