-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcreate-an-accept-transaction.py
116 lines (98 loc) · 5.34 KB
/
create-an-accept-transaction.py
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
import os, sys
import imp
import time
import pathlib
import glob
#this library will allow the user to record the time of the transaction
from authorizenet import apicontractsv1
from authorizenet.apicontrollers import *
constants = imp.load_source('modulename', 'constants.py')
from decimal import *
def create_an_accept_transaction(amount):
# Create a merchantAuthenticationType object with authentication details
# retrieved from the constants file
merchantAuth = apicontractsv1.merchantAuthenticationType()
merchantAuth.name = constants.apiLoginId
merchantAuth.transactionKey = constants.transactionKey
# Set the transaction's refId
refId = "ref {}".format(time.time())
# Create the payment object for a payment nonce
opaqueData = apicontractsv1.opaqueDataType()
opaqueData.dataDescriptor = "COMMON.ACCEPT.INAPP.PAYMENT"
opaqueData.dataValue = "119eyJjb2RlIjoiNTBfMl8wNjAwMDUyN0JEODE4RjQxOUEyRjhGQkIxMkY0MzdGQjAxQUIwRTY2NjhFNEFCN0VENzE4NTUwMjlGRUU0M0JFMENERUIwQzM2M0ExOUEwMDAzNzlGRDNFMjBCODJEMDFCQjkyNEJDIiwidG9rZW4iOiI5NDkwMjMyMTAyOTQwOTk5NDA0NjAzIiwidiI6IjEuMSJ9"
# Add the payment data to a paymentType object
paymentOne = apicontractsv1.paymentType()
paymentOne.opaqueData = opaqueData
# Create order information
order = apicontractsv1.orderType()
order.invoiceNumber = "10101"
order.description = "Golf Shirts"
# 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 = "[email protected]"
# Add values for transaction settings
duplicateWindowSetting = apicontractsv1.settingType()
duplicateWindowSetting.settingName = "duplicateWindow"
duplicateWindowSetting.settingValue = "600"
settings = apicontractsv1.ArrayOfSetting()
settings.setting.append(duplicateWindowSetting)
# Create a transactionRequestType object and add the previous objects to it
transactionrequest = apicontractsv1.transactionRequestType()
transactionrequest.transactionType = "authCaptureTransaction"
transactionrequest.amount = amount
transactionrequest.order = order
transactionrequest.payment = paymentOne
transactionrequest.billTo = customerAddress
transactionrequest.customer = customerData
transactionrequest.transactionSettings = settings
# Assemble the complete transaction request
createtransactionrequest = apicontractsv1.createTransactionRequest()
createtransactionrequest.merchantAuthentication = merchantAuth
createtransactionrequest.refId = refId
createtransactionrequest.transactionRequest = transactionrequest
# Create the controller and get response
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') == 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 ('Auth Code: %s' % response.transactionResponse.authCode)
print ('Description: %s' % response.transactionResponse.messages.message[0].description)
else:
print ('Failed Transaction.')
if hasattr(response.transactionResponse, 'errors') == 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') == True and hasattr(response.transactionResponse, 'errors') == 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])):
create_an_accept_transaction(constants.amount)