|
1 |
| -import os, sys |
| 1 | +""" |
| 2 | +Charge a credit card |
| 3 | +""" |
| 4 | + |
2 | 5 | import imp
|
| 6 | +import os |
| 7 | +import sys |
3 | 8 |
|
4 | 9 | from authorizenet import apicontractsv1
|
5 |
| -from authorizenet.apicontrollers import * |
6 |
| -constants = imp.load_source('modulename', 'constants.py') |
7 |
| -from decimal import * |
| 10 | +from authorizenet.apicontrollers import createTransactionController |
| 11 | + |
| 12 | +CONSTANTS = imp.load_source('modulename', 'constants.py') |
| 13 | + |
8 | 14 |
|
9 | 15 | def charge_credit_card(amount):
|
| 16 | + """ |
| 17 | + Charge a credit card |
| 18 | + """ |
| 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 |
| 25 | + |
| 26 | + # Create the payment data for a credit card |
| 27 | + creditCard = apicontractsv1.creditCardType() |
| 28 | + creditCard.cardNumber = "4111111111111111" |
| 29 | + creditCard.expirationDate = "2020-12" |
| 30 | + creditCard.cardCode = "123" |
| 31 | + |
| 32 | + # Add the payment data to a paymentType object |
| 33 | + payment = apicontractsv1.paymentType() |
| 34 | + payment.creditCard = creditCard |
| 35 | + |
| 36 | + # Create order information |
| 37 | + order = apicontractsv1.orderType() |
| 38 | + order.invoiceNumber = "10101" |
| 39 | + order.description = "Golf Shirts" |
| 40 | + |
| 41 | + # Set the customer's Bill To address |
| 42 | + customerAddress = apicontractsv1.customerAddressType() |
| 43 | + customerAddress.firstName = "Ellen" |
| 44 | + customerAddress.lastName = "Johnson" |
| 45 | + customerAddress.company = "Souveniropolis" |
| 46 | + customerAddress.address = "14 Main Street" |
| 47 | + customerAddress.city = "Pecan Springs" |
| 48 | + customerAddress.state = "TX" |
| 49 | + customerAddress.zip = "44628" |
| 50 | + customerAddress.country = "USA" |
| 51 | + |
| 52 | + # Set the customer's identifying information |
| 53 | + customerData = apicontractsv1.customerDataType() |
| 54 | + customerData.type = "individual" |
| 55 | + customerData.id = "99999456654" |
| 56 | + customerData.email = "EllenJohnson@example.com" |
| 57 | + |
| 58 | + # Add values for transaction settings |
| 59 | + duplicateWindowSetting = apicontractsv1.settingType() |
| 60 | + duplicateWindowSetting.settingName = "duplicateWindow" |
| 61 | + duplicateWindowSetting.settingValue = "600" |
| 62 | + settings = apicontractsv1.ArrayOfSetting() |
| 63 | + settings.setting.append(duplicateWindowSetting) |
| 64 | + |
| 65 | + # setup individual line items |
| 66 | + line_item_1 = apicontractsv1.lineItemType() |
| 67 | + line_item_1.itemId = "12345" |
| 68 | + line_item_1.name = "first" |
| 69 | + line_item_1.description = "Here's the first line item" |
| 70 | + line_item_1.quantity = "2" |
| 71 | + line_item_1.unitPrice = "12.95" |
| 72 | + line_item_2 = apicontractsv1.lineItemType() |
| 73 | + line_item_2.itemId = "67890" |
| 74 | + line_item_2.name = "second" |
| 75 | + line_item_2.description = "Here's the second line item" |
| 76 | + line_item_2.quantity = "3" |
| 77 | + line_item_2.unitPrice = "7.95" |
| 78 | + |
| 79 | + # build the array of line items |
| 80 | + line_items = apicontractsv1.ArrayOfLineItem() |
| 81 | + line_items.lineItem.append(line_item_1) |
| 82 | + line_items.lineItem.append(line_item_2) |
| 83 | + |
| 84 | + # Create a transactionRequestType object and add the previous objects to it. |
| 85 | + transactionrequest = apicontractsv1.transactionRequestType() |
| 86 | + transactionrequest.transactionType = "authCaptureTransaction" |
| 87 | + transactionrequest.amount = amount |
| 88 | + transactionrequest.payment = payment |
| 89 | + transactionrequest.order = order |
| 90 | + transactionrequest.billTo = customerAddress |
| 91 | + transactionrequest.customer = customerData |
| 92 | + transactionrequest.transactionSettings = settings |
| 93 | + transactionrequest.lineItems = line_items |
| 94 | + |
| 95 | + # Assemble the complete transaction request |
| 96 | + createtransactionrequest = apicontractsv1.createTransactionRequest() |
| 97 | + createtransactionrequest.merchantAuthentication = merchantAuth |
| 98 | + createtransactionrequest.refId = "MerchantID-0001" |
| 99 | + createtransactionrequest.transactionRequest = transactionrequest |
| 100 | + # Create the controller |
| 101 | + createtransactioncontroller = createTransactionController( |
| 102 | + createtransactionrequest) |
| 103 | + createtransactioncontroller.execute() |
| 104 | + |
| 105 | + response = createtransactioncontroller.getresponse() |
| 106 | + |
| 107 | + if response is not None: |
| 108 | + # Check to see if the API request was successfully received and acted upon |
| 109 | + if response.messages.resultCode == "Ok": |
| 110 | + # Since the API request was successful, look for a transaction response |
| 111 | + # and parse it to display the results of authorizing the card |
| 112 | + if hasattr(response.transactionResponse, 'messages') is True: |
| 113 | + print( |
| 114 | + 'Successfully created transaction with Transaction ID: %s' |
| 115 | + % response.transactionResponse.transId) |
| 116 | + print('Transaction Response Code: %s' % |
| 117 | + response.transactionResponse.responseCode) |
| 118 | + print('Message Code: %s' % |
| 119 | + response.transactionResponse.messages.message[0].code) |
| 120 | + print('Description: %s' % response.transactionResponse. |
| 121 | + messages.message[0].description) |
| 122 | + else: |
| 123 | + print('Failed Transaction.') |
| 124 | + if hasattr(response.transactionResponse, 'errors') is True: |
| 125 | + print('Error Code: %s' % str(response.transactionResponse. |
| 126 | + errors.error[0].errorCode)) |
| 127 | + print( |
| 128 | + 'Error message: %s' % |
| 129 | + response.transactionResponse.errors.error[0].errorText) |
| 130 | + # Or, print errors if the API request wasn't successful |
| 131 | + else: |
| 132 | + print('Failed Transaction.') |
| 133 | + if hasattr(response, 'transactionResponse') is True and hasattr( |
| 134 | + response.transactionResponse, 'errors') is True: |
| 135 | + print('Error Code: %s' % str( |
| 136 | + response.transactionResponse.errors.error[0].errorCode)) |
| 137 | + print('Error message: %s' % |
| 138 | + response.transactionResponse.errors.error[0].errorText) |
| 139 | + else: |
| 140 | + print('Error Code: %s' % |
| 141 | + response.messages.message[0]['code'].text) |
| 142 | + print('Error message: %s' % |
| 143 | + response.messages.message[0]['text'].text) |
| 144 | + else: |
| 145 | + print('Null Response.') |
| 146 | + |
| 147 | + return response |
| 148 | + |
10 | 149 |
|
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 data for a credit card |
18 |
| - creditCard = apicontractsv1.creditCardType() |
19 |
| - creditCard.cardNumber = "4111111111111111" |
20 |
| - creditCard.expirationDate = "2020-12" |
21 |
| - creditCard.cardCode = "123" |
22 |
| - |
23 |
| - # Add the payment data to a paymentType object |
24 |
| - payment = apicontractsv1.paymentType() |
25 |
| - payment.creditCard = creditCard |
26 |
| - |
27 |
| - # Create order information |
28 |
| - order = apicontractsv1.orderType() |
29 |
| - order.invoiceNumber = "10101" |
30 |
| - order.description = "Golf Shirts" |
31 |
| - |
32 |
| - # Set the customer's Bill To address |
33 |
| - customerAddress = apicontractsv1.customerAddressType() |
34 |
| - customerAddress.firstName = "Ellen" |
35 |
| - customerAddress.lastName = "Johnson" |
36 |
| - customerAddress.company = "Souveniropolis" |
37 |
| - customerAddress.address = "14 Main Street" |
38 |
| - customerAddress.city = "Pecan Springs" |
39 |
| - customerAddress.state = "TX" |
40 |
| - customerAddress.zip = "44628" |
41 |
| - customerAddress.country = "USA" |
42 |
| - |
43 |
| - # Set the customer's identifying information |
44 |
| - customerData = apicontractsv1.customerDataType() |
45 |
| - customerData.type = "individual" |
46 |
| - customerData.id = "99999456654" |
47 |
| - customerData.email = "EllenJohnson@example.com" |
48 |
| - |
49 |
| - # Add values for transaction settings |
50 |
| - duplicateWindowSetting = apicontractsv1.settingType(); |
51 |
| - duplicateWindowSetting.settingName = "duplicateWindow" |
52 |
| - duplicateWindowSetting.settingValue = "600" |
53 |
| - settings = apicontractsv1.ArrayOfSetting() |
54 |
| - settings.setting.append(duplicateWindowSetting) |
55 |
| - |
56 |
| - # Create a transactionRequestType object and add the previous objects to it. |
57 |
| - transactionrequest = apicontractsv1.transactionRequestType() |
58 |
| - transactionrequest.transactionType = "authCaptureTransaction" |
59 |
| - transactionrequest.amount = amount |
60 |
| - transactionrequest.payment = payment |
61 |
| - transactionrequest.order = order |
62 |
| - transactionrequest.billTo = customerAddress |
63 |
| - transactionrequest.customer = customerData |
64 |
| - transactionrequest.transactionSettings = settings |
65 |
| - |
66 |
| - # Assemble the complete transaction request |
67 |
| - createtransactionrequest = apicontractsv1.createTransactionRequest() |
68 |
| - createtransactionrequest.merchantAuthentication = merchantAuth |
69 |
| - createtransactionrequest.refId = "MerchantID-0001" |
70 |
| - createtransactionrequest.transactionRequest = transactionrequest |
71 |
| - # Create the controller |
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 |
| - charge_credit_card(constants.amount) |
| 150 | +if (os.path.basename(__file__) == os.path.basename(sys.argv[0])): |
| 151 | + charge_credit_card(CONSTANTS.amount) |
0 commit comments