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



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

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:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pp-adaptive/client.rb', line 86

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

#express_checkout_url(token) ⇒ Object



229
230
231
# File 'lib/pp-adaptive/client.rb', line 229

def express_checkout_url(token)
  "https://www.#{sandbox? ? "sandbox." : ""}paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=#{token}"
end

#express_handshake(options) {|String| ... } ⇒ String

Execute an express handshake (“SetExpressCheckout”) Request.

Examples:

response = client.express_handshake(email: "[email protected]", receiver_amount: 0.51, currency_code: "USD", return_url: "https://www.example.com/return_path", cancel_url: "https://www.example.com/cancel_path" )
puts response
=> "TOKEN=EC%2d32P548528Y663714N&TIMESTAMP=2016%2d03%2d08T11%3a39%3a34Z&CORRELATIONID=51efed895c8a8&ACK=Success&VERSION=124&BUILD=18316154"

Yields:

  • (String)

    optional way to receive the return value

Returns:

  • (String)

    a response object for the given operation



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pp-adaptive/client.rb', line 120

def express_handshake(options)
  hash = {
    "USER" => user_id,
    "PWD" => password,
    "SIGNATURE" => signature,
    "METHOD" => "SetExpressCheckout",
    "RETURNURL" => options[:return_url], # URL of your payment confirmation page
    "CANCELURL" => options[:cancel_url], # URL redirect if customer cancels payment
    "VERSION" => 124, # this may increase in the future
    "SOLUTIONTYPE" => "Sole",
    "LANDINGPAGE" => "Billing",
    "EMAIL" => options[:email],
    "PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID" => options[:receiver_email], # explictly set what paypal account will recieve the payment
    "PAYMENTREQUEST_0_PAYMENTACTION" => "SALE",
    "PAYMENTREQUEST_0_AMT" => options[:receiver_amount],
    "PAYMENTREQUEST_0_CURRENCYCODE" => options[:currency_code],
    "SUBJECT" => options[:receiver_email]
  }

  response = post_to_express_endpoint(hash)

  yield response if block_given?
rescue RestClient::Exception => e
  raise AdaptivePayments::Exception, e
end

#express_perform(options) {|String| ... } ⇒ String

Execute an express handshake (“SetExpressCheckout”) Request.

Examples:

response = client.express_perform(receiver_amount: 0.51, currency_code: "USD", token: "token_returned_with_user_from_paypal", PayerID: "id_returned_with_user_from_paypal"  )
puts response
=> "TOKEN=EC%2d77X70771J7730413M&SUCCESSPAGEREDIRECTREQUESTED=false&TIMESTAMP=2016%2d03%2d08T11%3a22%3a13Z&CORRELATIONID=46f0eef897abd&ACK=Success&VERSION=124&BUILD=18316154&INSURANCEOPTIONSELECTED=false&SHIPPINGOPTIONISDEFAULT=false&PAYMENTINFO_0_TRANSACTIONID=1BG7064136737244V&PAYMENTINFO_0_TRANSACTIONTYPE=expresscheckout&PAYMENTINFO_0_PAYMENTTYPE=instant&PAYMENTINFO_0_ORDERTIME=2016%2d03%2d08T11%3a22%3a12Z&PAYMENTINFO_0_AMT=49%2e95&PAYMENTINFO_0_TAXAMT=0%2e00&PAYMENTINFO_0_CURRENCYCODE=EUR&PAYMENTINFO_0_PAYMENTSTATUS=Pending&PAYMENTINFO_0_PENDINGREASON=multicurrency&PAYMENTINFO_0_REASONCODE=None&PAYMENTINFO_0_PROTECTIONELIGIBILITY=Ineligible&PAYMENTINFO_0_PROTECTIONELIGIBILITYTYPE=None&PAYMENTINFO_0_SECUREMERCHANTACCOUNTID=97HKEN2K8JCZY&PAYMENTINFO_0_ERRORCODE=0&PAYMENTINFO_0_ACK=Success"

Yields:

  • (String)

    optional way to receive the return value

Returns:

  • (String)

    a response object for the given operation



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pp-adaptive/client.rb', line 158

def express_perform(options)
  hash = {
    "USER" => user_id,
    "PWD" => password,
    "SIGNATURE" => signature,
    "METHOD" => "DoExpressCheckoutPayment",
    "VERSION" => 124,
    "TOKEN" => options[:token],
    "PAYERID" => options[:PayerID],
    "PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID" => options[:receiver_email], # explictly set what paypal account will recieve the payment
    "PAYMENTREQUEST_0_PAYMENTACTION" => "SALE",
    "PAYMENTREQUEST_0_AMT" => options[:receiver_amount],
    "PAYMENTREQUEST_0_CURRENCYCODE" => options[:currency_code],
    "SUBJECT" => options[:receiver_email]
  }

  response = post_to_express_endpoint(hash)

  yield response if block_given?
rescue RestClient::Exception => e
  raise AdaptivePayments::Exception, e
end

#mobile?Boolean

Test if the client is using the responsive mobile checkout.

Returns:

  • (Boolean)

    true if using the responsive mobile checkout



56
57
58
# File 'lib/pp-adaptive/client.rb', line 56

def mobile?
  checkout_type == "mobile"
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



220
221
222
223
224
225
226
227
# File 'lib/pp-adaptive/client.rb', line 220

def payment_url(response)
  case checkout_type
  when "mobile"
    mobile_payment_url(response)
  else # for default "desktop" and as a fallback
    desktop_payment_url(response)
  end
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



188
189
190
191
192
193
194
195
# File 'lib/pp-adaptive/client.rb', line 188

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.



48
49
50
# File 'lib/pp-adaptive/client.rb', line 48

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

#sandbox?Boolean

Test if the client is using the sandbox.

Returns:

  • (Boolean)

    true if using the sandbox



64
65
66
# File 'lib/pp-adaptive/client.rb', line 64

def sandbox?
  !!@sandbox
end