Class: AdaptivePayments::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pp-adaptive/client.rb

Overview

The principle hub through which all requests and responses are passed.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize the client with the given options.

Options can also be passed via the accessors, if prefered.

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options



36
37
38
39
# File 'lib/pp-adaptive/client.rb', line 36

def initialize(options = {})
  super
  self.sandbox = options[:sandbox]
end

Instance Method Details

#execute(*args) {|AbstractResponse| ... } ⇒ AbstractResponse

Execute a Request.

Examples:

response = client.execute(:Refund, pay_key: "abc", amount: 42)
puts response.ack_code

Yields:

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pp-adaptive/client.rb', line 72

def execute(*args)
  request =
    case args.size
    when 1 then args[0]
    when 2 then AbstractRequest.for_operation(args[0]).new(args[1])
    else
      raise ArgumentError, "Invalid arguments: #{args.size} for (1..2)"
    end

  resource = RestClient::Resource.new(api_url, :headers => headers)
  response = resource[request.class.operation.to_s].post(
    request.to_json,
    :content_type => :json,
    :accept       => :json
  )
  request.class.build_response(response).tap do |res|
    yield res if block_given?
  end
rescue RestClient::Exception => e
  raise AdaptivePayments::Exception, e
end

#payment_url(response) ⇒ String

When initiating a payment, get the URL on paypal.com to send the user to.

Parameters:

  • response (PayResponse)

    the response when setting up the payment

Returns:

  • (String)

    the URL on paypal.com to send the user to



117
118
119
120
121
122
123
124
# File 'lib/pp-adaptive/client.rb', line 117

def payment_url(response)
  [
    "https://www.",
    ("sandbox." if sandbox?),
    "paypal.com/webscr?cmd=_ap-payment&paykey=",
    response.pay_key
  ].join
end

#preapproval_url(response) ⇒ String

When initiating a preapproval, get the URL on paypal.com to send the user to.

Parameters:

Returns:

  • (String)

    the URL on paypal.com to send the user to



101
102
103
104
105
106
107
108
# File 'lib/pp-adaptive/client.rb', line 101

def preapproval_url(response)
  [
    "https://www.",
    ("sandbox." if sandbox?),
    "paypal.com/webscr?cmd=_ap-preapproval&preapprovalkey=",
    response.preapproval_key
  ].join
end

#sandbox=(flag) ⇒ Object

Turn on/off sandbox mode.



42
43
44
# File 'lib/pp-adaptive/client.rb', line 42

def sandbox=(flag)
  @sandbox = !!flag
end

#sandbox?Boolean

Test if the client is using the sandbox.

Returns:

  • (Boolean)

    true if using the sandbox



50
51
52
# File 'lib/pp-adaptive/client.rb', line 50

def sandbox?
  !!@sandbox
end