|
1 |
| -import os, sys |
| 1 | +""" |
| 2 | +Debit a bank account |
| 3 | +""" |
| 4 | + |
2 | 5 | import imp
|
| 6 | +import os |
| 7 | +import sys |
3 | 8 | import random
|
4 | 9 |
|
5 |
| - |
6 | 10 | 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 |
11 | 12 |
|
12 |
| -def debit_bank_account(): |
| 13 | +CONSTANTS = imp.load_source('modulename', 'constants.py') |
13 | 14 |
|
14 |
| - amount = str(round(random.random()*100, 2)) |
15 | 15 |
|
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 | + """ |
19 | 20 |
|
| 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 |
20 | 26 |
|
21 |
| - payment = apicontractsv1.paymentType() |
22 |
| - |
23 |
| - bankAccountType = apicontractsv1.bankAccountType() |
| 27 | + # Create the payment data for a bank account |
| 28 | + bankAccount = apicontractsv1.bankAccountType() |
24 | 29 | 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" |
29 | 34 |
|
| 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. |
30 | 88 | transactionrequest = apicontractsv1.transactionRequestType()
|
31 | 89 | transactionrequest.transactionType = "authCaptureTransaction"
|
32 |
| - transactionrequest.amount = Decimal(amount) |
| 90 | + transactionrequest.amount = amount |
33 | 91 | 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 |
36 | 97 |
|
| 98 | + # Assemble the complete transaction request |
37 | 99 | createtransactionrequest = apicontractsv1.createTransactionRequest()
|
38 | 100 | createtransactionrequest.merchantAuthentication = merchantAuth
|
39 | 101 | createtransactionrequest.refId = "MerchantID-0001"
|
40 |
| - |
41 | 102 | createtransactionrequest.transactionRequest = transactionrequest
|
42 |
| - createtransactioncontroller = createTransactionController(createtransactionrequest) |
| 103 | + # Create the controller |
| 104 | + createtransactioncontroller = createTransactionController( |
| 105 | + createtransactionrequest) |
43 | 106 | createtransactioncontroller.execute()
|
44 | 107 |
|
45 | 108 | response = createtransactioncontroller.getresponse()
|
46 | 109 |
|
47 | 110 | if response is not None:
|
| 111 | + # Check to see if the API request was successfully received and acted upon |
48 | 112 | 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) |
54 | 125 | 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 |
59 | 134 | 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) |
64 | 142 | 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) |
67 | 147 | else:
|
68 |
| - print ('Null Response.') |
| 148 | + print('Null Response.') |
69 | 149 |
|
70 | 150 | return response
|
71 | 151 |
|
72 | 152 | 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