github.com/schmorrison/Zoho@v1.1.4/subscriptions/invoices.go (about)

     1  package subscriptions
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	zoho "github.com/schmorrison/Zoho"
     8  )
     9  
    10  type InvoiceStatus string
    11  
    12  // Proper names for Invoice statuses
    13  const (
    14  	InvoiceStatusAll           InvoiceStatus = "Status.All"
    15  	InvoiceStatusSent          InvoiceStatus = "Status.Sent"
    16  	InvoiceStatusDraft         InvoiceStatus = "Status.Draft"
    17  	InvoiceStatusOverDue       InvoiceStatus = "Status.OverDue"
    18  	InvoiceStatusPaid          InvoiceStatus = "Status.Paid"
    19  	InvoiceStatusPartiallyPaid InvoiceStatus = "Status.PartiallyPaid"
    20  	InvoiceStatusVoid          InvoiceStatus = "Status.Void"
    21  	InvoiceStatusUnpaid        InvoiceStatus = "Status.Unpaid"
    22  	InvoiceStatusPending       InvoiceStatus = "Status.Pending" // Pending status is not present in zoho documentation, but works
    23  )
    24  
    25  // listInvoicesWithParams will return the list of invoices that match the given invoice status
    26  // and additional filter defined by parameter name and value (allows to filter by `customer_id` and `subscription_id`)
    27  // https://www.zoho.com/subscriptions/api/v1/#Invoices_List_all_invoices
    28  func (s *API) listInvoicesWithParams(
    29  	status InvoiceStatus,
    30  	paramName, paramValue string,
    31  ) (data InvoicesResponse, err error) {
    32  	if status == "" {
    33  		status = InvoiceStatusAll
    34  	}
    35  	endpoint := zoho.Endpoint{
    36  		Name:         "invoices",
    37  		URL:          fmt.Sprintf("https://subscriptions.zoho.%s/api/v1/invoices", s.ZohoTLD),
    38  		Method:       zoho.HTTPGet,
    39  		ResponseData: &InvoicesResponse{},
    40  		URLParameters: map[string]zoho.Parameter{
    41  			"filter_by": zoho.Parameter(status),
    42  		},
    43  		Headers: map[string]string{
    44  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
    45  		},
    46  	}
    47  
    48  	if paramName != "" {
    49  		endpoint.URLParameters[paramName] = zoho.Parameter(paramValue)
    50  	}
    51  
    52  	err = s.Zoho.HTTPRequest(&endpoint)
    53  	if err != nil {
    54  		return InvoicesResponse{}, fmt.Errorf("Failed to retrieve invoices: %s", err)
    55  	}
    56  
    57  	if v, ok := endpoint.ResponseData.(*InvoicesResponse); ok {
    58  		return *v, nil
    59  	}
    60  
    61  	return InvoicesResponse{}, fmt.Errorf("Data retrieved was not 'InvoicesResponse'")
    62  }
    63  
    64  // ListAllInvoices will return the list of invoices that match the given invoice status
    65  // https://www.zoho.com/subscriptions/api/v1/#Invoices_List_all_invoices
    66  func (s *API) ListAllInvoices(status InvoiceStatus) (data InvoicesResponse, err error) {
    67  	return s.listInvoicesWithParams(status, "", "")
    68  }
    69  
    70  // ListInvoicesForSubscription will return the list of invoices that match the given invoice status and subscription ID
    71  // https://www.zoho.com/subscriptions/api/v1/#Invoices_List_all_invoices
    72  func (s *API) ListInvoicesForSubscription(
    73  	status InvoiceStatus,
    74  	subscriptionID string,
    75  ) (data InvoicesResponse, err error) {
    76  	return s.listInvoicesWithParams(status, "subscription_id", subscriptionID)
    77  }
    78  
    79  // ListInvoicesForSubscription will return the list of invoices that match the given invoice status and customer ID
    80  // https://www.zoho.com/subscriptions/api/v1/#Invoices_List_all_invoices
    81  func (s *API) ListInvoicesForCustomer(
    82  	status InvoiceStatus,
    83  	customerID string,
    84  ) (data InvoicesResponse, err error) {
    85  	return s.listInvoicesWithParams(status, "customer_id", customerID)
    86  }
    87  
    88  // GetInvoice will return the subscription specified by id
    89  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Retrieve_a_subscription
    90  func (s *API) GetInvoice(id string) (data InvoiceResponse, err error) {
    91  	endpoint := zoho.Endpoint{
    92  		Name: "invoices",
    93  		URL: fmt.Sprintf(
    94  			"https://subscriptions.zoho.%s/api/v1/invoices/%s",
    95  			s.ZohoTLD,
    96  			id,
    97  		),
    98  		Method:       zoho.HTTPGet,
    99  		ResponseData: &InvoiceResponse{},
   100  		Headers: map[string]string{
   101  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   102  		},
   103  	}
   104  
   105  	err = s.Zoho.HTTPRequest(&endpoint)
   106  	if err != nil {
   107  		return InvoiceResponse{}, fmt.Errorf("Failed to retrieve invoice (%s): %s", id, err)
   108  	}
   109  
   110  	if v, ok := endpoint.ResponseData.(*InvoiceResponse); ok {
   111  		return *v, nil
   112  	}
   113  
   114  	return InvoiceResponse{}, fmt.Errorf("Data retrieved was not 'InvoiceResponse'")
   115  }
   116  
   117  // AddAttachment attaches a file to an invoice
   118  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Add_attachment_to_an_invoice
   119  func (s *API) AddAttachment(
   120  	id, file string,
   121  	canSendInEmail bool,
   122  ) (data AttachementResponse, err error) {
   123  	endpoint := zoho.Endpoint{
   124  		Name: "invoices",
   125  		URL: fmt.Sprintf(
   126  			"https://subscriptions.zoho.%s/api/v1/invoices/%s/attachment",
   127  			s.ZohoTLD,
   128  			id,
   129  		),
   130  		Method:       zoho.HTTPPost,
   131  		ResponseData: &AttachementResponse{},
   132  		Attachment:   file,
   133  		BodyFormat:   zoho.FILE,
   134  		URLParameters: map[string]zoho.Parameter{
   135  			"can_send_in_mail": zoho.Parameter(strconv.FormatBool(canSendInEmail)),
   136  		},
   137  		Headers: map[string]string{
   138  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   139  		},
   140  	}
   141  
   142  	err = s.Zoho.HTTPRequest(&endpoint)
   143  	if err != nil {
   144  		return AttachementResponse{}, fmt.Errorf(
   145  			"Failed to attach file to invoice (%s): %s",
   146  			id,
   147  			err,
   148  		)
   149  	}
   150  
   151  	if v, ok := endpoint.ResponseData.(*AttachementResponse); ok {
   152  		return *v, nil
   153  	}
   154  
   155  	return AttachementResponse{}, fmt.Errorf("Data retrieved was not 'AttachementResponse'")
   156  }
   157  
   158  // EmailInvoice sends an invoice in email
   159  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Email_an_invoice
   160  func (s *API) EmailInvoice(
   161  	id string,
   162  	request EmailInvoiceRequest,
   163  ) (data EmailInvoiceResponse, err error) {
   164  	endpoint := zoho.Endpoint{
   165  		Name: "invoices",
   166  		URL: fmt.Sprintf(
   167  			"https://subscriptions.zoho.%s/api/v1/invoices/%s/email",
   168  			s.ZohoTLD,
   169  			id,
   170  		),
   171  		Method:       zoho.HTTPPost,
   172  		ResponseData: &EmailInvoiceResponse{},
   173  		RequestBody:  request,
   174  		Headers: map[string]string{
   175  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   176  		},
   177  	}
   178  
   179  	err = s.Zoho.HTTPRequest(&endpoint)
   180  	if err != nil {
   181  		return EmailInvoiceResponse{}, fmt.Errorf("Failed to email invoice (%s): %s", id, err)
   182  	}
   183  
   184  	if v, ok := endpoint.ResponseData.(*EmailInvoiceResponse); ok {
   185  		return *v, nil
   186  	}
   187  
   188  	return EmailInvoiceResponse{}, fmt.Errorf("Data retrieved was not 'EmailInvoiceResponse'")
   189  }
   190  
   191  // AddItems adds items to pending invoice
   192  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Add_items_to_a_pending_invoice
   193  func (s *API) AddItems(id string, request AddItemsRequest) (data AddItemsResponse, err error) {
   194  	endpoint := zoho.Endpoint{
   195  		Name: "invoices",
   196  		URL: fmt.Sprintf(
   197  			"https://subscriptions.zoho.%s/api/v1/invoices/%s/lineitems",
   198  			s.ZohoTLD,
   199  			id,
   200  		),
   201  		Method:       zoho.HTTPPost,
   202  		ResponseData: &AddItemsResponse{},
   203  		RequestBody:  request,
   204  		Headers: map[string]string{
   205  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   206  		},
   207  	}
   208  
   209  	err = s.Zoho.HTTPRequest(&endpoint)
   210  	if err != nil {
   211  		return AddItemsResponse{}, fmt.Errorf("Failed to add items to invoice (%s): %s", id, err)
   212  	}
   213  
   214  	if v, ok := endpoint.ResponseData.(*AddItemsResponse); ok {
   215  		return *v, nil
   216  	}
   217  
   218  	return AddItemsResponse{}, fmt.Errorf("Data retrieved was not 'AddItemsResponse'")
   219  }
   220  
   221  // CollectChargeViaCreditCard collects charge via credit card
   222  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Collect_charge_via_credit_card
   223  // Note: Real life reply for this request differs from Zoho documentation,
   224  // so CollectChargeViaCreditCardResponse was updated to include both top level objects:
   225  // - 'payment' per documentation
   226  // - 'invoice' per real life reply
   227  func (s *API) CollectChargeViaCreditCard(
   228  	id string,
   229  	request CollectChargeViaCreditCardRequest,
   230  ) (data CollectChargeViaCreditCardResponse, err error) {
   231  	endpoint := zoho.Endpoint{
   232  		Name: "invoices",
   233  		URL: fmt.Sprintf(
   234  			"https://subscriptions.zoho.%s/api/v1/invoices/%s/collect",
   235  			s.ZohoTLD,
   236  			id,
   237  		),
   238  		Method:       zoho.HTTPPost,
   239  		ResponseData: &CollectChargeViaCreditCardResponse{},
   240  		RequestBody:  request,
   241  		Headers: map[string]string{
   242  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   243  		},
   244  	}
   245  
   246  	err = s.Zoho.HTTPRequest(&endpoint)
   247  	if err != nil {
   248  		return CollectChargeViaCreditCardResponse{}, fmt.Errorf(
   249  			"Failed to collect charge via credit card (%s): %s",
   250  			id,
   251  			err,
   252  		)
   253  	}
   254  
   255  	if v, ok := endpoint.ResponseData.(*CollectChargeViaCreditCardResponse); ok {
   256  		return *v, nil
   257  	}
   258  
   259  	return CollectChargeViaCreditCardResponse{}, fmt.Errorf(
   260  		"Data retrieved was not 'CollectChargeViaCreditCardResponse'",
   261  	)
   262  }
   263  
   264  // CollectChargeViaBankAccount collects charge via bank account
   265  // https://www.zoho.com/subscriptions/api/v1/#Invoices_Collect_charge_via_bank_account
   266  func (s *API) CollectChargeViaBankAccount(
   267  	id string,
   268  	request CollectChargeViaBankAccountRequest,
   269  ) (data CollectChargeViaBankAccountResponse, err error) {
   270  	endpoint := zoho.Endpoint{
   271  		Name: "invoices",
   272  		URL: fmt.Sprintf(
   273  			"https://subscriptions.zoho.%s/api/v1/invoices/%s/collect",
   274  			s.ZohoTLD,
   275  			id,
   276  		),
   277  		Method:       zoho.HTTPPost,
   278  		ResponseData: &CollectChargeViaBankAccountResponse{},
   279  		RequestBody:  request,
   280  		Headers: map[string]string{
   281  			ZohoSubscriptionsEndpointHeader: s.OrganizationID,
   282  		},
   283  	}
   284  
   285  	err = s.Zoho.HTTPRequest(&endpoint)
   286  	if err != nil {
   287  		return CollectChargeViaBankAccountResponse{}, fmt.Errorf(
   288  			"Failed to collect charge via bank account (%s): %s",
   289  			id,
   290  			err,
   291  		)
   292  	}
   293  
   294  	if v, ok := endpoint.ResponseData.(*CollectChargeViaBankAccountResponse); ok {
   295  		return *v, nil
   296  	}
   297  
   298  	return CollectChargeViaBankAccountResponse{}, fmt.Errorf(
   299  		"Data retrieved was not 'CollectChargeViaBankAccountResponse'",
   300  	)
   301  }
   302  
   303  type CollectChargeViaBankAccountRequest struct {
   304  	AccountID string `json:"account_id"`
   305  }
   306  
   307  type CollectChargeViaBankAccountResponse struct {
   308  	Code    int64                        `json:"code"`
   309  	Message string                       `json:"message"`
   310  	Invoice CollectChargeInvoiceResponse `json:"invoice,omitempty"`
   311  	Payment struct {
   312  		PaymentID       string  `json:"payment_id"`
   313  		PaymentMode     string  `json:"payment_mode"`
   314  		Amount          float64 `json:"amount"`
   315  		AmountRefunded  float64 `json:"amount_refunded"`
   316  		BankCharges     float64 `json:"bank_charges"`
   317  		Date            string  `json:"date"`
   318  		Status          string  `json:"status"`
   319  		ReferenceNumber string  `json:"reference_number"`
   320  		DueDate         string  `json:"due_date"`
   321  		AmountDue       float64 `json:"amount_due"`
   322  		Description     string  `json:"description"`
   323  		CustomerID      string  `json:"customer_id"`
   324  		CustomerName    string  `json:"customer_name"`
   325  		Email           string  `json:"email"`
   326  		Autotransaction struct {
   327  			AutotransactionID    string `json:"autotransaction_id"`
   328  			PaymentGateway       string `json:"payment_gateway"`
   329  			GatewayTransactionID string `json:"gateway_transaction_id"`
   330  			GatewayErrorMessage  string `json:"gateway_error_message"`
   331  			AccountID            string `json:"account_id"`
   332  		} `json:"autotransaction"`
   333  		Invoices []struct {
   334  			InvoiceID     string  `json:"invoice_id"`
   335  			InvoiceNumber string  `json:"invoice_number"`
   336  			Date          string  `json:"date"`
   337  			InvoiceAmount float64 `json:"invoice_amount"`
   338  			AmountApplied float64 `json:"amount_applied"`
   339  			BalanceAmount float64 `json:"balance_amount"`
   340  		} `json:"invoices"`
   341  		CurrencyCode   string        `json:"currency_code"`
   342  		CurrencySymbol string        `json:"currency_symbol"`
   343  		CustomFields   []CustomField `json:"custom_fields"`
   344  		CreatedTime    string        `json:"created_time"`
   345  		UpdatedTime    string        `json:"updated_time"`
   346  	} `json:"payment,omitempty"`
   347  }
   348  
   349  type CollectChargeViaCreditCardRequest struct {
   350  	CardID string `json:"card_id"`
   351  }
   352  
   353  type CollectChargeViaCreditCardResponse struct {
   354  	Code    int64                        `json:"code"`
   355  	Message string                       `json:"message"`
   356  	Invoice CollectChargeInvoiceResponse `json:"invoice,omitempty"`
   357  	Payment struct {
   358  		PaymentID       string  `json:"payment_id"`
   359  		PaymentMode     string  `json:"payment_mode"`
   360  		Amount          float64 `json:"amount"`
   361  		AmountRefunded  float64 `json:"amount_refunded"`
   362  		BankCharges     float64 `json:"bank_charges"`
   363  		Date            string  `json:"date"`
   364  		Status          string  `json:"status"`
   365  		ReferenceNumber string  `json:"reference_number"`
   366  		Description     string  `json:"description"`
   367  		CustomerID      string  `json:"customer_id"`
   368  		CustomerName    string  `json:"customer_name"`
   369  		Email           string  `json:"email"`
   370  		Autotransaction struct {
   371  			AutotransactionID    string `json:"autotransaction_id"`
   372  			PaymentGateway       string `json:"payment_gateway"`
   373  			GatewayTransactionID string `json:"gateway_transaction_id"`
   374  			GatewayErrorMessage  string `json:"gateway_error_message"`
   375  			CardID               string `json:"card_id"`
   376  			LastFourDigits       string `json:"last_four_digits"`
   377  			ExpiryMonth          int64  `json:"expiry_month"`
   378  			ExpiryYear           int64  `json:"expiry_year"`
   379  		} `json:"autotransaction"`
   380  		Invoices []struct {
   381  			InvoiceID     string  `json:"invoice_id"`
   382  			InvoiceNumber string  `json:"invoice_number"`
   383  			Date          string  `json:"date"`
   384  			InvoiceAmount float64 `json:"invoice_amount"`
   385  			AmountApplied float64 `json:"amount_applied"`
   386  			BalanceAmount float64 `json:"balance_amount"`
   387  		} `json:"invoices"`
   388  		CurrencyCode   string        `json:"currency_code"`
   389  		CurrencySymbol string        `json:"currency_symbol"`
   390  		CustomFields   []CustomField `json:"custom_fields"`
   391  		CreatedTime    string        `json:"created_time"`
   392  		UpdatedTime    string        `json:"updated_time"`
   393  	} `json:"payment,omitempty"`
   394  }
   395  
   396  type CollectChargeInvoiceResponse struct {
   397  	AchPaymentInitiated     bool    `json:"ach_payment_initiated"`
   398  	Adjustment              float64 `json:"adjustment"`
   399  	AdjustmentDescription   string  `json:"adjustment_description"`
   400  	AllowPartialPayments    bool    `json:"allow_partial_payments"`
   401  	ApproverID              string  `json:"approver_id"`
   402  	AutoRemindersConfigured bool    `json:"auto_reminders_configured"`
   403  	Balance                 float64 `json:"balance"`
   404  	BcyAdjustment           float64 `json:"bcy_adjustment"`
   405  	BcyDiscountTotal        float64 `json:"bcy_discount_total"`
   406  	BcyShippingCharge       float64 `json:"bcy_shipping_charge"`
   407  	BcySubTotal             float64 `json:"bcy_sub_total"`
   408  	BcyTaxTotal             float64 `json:"bcy_tax_total"`
   409  	BcyTotal                float64 `json:"bcy_total"`
   410  	BillingAddress          struct {
   411  		Address   string `json:"address"`
   412  		Attention string `json:"attention"`
   413  		City      string `json:"city"`
   414  		Country   string `json:"country"`
   415  		Fax       string `json:"fax"`
   416  		Phone     string `json:"phone"`
   417  		State     string `json:"state"`
   418  		Street    string `json:"street"`
   419  		Street2   string `json:"street2"`
   420  		Zip       string `json:"zip"`
   421  	} `json:"billing_address"`
   422  	CanEditItems       bool   `json:"can_edit_items"`
   423  	CanSendInMail      bool   `json:"can_send_in_mail"`
   424  	CanSendInvoiceSms  bool   `json:"can_send_invoice_sms"`
   425  	CanSkipPaymentInfo bool   `json:"can_skip_payment_info"`
   426  	ClientViewedTime   string `json:"client_viewed_time"`
   427  	Contactpersons     []struct {
   428  		ContactpersonID string `json:"contactperson_id"`
   429  		Email           string `json:"email"`
   430  		Mobile          string `json:"mobile"`
   431  		Phone           string `json:"phone"`
   432  		ZcrmContactID   string `json:"zcrm_contact_id"`
   433  	} `json:"contactpersons"`
   434  	Coupons         []Coupon `json:"coupons"`
   435  	CreatedByID     string   `json:"created_by_id"`
   436  	CreatedDate     string   `json:"created_date"`
   437  	CreatedTime     string   `json:"created_time"`
   438  	Credits         []Credit `json:"credits"`
   439  	CreditsApplied  float64  `json:"credits_applied"`
   440  	CurrencyCode    string   `json:"currency_code"`
   441  	CurrencyID      string   `json:"currency_id"`
   442  	CurrencySymbol  string   `json:"currency_symbol"`
   443  	CustomFieldHash struct {
   444  	} `json:"custom_field_hash"`
   445  	CustomFields            []CustomField `json:"custom_fields"`
   446  	CustomerCustomFieldHash struct {
   447  	} `json:"customer_custom_field_hash"`
   448  	CustomerCustomFields        []CustomField `json:"customer_custom_fields"`
   449  	CustomerID                  string        `json:"customer_id"`
   450  	CustomerName                string        `json:"customer_name"`
   451  	Date                        string        `json:"date"`
   452  	DiscountPercent             float64       `json:"discount_percent"`
   453  	DiscountTotal               float64       `json:"discount_total"`
   454  	Documents                   []interface{} `json:"documents"`
   455  	DueDate                     string        `json:"due_date"`
   456  	Email                       string        `json:"email"`
   457  	ExchangeRate                float64       `json:"exchange_rate"`
   458  	InprocessTransactionPresent bool          `json:"inprocess_transaction_present"`
   459  	InvoiceDate                 string        `json:"invoice_date"`
   460  	InvoiceID                   string        `json:"invoice_id"`
   461  	InvoiceItems                []struct {
   462  		AccountID        string        `json:"account_id"`
   463  		AccountName      string        `json:"account_name"`
   464  		Code             string        `json:"code"`
   465  		Description      string        `json:"description"`
   466  		DiscountAmount   float64       `json:"discount_amount"`
   467  		ItemCustomFields []CustomField `json:"item_custom_fields"`
   468  		ItemID           string        `json:"item_id"`
   469  		ItemTotal        float64       `json:"item_total"`
   470  		Name             string        `json:"name"`
   471  		Price            float64       `json:"price"`
   472  		ProductID        string        `json:"product_id"`
   473  		ProductType      string        `json:"product_type"`
   474  		Quantity         int64         `json:"quantity"`
   475  		Tags             []Tag         `json:"tags"`
   476  		TaxID            string        `json:"tax_id"`
   477  		TaxName          string        `json:"tax_name"`
   478  		TaxPercentage    float64       `json:"tax_percentage"`
   479  		TaxType          string        `json:"tax_type"`
   480  		Unit             string        `json:"unit"`
   481  	} `json:"invoice_items"`
   482  	InvoiceNumber          string `json:"invoice_number"`
   483  	InvoiceURL             string `json:"invoice_url"`
   484  	IsInclusiveTax         bool   `json:"is_inclusive_tax"`
   485  	IsReverseChargeApplied bool   `json:"is_reverse_charge_applied"`
   486  	IsViewedByClient       bool   `json:"is_viewed_by_client"`
   487  	IsViewedInMail         bool   `json:"is_viewed_in_mail"`
   488  	LastModifiedByID       string `json:"last_modified_by_id"`
   489  	MailFirstViewedTime    string `json:"mail_first_viewed_time"`
   490  	MailLastViewedTime     string `json:"mail_last_viewed_time"`
   491  	Notes                  string `json:"notes"`
   492  	Number                 string `json:"number"`
   493  	PageWidth              string `json:"page_width"`
   494  	PaymentExpectedDate    string `json:"payment_expected_date"`
   495  	PaymentGateways        []struct {
   496  		PaymentGateway string `json:"payment_gateway"`
   497  	} `json:"payment_gateways"`
   498  	PaymentMade            int64  `json:"payment_made"`
   499  	PaymentReminderEnabled bool   `json:"payment_reminder_enabled"`
   500  	PaymentTerms           int64  `json:"payment_terms"`
   501  	PaymentTermsLabel      string `json:"payment_terms_label"`
   502  	Payments               []struct {
   503  		Amount               float64 `json:"amount"`
   504  		AmountRefunded       float64 `json:"amount_refunded"`
   505  		BankCharges          float64 `json:"bank_charges"`
   506  		CardType             string  `json:"card_type"`
   507  		Date                 string  `json:"date"`
   508  		Description          string  `json:"description"`
   509  		ExchangeRate         float64 `json:"exchange_rate"`
   510  		GatewayTransactionID string  `json:"gateway_transaction_id"`
   511  		InvoicePaymentID     string  `json:"invoice_payment_id"`
   512  		LastFourDigits       string  `json:"last_four_digits"`
   513  		PaymentID            string  `json:"payment_id"`
   514  		PaymentMode          string  `json:"payment_mode"`
   515  		ReferenceNumber      string  `json:"reference_number"`
   516  		SettlementStatus     string  `json:"settlement_status"`
   517  	} `json:"payments"`
   518  	PricePrecision  int64  `json:"price_precision"`
   519  	PricebookID     string `json:"pricebook_id"`
   520  	ReferenceID     string `json:"reference_id"`
   521  	ReferenceNumber string `json:"reference_number"`
   522  	SalespersonID   string `json:"salesperson_id"`
   523  	SalespersonName string `json:"salesperson_name"`
   524  	ShippingAddress struct {
   525  		Address   string `json:"address"`
   526  		Attention string `json:"attention"`
   527  		City      string `json:"city"`
   528  		Country   string `json:"country"`
   529  		Fax       string `json:"fax"`
   530  		Phone     string `json:"phone"`
   531  		State     string `json:"state"`
   532  		Street    string `json:"street"`
   533  		Street2   string `json:"street2"`
   534  		Zip       string `json:"zip"`
   535  	} `json:"shipping_address"`
   536  	ShippingCharge                        float64       `json:"shipping_charge"`
   537  	ShippingChargeExclusiveOfTax          float64       `json:"shipping_charge_exclusive_of_tax"`
   538  	ShippingChargeExclusiveOfTaxFormatted string        `json:"shipping_charge_exclusive_of_tax_formatted"`
   539  	ShippingChargeInclusiveOfTax          float64       `json:"shipping_charge_inclusive_of_tax"`
   540  	ShippingChargeInclusiveOfTaxFormatted string        `json:"shipping_charge_inclusive_of_tax_formatted"`
   541  	ShippingChargeTax                     string        `json:"shipping_charge_tax"`
   542  	ShippingChargeTaxFormatted            string        `json:"shipping_charge_tax_formatted"`
   543  	ShippingChargeTaxID                   string        `json:"shipping_charge_tax_id"`
   544  	ShippingChargeTaxName                 string        `json:"shipping_charge_tax_name"`
   545  	ShippingChargeTaxPercentage           string        `json:"shipping_charge_tax_percentage"`
   546  	ShippingChargeTaxType                 string        `json:"shipping_charge_tax_type"`
   547  	Status                                string        `json:"status"`
   548  	StopReminderUntilPaymentExpectedDate  bool          `json:"stop_reminder_until_payment_expected_date"`
   549  	SubTotal                              float64       `json:"sub_total"`
   550  	SubmitterID                           string        `json:"submitter_id"`
   551  	Subscriptions                         []interface{} `json:"subscriptions"`
   552  	TaxRounding                           string        `json:"tax_rounding"`
   553  	TaxTotal                              float64       `json:"tax_total"`
   554  	Taxes                                 []interface{} `json:"taxes"`
   555  	TemplateID                            string        `json:"template_id"`
   556  	TemplateName                          string        `json:"template_name"`
   557  	TemplateType                          string        `json:"template_type"`
   558  	Terms                                 string        `json:"terms"`
   559  	Total                                 float64       `json:"total"`
   560  	TransactionType                       string        `json:"transaction_type"`
   561  	UnbilledChargesID                     string        `json:"unbilled_charges_id"`
   562  	UnusedCreditsReceivableAmount         float64       `json:"unused_credits_receivable_amount"`
   563  	UpdatedTime                           string        `json:"updated_time"`
   564  	VatTreatment                          string        `json:"vat_treatment"`
   565  	WriteOffAmount                        float64       `json:"write_off_amount"`
   566  	ZcrmPotentialID                       string        `json:"zcrm_potential_id"`
   567  }
   568  
   569  type AddItemsRequest struct {
   570  	InvoiceItems []InvoiceItemRequest `json:"invoice_items,omitempty"`
   571  }
   572  
   573  type InvoiceItemRequest struct {
   574  	Code           string  `json:"code,omitempty"`
   575  	ProductID      string  `json:"product_id,omitempty"`
   576  	Name           string  `json:"name,omitempty"`
   577  	Description    string  `json:"description,omitempty"`
   578  	Price          float64 `json:"price,omitempty"`
   579  	Quantity       float64 `json:"quantity,omitempty"`
   580  	TaxID          string  `json:"tax_id,omitempty"`
   581  	TaxExemptionID string  `json:"tax_exemption_id,omitempty"`
   582  }
   583  
   584  type AddItemsResponse struct {
   585  	Code    int64  `json:"code"`
   586  	Message string `json:"message"`
   587  	Invoice struct {
   588  		InvoiceID           string `json:"invoice_id"`
   589  		Number              string `json:"number"`
   590  		Status              string `json:"status"`
   591  		InvoiceDate         string `json:"invoice_date"`
   592  		DueDate             string `json:"due_date"`
   593  		PaymentExpectedDate string `json:"payment_expected_date"`
   594  		AchPaymentInitiated bool   `json:"ach_payment_initiated"`
   595  		TransactionType     string `json:"transaction_type"`
   596  		CustomerID          string `json:"customer_id"`
   597  		CustomerName        string `json:"customer_name"`
   598  		Email               string `json:"email"`
   599  		InvoiceItems        []struct {
   600  			ItemID           string        `json:"item_id"`
   601  			Name             string        `json:"name"`
   602  			Description      string        `json:"description"`
   603  			Tags             []Tag         `json:"tags"`
   604  			ItemCustomFields []CustomField `json:"item_custom_fields"`
   605  			Code             string        `json:"code"`
   606  			Price            float64       `json:"price"`
   607  			Quantity         float64       `json:"quantity"`
   608  			DiscountAmount   float64       `json:"discount_amount"`
   609  			ItemTotal        float64       `json:"item_total"`
   610  			TaxID            string        `json:"tax_id"`
   611  			ProductType      string        `json:"product_type"`
   612  			HsnOrSac         string        `json:"hsn_or_sac"`
   613  			TaxExemptionID   string        `json:"tax_exemption_id"`
   614  			TaxExemptionCode string        `json:"tax_exemption_code"`
   615  		} `json:"invoice_items"`
   616  		Coupons []struct {
   617  			CouponCode     string  `json:"coupon_code"`
   618  			CouponName     string  `json:"coupon_name"`
   619  			DiscountAmount float64 `json:"discount_amount"`
   620  		} `json:"coupons"`
   621  		Credits []struct {
   622  			CreditnoteID      string  `json:"creditnote_id"`
   623  			CreditnotesNumber string  `json:"creditnotes_number"`
   624  			CreditedDate      string  `json:"credited_date"`
   625  			CreditedAmount    float64 `json:"credited_amount"`
   626  		} `json:"credits"`
   627  		Total          float64 `json:"total"`
   628  		PaymentMade    float64 `json:"payment_made"`
   629  		Balance        float64 `json:"balance"`
   630  		CreditsApplied float64 `json:"credits_applied"`
   631  		WriteOffAmount float64 `json:"write_off_amount"`
   632  		Payments       []struct {
   633  			PaymentID            string  `json:"payment_id"`
   634  			PaymentMode          string  `json:"payment_mode"`
   635  			InvoicePaymentID     string  `json:"invoice_payment_id"`
   636  			GatewayTransactionID string  `json:"gateway_transaction_id"`
   637  			Description          string  `json:"description"`
   638  			Date                 string  `json:"date"`
   639  			ReferenceNumber      string  `json:"reference_number"`
   640  			Amount               float64 `json:"amount"`
   641  			BankCharges          float64 `json:"bank_charges"`
   642  			ExchangeRate         float64 `json:"exchange_rate"`
   643  		} `json:"payments"`
   644  		CurrencyCode    string  `json:"currency_code"`
   645  		CurrencySymbol  string  `json:"currency_symbol"`
   646  		CreatedTime     string  `json:"created_time"`
   647  		UpdatedTime     string  `json:"updated_time"`
   648  		SalespersonID   string  `json:"salesperson_id"`
   649  		SalespersonName string  `json:"salesperson_name"`
   650  		InvoiceURL      string  `json:"invoice_url"`
   651  		BillingAddress  Address `json:"billing_address"`
   652  		ShippingAddress Address `json:"shipping_address"`
   653  		Comments        []struct {
   654  			CommentID       string `json:"comment_id"`
   655  			Description     string `json:"description"`
   656  			CommentedByID   string `json:"commented_by_id"`
   657  			CommentedBy     string `json:"commented_by"`
   658  			CommentType     string `json:"comment_type"`
   659  			Time            string `json:"time"`
   660  			OperationType   string `json:"operation_type"`
   661  			TransactionID   string `json:"transaction_id"`
   662  			TransactionType string `json:"transaction_type"`
   663  		} `json:"comments"`
   664  		CustomFields []CustomField `json:"custom_fields"`
   665  	} `json:"invoice"`
   666  }
   667  
   668  type EmailInvoiceRequest struct {
   669  	ToMailIds []string `json:"to_mail_ids"`
   670  	CcMailIds []string `json:"cc_mail_ids"`
   671  	Subject   string   `json:"subject"`
   672  	Body      string   `json:"body"`
   673  }
   674  
   675  type EmailInvoiceResponse struct {
   676  	Code    int64  `json:"code"`
   677  	Message string `json:"message"`
   678  }
   679  
   680  type AttachmentRequest struct {
   681  	CanSendInEmail bool `json:"can_send_in_mail"`
   682  }
   683  
   684  type AttachementResponse struct {
   685  	Code      int64  `json:"code"`
   686  	Message   string `json:"message"`
   687  	Documents []struct {
   688  		FileName          string `json:"file_name"`
   689  		FileType          string `json:"file_type"`
   690  		FileSize          int64  `json:"file_size"`
   691  		FileSizeFormatted string `json:"file_size_formatted"`
   692  		DocumentID        string `json:"document_id"`
   693  		AttachmentOrder   int64  `json:"attachment_order"`
   694  	} `json:"documents"`
   695  }
   696  
   697  type InvoicesResponse struct {
   698  	Invoices []Invoice `json:"invoices"`
   699  	Code     int64     `json:"code"`
   700  	Message  string    `json:"message"`
   701  }
   702  
   703  type InvoiceResponse struct {
   704  	Invoice Invoice `json:"invoice"`
   705  	Code    int64   `json:"code"`
   706  	Message string  `json:"message"`
   707  }
   708  
   709  type Invoice struct {
   710  	InvoiceID            string        `json:"invoice_id,omitempty"`
   711  	Number               string        `json:"number,omitempty"`
   712  	Status               string        `json:"status,omitempty"`
   713  	InvoiceDate          string        `json:"invoice_date,omitempty"`
   714  	DueDate              string        `json:"due_date,omitempty"`
   715  	CustomerID           string        `json:"customer_id,omitempty"`
   716  	CustomerName         string        `json:"customer_name,omitempty"`
   717  	Email                string        `json:"email,omitempty"`
   718  	Balance              float64       `json:"balance,omitempty"`
   719  	Total                float64       `json:"total,omitempty"`
   720  	PaymentMade          float64       `json:"payment_made,omitempty"`
   721  	CreditsApplied       float64       `json:"credits_applied,omitempty"`
   722  	WriteOffAmount       float64       `json:"write_off_amount,omitempty"`
   723  	CurrencyCode         string        `json:"currency_code,omitempty"`
   724  	CurrencySymbol       string        `json:"currency_symbol,omitempty"`
   725  	HasAttachment        bool          `json:"has_attachment,omitempty"`
   726  	CreatedTime          string        `json:"created_time,omitempty"`
   727  	UpdatedTime          string        `json:"updated_time,omitempty"`
   728  	SalespersonID        string        `json:"salesperson_id,omitempty"`
   729  	SalespersonName      string        `json:"salesperson_name,omitempty"`
   730  	InvoiceUrl           string        `json:"invoice_url,omitempty"`
   731  	PaymentExpectedDate  string        `json:"payment_expected_date,omitempty"`
   732  	ArchPaymentInitiated interface{}   `json:"ach_payment_initiated,omitempty"` // per documentation this field should be bool, but received empty string
   733  	TransactionType      string        `json:"transaction_type,omitempty"`
   734  	InvoiceItems         []InvoiceItem `json:"invoice_items,omitempty"`
   735  	Coupons              []Coupon      `json:"coupons,omitempty"`
   736  	Credits              []Credit      `json:"credits,omitempty"`
   737  	Payments             []Payment     `json:"payments,omitempty"`
   738  	BillingAddress       Address       `json:"billing_address,omitempty"`
   739  	ShippingAddress      Address       `json:"shipping_address,omitempty"`
   740  	Comments             []Comment     `json:"comments,omitempty"`
   741  	CustomFields         []CustomField `json:"custom_fields,omitempty"`
   742  	CanSendInEmail       bool          `json:"can_send_in_mail,omitempty"`
   743  	Documents            []Document    `json:"documents,omitempty"`
   744  }
   745  
   746  type InvoiceItem struct {
   747  	ItemID           string        `json:"item_id,omitempty"`
   748  	Name             string        `json:"name,omitempty"`
   749  	Description      string        `json:"description,omitempty"`
   750  	Code             string        `json:"code,omitempty"`
   751  	Tags             []Tag         `json:"tags,omitempty"`
   752  	ItemCustomFields []CustomField `json:"item_custom_fields,omitempty"`
   753  	Price            float64       `json:"price,omitempty"`
   754  	Quantity         float64       `json:"quantity,omitempty"`
   755  	DiscountAmount   float64       `json:"discount_amount,omitempty"`
   756  	ItemTotal        float64       `json:"item_total,omitempty"`
   757  	TaxID            string        `json:"tax_id,omitempty"`
   758  	TaxExemptionID   string        `json:"tax_exemption_id,omitempty"`
   759  	TaxExemptionCode string        `json:"tax_exemption_code,omitempty"`
   760  }
   761  
   762  type Coupon struct {
   763  	CouponCode     string  `json:"coupon_code,omitempty"`
   764  	CouponName     string  `json:"coupon_name,omitempty"`
   765  	DiscountAmount float64 `json:"discount_amount,omitempty"`
   766  }
   767  
   768  type Credit struct {
   769  	CreditnoteID      string  `json:"creditnote_id,omitempty"`
   770  	CreditnotesNumber string  `json:"creditnotes_number,omitempty"`
   771  	CreditedDate      string  `json:"credited_date,omitempty"`
   772  	CreditedAmount    float64 `json:"credited_amount,omitempty"`
   773  }
   774  
   775  type Payment struct {
   776  	PaymentID            string  `json:"payment_id,omitempty"`
   777  	PaymentMode          string  `json:"payment_mode,omitempty"`
   778  	InvoicePaymentID     string  `json:"invoice_payment_id,omitempty"`
   779  	AmountRefunded       float64 `json:"amount_refunded,omitempty"`
   780  	GatewayTransactionID string  `json:"gateway_transaction_id,omitempty"`
   781  	Description          string  `json:"description,omitempty"`
   782  	Date                 string  `json:"date,omitempty"`
   783  	ReferenceNumber      string  `json:"reference_number,omitempty"`
   784  	Amount               float64 `json:"amount,omitempty"`
   785  	BankCharges          float64 `json:"bank_charges,omitempty"`
   786  	ExchangeRate         float64 `json:"exchange_rate,omitempty"`
   787  }
   788  
   789  type Comment struct {
   790  	CommentID       string `json:"comment_id,omitempty"`
   791  	Description     string `json:"description,omitempty"`
   792  	CommentedByID   string `json:"commented_by_id,omitempty"`
   793  	CommentedBy     string `json:"commented_by,omitempty"`
   794  	CommentType     string `json:"comment_type,omitempty"`
   795  	Time            string `json:"time,omitempty"`
   796  	OperationType   string `json:"operation_type,omitempty"`
   797  	TransactionID   string `json:"transaction_id,omitempty"`
   798  	TransactionType string `json:"transaction_type,omitempty"`
   799  }
   800  
   801  type Document struct {
   802  	FileName          string `json:"file_name,omitempty"`
   803  	FileType          string `json:"file_type,omitempty"`
   804  	FileSize          int64  `json:"file_size,omitempty"`
   805  	FileSizeFormatted string `json:"file_size_formatted,omitempty"`
   806  	DocumentID        string `json:"document_id,omitempty"`
   807  	AttachmentOrder   int64  `json:"attachment_order,omitempty"`
   808  }
   809  
   810  type Tag struct {
   811  	TagID       string `json:"tag_id,omitempty"`
   812  	TagOptionID string `json:"tag_option_id,omitempty"`
   813  }