Пример #1
0
def apiCall(request):
    access_token = request.session.get('accessToken', None)
    if access_token is None:
        return HttpResponse(
            'Your Bearer token has expired, please initiate C2QB flow again')

    realmId = request.session['realmId']
    if realmId is None:
        return HttpResponse(
            'No realm ID. QBO calls only work if the payment scope was passed!'
        )

    refresh_token = request.session['refreshToken']
    create_charge_response, status_code = createCharge(access_token)
    print(create_charge_response)
    print(status_code)

    if status_code >= 400:
        # if call to QBO doesn't succeed then get a new bearer token from refresh token and try again
        bearer = getBearerTokenFromRefreshToken(refresh_token)
        updateSession(request, bearer.accessToken, bearer.refreshToken,
                      realmId)
        create_charge_response, status_code = createCharge(bearer.accessToken)
        if status_code >= 400:
            return HttpResponseServerError()
    return HttpResponse('Charge create response: ' +
                        str(create_charge_response))
Пример #2
0
def apiCall(request):
    access_token = request.session.get('accessToken', None)
    if access_token is None:
        return HttpResponse(
            'Your Bearer token has expired, please initiate C2QB flow again')

    realmId = request.session['realmId']
    if realmId is None:
        return HttpResponse(
            'No realm ID. QBO calls only work if the accounting scope was passed!'
        )

    refresh_token = request.session['refreshToken']
    company_info_response, status_code = getCompanyInfo(access_token, realmId)

    if status_code >= 400:
        # if call to QBO doesn't succeed then get a new bearer token from refresh token and try again
        bearer = getBearerTokenFromRefreshToken(refresh_token)
        updateSession(request, bearer.accessToken, bearer.refreshToken,
                      realmId)
        company_info_response, status_code = getCompanyInfo(
            bearer.accessToken, realmId)
        if status_code >= 400:
            return HttpResponseServerError()
    company_name = company_info_response['CompanyInfo']['CompanyName']
    address = company_info_response['CompanyInfo']['CompanyAddr']
    return HttpResponse('Company Name: ' + company_name +
                        ', Company Address: ' + address['Line1'] + ', ' +
                        address['City'] + ', ' + ' ' + address['PostalCode'])
Пример #3
0
def refreshTokenCall(request):
    refresh_token = request.session.get('refreshToken', None)
    if refresh_token is None:
        return HttpResponse('Not authorized')
    bearer = getBearerTokenFromRefreshToken(refresh_token)

    if isinstance(bearer, str):
        return HttpResponse(bearer)
    else:
        return HttpResponse('Access Token: ' + bearer.accessToken +
                            ', Refresh Token: ' + bearer.refreshToken)
def connected(request):
    access_token = request.session.get('accessToken', None)
    if access_token is None:
        return HttpResponse(
            'Your Bearer token has expired, please initiate Sign In With Intuit flow again'
        )

    refresh_token = request.session.get('refreshToken', None)
    realmId = request.session['realmId']
    if realmId is None:
        user_profile_response, status_code = getUserProfile(access_token)

        if status_code >= 400:
            # if call to User Profile Service doesn't succeed then get a new bearer token from refresh token
            # and try again
            bearer = getBearerTokenFromRefreshToken(refresh_token)
            user_profile_response, status_code = getUserProfile(
                bearer.accessToken)
            updateSession(request,
                          bearer.accessToken,
                          bearer.refreshToken,
                          request.session.get('realmId', None),
                          name=user_profile_response.get('givenName', None))

            if status_code >= 400:
                return HttpResponseServerError()
        c = {
            'first_name': user_profile_response.get('givenName', ' '),
        }
    else:
        if request.session.get('name') is None:
            name = ''
        else:
            name = request.session.get('name')
        c = {
            'first_name': name,
        }

    session = Session()
    c['customer_balances'] = session.query(CustomerBalance).all()
    session.close()
    return render(request, 'connected.html', context=c)
def apiCall(request):
    access_token = request.session.get('accessToken', None)
    if access_token is None:
        return HttpResponse(
            'Your Bearer token has expired, please initiate C2QB flow again')

    realmId = request.session['realmId']
    if realmId is None:
        return HttpResponse(
            'No realm ID. QBO calls only work if the accounting scope was passed!'
        )

    refresh_token = request.session['refreshToken']
    customer_balance_response, status_code = getCustomerBalance(
        access_token, realmId)

    if status_code >= 400:
        # if call to QBO doesn't succeed then get a new bearer token from refresh token and try again
        bearer = getBearerTokenFromRefreshToken(refresh_token)
        updateSession(request, bearer.accessToken, bearer.refreshToken,
                      realmId)
        customer_balance_response, status_code = getCustomerBalance(
            bearer.accessToken, realmId)
        if status_code >= 400:
            return HttpResponseServerError()

    columns = [
        column['ColTitle'] or column['ColType']
        for column in customer_balance_response['Columns']['Column']
    ]
    rows = transform_customer_balance_rows(
        customer_balance_response['Rows']['Row'])['rows']
    data_frame = pd.DataFrame.from_dict(rows, orient='index', columns=columns)

    data_frame.rename(columns={
        'Customer': 'customer',
        'Total': 'balance'
    },
                      inplace=True)
    data_frame.to_sql('customer_balance',
                      con=settings.DB_ENGINE,
                      if_exists='append',
                      index=False)

    time = parse(customer_balance_response['Header']['Time'])
    updated_log = UpdateLog(date=time.date())
    session = Session()
    session.add(updated_log)
    session.commit()
    report_name = customer_balance_response['Header']['ReportName']
    date_macro = customer_balance_response['Header'][
        'DateMacro'] if 'DateMacro' in customer_balance_response[
            'Header'] else customer_balance_response['Header']['EndPeriod']

    # update_report = {'report_name': report_name, 'date_macro': date_macro, 'updated_at': time.strftime("%m/%d/%Y"),
    #                  'columns': ', '.join(columns), 'no_of_rows': len(rows),
    #                  'customer_balances': session.query(CustomerBalance).all()}
    session.close()
    return HttpResponse('Report Name: ' + report_name + ', From: ' +
                        date_macro + ', Updated At: ' +
                        time.strftime("%m/%d/%Y") + ', Columns: ' +
                        ', '.join(columns) + ', Number of rows fetched: ' +
                        str(len(rows)))