如何使用Rest API v2 with .NET将销售收据添加到QuickBooks Online中

时间:2013-09-08 03:54:48

标签: .net rest intuit-partner-platform quickbooks-online

我已经尝试了很多工作。我终于找到了一些代码来创建销售收据,但是当我尝试添加销售收据时,我收到一个错误:“'',十六进制值0x1F,是一个无效字符。第1行,第1位。”。我正在使用Visual Web Developer。这是我提出的代码:

realmId = "removed";
            accessToken = "removed";
            accessTokenSecret = "removed";
            consumerKey = ConfigurationManager.AppSettings["consumerKey"];
            consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];
            dataSourcetype = IntuitServicesType.QBO;


            OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
            ServiceContext context = new ServiceContext(oauthValidator, realmId, dataSourcetype);
            DataServices commonService = new DataServices(context);



            Intuit.Ipp.Data.Qbo.SalesReceipt qboSalesReceipt = new Intuit.Ipp.Data.Qbo.SalesReceipt();

            //Create Header
            Intuit.Ipp.Data.Qbo.SalesReceiptHeader qboSalesReceiptHeader = new Intuit.Ipp.Data.Qbo.SalesReceiptHeader();
            qboSalesReceiptHeader.DocNumber = "abc123";
            qboSalesReceiptHeader.TxnDate = DateTime.Now;
            qboSalesReceiptHeader.TxnDateSpecified = true;
            qboSalesReceiptHeader.Note = "Sales Receipt Note";
            qboSalesReceiptHeader.CustomerId = new IdType() { idDomain = idDomainEnum.QBO, Value = "49" };

            //Set Payment Detail in Header
            /*Intuit.Ipp.Data.Qbo.PaymentDetail qboSalesReceiptHeaderPaymentDetail = new Intuit.Ipp.Data.Qbo.PaymentDetail();
            qboSalesReceiptHeaderPaymentDetail.Item = new CashPayment { Desc = "Cash Payment Ref #" };
            qboSalesReceiptHeader.Detail = qboSalesReceiptHeaderPaymentDetail;*/

            //Set Header
            qboSalesReceipt.Header = qboSalesReceiptHeader;

            //Set Line Item
            ItemsChoiceType2[] salesReceiptAttributes = { ItemsChoiceType2.ItemId, ItemsChoiceType2.UnitPrice, ItemsChoiceType2.Qty };
            object[] salesReceiptLineValues = { new IdType() { idDomain = idDomainEnum.QBO, Value = "5" }, new decimal(33), new decimal(2) };
            var salesReceiptLine = new SalesReceiptLine();
            salesReceiptLine.Amount = 66;
            salesReceiptLine.AmountSpecified = true;
            salesReceiptLine.Desc = "test " + DateTime.Now.ToShortDateString();
            salesReceiptLine.ItemsElementName = salesReceiptAttributes;
            salesReceiptLine.Items = salesReceiptLineValues;
            salesReceiptLine.ServiceDate = DateTime.Now;
            salesReceiptLine.ServiceDateSpecified = true;

            //Call IDS to Create Sales Receipt
           Intuit.Ipp.Data.Qbo.SalesReceipt addedSalesReceipt = commonService.Add(qboSalesReceipt) as SalesReceipt;
            //Intuit.Ipp.Data.Qbo.SalesReceipt addedSalesReceipt = commonService.Add(qboSalesReceipt);

            string message = "Sales receipt add complete";

            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", message);

无论我做什么,我都无法让它发挥作用。我非常感谢任何人的帮助。谢谢

1 个答案:

答案 0 :(得分:2)

尝试使用fiddler等一些工具捕获原始XML请求。然后验证XML结构是否正常(它也将检查编码问题)。您可以尝试使用Notepad ++编辑器的XML工具插件进行此检查。

您也可以在ApiExplorer工具中测试此请求XML。

参考 - https://developer.intuit.com/apiexplorer?apiname=V2QBO

修改

QBO参考 - https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/v2/0400_quickbooks_online/salesreceipt

为V2 QBO创建XML的示例

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SalesReceipt xmlns="http://www.intuit.com/sb/cdm/v2" xmlns:ns2="http://www.intuit.com/sb/cdm/qbopayroll/v1" xmlns:ns3="http://www.intuit.com/sb/cdm/qbo">
    <Header>
        <DocNumber>abc123</DocNumber>
        <TxnDate>2013-09-09-07:00</TxnDate>
        <Note>Sales Receipt Note</Note>     
        <CustomerId idDomain="QBO">1</CustomerId>
    </Header>
    <Line>
        <Id>1</Id>
        <Desc>Item1</Desc>
        <Amount>100.00</Amount>
        <Taxable>false</Taxable>
        <ItemId>3</ItemId>
    </Line>
</SalesReceipt>

由于