Portal:Payment Processing

From Perl Guilds - Getting Medieval with Perl
Jump to navigation Jump to search

Introduction[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

  • 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.
  • PayPal has a button generator also - see this.

Cutting-out PayPal and Strip[edit | edit source]

Background on Payment Gateways and Processors[edit | edit source]

wanted! - secret information on setting up your own merchant accounts, dealing directly with payment gateways like First Data

List of Payment Processors[edit | edit source]