Class: AdaptivePayments::Client
- Inherits:
-
Object
- Object
- AdaptivePayments::Client
- Defined in:
- lib/pp-adaptive/client.rb
Overview
The principle hub through which all requests and responses are passed.
Instance Method Summary collapse
-
#execute(*args) {|AbstractResponse| ... } ⇒ AbstractResponse
Execute a Request.
- #express_checkout_url(token) ⇒ Object
-
#express_handshake(options) {|String| ... } ⇒ String
Execute an express handshake (“SetExpressCheckout”) Request.
-
#express_perform(options) {|String| ... } ⇒ String
Execute an express handshake (“SetExpressCheckout”) Request.
-
#initialize(options = {}) ⇒ Client
constructor
Initialize the client with the given options.
-
#mobile? ⇒ Boolean
Test if the client is using the responsive mobile checkout.
-
#payment_url(response) ⇒ String
When initiating a payment, get the URL on paypal.com to send the user to.
-
#preapproval_url(response) ⇒ String
When initiating a preapproval, get the URL on paypal.com to send the user to.
-
#sandbox=(flag) ⇒ Object
Turn on/off sandbox mode.
-
#sandbox? ⇒ Boolean
Test if the client is using the sandbox.
Constructor Details
#initialize(options = {}) ⇒ Client
Initialize the client with the given options.
Options can also be passed via the accessors, if prefered.
42 43 44 45 |
# File 'lib/pp-adaptive/client.rb', line 42 def initialize( = {}) super self.sandbox = [:sandbox] end |
Instance Method Details
#execute(*args) {|AbstractResponse| ... } ⇒ AbstractResponse
Execute a Request.
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.
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() hash = { "USER" => user_id, "PWD" => password, "SIGNATURE" => signature, "METHOD" => "SetExpressCheckout", "RETURNURL" => [:return_url], # URL of your payment confirmation page "CANCELURL" => [:cancel_url], # URL redirect if customer cancels payment "VERSION" => 124, # this may increase in the future "SOLUTIONTYPE" => "Sole", "LANDINGPAGE" => "Billing", "EMAIL" => [:email], "PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID" => [:receiver_email], # explictly set what paypal account will recieve the payment "PAYMENTREQUEST_0_PAYMENTACTION" => "SALE", "PAYMENTREQUEST_0_AMT" => [:receiver_amount], "PAYMENTREQUEST_0_CURRENCYCODE" => [:currency_code], "SUBJECT" => [: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.
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() hash = { "USER" => user_id, "PWD" => password, "SIGNATURE" => signature, "METHOD" => "DoExpressCheckoutPayment", "VERSION" => 124, "TOKEN" => [:token], "PAYERID" => [:PayerID], "PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID" => [:receiver_email], # explictly set what paypal account will recieve the payment "PAYMENTREQUEST_0_PAYMENTACTION" => "SALE", "PAYMENTREQUEST_0_AMT" => [:receiver_amount], "PAYMENTREQUEST_0_CURRENCYCODE" => [:currency_code], "SUBJECT" => [: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.
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.
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.
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.
64 65 66 |
# File 'lib/pp-adaptive/client.rb', line 64 def sandbox? !!@sandbox end |