Skip to content

Commit ef8a95b

Browse files
authored
add more examples/expand testing (AuthorizeNet#30)
* unify the authOnly and authCapture samples * Formatting and minor wording clarifications * add more parameters and comments * fix conditional make this check explicit for future compatibility * more versions
1 parent 8aa264a commit ef8a95b

8 files changed

Lines changed: 114 additions & 27 deletions

File tree

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ python:
55
- "2.7"
66
- "3.4"
77
- "3.5"
8-
# - "pypy" Disabling pypy until travis moves to newer version, known issue with lxml crashing pypy
8+
- "3.6"
9+
- "3.7-dev"
10+
# - "pypy"
11+
# - "pypy3"
12+
# pypy will just crash due to an incompatibility with lxml.
13+
# Newer versions of pypy seem to crash also, so we probably have to fix with a newer version of lxml
914

1015
install:
1116
- pip install authorizenet

PaymentTransactions/authorize-credit-card.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,82 @@
11
import os, sys
22
import imp
3-
import random
43

54
from authorizenet import apicontractsv1
65
from authorizenet.apicontrollers import *
76
constants = imp.load_source('modulename', 'constants.py')
87
from decimal import *
98

10-
def authorize_credit_card():
11-
12-
amount = str(round(random.random()*100, 2))
9+
def authorize_credit_card(amount):
1310

11+
# Create a merchantAuthenticationType object with authentication details
12+
# retrieved from the constants file
1413
merchantAuth = apicontractsv1.merchantAuthenticationType()
1514
merchantAuth.name = constants.apiLoginId
1615
merchantAuth.transactionKey = constants.transactionKey
1716

17+
# Create the payment data for a credit card
1818
creditCard = apicontractsv1.creditCardType()
1919
creditCard.cardNumber = "4111111111111111"
2020
creditCard.expirationDate = "2020-12"
21+
creditCard.cardCode = "123"
2122

23+
# Add the payment data to a paymentType object
2224
payment = apicontractsv1.paymentType()
2325
payment.creditCard = creditCard
26+
27+
# Create order information
28+
order = apicontractsv1.orderType()
29+
order.invoiceNumber = "10101"
30+
order.description = "Golf Shirts"
31+
32+
# Set the customer's Bill To address
33+
customerAddress = apicontractsv1.customerAddressType()
34+
customerAddress.firstName = "Ellen"
35+
customerAddress.lastName = "Johnson"
36+
customerAddress.company = "Souveniropolis"
37+
customerAddress.address = "14 Main Street"
38+
customerAddress.city = "Pecan Springs"
39+
customerAddress.state = "TX"
40+
customerAddress.zip = "44628"
41+
customerAddress.country = "USA"
42+
43+
# Set the customer's identifying information
44+
customerData = apicontractsv1.customerDataType()
45+
customerData.type = "individual"
46+
customerData.id = "99999456654"
47+
customerData.email = "[email protected]"
48+
49+
# Add values for transaction settings
50+
duplicateWindowSetting = apicontractsv1.settingType();
51+
duplicateWindowSetting.settingName = "duplicateWindow"
52+
duplicateWindowSetting.settingValue = "600"
2453

54+
# Create a transactionRequestType object and add the previous objects to it.
2555
transactionrequest = apicontractsv1.transactionRequestType()
2656
transactionrequest.transactionType = "authOnlyTransaction"
27-
transactionrequest.amount = Decimal (amount)
57+
transactionrequest.amount = amount
2858
transactionrequest.payment = payment
59+
transactionrequest.order = order
60+
transactionrequest.billTo = customerAddress
61+
transactionrequest.customer = customerData
62+
transactionrequest.transactionSettings = duplicateWindowSetting
2963

30-
64+
# Assemble the complete transaction request
3165
createtransactionrequest = apicontractsv1.createTransactionRequest()
3266
createtransactionrequest.merchantAuthentication = merchantAuth
3367
createtransactionrequest.refId = "MerchantID-0001"
34-
3568
createtransactionrequest.transactionRequest = transactionrequest
69+
# Create the controller
3670
createtransactioncontroller = createTransactionController(createtransactionrequest)
3771
createtransactioncontroller.execute()
3872

3973
response = createtransactioncontroller.getresponse()
4074

4175
if response is not None:
76+
# Check to see if the API request was successfully received and acted upon
4277
if response.messages.resultCode == "Ok":
78+
# Since the API request was successful, look for a transaction response
79+
# and parse it to display the results of authorizing the card
4380
if hasattr(response.transactionResponse, 'messages') == True:
4481
print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId);
4582
print ('Transaction Response Code: %s' % response.transactionResponse.responseCode);
@@ -50,6 +87,7 @@ def authorize_credit_card():
5087
if hasattr(response.transactionResponse, 'errors') == True:
5188
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode));
5289
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
90+
# Or, print errors if the API request wasn't successful
5391
else:
5492
print ('Failed Transaction.');
5593
if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
@@ -64,4 +102,4 @@ def authorize_credit_card():
64102
return response
65103

66104
if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
67-
authorize_credit_card()
105+
authorize_credit_card(constants.amount)

PaymentTransactions/charge-credit-card.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,76 @@
77
from decimal import *
88

99
def charge_credit_card(amount):
10+
11+
# Create a merchantAuthenticationType object with authentication details
12+
# retrieved from the constants file
1013
merchantAuth = apicontractsv1.merchantAuthenticationType()
1114
merchantAuth.name = constants.apiLoginId
1215
merchantAuth.transactionKey = constants.transactionKey
1316

17+
# Create the payment data for a credit card
1418
creditCard = apicontractsv1.creditCardType()
1519
creditCard.cardNumber = "4111111111111111"
1620
creditCard.expirationDate = "2020-12"
21+
creditCard.cardCode = "123"
1722

23+
# Add the payment data to a paymentType object
1824
payment = apicontractsv1.paymentType()
1925
payment.creditCard = creditCard
26+
27+
# Create order information
28+
order = apicontractsv1.orderType()
29+
order.invoiceNumber = "10101"
30+
order.description = "Golf Shirts"
31+
32+
# Set the customer's Bill To address
33+
customerAddress = apicontractsv1.customerAddressType()
34+
customerAddress.firstName = "Ellen"
35+
customerAddress.lastName = "Johnson"
36+
customerAddress.company = "Souveniropolis"
37+
customerAddress.address = "14 Main Street"
38+
customerAddress.city = "Pecan Springs"
39+
customerAddress.state = "TX"
40+
customerAddress.zip = "44628"
41+
customerAddress.country = "USA"
42+
43+
# Set the customer's identifying information
44+
customerData = apicontractsv1.customerDataType()
45+
customerData.type = "individual"
46+
customerData.id = "99999456654"
47+
customerData.email = "[email protected]"
48+
49+
# Add values for transaction settings
50+
duplicateWindowSetting = apicontractsv1.settingType();
51+
duplicateWindowSetting.settingName = "duplicateWindow"
52+
duplicateWindowSetting.settingValue = "600"
2053

54+
# Create a transactionRequestType object and add the previous objects to it.
2155
transactionrequest = apicontractsv1.transactionRequestType()
2256
transactionrequest.transactionType = "authCaptureTransaction"
2357
transactionrequest.amount = amount
2458
transactionrequest.payment = payment
59+
transactionrequest.order = order
60+
transactionrequest.billTo = customerAddress
61+
transactionrequest.customer = customerData
62+
transactionrequest.transactionSettings = duplicateWindowSetting
2563

26-
64+
# Assemble the complete transaction request
2765
createtransactionrequest = apicontractsv1.createTransactionRequest()
2866
createtransactionrequest.merchantAuthentication = merchantAuth
2967
createtransactionrequest.refId = "MerchantID-0001"
30-
3168
createtransactionrequest.transactionRequest = transactionrequest
69+
# Create the controller
3270
createtransactioncontroller = createTransactionController(createtransactionrequest)
3371
createtransactioncontroller.execute()
3472

3573
response = createtransactioncontroller.getresponse()
3674

3775
if response is not None:
76+
# Check to see if the API request was successfully received and acted upon
3877
if response.messages.resultCode == "Ok":
78+
# Since the API request was successful, look for a transaction response
79+
# and parse it to display the results of authorizing the card
3980
if hasattr(response.transactionResponse, 'messages') == True:
4081
print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId);
4182
print ('Transaction Response Code: %s' % response.transactionResponse.responseCode);
@@ -46,6 +87,7 @@ def charge_credit_card(amount):
4687
if hasattr(response.transactionResponse, 'errors') == True:
4788
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode));
4889
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
90+
# Or, print errors if the API request wasn't successful
4991
else:
5092
print ('Failed Transaction.');
5193
if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
@@ -60,4 +102,4 @@ def charge_credit_card(amount):
60102
return response
61103

62104
if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
63-
charge_credit_card(constants.amount)
105+
charge_credit_card(constants.amount)

PaymentTransactions/get-an-accept-payment-page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def get_an_accept_payment_page(amount):
4444

4545
print('Token : %s' % paymentPageResponse.token)
4646

47-
if paymentPageResponse.messages:
47+
if paymentPageResponse.messages is not None:
4848
print('Message Code : %s' % paymentPageResponse.messages.message[0]['code'].text)
4949
print('Message Text : %s' % paymentPageResponse.messages.message[0]['text'].text)
5050
else:
51-
if paymentPageResponse.messages:
51+
if paymentPageResponse.messages is not None:
5252
print('Failed to get batch statistics.\nCode:%s \nText:%s' % (paymentPageResponse.messages.message[0]['code'].text,paymentPageResponse.messages.message[0]['text'].text))
5353

5454
return paymentPageResponse

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Sample Python Code for Authorize.Net
22
[![Build Status](https://travis-ci.org/AuthorizeNet/sample-code-python.png?branch=master)](https://travis-ci.org/AuthorizeNet/sample-code-python)
33

4-
This repository contains working code samples which demonstrate Python integration with the Authorize.Net Python SDK (https://github.com/AuthorizeNet/sdk-python).
5-
The samples are organized just like our API, which you can also try out directly here: http://developer.authorize.net/api/reference
4+
This repository contains working code samples which demonstrate Python integration with the [Authorize.Net Python SDK](https://github.com/AuthorizeNet/sdk-python).
5+
The samples are organized just like our API, which you can also try out directly at our [API Reference Guide](http://developer.authorize.net/api/reference).
66

77

88
## Using the Sample Code
99

10-
The samples are all completely independent and self-contained so you can look at them to get a gist of how the method works, you can use the snippets to try in your own sample project, or you can run each sample from the command line.
10+
The samples are all completely independent and self-contained. You can analyze them to get an understanding of how a particular method works, or you can use the snippets as a starting point for your own project.
11+
12+
You can also run each sample directly from the command line.
1113

1214
## Running the Samples
1315
Clone this repository.

TransactionReporting/get-batch-statistics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ def get_batch_statistics():
3636
print('Refund Amount : %s' % statistic.refundAmount)
3737
print('Decline Count : %s' % statistic.declineCount)
3838

39-
if batchStatisticsResponse.messages:
39+
if batchStatisticsResponse.messages is not None:
4040
print('Message Code : %s' % batchStatisticsResponse.messages.message[0]['code'].text)
4141
print('Message Text : %s' % batchStatisticsResponse.messages.message[0]['text'].text)
4242
else:
43-
if batchStatisticsResponse.messages:
43+
if batchStatisticsResponse.messages is not None:
4444
print('Failed to get batch statistics.\nCode:%s \nText:%s' % (batchStatisticsResponse.messages.message[0]['code'].text,batchStatisticsResponse.messages.message[0]['text'].text))
4545

4646
return batchStatisticsResponse

TransactionReporting/get-transaction-list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def get_transaction_list():
3131
print('Amount Type : %s' % transaction.accountType)
3232
print('Settle Amount : %s' % transaction.settleAmount)
3333

34-
if transactionListResponse.messages:
34+
if transactionListResponse.messages is not None:
3535
print('Message Code : %s' % transactionListResponse.messages.message[0]['code'].text)
3636
print('Message Text : %s' % transactionListResponse.messages.message[0]['text'].text)
3737
else:
38-
if transactionListResponse.messages:
38+
if transactionListResponse.messages is not None:
3939
print('Failed to get transaction list.\nCode:%s \nText:%s' % (transactionListResponse.messages.message[0]['code'].text,transactionListResponse.messages.message[0]['text'].text))
4040

4141
return transactionListResponse

test-runner.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def create_customer_profile_from_transaction(self):
5555

5656
#Create transaction
5757
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
58-
response = modl.authorize_credit_card()
58+
response = modl.authorize_credit_card(self.getAmount())
5959

6060
#create customer payment profile for above transaction
6161
modl = imp.load_source('modulename', 'CustomerProfiles/create-customer-profile-from-transaction.py')
@@ -311,7 +311,7 @@ def validate_customer_payment_profile(self):
311311
def authorize_credit_card(self):
312312
print("authorize_credit_card")
313313
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
314-
return modl.authorize_credit_card()
314+
return modl.authorize_credit_card(self.getAmount())
315315

316316
def capture_funds_authorized_through_another_channel(self):
317317
print("capture_funds_authorized_through_another_channel")
@@ -322,7 +322,7 @@ def capture_previously_authorized_amount(self):
322322
print("capture_previously_authorized_amount")
323323

324324
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
325-
response = modl.authorize_credit_card()
325+
response = modl.authorize_credit_card(self.getAmount())
326326

327327
modl = imp.load_source('modulename', 'PaymentTransactions/capture-previously-authorized-amount.py')
328328
return modl.capture_previously_authorized_amount(response.transactionResponse.transId)
@@ -372,7 +372,7 @@ def refund_transaction(self):
372372
print("refund_transaction")
373373

374374
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
375-
response = modl.authorize_credit_card()
375+
response = modl.authorize_credit_card(self.getAmount())
376376

377377
modl = imp.load_source('modulename', 'PaymentTransactions/refund-transaction.py')
378378
return modl.refund_transaction(response.transactionResponse.transId)
@@ -386,7 +386,7 @@ def void_transaction(self):
386386
print("void_transaction")
387387

388388
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
389-
response = modl.authorize_credit_card()
389+
response = modl.authorize_credit_card(self.getAmount())
390390

391391
modl = imp.load_source('modulename', 'PaymentTransactions/void-transaction.py')
392392
return modl.void_transaction(response.transactionResponse.transId)
@@ -559,7 +559,7 @@ def get_transaction_details(self):
559559
print("get_transaction_details")
560560

561561
modl = imp.load_source('modulename', 'PaymentTransactions/authorize-credit-card.py')
562-
response = modl.authorize_credit_card()
562+
response = modl.authorize_credit_card(self.getAmount())
563563

564564
modl = imp.load_source('modulename', 'TransactionReporting/get-transaction-details.py')
565565
return modl.get_transaction_details(response.transactionResponse.transId)

0 commit comments

Comments
 (0)