Net::PayPal - Perl extension for PayPal's REST API server
use Net::PayPal; my $p = Net::PayPal->new($client_id, $client_secret); my $payment = $p->cc_payment({ cc_number => '4353185781082049', cc_type => 'visa', cc_expire_month => 3, cc_expire_year => 2018, amount => 19.95, }); unless ( $payment ) { die $p->error; } unless ( $payment->{state} eq "approved" ) { printf("Your payment was not approved"); }
Since as of this writing (March 10th, 2013) PayPal's REST api was still in BETA state it's fair to consider Net::PayPal is an ALPHA software, meaning any part of this module may change in subsequent releases. In the meantime any suggestions and feedback and contributions are welcome.
Consult CHANGES file in the root folder of the distribution before upgrading
Net::PayPal implements PayPal's REST API. Visit http://developer.paypal.com for further information.
To start using Net::PayPal the following actions must be completed to gain access to API endpoints:
Sign up for a developer account by visiting http://developer.paypal.com. It is free!
Under "Applications" tab (after signing into developer.paypal.com) make note of secret and client_id. You will need these two identifiers to interact with PayPal's API server
secret
client_id
Create Net::PayPal instance using secret and client_id identifiers.
As of this writing the following APIs are implemented. As PayPal's REST Api evolves this module will evolve together
See rest() method for everything else
Following methods are available
Creates and returns an instance of Net::PayPal class. If it's the first time you call this method within 8 hour period it will attempt to authenticate the instance by submitting your credentials to paypal's /v1/oauth/token API. The access token is then cached for 8 hour period in your system's temp folder.
access_token is a very sensitive data. For this reason Net::PayPal encrypts this data using Blowfish algorithm, using your secret as key. As long as you can keep your secret identifier in secret your access token is reasonably safe!
access_token
Caching is very useful. Without cahing each API call in separate processes must attempt to authenticate the API, thus slowing down each API call. By making use of caching technique a separate token is stored for each client_id in the temp folder.
Charges a credit card:
my $payment = $p->cc_payment({ cc_number => '4353185781082049', cc_type => 'visa', cc_expire_month => 3, cc_expire_year => 2018, first_name => 'Sherzod', last_name => 'Ruzmetov', amount => 19.95, }) or die $p->error;
You may choose to store id payment attribute should you wish to lookup payment details in the future. The state of the payment is stored in 'state' attribute:
id
unless ( $payment->{state} eq 'approved' ) { die "Your payment wasn't approved"; }
On error returns undef. Last error message can be queried through error() class method.
The same as cc_payment(), except using a credit card stored in vault
my $payment = $p->cc_payment({ id => 'CARD-ADFA13413241241324' amount => '19.95', currency=> 'USD' });
id is the result of previously invoked store_cc().
Returns previously processed payment information, given the payment ID.
my $payment = $p->get_payment( 'PAY-9D023728F47376036KE5OTKY' );
Returns list of previously processed payments.
my @payments = $p->get_payments;
Stores a credit card profile in the vault:
my $cc = $p->store_cc({ cc_number => '4353185781082049', cc_type => 'visa', cc_expire_month => '3', cc_expire_year => '201'8, cvv2 => '420', first_name => 'Sherzod', last_name => 'Ruzmetov' });
id is probably the most important attribute of the response data. To make a payment using the stored CC see stored_cc_payment() method.
Retrieves stored CC information from the database. Usual, in real world applications there is rarely a need for this method. Since once can already charge a credit card without retrieving it completely.
my $cc = get_cc( $id );
To make up for missing API methods and PayPal's future upgrades to its REST API I decided to provide this convenience method. Basically all other methods of Net::PayPal rely on this method to make things happen. It takes care of all OAuth2 specific authentication that PayPal requires.
For example:
my $r = $pp->cc_payment({ cc_number => '...', cc_type => 'visa', cc_expire_month => '...', cc_expire_year => '...', cvv2 => '...', amount => '19.95', currency => 'USD' });
Is equivalent to:
my $r = $pp->rest('POST', '/v1/payments/payment', { intent => 'sale', payer => { payment_method => "credit_card", funding_instruments => [{credit_card => { number => '...', type => 'visa', expire_month => '...', expire_year => ''', cvv2 => '' }} ], transactions => [{ amount => { total => 19.95, currency => "USD" }, }] });
To learn more about the contents of REST request refer to PayPal's REST API documentation located on https://developer.paypal.com/webapps/developer/docs/api/
rest() really shines if you decided to subclass Net::PayPal.
All the above methods invoke the sandbox API hosted at api.sandbox.paypal.com. Once you're done developing your tool you must go live by calling live(1) BEFORE calling new():
Net::PayPal->live( 1 ); my $pp = Net::PayPal->new($client_id, $secret);
Net::PayPal relies on the following Perl modules. Without these writing this tool would've been very painful, to say the least:
Crypt::SSLeay by Gisle Aas and et. al.
Crypt::Blowfish by Systemics Ltd. and et. al.
Crypt::CBC by Lincoln Stein
Cache::FileCache by DeWitt Clinton
LWP by Gisle Aas
JSON by Makamaka Hannyaharamitu
Sherzod B. Ruzmetov <sherzodr@cpan.org>
Copyright (C) 2013 Sherzod B. Ruzmetov.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install Net::PayPal, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::PayPal
CPAN shell
perl -MCPAN -e shell install Net::PayPal
For more information on module installation, please visit the detailed CPAN module installation guide.