Integration Quickbook online api to rails app

2019-06-07 17:38发布

问题:

I am integrating

rails application to Quickbooks online

using API.

Use gem Quickbooks-ruby

But want to add discount, add taxes into invoice but not success even not found how to pass in API.

回答1:

invoice = Quickbooks::Model::Invoice.new
invoice.customer_id = 1
invoice.txn_date = Date.civil(2014, 3, 27)
invoice.doc_number = "001"

transaction_tax = Quickbooks::Model::TransactionTaxDetail.new
# Point to a saved tax code in QBO, e.g. this points to id = 2, 
# which is a NYC tax code saved on QBO account = 10% sales tax
transaction_tax.txn_tax_code_id = 2
transaction_tax.total_tax =  134.10
invoice.txn_tax_detail = transaction_tax

sales_line_item = Quickbooks::Model::InvoiceLineItem.new
sales_line_item.amount = 1490
sales_line_item.description = "CCM ice skates"
sales_line_item.sales_item! do |detail|
  detail.unit_price = 149
  detail.quantity = 10
  detail.item_id = 1 # Item ID here
  detail.tax_code_id = 'TAX' # for US must be 'NON' or 'TAX'
end

discount_line_item = Quickbooks::Model::InvoiceLineItem.new
discount_line_item.amount = 149
discount_line_item.discount_item! do |detail|
  detail.discount_percent = 10
  detail.percent_based = true
  detail.discount_account_id = 99
end

invoice.line_items << sales_line_item
invoice.line_items << discount_line_item

service = Quickbooks::Service::Invoice.new
service.access_token = OAuth::AccessToken.new($qb_oauth_consumer, "token", "secret")
service.company_id = "9991111222"
created_invoice = service.create(invoice)


回答2:

There should be something like JAXB in ruby for object serialization/deserialization.

From the following link, you can download QB endpoint definations and data class defination(as XSD). You need to generate data classes from it.

https://developer.intuit.com/docs/@api/deki/files/2466/v3.1_dataservices.zip

Then using any standard ruby based OAuth lib, you can make call to QB API Endpoints. You can use the setter methods of the data class( in your case - invoice ) to populate data/to construct the payload. ( I don't have any ready example of this. But I guess it is not hard to find in net)

For doc, you can refer the following two links.

https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/030_entity_services_reference/invoice

https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/030_entity_services_reference/invoice#DiscountLineDetail

Thanks