Class: PaypalAdaptive::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
17
# File 'lib/request.rb', line 12

def initialize(env = nil)
  @env = env
  @@config ||= PaypalAdaptive::Config.new(@env)
  @@api_base_url ||= @@config.api_base_url
  @@headers ||= @@config.headers
end

Instance Method Details

#call_api(data, path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/request.rb', line 79

def call_api(data, path)
  logger = data.delete(:logger) || data.delete("logger")
  #hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
  api_request_data = JSON.unparse(data) rescue data.to_json
  url = URI.parse @@api_base_url
  http = Net::HTTP.new(url.host, 443)
  http.use_ssl = (url.scheme == 'https')

  logger.request(path, api_request_data, @@headers) if logger
  resp, response_data = http.post(path, api_request_data, @@headers)
  logger.response(resp.code, response_data) if logger

  JSON.parse(response_data)
end

#cancel_preapproval(data) ⇒ Object

Raises:



54
55
56
57
58
# File 'lib/request.rb', line 54

def cancel_preapproval(data)
  raise NoDataError unless data

  call_api(data, "/AdaptivePayments/CancelPreapproval")
end

#convert_currency(data) ⇒ Object

Raises:



60
61
62
63
64
# File 'lib/request.rb', line 60

def convert_currency(data)
  raise NoDataError unless data

  call_api(data, "/AdaptivePayments/ConvertCurrency")
end

#create_account(data) ⇒ Object

Raises:



72
73
74
75
76
77
# File 'lib/request.rb', line 72

def (data)
  raise NoDataError unless data

  response_data = call_api(data, "/AdaptiveAccounts/CreateAccount")
  PaypalAdaptive::Response.new(response_data, @env)
end

#pay(data) ⇒ Object

Raises:



28
29
30
31
32
33
# File 'lib/request.rb', line 28

def pay(data)
  raise NoDataError unless data

  response_data = call_api(data, "/AdaptivePayments/Pay")
  PaypalAdaptive::Response.new(response_data, @env)
end

#payment_details(data) ⇒ Object

Raises:



35
36
37
38
39
# File 'lib/request.rb', line 35

def payment_details(data)
  raise NoDataError unless data

  call_api(data, "/AdaptivePayments/PaymentDetails")
end

#preapproval(data) ⇒ Object

Raises:



41
42
43
44
45
46
# File 'lib/request.rb', line 41

def preapproval(data)
  raise NoDataError unless data

  response_data = call_api(data, "/AdaptivePayments/Preapproval")
  PaypalAdaptive::Response.new(response_data, @env)
end

#preapproval_details(data) ⇒ Object

Raises:



48
49
50
51
52
# File 'lib/request.rb', line 48

def preapproval_details(data)
  raise NoDataError unless data

  call_api(data, "/AdaptivePayments/PreapprovalDetails")
end

#refund(data) ⇒ Object

Raises:



66
67
68
69
70
# File 'lib/request.rb', line 66

def refund(data)
  raise NoDataError unless data

  call_api(data, "/AdaptivePayments/Refund")
end

#validateObject



19
20
21
22
23
24
25
26
# File 'lib/request.rb', line 19

def validate
  #TODO the receiverList field not validating properly

  # @@schema_filepath = "../lib/pay_request_schema.json"
  # @@schema = File.open(@@schema_filepath, "rb"){|f| JSON.parse(f.read)}
  # see page 42 of PP Adaptive Payments PDF for explanation of all fields.
  #JSON::Schema.validate(@data, @@schema)
end