in reply to How do you use Paypal IPN with Dancer2?

I have implemented it recently using HTTP::Server::Simple. I think the process should be similar on Dancer2.

(Also your server's IPN URL must use HTTPS and must not contain an IP address or a port number.)

The code is something like this:
my $USE_SIMULATOR = 0; # 0 = live, 1 = sandbox / simulator my $LIVE_URL = 'https://www.paypal.com/cgi-bin/webscr'; # live my $SANDBOX_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; # s +andbox my $PAYPAL_HOST_HEADER = ($USE_SIMULATOR ? 'www.sandbox.paypal.com' : +'www.paypal.com'); my $RESPONSE_URL = ($USE_SIMULATOR ? $SANDBOX_URL : $LIVE_URL); # read POST params my $postdata = read_all ($content_length, $server->stdin_handle); my $post = (length ($postdata) > 0) ? CGI->new ($postdata)->Vars : {}; # extract some transaction details my $txn_type = $post->{txn_type}; my $txn_id = $post->{txn_id}; my $payment_status = lc ($post->{payment_status}); # respond to Paypal's initial HTTP request print "HTTP/1.1 200 OK\r\n\r\n"; # send back the original POST data with our extra field "cmd=_notify-v +alidate" to get an INVALID or VERIFIED response # this makes a new HTTP request my $ua = LWP::UserAgent->new (ssl_opts => { verify_hostname => 1 }); my $req = HTTP::Request->new ('POST', $RESPONSE_URL); $req->content_type('application/x-www-form-urlencoded'); $req->header(Host => $PAYPAL_HOST_HEADER); $postdata = 'cmd=_notify-validate&' . $postdata; $req->content($postdata); my $res = $ua->request($req); # process the response if ($res->is_error) { # connection error ... } elsif ($res->content eq 'VERIFIED') { # ok ... ( further processing ) } elsif ($res->content eq 'INVALID') { # error ... } else { # unexpected error ... }

Replies are listed 'Best First'.
Re^2: How do you use Paypal IPN with Dancer2?
by $h4X4_|=73}{ (Monk) on Jul 18, 2016 at 10:05 UTC

    (Also your server's IPN URL must use HTTPS
    This is not true. Some people have it working on HTTP.

    What you are doing with this part of the code can cause servers to return HTTP 500 Internal Server Error because of malformed header format. The server always sets that header not your program. PayPal requires the HTTP 200 response to be a text/plain header.

    # respond to Paypal's initial HTTP request print "HTTP/1.1 200 OK\r\n\r\n";

      Thank you for the feedback!

      I have edited the part about using HTTPS-only URL-s.

      The code where the HTTP 200 header is returned seemed to work for me in that exact form.*
      (Note, I was using HTTP::Server::Simple, where your program has to print the HTTP status line and all headers.)

      * Actually I'm not sure about that. I will check it sometime soon and update this post if I can verify that.