def setUp(self): utility.helper.setpropertyfile('anet_python_sdk_properties.ini') self.amount = str(round(random.random() * 100, 2)) self.merchantAuthentication = apicontractsv1.merchantAuthenticationType( ) self.merchantAuthentication.name = utility.helper.getproperty( 'api.login.id') self.merchantAuthentication.transactionKey = utility.helper.getproperty( 'transaction.key') self.ref_id = 'Sample' self.dateOne = datetime.date(2020, 8, 30) self.interval = CTD_ANON() self.interval.length = 1 self.interval.unit = 'months' self.paymentScheduleOne = apicontractsv1.paymentScheduleType() self.paymentScheduleOne.interval = self.interval self.paymentScheduleOne.startDate = self.dateOne self.paymentScheduleOne.totalOccurrences = 12 self.paymentScheduleOne.trialOccurrences = 1 self.creditCardOne = apicontractsv1.creditCardType() self.creditCardOne.cardNumber = "4111111111111111" self.creditCardOne.expirationDate = "2020-12" self.payment = apicontractsv1.paymentType() self.payment.creditCard = self.creditCardOne self.customerOne = apicontractsv1.nameAndAddressType() self.customerOne.firstName = "John" self.customerOne.lastName = "Smith" self.customerData = apicontractsv1.customerDataType() self.customerData.id = "99999456654" self.subscriptionOne = apicontractsv1.ARBSubscriptionType() self.subscriptionOne.paymentSchedule = self.paymentScheduleOne self.subscriptionOne.amount = Decimal( str(round(random.random() * 100, 2))) self.subscriptionOne.trialAmount = Decimal('0.03') self.subscriptionOne.payment = self.payment self.subscriptionOne.billTo = self.customerOne self.order = apicontractsv1.orderType() self.order.invoiceNumber = "INV-21345" self.order.description = "Product description" self.billTo = apicontractsv1.customerAddressType() self.billTo.firstName = "Ellen" self.billTo.lastName = "Johnson" self.billTo.company = "Souveniropolis" self.billTo.address = "14 Main St" self.billTo.city = "Seattle" self.billTo.state = "WA" self.billTo.zip = "98122" self.billTo.country = "USA"
def setUp(self): utility.helper.setpropertyfile('anet_python_sdk_properties.ini') self.amount = str(round(random.random()*100, 2)) self.merchantAuthentication = apicontractsv1.merchantAuthenticationType() self.merchantAuthentication.name = helper.getproperty('api_login_id') self.merchantAuthentication.transactionKey = helper.getproperty('transaction_key') self.ref_id = 'Sample' self.dateOne = datetime.date(2020, 8, 30) self.interval = CTD_ANON() self.interval.length = 1 self.interval.unit = 'months' self.paymentScheduleOne = apicontractsv1.paymentScheduleType() self.paymentScheduleOne.interval = self.interval self.paymentScheduleOne.startDate = self.dateOne self.paymentScheduleOne.totalOccurrences = 12 self.paymentScheduleOne.trialOccurrences = 1 self.creditCardOne = apicontractsv1.creditCardType() self.creditCardOne.cardNumber = "4111111111111111" self.creditCardOne.expirationDate = "2020-12" self.payment = apicontractsv1.paymentType() self.payment.creditCard = self.creditCardOne self.customerOne = apicontractsv1.nameAndAddressType() self.customerOne.firstName = "John" self.customerOne.lastName = "Smith" self.customerData = apicontractsv1.customerDataType() self.customerData.id = "99999456654" self.subscriptionOne = apicontractsv1.ARBSubscriptionType() self.subscriptionOne.paymentSchedule = self.paymentScheduleOne self.subscriptionOne.amount = Decimal(self.amount) self.subscriptionOne.trialAmount = Decimal ('0.03') self.subscriptionOne.payment = self.payment self.subscriptionOne.billTo = self.customerOne self.order = apicontractsv1.orderType() self.order.invoiceNumber = "INV-21345" self.order.description = "Product description" self.billTo = apicontractsv1.customerAddressType() self.billTo.firstName = "Ellen" self.billTo.lastName = "Johnson" self.billTo.company = "Souveniropolis" self.billTo.address = "14 Main St" self.billTo.city = "Seattle" self.billTo.state = "WA" self.billTo.zip = "98122" self.billTo.country = "USA"
def _create_transaction_request_for_profile(self, transaction, customer_profile, payment_profile): """ Create an authorize.net transaction request object to a customer id :param transaction: A Silver transaction with a AuthorizeNet payment method. :param customer_profile: The authorize.net customer profile ID :param payment_profile: The authorize.net customer payment profile ID :return: An authorize.net TransactionRequest """ profile_to_charge = apicontractsv1.customerProfilePaymentType() profile_to_charge.customerProfileId = customer_profile profile_to_charge.paymentProfile = apicontractsv1.paymentProfile() profile_to_charge.paymentProfile.paymentProfileId = payment_profile payment = apicontractsv1.paymentType() payment.profile = profile_to_charge # Create order information order = apicontractsv1.orderType() order.invoiceNumber = transaction.document.series order.description = "\n".join(map(str, transaction.document.entries)) # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = transaction.customer.id # TODO: right id field? customerData.email = transaction.customer.email settings = self._create_transaction_settings() _tx_request = apicontractsv1.transactionRequestType() _tx_request.transactionType = "authCaptureTransaction" _tx_request.amount = transaction.amount _tx_request.payment = payment _tx_request.order = order # _tx_request.billTo = customerAddress _tx_request.customer = customerData _tx_request.transactionSettings = settings # _tx_request.lineItems = line_items _request = apicontractsv1.createTransactionRequest() _request.merchantAuthentication = self.merchantAuth _request.refId = self.merchantId _request.transactionRequest = _tx_request return _request
def charge_credit_card(amount): """ Charge a credit card """ # 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" # 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 = "*****@*****.**" # 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" # build the array of line items line_items = apicontractsv1.ArrayOfLineItem() line_items.lineItem.append(line_item_1) line_items.lineItem.append(line_item_2) # Create a transactionRequestType object and add the previous objects to it. transactionrequest = apicontractsv1.transactionRequestType() transactionrequest.transactionType = "authCaptureTransaction" transactionrequest.amount = amount transactionrequest.payment = payment transactionrequest.order = order transactionrequest.billTo = customerAddress transactionrequest.customer = customerData 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
def authorize( payment_information: PaymentData, config: GatewayConfig, user_id: Optional[int] = None, ) -> GatewayResponse: """Based on AcceptSuite create-an-accept-payment-transaction example. https://github.com/AuthorizeNet/sample-code-python/blob/master/AcceptSuite/create-an-accept-payment-transaction.py """ kind = TransactionKind.CAPTURE if config.auto_capture else TransactionKind.AUTH merchant_auth = _get_merchant_auth(config.connection_params) # The Saleor token is the authorize.net "opaque data" opaque_data = apicontractsv1.opaqueDataType() opaque_data.dataDescriptor = "COMMON.ACCEPT.INAPP.PAYMENT" opaque_data.dataValue = payment_information.token payment_one = apicontractsv1.paymentType() payment_one.opaqueData = opaque_data order = apicontractsv1.orderType() order.invoiceNumber = payment_information.order_id order.description = payment_information.graphql_payment_id # An auth.net "profile" id is the id generated by auth.net. # It is not the Saleor user id. customer_id = (payment_information.customer_id if payment_information.reuse_source else None) customer_data = apicontractsv1.customerDataType() customer_data.type = "individual" if user_id: customer_data.id = str(user_id) customer_data.email = payment_information.customer_email transaction_request = apicontractsv1.transactionRequestType() transaction_request.transactionType = ("authCaptureTransaction" if config.auto_capture else "authOnlyTransaction") transaction_request.amount = payment_information.amount transaction_request.currencyCode = payment_information.currency transaction_request.order = order transaction_request.payment = payment_one transaction_request.customer = customer_data if payment_information.reuse_source and customer_id is None: profile = apicontractsv1.customerProfilePaymentType() profile.createProfile = True transaction_request.profile = profile if payment_information.billing: customer_address = apicontractsv1.customerAddressType() customer_address.firstName = payment_information.billing.first_name customer_address.lastName = payment_information.billing.last_name customer_address.company = payment_information.billing.company_name # authorize.net support says we should not attempt submitting street_address_2 customer_address.address = payment_information.billing.street_address_1 customer_address.city = payment_information.billing.city customer_address.state = payment_information.billing.country_area customer_address.zip = payment_information.billing.postal_code customer_address.country = payment_information.billing.country transaction_request.billTo = customer_address create_transaction_request = apicontractsv1.createTransactionRequest() create_transaction_request.merchantAuthentication = merchant_auth create_transaction_request.refId = str(payment_information.payment_id) create_transaction_request.transactionRequest = transaction_request response = _make_request(create_transaction_request, config.connection_params) ( success, error, transaction_id, transaction_response, raw_response, ) = _handle_authorize_net_response(response) psp_reference = None if transaction_id: psp_reference = transaction_id elif payment_information.token: transaction_id = payment_information.token if hasattr(response, "profileResponse") and hasattr( response.profileResponse, "customerProfileId"): customer_id = response.profileResponse.customerProfileId payment_method_info = _authorize_net_account_to_payment_method_info( transaction_response) return GatewayResponse( is_success=success, action_required=False, transaction_id=transaction_id, amount=payment_information.amount, currency=payment_information.currency, error=error, payment_method_info=payment_method_info, kind=kind, raw_response=raw_response, customer_id=customer_id, psp_reference=str(psp_reference) if psp_reference else None, )
def charge_credit_card(): #try: # 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 = cardCode # Add the payment data to a paymentType object payment = apicontractsv1.paymentType() payment.creditCard = creditCard # Create order information order = apicontractsv1.orderType() order.invoiceNumber = "110011" order.description = "Lost a Bet" # Set the customer's Bill To address fullname1 = f.get('/Users/' + loser, 'name') customerAddress = apicontractsv1.customerAddressType() customerAddress.firstName = "John" customerAddress.lastName = "Doe" customerAddress.company = "Souveniropolis" customerAddress.address1 = "14 Main Street" customerAddress.city1 = "Pecan Springs" customerAddress.state1 = "TX" customerAddress.zip1 = "44628" customerAddress.country1 = "USA" # Add values for transaction settings duplicateWindowSetting = apicontractsv1.settingType() duplicateWindowSetting.settingName = "duplicateWindow" duplicateWindowSetting.settingValue = "600" settings = apicontractsv1.ArrayOfSetting() settings.setting.append(duplicateWindowSetting) customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = "99999456657" customerData.email = "*****@*****.**" # Create a transactionRequestType object and add the previous objects to it. #charged transactionrequest = apicontractsv1.transactionRequestType() transactionrequest.transactionType = "authCaptureTransaction" transactionrequest.amount = amount transactionrequest.payment = payment transactionrequest.order = order transactionrequest.billTo = customerAddress transactionrequest.customer = customerData transactionrequest.transactionSettings = settings # 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
def create_customer_data(self): customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = str(self.invoice.profile.user.pk) customerData.email = self.invoice.profile.user.email return customerData
def createTransactionRequest(cart, refId, opaque_data, contact_info, transactionType='authCaptureTransaction'): # Get Authorize.net API credentials merchantAuth = _getMerchantAuth() # Get payment info payment = _getPayment(opaque_data) # Create order information order = apicontractsv1.orderType() order.description = refId # Set the customer's Bill To address customerAddress = apicontractsv1.customerAddressType() customerAddress.firstName = contact_info.get( 'first_name', contact_info.get('name_on_card', '')) customerAddress.lastName = contact_info.get('last_name', '') customerAddress.address = contact_info['address'] customerAddress.city = contact_info['city'] customerAddress.state = contact_info['state'] customerAddress.zip = contact_info['zip'] customerAddress.country = contact_info['country'] customerAddress.phoneNumber = contact_info['phone'] # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.email = contact_info['email'] # @@@ shipping # 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 = transactionType transactionrequest.amount = six.text_type(cart.amount) transactionrequest.order = order transactionrequest.payment = payment transactionrequest.billTo = customerAddress transactionrequest.customer = customerData transactionrequest.transactionSettings = settings transactionrequest.lineItems = _getLineItems(cart) # Assemble the complete transaction request createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.transactionRequest = transactionrequest with enhanced_authnet_logging(): controller = createTransactionController(createtransactionrequest) if config.IN_PRODUCTION: controller.setenvironment(constants.PRODUCTION) controller.execute() response = controller.getresponse() logger.info('createTransactionController response: {}'.format( response.__repr__())) defaultMsg = 'Your card could not be processed.' if controller._httpResponse: logger.info('Authorize.net response: {}'.format( controller._httpResponse)) if response.messages.resultCode == 'Ok': if response.transactionResponse.responseCode != 1: # Approved raise PaymentProcessingException(defaultMsg) return response else: raise PaymentProcessingException(response.messages.message[0].text or defaultMsg)
def createTransactionRequest( cart, refId, opaque_data, contact_info, transactionType='authCaptureTransaction'): # Get Authorize.net API credentials merchantAuth = _getMerchantAuth() # Get payment info payment = _getPayment(opaque_data) # Create order information order = apicontractsv1.orderType() order.description = refId # Set the customer's Bill To address customerAddress = apicontractsv1.customerAddressType() customerAddress.firstName = contact_info.get( 'first_name', contact_info.get('name_on_card', '')) customerAddress.lastName = contact_info.get('last_name', '') customerAddress.address = contact_info['address'] customerAddress.city = contact_info['city'] customerAddress.state = contact_info['state'] customerAddress.zip = contact_info['zip'] customerAddress.country = contact_info['country'] customerAddress.phoneNumber = contact_info['phone'] # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.email = contact_info['email'] # @@@ shipping # 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 = transactionType transactionrequest.amount = str(cart.amount) transactionrequest.order = order transactionrequest.payment = payment transactionrequest.billTo = customerAddress transactionrequest.customer = customerData transactionrequest.transactionSettings = settings transactionrequest.lineItems = _getLineItems(cart) # Assemble the complete transaction request createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.transactionRequest = transactionrequest # Create the controller and get response createtransactioncontroller = createTransactionController( createtransactionrequest) if config.IN_PRODUCTION: createtransactioncontroller.setenvironment(constants.PRODUCTION) createtransactioncontroller.execute() response = createtransactioncontroller.getresponse() if response.messages.resultCode == 'Ok': if response.transactionResponse.responseCode != 1: # Approved raise PaymentProcessingException( 'Your card could not be processed.') return response else: raise PaymentProcessingException(response.messages.message[0].text)
def _create_transaction_request(self, transaction): """ Create an authorize.net transaction request object from whatever silver is passing over. :param transaction: A Silver transaction with a AuthorizeNet payment method. :return: An authorize.net TransactionRequest """ logger.info("creating transaction request") payment = apicontractsv1.paymentType() payment.creditCard = self._create_credit_card(transaction.payment_method.customer) # Create order information order = apicontractsv1.orderType() order.invoiceNumber = "%s-%d" % (transaction.document.series, transaction.document.number) if len(transaction.document.entries) > 0: issue_date = transaction.document.issue_date.strftime("%m/%d/%Y") dsc = issue_date + " " + "\n".join(map(str, transaction.document.entries)) else: dsc = "" if len(dsc) > 80: dsc = dsc[0:80] # Construct line items if len(transaction.document.entries) > 0: line_items = apicontractsv1.ArrayOfLineItem() for e in transaction.document.entries: line_items.lineItem.append(self._create_entry_line_item(e)) else: line_items = False order.description = dsc # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = str(transaction.customer.id) customerData.email = transaction.customer.email settings = self._create_transaction_settings() _tx_request = apicontractsv1.transactionRequestType() _tx_request.transactionType = "authCaptureTransaction" _tx_request.amount = transaction.amount _tx_request.payment = payment _tx_request.order = order _tx_request.billTo = self._create_bill_to(transaction.customer) _tx_request.customer = customerData _tx_request.transactionSettings = settings if line_items: _tx_request.lineItems = line_items _request = apicontractsv1.createTransactionRequest() _request.merchantAuthentication = self.merchantAuth _request.refId = self.merchantId _request.transactionRequest = _tx_request return _request
def create_an_accept_payment_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 = "*****@*****.**" # 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
def charge_credit_card(amount): """ Charge a credit card """ # 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() card_types = ['visa', 'discover', 'mastercard', 'jcb'] creditCard.cardNumber = fake.credit_card_number( card_type=random.choice(card_types)) creditCard.expirationDate = fake.credit_card_expire() creditCard.cardCode = fake.credit_card_security_code() # Add the payment data to a paymentType object payment = apicontractsv1.paymentType() payment.creditCard = creditCard # Create order information order = apicontractsv1.orderType() order.invoiceNumber = str(random.randint(1000, 3000)) order.description = fake.bs() # Set the customer's Bill To address customerAddress = apicontractsv1.customerAddressType() customerAddress.firstName = fake.first_name() customerAddress.lastName = fake.last_name() customerAddress.company = fake.bs() customerAddress.address = fake.street_address() customerAddress.city = fake.city() customerAddress.state = fake.address().split()[-1].split()[0] customerAddress.zip = fake.postalcode_in_state() customerAddress.country = fake.country() customerAddress.phoneNumber = fake.phone_number() # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = fake.upc_e() customerData.email = fake.email() # 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 = fake.catch_phrase() 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 = fake.catch_phrase() line_item_2.quantity = "3" line_item_2.unitPrice = "7.95" line_item_3 = apicontractsv1.lineItemType() line_item_3.itemId = "ID num goes here" line_item_3.name = "third" line_item_3.description = fake.catch_phrase() line_item_3.quantity = "12" line_item_3.unitPrice = "100.00" # build the array of line items line_items = apicontractsv1.ArrayOfLineItem() line_items.lineItem.append(line_item_1) line_items.lineItem.append(line_item_2) line_items.lineItem.append(line_item_3) # Create a transactionRequestType object and add the previous objects to it. transactionrequest = apicontractsv1.transactionRequestType() transactionrequest.transactionType = "authCaptureTransaction" transactionrequest.amount = amount transactionrequest.payment = payment transactionrequest.order = order transactionrequest.billTo = customerAddress transactionrequest.customer = customerData transactionrequest.transactionSettings = settings transactionrequest.lineItems = line_items # Assemble the complete transaction request createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.refId = "1234-3432" 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
def charge_credit_card(amount): """ Charge a credit card """ # 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 = input("Credit card number please! ") creditCard.expirationDate = input("Expiration date (year-mo): ") creditCard.cardCode = input("CVV Code") # Add the payment data to a paymentType object payment = apicontractsv1.paymentType() payment.creditCard = creditCard # Create order information order = apicontractsv1.orderType() order.invoiceNumber = input("Invoice Number: ") order.description = input("Order description: ") # Set the customer's Bill To address customerAddress = apicontractsv1.customerAddressType() customerAddress.firstName = input("Customer first name: ") customerAddress.lastName = input("Customer last name: ") customerAddress.company = input("Customer company: ") customerAddress.address = input("Customer address: ") customerAddress.city = input("City: ") customerAddress.state = input("State: ") customerAddress.zip = input("Zip code: ") customerAddress.country = input("Country; ") # Set the customer's identifying information customerData = apicontractsv1.customerDataType() customerData.type = "individual" customerData.id = "99999456654" customerData.email = input("Customer email: ") # 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 = int(input("How many do you want? ")) line_item_1.unitPrice = line_item_1.quantity # print("That will cost " + line_item_1.quantity) # build the array of line items line_items = apicontractsv1.ArrayOfLineItem() line_items.lineItem.append(line_item_1) # Create a transactionRequestType object and add the previous objects to it. transactionrequest = apicontractsv1.transactionRequestType() transactionrequest.transactionType = "authCaptureTransaction" transactionrequest.amount = amount transactionrequest.payment = payment transactionrequest.order = order transactionrequest.billTo = customerAddress transactionrequest.customer = customerData 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() print("Thank you for shopping!") 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
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 = "*****@*****.**" # 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