forked from ianhalpern/python-payment-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
152 lines (124 loc) · 4.94 KB
/
README
File metadata and controls
152 lines (124 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
Python Payment Processor - (c) Ian Halpern 2010
Python Payment Processor is a library for python that acts as a simple abstraction around various payment methods and payment gateways. It includes support for standard gateways such as Authorize.net and is always working to expand it's supported gateway repertoire. Python Payment is currently being used in production systems transferring tens of thousands of dollars a month.
Python Payment Processor is released under the MIT license.
*A special thanks to Brandon Stoner for collaborating on the initial concept design.
INSTALL:
To install python-payment-processor on a linux system run:
$ sudo ./setup.py install
HOW TO:
Using python-payment-processor is very straight forward.
Here is a simple example using the authroize.net payment gateway. This example
creates a authorize.net payment gateway and a credit card transaction and
processes the transaction.
----------------------------------------------------
import payment_processor
from payment_processor.exceptions import *
# the authorize.net gateway requires valid authorize.net 'login' and 'trans_key' variables
gateway = payment_processor.Gateway( 'authorize.net', login='XXX', trans_key='XXX' )
# other authroize.net variables include 'use_test_url' which, if set,
# will use https://test.authorize.net/ instead of https://secure.authorize.net
# if you are using a developer account
card = payment_processor.methods.CreditCard(
card_number=4011111111111111,
expiration_date=datetime.datetime( 2014, 1, 1 ),
first_name='First',
last_name='Last',
zip_code='10001',
address='1 Somewhere Ave',
city='New York',
state='NY'
)
payment = payment_processor.PaymentInfo(
amount = 20,
customer_id = 1,
order_number = '43DJ-7203-D897-SS97',
ship_first_name = 'First',
ship_last_name = 'Last',
ship_address = '1 Somewhere Ave',
ship_city = 'New York',
ship_state = 'NY',
ship_email = 'email@example.com',
ship_phone = '222-333-4444',
ip = '65.192.14.10',
description = 'Some Order'
)
t = payment_processor.Transaction( payment=payment, method=card, gateway=gateway )
try:
t.process()
except TransactionDeclined:
# The transaction requested was declined for such reasons as insufficient funds or flagged for fraud.
raise
except InvalidCardNumber:
# The credit card number provided was invalid.
raise
except InvalidCardExpirationDate:
# The credit card expiration date provided was invalid.
raise
except InvalidCardCode:
# The credit card code provided was invalid.
raise
except InvalidRoutingNumber:
# The routing number provided was invalid (only applicable to Check methods).
raise
except InvalidAccountNumber:
# The account number provided was invalid (only applicable to Check methods).
raise
except InvalidBillingAddress:
# The billing address provided was invalid.
raise
except InvalidBillingZipcode:
# The billing zipcode provided was invalid.
raise
except TransactionAmountLimitExceeded:
# The per-transaction limit was exceeded.
raise
except TransactionFailed:
# The transaction failed for other reasons usually relating using python-payment in ways unsupported by the gateway.
raise
----------------------------------------------------
Using payment_processor.Gateway always take a string as the first argument which is used as
a simple way to dynamically load a specific gateway found in payment.gateways.
You can easily create a gateway outside of the python-payment-processor module, just create a
class that overloads payment.gateways.GenericGateway.
----------------------------------------------------
from payment_processor.gateways import GenericGateway
class MyGateway( GenericGateway )
...
----------------------------------------------------
You can use payment_processor asynchronously
----------------------------------------------------
def callback( t ):
try:
t.handleResponse()
except TransactionDeclined:
# The transaction requested was declined for such reasons as insufficient funds or flagged for fraud.
raise
except InvalidCardNumber:
# The credit card number provided was invalid.
raise
except InvalidCardExpirationDate:
# The credit card expiration date provided was invalid.
raise
except InvalidCardCode:
# The credit card code provided was invalid.
raise
except InvalidRoutingNumber:
# The routing number provided was invalid (only applicable to Check methods).
raise
except InvalidAccountNumber:
# The account number provided was invalid (only applicable to Check methods).
raise
except InvalidBillingAddress:
# The billing address provided was invalid.
raise
except InvalidBillingZipcode:
# The billing zipcode provided was invalid.
raise
except TransactionAmountLimitExceeded:
# The per-transaction limit was exceeded.
raise
except TransactionFailed:
# The transaction failed for other reasons usually relating using python-payment in ways unsupported by the gateway.
raise
t = payment_processor.Transaction( payment=payment, method=card, gateway=gateway, async=True, callback=callback )
t.process()