
Security News
Another Round of TEA Protocol Spam Floods npm, But It’s Not a Worm
Recent coverage mislabels the latest TEA protocol spam as a worm. Here’s what’s actually happening.
onlinepayment
Advanced tools
This module provides an API wrapper around a variety of payment providers. Using this module you can write code that will work the same regardless of the payment provider in use.
Examples::
from onlinepayment import OnlinePayment
auth= { 'login': 'YOUR LOGIN HERE', 'key': 'YOUR KEY HERE' }
op = OnlinePayment('authnet', test_mode=True, auth=auth)
auth= { 'username': 'YOUR USERNAME HERE', 'password': 'YOUR PASSWORD HERE', 'vendor': 'YOUR VENDOR HERE', 'product': 'YOUR PRODUCT HERE' }
op = OnlinePayment('paypal', test_mode=True, auth=auth)
try: result = op.sale(first_name = 'Joe', last_name = 'Example', address = '100 Example Ln.', city = 'Exampleville', state = 'NY', zip = '10001', amount = '2.00', card_num = '4007000000027', exp_date = '0530', card_code = '1234')
except conn.TransactionDeclined: # do something when the transaction fails
except conn.CardExpired: # tell the user their card is expired
except conn.ProcessorException: # handle all other possible processor-generated exceptions generically
success = result.success code = result.code message = result.message trans_id = result.trans_id
orig = result.orig
Before you can use this module you must install one or more payment processors. To install the PayPal payflowpro package::
To install the zc.authorizedotnet package and the authorize package (for recurring support)::
If you want authorize.net support you'll need to install a patched version of zc.ssl. Hopefully someday this will be released by the Zope devs, but so far I haven't heard anything back. Download the zc.ssl source package from here::
Now download and apply my zc-ssl-timeout.patch::
And install the patched module::
(You may also need to edit setup.py and remove the 'ssl-for-setuptools' dependecy. I did, although it may be a quirk of my Python install rather than a general problem.)
Once you have a payment processor you can install this module:
When creating a new connection the following named parameters are available::
auth - required parameter containing connection-specific login info.
debug - causes the connection classes to emit debugging info via logging calls.
test_mode - sets the connection to test-mode. The actual meaning is processor-specific but in all cases it should mean that no charges will actually happen.
The following methods are available to initiate transactions:
Process a charge on a card immediately.
Requests authorization for a charge but does not process it. The result object will contain the trans_id needed to complete the charge with capture.
Processes a previously authorized transaction.
Voids a previous transaction. In most cases this must be a recent transaction which has not yet actually resulted in funds transfered.
Credits a previously charged amount of money back to the payer.
Creates a subscription that will charge the provided card on an ongoing basis.
Updates the settings for an existing subscription.
Cancels a subscription.
All transaction methods accept the same parameters, although not all parameters may be applicable to every transaction type. The actual rules vary according to the processor, unfortunately. The list of parameters accepted is::
address
amount
card_code
card_num
city
company
country
description
email
exp_date
extra
first_name
invoice_num
last_name
phone
ship_to_address
ship_to_company
ship_to_country
ship_to_city
ship_to_first_name
ship_to_last_name
ship_to_state
ship_to_zip
state
tax
trans_id
zip
In cases where these names do not match the names expected by the processor a mapping is employed. If you need to pass parameters that have no equivalent above, you can pass them via the 'extra' parameter, which accepts a dictionary of key-value pairs. For example, to pass in the special 'duplicate_window' parameter to authorize.net::
op.sale(..., extra={'duplicate_window':0})
You can also set extra parameters when you create your OnlinePayment object and they will be applied to all transaction methods called::
op = OnlinePayment('authnet', auth=..., extra={'duplicate_window':0})
Successful transactions return an OnlinePaymentResults object. This object has the following attributes::
success - true if the transaction succeeded
code - the result code returned by the processor.
message - the message returned by the processor.
trans_id - the transaction ID returned by the processor (aka pnref for payflowpro). This is the value you need to keep to be able to call capture(), void() or credit() later.
orig - the raw data returned by the underlying connection library. You might need this for debugging purposes.
Unsuccessful transactions result in...
When a transaction fails an exception will be raised. The exception classes are all attributes of the connection object, so you don't need to import anything to reference them.
All exceptions raised during transaction processing are sub-classes of ProcessorException, so if you want to just catch all possible errors and handle them the same you can write::
try: result = op.authorize(amount='2.00', cc_num='4007000000027', cc_exp='0530') except op.ProcessorException: print("It didn't work and I don't care why. Cry some more!")
The exception object contains information about why it failed, aside from its type. The attributes available are::
code - the result code returned by the processor.
msg - the message returned by the processor.
result - the OnlinePaymentResult object which would have been returned if the request had succeed
You might use them to log the processor's code and message::
try: result = conn.authorize(amount='2.00', cc_num='4007000000027', cc_exp='0530') except conn.ProcessorException as e: log.warning("Processor returned code %d, message %s" % (e.code, e.msg))
In addition a method is available called description() which contains something reasonable to display to an end user. For example::
except op.ProcessorException, e: print("Sorry, that didn't work. %s" % e.description())
In the case of a CardExpired exception that would print::
Sorry, that didn't work. The credit card you entered is expired.
You can also catch and handle specific error conditions. The available classes are::
AVSFailure
AcceptedForReview
AmountInvalid
AmountTooHigh
AuthError
CCVInvalid
CardExpirationInvalid
CardExpired
CardNumberInvalid
DuplicateTransaction
FraudCheckFailed
InvalidCardType
TransactionDeclined
TryAgainLater
Of course this isn't a complete list of all the errors any processor could return. I've taken the approach that it's most useful to identify the errors that a user might reasonable be able to fix. The rest just end up as generic ProcessorException errors and you can examine their code and message if you wish to differentiate.
The authorize.net connection class uses the zc.authorizedotnet library. You can learn more about it here:
http://pypi.python.org/pypi/zc.authorizedotnet/1.3
Since this module started with auth.net you'll find that the parameter names are mostly the same.
This module performs the following mapping to express parameters in Authorize.net language::
'term' : 'length',
'payperiod' : 'unit',
Auth parameters should look like::
auth= { 'login': 'YOUR LOGIN HERE', 'key': 'YOUR KEY HERE' }
The paypal connection class uses the payflowpro library. The project is here:
http://code.google.com/p/python-payflowpro/
This module performs the following mapping to express parameters in payflowpro language::
'card_num' : 'acct',
'card_code' : 'cvv2',
'exp_date' : 'expdate',
'amount' : 'amt',
'address' : 'street',
'company' : 'companyname',
'description' : 'comment1',
'ship_to_address' : 'shiptostreet',
'ship_to_first_name' : 'shiptofirstname',
'ship_to_last_name' : 'shiptolastname',
'ship_to_country' : 'shiptocountry',
'ship_to_city' : 'shiptocity',
'ship_to_state' : 'shiptostate',
'ship_to_zip' : 'shiptozip',
'first_name' : 'firstname',
'last_name' : 'lastname',
'phone' : 'phonenum',
'invoice_num' : 'ponum',
If you need to set any parameters not in this set just use the 'extra' parameter described above.
Note that the payflowpro API only uses trans_id for credit(), void() and capture(). You can pass other values but they are ignored.
Auth parameter setup should look like::
auth= { 'username': 'YOUR USERNAME HERE', 'password': 'YOUR PASSWORD HERE', 'vendor': 'YOUR VENDOR HERE', 'product': 'YOUR PRODUCT HERE' }
Sam Tregar - framework, one-time processing
Aaron Ross - recurring billing
We Also Walk Dogs - maintainance, bug fixes, morale boost
Jason Kohles - created Business::OnlinePayment, the model for this module
Copyright (c) 2009, 2010, We Also Walk Dogs All rights reserved.
Onlinepayment is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
FAQs
a generic Python API for making online payments
We found that onlinepayment demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Recent coverage mislabels the latest TEA protocol spam as a worm. Here’s what’s actually happening.

Security News
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads

Research
/Security News
A malicious Chrome extension posing as an Ethereum wallet steals seed phrases by encoding them into Sui transactions, enabling full wallet takeover.