Skip to content

Commit e02f885

Browse files
committed
cleanup echeck samples
1 parent bfae01b commit e02f885

File tree

2 files changed

+188
-77
lines changed

2 files changed

+188
-77
lines changed
Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,99 @@
1-
import os, sys
1+
"""
2+
Credit a bank account
3+
"""
4+
25
import imp
6+
import os
7+
import sys
8+
import random
39

410
from authorizenet import apicontractsv1
5-
from authorizenet.apicontrollers import *
6-
constants = imp.load_source('modulename', 'constants.py')
7-
from decimal import *
8-
from authorizenet.apicontractsv1 import bankAccountType, accountTypeEnum
11+
from authorizenet.apicontrollers import createTransactionController
912

10-
def credit_bank_account():
11-
merchantAuth = apicontractsv1.merchantAuthenticationType()
12-
merchantAuth.name = constants.apiLoginId
13-
merchantAuth.transactionKey = constants.transactionKey
13+
CONSTANTS = imp.load_source('modulename', 'constants.py')
1414

1515

16-
payment = apicontractsv1.paymentType()
16+
def credit_bank_account(amount):
17+
"""
18+
Credit a bank account
19+
"""
20+
# Create a merchantAuthenticationType object with authentication details
21+
# retrieved from the constants file
22+
merchantAuth = apicontractsv1.merchantAuthenticationType()
23+
merchantAuth.name = CONSTANTS.apiLoginId
24+
merchantAuth.transactionKey = CONSTANTS.transactionKey
1725

18-
bankAccountType = apicontractsv1.bankAccountType()
26+
# Create the payment data for a bank account
27+
bankAccount = apicontractsv1.bankAccountType()
1928
accountType = apicontractsv1.bankAccountTypeEnum
20-
bankAccountType.accountType = accountType.checking
21-
bankAccountType.routingNumber = "121042882"
22-
bankAccountType.accountNumber = "12345678"
23-
bankAccountType.nameOnAccount = "John Doe"
29+
bankAccount.accountType = accountType.checking
30+
bankAccount.routingNumber = "121042882"
31+
bankAccount.accountNumber = "1234567890"
32+
bankAccount.nameOnAccount = "John Doe"
2433

34+
# Add the payment data to a paymentType object
35+
payment = apicontractsv1.paymentType()
36+
payment.bankAccount = bankAccount
37+
38+
# Create a transactionRequestType object and add the previous objects to it.
2539
transactionrequest = apicontractsv1.transactionRequestType()
2640
transactionrequest.transactionType = "refundTransaction"
27-
transactionrequest.amount = Decimal ('2.55')
41+
transactionrequest.amount = amount
2842
transactionrequest.payment = payment
29-
transactionrequest.payment.bankAccount = bankAccountType
30-
3143

44+
# Assemble the complete transaction request
3245
createtransactionrequest = apicontractsv1.createTransactionRequest()
3346
createtransactionrequest.merchantAuthentication = merchantAuth
3447
createtransactionrequest.refId = "MerchantID-0001"
35-
3648
createtransactionrequest.transactionRequest = transactionrequest
37-
createtransactioncontroller = createTransactionController(createtransactionrequest)
49+
# Create the controller
50+
createtransactioncontroller = createTransactionController(
51+
createtransactionrequest)
3852
createtransactioncontroller.execute()
3953

4054
response = createtransactioncontroller.getresponse()
4155

4256
if response is not None:
57+
# Check to see if the API request was successfully received and acted upon
4358
if response.messages.resultCode == "Ok":
44-
if hasattr(response.transactionResponse, 'messages') == True:
45-
print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId)
46-
print ('Transaction Response Code: %s' % response.transactionResponse.responseCode)
47-
print ('Message Code: %s' % response.transactionResponse.messages.message[0].code)
48-
print ('Description: %s' % response.transactionResponse.messages.message[0].description)
59+
# Since the API request was successful, look for a transaction response
60+
# and parse it to display the results of authorizing the card
61+
if hasattr(response.transactionResponse, 'messages') is True:
62+
print(
63+
'Successfully created transaction with Transaction ID: %s'
64+
% response.transactionResponse.transId)
65+
print('Transaction Response Code: %s' %
66+
response.transactionResponse.responseCode)
67+
print('Message Code: %s' %
68+
response.transactionResponse.messages.message[0].code)
69+
print('Description: %s' % response.transactionResponse.
70+
messages.message[0].description)
4971
else:
50-
print ('Failed Transaction.')
51-
if hasattr(response.transactionResponse, 'errors') == True:
52-
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode))
53-
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText)
72+
print('Failed Transaction.')
73+
if hasattr(response.transactionResponse, 'errors') is True:
74+
print('Error Code: %s' % str(response.transactionResponse.
75+
errors.error[0].errorCode))
76+
print(
77+
'Error message: %s' %
78+
response.transactionResponse.errors.error[0].errorText)
79+
# Or, print errors if the API request wasn't successful
5480
else:
55-
print ('Failed Transaction.')
56-
if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
57-
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode))
58-
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText)
81+
print('Failed Transaction.')
82+
if hasattr(response, 'transactionResponse') is True and hasattr(
83+
response.transactionResponse, 'errors') is True:
84+
print('Error Code: %s' % str(
85+
response.transactionResponse.errors.error[0].errorCode))
86+
print('Error message: %s' %
87+
response.transactionResponse.errors.error[0].errorText)
5988
else:
60-
print ('Error Code: %s' % response.messages.message[0]['code'].text)
61-
print ('Error message: %s' % response.messages.message[0]['text'].text)
89+
print('Error Code: %s' %
90+
response.messages.message[0]['code'].text)
91+
print('Error message: %s' %
92+
response.messages.message[0]['text'].text)
6293
else:
63-
print ('Null Response.')
94+
print('Null Response.')
6495

6596
return response
6697

6798
if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
68-
credit_bank_account()
99+
credit_bank_account(str(round(random.random()*100, 2)))
Lines changed: 120 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,153 @@
1-
import os, sys
1+
"""
2+
Debit a bank account
3+
"""
4+
25
import imp
6+
import os
7+
import sys
38
import random
49

5-
610
from authorizenet import apicontractsv1
7-
from authorizenet.apicontrollers import *
8-
constants = imp.load_source('modulename', 'constants.py')
9-
from decimal import *
10-
from authorizenet.apicontractsv1 import bankAccountType, accountTypeEnum
11+
from authorizenet.apicontrollers import createTransactionController
1112

12-
def debit_bank_account():
13+
CONSTANTS = imp.load_source('modulename', 'constants.py')
1314

14-
amount = str(round(random.random()*100, 2))
1515

16-
merchantAuth = apicontractsv1.merchantAuthenticationType()
17-
merchantAuth.name = constants.apiLoginId
18-
merchantAuth.transactionKey = constants.transactionKey
16+
def debit_bank_account(amount):
17+
"""
18+
Debit a bank account
19+
"""
1920

21+
# Create a merchantAuthenticationType object with authentication details
22+
# retrieved from the constants file
23+
merchantAuth = apicontractsv1.merchantAuthenticationType()
24+
merchantAuth.name = CONSTANTS.apiLoginId
25+
merchantAuth.transactionKey = CONSTANTS.transactionKey
2026

21-
payment = apicontractsv1.paymentType()
22-
23-
bankAccountType = apicontractsv1.bankAccountType()
27+
# Create the payment data for a bank account
28+
bankAccount = apicontractsv1.bankAccountType()
2429
accountType = apicontractsv1.bankAccountTypeEnum
25-
bankAccountType.accountType = accountType.checking
26-
bankAccountType.routingNumber = "121042882"
27-
bankAccountType.accountNumber = "123456789"
28-
bankAccountType.nameOnAccount = "John Doe"
30+
bankAccount.accountType = accountType.checking
31+
bankAccount.routingNumber = "121042882"
32+
bankAccount.accountNumber = "1234567890"
33+
bankAccount.nameOnAccount = "John Doe"
2934

35+
# Add the payment data to a paymentType object
36+
payment = apicontractsv1.paymentType()
37+
payment.bankAccount = bankAccount
38+
39+
# Create order information
40+
order = apicontractsv1.orderType()
41+
order.invoiceNumber = "10101"
42+
order.description = "Golf Shirts"
43+
44+
# Set the customer's Bill To address
45+
customerAddress = apicontractsv1.customerAddressType()
46+
customerAddress.firstName = "Ellen"
47+
customerAddress.lastName = "Johnson"
48+
customerAddress.company = "Souveniropolis"
49+
customerAddress.address = "14 Main Street"
50+
customerAddress.city = "Pecan Springs"
51+
customerAddress.state = "TX"
52+
customerAddress.zip = "44628"
53+
customerAddress.country = "USA"
54+
55+
# Set the customer's identifying information
56+
customerData = apicontractsv1.customerDataType()
57+
customerData.type = "individual"
58+
customerData.id = "99999456654"
59+
customerData.email = "EllenJohnson@example.com"
60+
61+
# Add values for transaction settings
62+
duplicateWindowSetting = apicontractsv1.settingType()
63+
duplicateWindowSetting.settingName = "duplicateWindow"
64+
duplicateWindowSetting.settingValue = "60"
65+
settings = apicontractsv1.ArrayOfSetting()
66+
settings.setting.append(duplicateWindowSetting)
67+
68+
# setup individual line items
69+
line_item_1 = apicontractsv1.lineItemType()
70+
line_item_1.itemId = "12345"
71+
line_item_1.name = "first"
72+
line_item_1.description = "Here's the first line item"
73+
line_item_1.quantity = "2"
74+
line_item_1.unitPrice = "12.95"
75+
line_item_2 = apicontractsv1.lineItemType()
76+
line_item_2.itemId = "67890"
77+
line_item_2.name = "second"
78+
line_item_2.description = "Here's the second line item"
79+
line_item_2.quantity = "3"
80+
line_item_2.unitPrice = "7.95"
81+
82+
# build the array of line items
83+
line_items = apicontractsv1.ArrayOfLineItem()
84+
line_items.lineItem.append(line_item_1)
85+
line_items.lineItem.append(line_item_2)
86+
87+
# Create a transactionRequestType object and add the previous objects to it.
3088
transactionrequest = apicontractsv1.transactionRequestType()
3189
transactionrequest.transactionType = "authCaptureTransaction"
32-
transactionrequest.amount = Decimal(amount)
90+
transactionrequest.amount = amount
3391
transactionrequest.payment = payment
34-
transactionrequest.payment.bankAccount = bankAccountType
35-
92+
transactionrequest.order = order
93+
transactionrequest.billTo = customerAddress
94+
transactionrequest.customer = customerData
95+
transactionrequest.transactionSettings = settings
96+
transactionrequest.lineItems = line_items
3697

98+
# Assemble the complete transaction request
3799
createtransactionrequest = apicontractsv1.createTransactionRequest()
38100
createtransactionrequest.merchantAuthentication = merchantAuth
39101
createtransactionrequest.refId = "MerchantID-0001"
40-
41102
createtransactionrequest.transactionRequest = transactionrequest
42-
createtransactioncontroller = createTransactionController(createtransactionrequest)
103+
# Create the controller
104+
createtransactioncontroller = createTransactionController(
105+
createtransactionrequest)
43106
createtransactioncontroller.execute()
44107

45108
response = createtransactioncontroller.getresponse()
46109

47110
if response is not None:
111+
# Check to see if the API request was successfully received and acted upon
48112
if response.messages.resultCode == "Ok":
49-
if hasattr(response.transactionResponse, 'messages') == True:
50-
print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId)
51-
print ('Transaction Response Code: %s' % response.transactionResponse.responseCode)
52-
print ('Message Code: %s' % response.transactionResponse.messages.message[0].code)
53-
print ('Description: %s' % response.transactionResponse.messages.message[0].description)
113+
# Since the API request was successful, look for a transaction response
114+
# and parse it to display the results of authorizing the card
115+
if hasattr(response.transactionResponse, 'messages') is True:
116+
print(
117+
'Successfully created transaction with Transaction ID: %s'
118+
% response.transactionResponse.transId)
119+
print('Transaction Response Code: %s' %
120+
response.transactionResponse.responseCode)
121+
print('Message Code: %s' %
122+
response.transactionResponse.messages.message[0].code)
123+
print('Description: %s' % response.transactionResponse.
124+
messages.message[0].description)
54125
else:
55-
print ('Failed Transaction.')
56-
if hasattr(response.transactionResponse, 'errors') == True:
57-
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode))
58-
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText)
126+
print('Failed Transaction.')
127+
if hasattr(response.transactionResponse, 'errors') is True:
128+
print('Error Code: %s' % str(response.transactionResponse.
129+
errors.error[0].errorCode))
130+
print(
131+
'Error message: %s' %
132+
response.transactionResponse.errors.error[0].errorText)
133+
# Or, print errors if the API request wasn't successful
59134
else:
60-
print ('Failed Transaction.')
61-
if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
62-
print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode))
63-
print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText)
135+
print('Failed Transaction.')
136+
if hasattr(response, 'transactionResponse') is True and hasattr(
137+
response.transactionResponse, 'errors') is True:
138+
print('Error Code: %s' % str(
139+
response.transactionResponse.errors.error[0].errorCode))
140+
print('Error message: %s' %
141+
response.transactionResponse.errors.error[0].errorText)
64142
else:
65-
print ('Error Code: %s' % response.messages.message[0]['code'].text)
66-
print ('Error message: %s' % response.messages.message[0]['text'].text)
143+
print('Error Code: %s' %
144+
response.messages.message[0]['code'].text)
145+
print('Error message: %s' %
146+
response.messages.message[0]['text'].text)
67147
else:
68-
print ('Null Response.')
148+
print('Null Response.')
69149

70150
return response
71151

72152
if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
73-
debit_bank_account()
153+
debit_bank_account(str(round(random.random()*100, 2)))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy