Hi theinfo,
Refer below steps to integrate QuickBooks.
Creating Intuit Developer Account
Navigate to Intuit Developer Portal (https://developer.intuit.com/app/developer/myapps)
Create an app and get Client ID and Client Secret.
For more details refer https://developer.intuit.com/app/developer/qbo/docs/get-started/start-developing-your-app
QuickBooks uses OAuth 2.0 for authentication. So Install following NuGet Packages.
Install-Package Intuit.Ipp.OAuth2PlatformClient
Install-Package Intuit.Ipp.Core
Install-Package Intuit.Ipp.Data
Install-Package Intuit.Ipp.QueryFilter
Add the following App Setting in Web.config file.
<appSettings>
<add key="ClientId" value="YOUR_CLIENT_ID"/>
<add key="ClientSecret" value="YOUR_CLIENT_SECRET"/>
<add key="RedirectUrl" value="https://localhost:xxxx/Callback.aspx"/>
<add key="Environment" value="sandbox"/>
</appSettings>
HTML
<asp:Button ID="btnConnect" runat="server" Text="Connect" OnClick="OnConnect" />
Connect
using Intuit.Ipp.OAuth2PlatformClient;
protected void OnConnect(object sender, EventArgs e)
{
var client = new OAuth2Client(
ConfigurationManager.AppSettings["ClientId"],
ConfigurationManager.AppSettings["ClientSecret"],
ConfigurationManager.AppSettings["RedirectUrl"],
ConfigurationManager.AppSettings["Environment"]
);
string authUrl = client.GetAuthorizationURL();
Response.Redirect(authUrl);
}
Callback Page
protected async void Page_Load(object sender, EventArgs e)
{
string code = Request.QueryString["code"];
string realmId = Request.QueryString["realmId"];
var client = new OAuth2Client(
ConfigurationManager.AppSettings["ClientId"],
ConfigurationManager.AppSettings["ClientSecret"],
ConfigurationManager.AppSettings["RedirectUrl"],
ConfigurationManager.AppSettings["Environment"]
);
var tokenResponse = await client.GetBearerTokenAsync(code);
Session["accessToken"] = tokenResponse.AccessToken;
Session["refreshToken"] = tokenResponse.RefreshToken;
Session["realmId"] = realmId;
}
Creating QuickBooks Service
using Intuit.Ipp.Core;
using Intuit.Ipp.Data;
using Intuit.Ipp.QueryFilter;
using Intuit.Ipp.Security;
public class QuickBooksService
{
public static DataService GetService(string accessToken, string realmId)
{
var context = new ServiceContext(
realmId,
IntuitServicesType.QBO,
new OAuth2RequestValidator(accessToken)
);
return new DataService(context);
}
}
Creating Customer
protected void CreateCustomer()
{
var service = QuickBooksService.GetService(
Session["accessToken"].ToString(),
Session["realmId"].ToString()
);
var customer = new Customer()
{
GivenName = "Mudassar",
FamilyName = "Mudassar",
PrimaryEmailAddr = new EmailAddress()
{
Address = "queries@aspsnippets.com"
}
};
var result = service.Add(customer);
}
Creating Invoice
protected void CreateInvoice()
{
var invoice = new Invoice()
{
CustomerRef = new ReferenceType { Value = customerId },
Line = new List<Line>()
{
new Line()
{
Amount = 50000,
DetailType = LineDetailTypeEnum.SalesItemLineDetail,
SalesItemLineDetail = new SalesItemLineDetail()
{
Qty = 1,
UnitPrice = 500
}
}
}
};
var result = service.Add(invoice);
}
Refresh Token
var newToken = await client.RefreshTokenAsync(refreshToken);
Session["accessToken"] = newToken.AccessToken;