Portal:Payment Processing: Difference between revisions
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
==Perl Modules== | ==Perl Modules== | ||
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]. | 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]. | ||
Modern APIs | |||
PayPal IPNs (instant payment notifications) are pretty primative and emply a passive approach to accepting payments. They're easy to set up quickly, but in the long run a more proactive model for handling payments on your site is better. PayPal does have a modern API, as does Stripe. | |||
See more at, | |||
* [https://metacpan.org/pod/Net::PayPal Net::PayPal] | |||
* [https://metacpan.org/pod/Net::Stripe Net::Stripe] | |||
==Example Dancer Handler for PayPal IPNs== | ==Example Dancer Handler for PayPal IPNs== |
Revision as of 14:34, 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.
Modern APIs
PayPal IPNs (instant payment notifications) are pretty primative and emply a passive approach to accepting payments. They're easy to set up quickly, but in the long run a more proactive model for handling payments on your site is better. PayPal does have a modern API, as does Stripe.
See more at,
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: