from __future__ import print_function
import time
import authorizenet_rest
from authorizenet_rest.rest import ApiException

from authorizenet_rest.apis.customers_api import CustomersApi
from pprint import pprint
import warnings
import os
import sys
import random

import imp
#constants = imp.load_source('modulename', 'constants.py')
#warnings.filterwarnings("ignore",category=DeprecationWarning)
ExternalConfig = imp.load_source('modulename', '.\ExternalConfiguration.py')

def create_customer():
    # create an instance of the API class
    ExternalConfig.setConfig();
    apiclient = ExternalConfig.configureApiClient();
    authorization = ExternalConfig.getAuthorization()
    api_instance = authorizenet_rest.apis.customers_api.CustomersApi(apiclient)
    futdate = ExternalConfig.futuredate()
    #customer_id = ""  # str | Customer ID
    identifier = authorizenet_rest.Identifier()
    identifier.email = "testt11@tessst.com"
    identifier.reference = "Testing DD"

    baseAddress = authorizenet_rest.BaseAddress()
    baseAddress.address1 = "ewwe"
    baseAddress.administrativeArea = "TX"
    baseAddress.company = "Souveniropolis"
    baseAddress.country = "USA"
    baseAddress.firstName = "Ellen"
    baseAddress.lastName = "Johnson"
    baseAddress.locality = "Pecan Springs"
    baseAddress.phoneNumber = "1234567891"
    baseAddress.postalCode = "44628"

    creditCard = authorizenet_rest.Card("4111111111111111", futdate, "VISA", "1220")
    bankAccount = authorizenet_rest.BankAccount("Checking", "000000204", "01245524321", "test", "WEB",
                                                                  "testbank", "131231242343424")
    fluidData = authorizenet_rest.FluidData("TestDesc", "TestVal", "TestKey")

    customerPayment = authorizenet_rest.CustomerPayment("1812938359", "1807546760")

    paymentInstrument = authorizenet_rest.PaymentInformation()
    paymentInstrument.bankAccount = ""
    paymentInstrument.creditCard = creditCard
    paymentInstrument.fluidData = ""
    paymentInstrument.customer = ""

    createPaymentMethod = authorizenet_rest.CreatePaymentMethod()
    createPaymentMethod.billTo = baseAddress
    createPaymentMethod.payment = paymentInstrument
    description = ''.join([random.choice("Sample descriptio checkn") for n in range(15)])
    createcustomer = authorizenet_rest.CreateCustomerAndPaymentMethod(description, identifier)
    #print(createcustomer)
    print("Check Output")
    try:
        # Create A Customer Address
        api_response = api_instance.create_customer_and_payment_method(createcustomer)
        print(api_response.id)
        return api_response.id
    except ApiException as e:
        print("Exception when calling AddressesApi->create_customer_address: %s\n" % e)

if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
    create_customer()