|
| 1 | +import os, sys |
| 2 | +import imp |
| 3 | + |
| 4 | +from authorizenet import apicontractsv1 |
| 5 | +from authorizenet.apicontrollers import * |
| 6 | +constants = imp.load_source('modulename', 'constants.py') |
| 7 | +from decimal import * |
| 8 | + |
| 9 | +def create-an-accept-payment-transaction(amount): |
| 10 | + |
| 11 | + # Create a merchantAuthenticationType object with authentication details |
| 12 | + # retrieved from the constants file |
| 13 | + merchantAuth = apicontractsv1.merchantAuthenticationType() |
| 14 | + merchantAuth.name = constants.apiLoginId |
| 15 | + merchantAuth.transactionKey = constants.transactionKey |
| 16 | + |
| 17 | + # Create the payment object for a payment nonce |
| 18 | + opaqueData = apicontractsv1.opaqueDataType() |
| 19 | + opaqueData.dataDescriptor = "COMMON.ACCEPT.INAPP.PAYMENT" |
| 20 | + opaqueData.dataValue = "119eyJjb2RlIjoiNTBfMl8wNjAwMDUyN0JEODE4RjQxOUEyRjhGQkIxMkY0MzdGQjAxQUIwRTY2NjhFNEFCN0VENzE4NTUwMjlGRUU0M0JFMENERUIwQzM2M0ExOUEwMDAzNzlGRDNFMjBCODJEMDFCQjkyNEJDIiwidG9rZW4iOiI5NDkwMjMyMTAyOTQwOTk5NDA0NjAzIiwidiI6IjEuMSJ9" |
| 21 | + |
| 22 | + # Add the payment data to a paymentType object |
| 23 | + paymentOne = apicontractsv1.paymentType() |
| 24 | + paymentOne.opaqueData = opaqueData |
| 25 | + |
| 26 | + # Create order information |
| 27 | + order = apicontractsv1.orderType() |
| 28 | + order.invoiceNumber = "10101" |
| 29 | + order.description = "Golf Shirts" |
| 30 | + |
| 31 | + # Set the customer's Bill To address |
| 32 | + customerAddress = apicontractsv1.customerAddressType() |
| 33 | + customerAddress.firstName = "Ellen" |
| 34 | + customerAddress.lastName = "Johnson" |
| 35 | + customerAddress.company = "Souveniropolis" |
| 36 | + customerAddress.address = "14 Main Street" |
| 37 | + customerAddress.city = "Pecan Springs" |
| 38 | + customerAddress.state = "TX" |
| 39 | + customerAddress.zip = "44628" |
| 40 | + customerAddress.country = "USA" |
| 41 | + |
| 42 | + # Set the customer's identifying information |
| 43 | + customerData = apicontractsv1.customerDataType() |
| 44 | + customerData.type = "individual" |
| 45 | + customerData.id = "99999456654" |
| 46 | + customerData.email = "EllenJohnson@example.com" |
| 47 | + |
| 48 | + # Add values for transaction settings |
| 49 | + duplicateWindowSetting = apicontractsv1.settingType(); |
| 50 | + duplicateWindowSetting.settingName = "duplicateWindow" |
| 51 | + duplicateWindowSetting.settingValue = "600" |
| 52 | + settings = apicontractsv1.ArrayOfSetting() |
| 53 | + settings.setting.append(duplicateWindowSetting) |
| 54 | + |
| 55 | + # Create a transactionRequestType object and add the previous objects to it |
| 56 | + transactionrequest = apicontractsv1.transactionRequestType() |
| 57 | + transactionrequest.transactionType = "authCaptureTransaction" |
| 58 | + transactionrequest.amount = amount |
| 59 | + transactionrequest.order = order |
| 60 | + transactionrequest.payment = paymentOne |
| 61 | + transactionrequest.billTo = customerAddress |
| 62 | + transactionrequest.customer = customerData |
| 63 | + transactionrequest.transactionSettings = settings |
| 64 | + |
| 65 | + # Assemble the complete transaction request |
| 66 | + createtransactionrequest = apicontractsv1.createTransactionRequest() |
| 67 | + createtransactionrequest.merchantAuthentication = merchantAuth |
| 68 | + createtransactionrequest.refId = "MerchantID-0001" |
| 69 | + createtransactionrequest.transactionRequest = transactionrequest |
| 70 | + |
| 71 | + # Create the controller and get response |
| 72 | + createtransactioncontroller = createTransactionController(createtransactionrequest) |
| 73 | + createtransactioncontroller.execute() |
| 74 | + |
| 75 | + response = createtransactioncontroller.getresponse() |
| 76 | + |
| 77 | + if response is not None: |
| 78 | + # Check to see if the API request was successfully received and acted upon |
| 79 | + if response.messages.resultCode == "Ok": |
| 80 | + # Since the API request was successful, look for a transaction response |
| 81 | + # and parse it to display the results of authorizing the card |
| 82 | + if hasattr(response.transactionResponse, 'messages') == True: |
| 83 | + print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId); |
| 84 | + print ('Transaction Response Code: %s' % response.transactionResponse.responseCode); |
| 85 | + print ('Message Code: %s' % response.transactionResponse.messages.message[0].code); |
| 86 | + print ('Description: %s' % response.transactionResponse.messages.message[0].description); |
| 87 | + else: |
| 88 | + print ('Failed Transaction.'); |
| 89 | + if hasattr(response.transactionResponse, 'errors') == True: |
| 90 | + print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode)); |
| 91 | + print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText); |
| 92 | + # Or, print errors if the API request wasn't successful |
| 93 | + else: |
| 94 | + print ('Failed Transaction.'); |
| 95 | + if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True: |
| 96 | + print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode)); |
| 97 | + print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText); |
| 98 | + else: |
| 99 | + print ('Error Code: %s' % response.messages.message[0]['code'].text); |
| 100 | + print ('Error message: %s' % response.messages.message[0]['text'].text); |
| 101 | + else: |
| 102 | + print ('Null Response.'); |
| 103 | + |
| 104 | + return response |
| 105 | + |
| 106 | +if(os.path.basename(__file__) == os.path.basename(sys.argv[0])): |
| 107 | + create-an-accept-payment-transaction(constants.amount) |
0 commit comments