简体   繁体   中英

Get User's PayPal Payments using PayPal ruby gem

I'm trying to use PayPal's ruby gem to do a simple task like this:

1 - User logs into my app using PayPal OAuth.

2 - User authorizes my app to access his account.

3 - My app makes a simple request to list 10 transactions of this User.

I'm following instructions from official PayPal's github repository ( https://github.com/paypal/PayPal-Ruby-SDK )

require 'paypal-sdk-rest'
include PayPal::SDK::REST
include PayPal::SDK::OpenIDConnect

# Update client_id, client_secret and redirect_uri
PayPal::SDK.configure({
  :openid_client_id     => "client_id",
  :openid_client_secret => "client_secret",
  :openid_redirect_uri  => "http://google.com"
})

# Generate URL to Get Authorize code
puts Tokeninfo.authorize_url( :scope => "openid profile" )

# Create tokeninfo by using AuthorizeCode from redirect_uri
tokeninfo = Tokeninfo.create("Replace with Authorize Code received on redirect_uri")
puts tokeninfo.to_hash

# Refresh tokeninfo object
tokeninfo = tokeninfo.refresh
puts tokeninfo.to_hash

After this simple setup code, I'm trying to get user's transactions, but I can't do this just using a Payment entity like below code:

Payment.all(count: 10)

It returns an error:

PayPal::SDK::Core::Exceptions::UnauthorizedAccess: Failed.  Response code = 401.  Response message = Unauthorized.  Response debug ID = 409003e14dd9a, 409003e14dd9a.

So, how can I authenticate a user into my app and after that use his token to do API operations?

I'm not sure you need all this.

Here is what I have done:

include PayPal::SDK::REST

PayPal::SDK.configure({ 
  openid_client_id: "client_id",
  openid_client_secret: "client_secret",
  openid_redirect_uri: "http://google.com"
})

Payment.all
=>

Request[get]: https://api.sandbox.paypal.com/v1/payments/payment
Request.body=null   request.header={"X-PAYPAL-SANDBOX-EMAIL-ADDRESS"=>"Platform.sdk.sell@gmail.com", "Authorization"=>"Bearer A101.CLf7NBF8CWMt0Lt4tc9OzrKs9RmGB8wuDBw11n1Ucn8DWTyws8A-nM5b68NYpn8F.qiud2q4fOpca2RSfhZElDV4fzLm", "Content-Type"=>"application/json", "User-Agent"=>"PayPalSDK/PayPal-Ruby-SDK 1.4.4 (paypal-sdk-core 1.4.4; ruby 2.3.0p0-x86_64-linux;OpenSSL 1.0.1f 6 Jan 2014)"}
Response[200]: OK, Duration: 30.002s
Response.body={"payments":[{"id":"PAY-
...etcetc

Worth noting, at this time it only shows payments created via the REST API.

Although, 401 is unauthorized, so it is likely you got the info incorrect. Make sure your client_id and client_secret is correct - you can find this information under the developer dashboard.

It does support this now using the Transactions API.

https://developer.paypal.com/docs/api/sync/v1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM