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

from authorizenet_rest.apis.paymentmethods_api import PaymentmethodsApi
from pprint import pprint
import os
import sys

ExternalConfig = imp.load_source('modulename', '.\ExternalConfiguration.py')

def create_customer_payment_method(customer_id):
    # create an instance of the API class
    ExternalConfig.setConfig()
    apiclient = ExternalConfig.configureApiClient();
    authorization = ExternalConfig.getAuthorization()
    futdate = ExternalConfig.futuredate()
    api_instance = authorizenet_rest.apis.paymentmethods_api.PaymentmethodsApi(apiclient)

    creditcard = authorizenet_rest.CreditCard("4111111111111110", "2018-12", "VISA", "123")
    bankAccount = authorizenet_rest.BankAccount("Checking", "000000204", "01245524321", "test", "WEB",
                                               "testbank", "131231242343424")
    fluidData = authorizenet_rest.FluidData("TestDesc", "TestVal", "TestKey")

    paymentInstrument = authorizenet_rest.PaymentInstrument()
    paymentInstrument.bankAccount = "null"
    paymentInstrument.creditCard = creditcard
    paymentInstrument.fluidData = "null"
    paymentInstrument.customer = "null"

    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"

    createPaymentMethod = authorizenet_rest.CreatePaymentMethod()
    createPaymentMethod.bill_to = baseAddress
    createPaymentMethod.payment_instrument = paymentInstrument
    print("Check Output")
    try:
        # Create Customer Payment Method
        api_response = api_instance.create_payment_method(customer_id, createPaymentMethod, authorization=authorization)
        pprint(api_response)
        return api_response.id
    except ApiException as e:
        print("Exception when calling PaymentmethodsApi->create_customer_payment_method: %s\n" % e)

if(os.path.basename(__file__) == os.path.basename(sys.argv[0])):
    create_customer_payment_method('1813141166')