-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathauthorize-credit-card.py
More file actions
156 lines (135 loc) · 6.18 KB
/
authorize-credit-card.py
File metadata and controls
156 lines (135 loc) · 6.18 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
153
154
155
156
"""
Authorize a credit card (without actually charging it)
"""
import imp
import os
import sys
from authorizenet import apicontractsv1
from authorizenet.apicontrollers import createTransactionController
from authorizenet.constants import constants
def authorize_credit_card(amount):
"""
Authorize a credit card (without actually charging it)
"""
# Create a merchantAuthenticationType object with authentication details
# retrieved from the constants file
merchantAuth = apicontractsv1.merchantAuthenticationType()
merchantAuth.name = constants.apiLoginId
merchantAuth.transactionKey = constants.transactionKey
# Create the payment data for a credit card
creditCard = apicontractsv1.creditCardType()
creditCard.cardNumber = "4111111111111111"
creditCard.expirationDate = "2020-12"
creditCard.cardCode = "123"
# Add the payment data to a paymentType object
payment = apicontractsv1.paymentType()
payment.creditCard = creditCard
# Create order information
order = apicontractsv1.orderType()
order.invoiceNumber = "10101"
order.description = "Golf Shirts"
order.discountAmount = "10"
# Set the customer's Bill To address
customerAddress = apicontractsv1.customerAddressType()
customerAddress.firstName = "Ellen"
customerAddress.lastName = "Johnson"
customerAddress.company = "Souveniropolis"
customerAddress.address = "14 Main Street"
customerAddress.city = "Pecan Springs"
customerAddress.state = "TX"
customerAddress.zip = "44628"
customerAddress.country = "USA"
# Set the customer's identifying information
customerData = apicontractsv1.customerDataType()
customerData.type = "individual"
customerData.id = "99999456654"
customerData.email = "EllenJohnson@example.com"
# Add values for transaction settings
duplicateWindowSetting = apicontractsv1.settingType()
duplicateWindowSetting.settingName = "duplicateWindow"
duplicateWindowSetting.settingValue = "600"
settings = apicontractsv1.ArrayOfSetting()
settings.setting.append(duplicateWindowSetting)
# setup individual line items
line_item_1 = apicontractsv1.lineItemType()
line_item_1.itemId = "12345"
line_item_1.name = "first"
line_item_1.description = "Here's the first line item"
line_item_1.quantity = "2"
line_item_1.unitPrice = "12.95"
line_item_2 = apicontractsv1.lineItemType()
line_item_2.itemId = "67890"
line_item_2.name = "second"
line_item_2.description = "Here's the second line item"
line_item_2.quantity = "3"
line_item_2.unitPrice = "7.95"
line_item_2.unitOfMeasure = "2"
# build the array of line items
line_items = apicontractsv1.ArrayOfLineItem()
line_items.lineItem.append(line_item_1)
line_items.lineItem.append(line_item_2)
otherTax = apicontractsv1.otherTaxType()
otherTax.localTaxAmount="5"
# Create a transactionRequestType object and add the previous objects to it.
transactionrequest = apicontractsv1.transactionRequestType()
transactionrequest.transactionType = "authOnlyTransaction"
transactionrequest.amount = amount
transactionrequest.payment = payment
transactionrequest.order = order
transactionrequest.billTo = customerAddress
transactionrequest.customer = customerData
transactionrequest.otherTax = otherTax
transactionrequest.transactionSettings = settings
transactionrequest.lineItems = line_items
# Assemble the complete transaction request
createtransactionrequest = apicontractsv1.createTransactionRequest()
createtransactionrequest.merchantAuthentication = merchantAuth
createtransactionrequest.refId = "MerchantID-0001"
createtransactionrequest.transactionRequest = transactionrequest
# Create the controller
createtransactioncontroller = createTransactionController(
createtransactionrequest)
createtransactioncontroller.execute()
response = createtransactioncontroller.getresponse()
if response is not None:
# Check to see if the API request was successfully received and acted upon
if response.messages.resultCode == "Ok":
# Since the API request was successful, look for a transaction response
# and parse it to display the results of authorizing the card
if hasattr(response.transactionResponse, 'messages') is True:
print(
'Successfully created transaction with Transaction ID: %s'
% response.transactionResponse.transId)
print('Transaction Response Code: %s' %
response.transactionResponse.responseCode)
print('Message Code: %s' %
response.transactionResponse.messages.message[0].code)
print('Description: %s' % response.transactionResponse.
messages.message[0].description)
else:
print('Failed Transaction.')
if hasattr(response.transactionResponse, 'errors') is True:
print('Error Code: %s' % str(response.transactionResponse.
errors.error[0].errorCode))
print(
'Error message: %s' %
response.transactionResponse.errors.error[0].errorText)
# Or, print errors if the API request wasn't successful
else:
print('Failed Transaction.')
if hasattr(response, 'transactionResponse') is True and hasattr(
response.transactionResponse, 'errors') is True:
print('Error Code: %s' % str(
response.transactionResponse.errors.error[0].errorCode))
print('Error message: %s' %
response.transactionResponse.errors.error[0].errorText)
else:
print('Error Code: %s' %
response.messages.message[0]['code'].text)
print('Error message: %s' %
response.messages.message[0]['text'].text)
else:
print('Null Response.')
return response
if (os.path.basename(__file__) == os.path.basename(sys.argv[0])):
authorize_credit_card(constants.amount)