Portal:Payment Processing: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 3: | Line 3: | ||
==Perl Modules== | ==Perl Modules== | ||
There are modules on CPAN for handingling things like PayPal "IPNs" | There are modules on CPAN for handingling things like PayPal "IPNs," e.g., [https://metacpan.org/pod/Business::PayPal::IPN Business::PayPal::IPN]. However, it can be done more natively using a modern web application framework such as [https://metacpan.org/pod/Dancer2 Dancer2]. | ||
==Example Dancer Handler for PayPal IPNs== | ==Example Dancer Handler for PayPal IPNs== |
Revision as of 14:30, 24 January 2025
Introduction
It is very easy to accept payments online using Perl; as this is a fundamental activity for any sustainable enterprise we shall endeavor to cover it as completely as possible here.
Perl Modules
There are modules on CPAN for handingling things like PayPal "IPNs," e.g., Business::PayPal::IPN. However, it can be done more natively using a modern web application framework such as Dancer2.
Example Dancer Handler for PayPal IPNs
Paypal IPNs are old school webhooks that tell Paypal to hit an URL whenever you receive a payment. We will not cover how to set this up through PP, but here's a sketch snippet that will create a Dancer2 app that handles IPNs.
package Application::ppipnd; use Dancer2; use HTTP::Tiny (); use Util::H2O::More qw/h2o/; our $VERSION = '1.0'; #my $PP_WEBSRC = q{https://www.sandbox.paypal.com/YOURTEST/webscr}; my $PP_WEBSRC = q{https://www.paypal.com/cgi-bin/webscr}; post '/ipn' => sub { my %body = params('body'); my $http = HTTP::Tiny->new; # handshake back to PayPal (this here you confirm it is actually from PayPal) my $resp = $http->post( $PP_WEBSRC, {%body} ); #... if you trust what $resp tells you, then process the request # save raw body $body{raw} = JSON::XS::encode_json( \%body ); # now process IPN (e.g.,...) my $ipn = h2o \%body, qw/raw txn_type subscr_id parent_txn_id option_selection1 option_selection2 txn_id first_name/; # hard pass if {txn_type} is not defined in %body if ( not $ipn->txn_type ) { printf qq{%s: %s request that contained no txn_type field. Replied as Teapot (418).\n}, INFO, REJECTED; status q{i_m_a_teapot}; #418 send_as html => q{I'm a teapot.}; } return q{ok}; # 200 OK is also required by PayPal's IPN sender to assume success };
Notes:
- PayPal IPNs require HTTPS
- There is an IPN generator to test at PayPal's developer site, but you can also employ a curl script, provided you get all the IPN variables correct; see an example here.