I'm trying to help a friend (a Grails programmer) that has a problem testing the Paypal IPN listener with IPN simulator: he get always an INVALID response to the post.
So i wrote a Ruby version of the listener using Sinatra and http gem but i got stuck me too.
Here the very short piece of code for Sinatra server:
require 'sinatra'
require 'http'
post '/' do
request.body.rewind
response = HTTP.post("https://ipnpb.sandbox.paypal.com/cgi-bin/webscr", body: "cmd=_notify-validate&#{request.body.read}")
logger.info "response word: #{response.to_s}" # INVALID
logger.info "response code: #{response.code}" # 200
end
I ran a tunnel to localhost (where Sinatra app is listening) using ngrok.
The app receive correctly the POST from the Paypal IPN simulator, add the required pair cmd=_notify-validate, return a POST. But the response is always "INVALID".
I tried to run the code on Heroku but in vain.
Any idea?
Thanks
Related
I have implemented a Stripe webhook to handle payment events in my Django rest framewrok application. When I test the webhook locally, it works as expected and I am able to receive and process the payment events. However, when I deploy the application and try to use the webhook, I receive a 301 error from Stripe. There is no response from the webhook call as shown in Stripe dashboard. The webhook URL is reachable and I am able to access it without any issues. I have also checked the logs and there are no errors on the server side, which mean that the content of post function is not executed.
I am not sure what is causing this issue and would appreciate any help in troubleshooting and fixing it. Thank you.
The webhook url
urlpatterns = [
path('stripe-webhook', stripe_webhook.as_view()),]
The webhook function is as shown:
class stripe_webhook(APIView):
def post(self, request):
#verify webhook request
print(request.body)
payload = request.body
sig_header = request.headers['STRIPE_SIGNATURE']
event = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError as e:
# Invalid payload
raise e
except stripe.error.SignatureVerificationError as e:
# Invalid signature
raise e
# Handle the event
if event['type'] == 'payment_intent.succeeded':
payment_intent = event['data']['object']
print(payment_intent)
else:
print('Unhandled event type {}'.format(event['type']))
return HttpResponse(status=200)
You might happen to have a middleware or a load balancer for the server hosting your webhook endpoint which might explain 301 status code. Just a guess though. You would likely want to talk to your hosting provider to see if they can shed any lights on why the requests are being redirected once they come in.
Some stripe events trigger a 503 Error at my webhhok endpoint in php.
E.g.: If i test payment_intent.succeeded or payment_method.attached in stripe backend I receive
Test-Webhook-Error: 503
Invalid encoding: ISO-8859-1
If i test payment_intent.created everything works fine.
HTTPS works properly.
I am trying to fetch from API server using Net::HTTP.
puts "#{uri}".green
response = Net::HTTP.new('glassdoor.com').start { |http|
# always proxy via your.proxy.addr:8080
response = http.get(uri, {'Accept' => 'application/json'})
puts "Res val: #{response.body}".blue
}
I got the uri from the console and pasted in the browser, and I received the JSON response.
But using the Ruby Net::HTTP get I receive some security message:
Why the difference? The browser and the Ruby script are behind the same public IP.
You were detected as a crawler (correctly, by the way). Note that those requests (from browser and the script) are not just the same. The browser sends some headers, such as accepted language, user agent etc. You can peek into it using web inspector tool in the browser. On the other side, in your script you only set Accept header (and to JSON, suspicious on its own, as browser would never do that). And you do not send any user agent. It's easy to see that this is an automates request, not natural traffic from the browser.
I am trying to use the Instagram API to create a rails background worker to query hashtags. I don't need to log in any other user but myself furthermore I don't want to have to use any browsers, just RESTful calls.
I'm trying to automate getting my access token in a Ruby script using the gem "rest-client" (https://github.com/rest-client/rest-client)
I can successfully navigate to the following url in a browser and get the access token from the response url
I have used both this URL:
https://www.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=xxx&response_type=token
BUT When I use the RESTful gem response = RestClient.get(url) the
response.headers['location'] is nil
I have also tried using the Instagram API URL but no luck: https://api.instagram.com/oauth/authorize/?client_id=xxx&redirect_uri=xxx&response_type=code
Anyone know how to get the access token completely programmatically in Ruby?
I think I'm missing the step to log in the user (which will be me). Not sure how to do this programatically.
Can I use the instagram API with a code or access token that never changes?
rest-client automatically request to get to redirect host.
https://github.com/rest-client/rest-client/blob/master/lib/restclient/abstract_response.rb
I have never used the instagram API yet, but the code to get "Location" on 302 is shown below.
# client
require 'net/http'
require 'uri'
url = URI.parse('http://localhost:2000')
res = Net::HTTP.start(url.host, url.port) do |http|
http.get('/')
end
puts "#{res.header['Location']} is https://google.com"
# test server
require 'socket'
server = TCPServer.new 2000
loop do
socket = server.accept
while header = socket.gets
break if header.chomp.empty?
puts header.chomp
end
socket.puts "HTTP/1.0 302"
socket.puts "Location: https://google.com"
socket.close
end
I hope that this information is helpful to you.
I have a simple problem that is getting me stuck. I have been following this yahoo documentation Yahoo OAuth 2.0 Guide . I have been able to generate authorization URL and even get the authorization code.(that is upto Step 3).
But now I am stuck in step 4: Exchange authorization code for Access Token. I am also using this StackOverflow question Yahoo API with Ruby on Rails and OAUTH2. This is my code(I'm using sinatra):
get '/yahoo/contacts/oauth2callback' do
client = OAuth2::Client.new($consumer_id, $consumer_secret, site: $yahoo_base_url, authorize_url: '/oauth2/request_auth', token_url: '/oauth2/get_token')
code = params[:code] if params[:code]
puts "Code: #{code}"
# token = client.auth_code.get_token(code, redirect_uri: $yahoo_redirect_url, headers: { "Authorization" => Basic })
token = client.auth_code.get_token(code, redirect_uri: $yahoo_redirect_url)
puts "THIS IS THE NEW TOKEN NOW: #{token}"
end
the variable used include:
# for yahoo application
$consumer_id = "dj0yJmk9Q1RKU2x2NTY3WWVxJmQ9WVdrOU1YWnRUV2cyTXpBbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1fth--"
$consumer_secret = "my_secret"
$yahoo_redirect_url = "http://localhost:4567/yahoo/contacts/oauth2callback"
What What is causing the error? Because the error source is this line.
token = client.auth_code.get_token(code, redirect_uri: $yahoo_redirect_url)
what i'm I doing wrong?
Update: I had written the error at the title, and it seems many people can't see it.
the error is
OAuth2::Error invalid_request: {“error”:“invalid_request”}
file: client.rb location: request line: 113
the returned url is looks like this:
http://localhost:4567/yahoo/contacts/oauth2callback?code=bck5tkm.
Where the code being taken is bck5tkm
Assuming you use the intridea OAuth 2.0 client (https://github.com/intridea/oauth2), you may be bumping in to a bug:
https://github.com/intridea/oauth2/pull/192
meaning that Yahoo refuses to permit client credentials in the request body. The pull request has not been merged yet so you'd need to apply that to your own code (or find another gem that works).
You may be using an expired or already used code(can be used just once). Or you have supplied not valid credentials in the HTTP Basic Authorization header on the POST request.