Skip to content

MjukBiltvatt/go-visma-net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A client library for the Visma Net Service API. For a better understanding of how it works, see the official documentation for the API.

Inspired by github.com/omniboost/go-visma.net.

Setup

go get github.com/MjukBiltvatt/go-visma-net

Usage

Creating a client

// Create an oath configuration, in this example we're using the client credentials flow
oauthConf := clientcredentials.Config{
	ClientID:     "isv_your_client_id",
	ClientSecret: "your_client_secret",
	TokenURL:     "https://connect.visma.com/connect/token",
	Scopes:       []string{
		"vismanet_erp_service_api:create",
		"vismanet_erp_service_api:delete",
		"vismanet_erp_service_api:read",
		"vismanet_erp_service_api:update"
	},
	EndpointParams: map[string][]string{
		"tenant_id": {"your_tenant_id"},
	},
}

// Create a http client using the oauth configuration
httpClient := oauthConf.Client(context.Background())

// Create a Visma.net client using the http client
client := vismanet.NewClient(httpClient)

Attachment

PUT /v1/attachment/{attachmentId}

Update an existing attachment with the specified ID.

req := testClient.NewPutAttachmentV1Request()
req.SetPathParams(PutAttachmentV1PathParams{AttachmentID: "attachment_id"})
req.SetBody(RequestAttachment{SendToAutoInvoice: true})
if err != nil {
	fmt.Println("Error updating attachment:", err)
}
fmt.Println("Attachment updated successfully:", resp.ResourceID())

Customer

GET /v1/customer/{customerCd}

Get a customer with a specific customer number.

req := client.NewGetCustomerV1Request()
req.SetPathParams(GetCustomerV1PathParams{
	customerCD: "customer_number",
})
resp, err := req.Do()
if err != nil {
	if resp.StatusCode() == http.StatusNotFound {
		fmt.Println("Customer not found")
	} else {
		fmt.Println("Error getting customer:", err)
	}
}
fmt.Println("Customer retrieved successfully:", resp.Customer.Number)

POST /v1/customer

Create a new customer.

req := client.NewPostCustomerV1Request()
req.SetBody(RequestCustomer{
	Name: "Test",
	MainAddress: &RequestNestedAddress{
		AddressLine1: NewStringValue("Testgatan 1"),
		City:         NewStringValue("Testdalen"),
		PostalCode:   NewStringValue("12345"),
		CountryID:    NewStringValue("SE"),
	},
})
resp, err := req.Do()
if err != nil {
	fmt.Println("Error creating customer:", err)
}
fmt.Println("Customer created successfully:", resp.ResourceID())

PUT /v1/customer/{customerCd}

Update an existing customer with the specified customer number.

req := testClient.NewPutCustomerV1Request()
req.SetPathParams(PutCustomerV1PathParams{
	customerCD: "customer_number",
})
req.SetBody(RequestCustomer{
	Name: "John Doe",
})
resp, err := req.Do()
if err != nil {
	fmt.Println("Error updating customer:", err)
}
fmt.Println("Customer updated successfully:", resp.ResourceID())

CustomerInvoice

GET /v1/customerinvoice/{invoiceNumber}

Get a customer invoice with a specific invoice number.

req := testClient.NewGetCustomerInvoiceV1Request()
req.SetPathParams(GetCustomerInvoiceV1PathParams{
	invoiceNumber: "invoice_number",
})
resp, err := req.Do()
if err != nil {
	if resp.StatusCode() == http.StatusNotFound {
		fmt.Println("Invoice not found")
	} else {
		fmt.Println("Error getting invoice:", err)
	}
}
fmt.Println("Invoice retrieved successfully:", resp.Customer.Number)

DELETE /v1/customerinvoice/{invoiceNumber}

Delete a customer invoice with a specific invoice number.

req := testClient.NewGetCustomerInvoiceV1Request()
req.SetPathParams(GetCustomerInvoiceV1PathParams{
	invoiceNumber: "invoice_number",
})
resp, err := req.Do()
if err != nil {
	if resp.StatusCode() == http.StatusNotFound {
		fmt.Println("Invoice not found")
	} else {
		fmt.Println("Error deleting invoice:", err)
	}
}
fmt.Println("Invoice deleted successfully")

POST /v1/customerinvoice/{invoiceNumber}/attachment

Upload a file as an attachment to a customer invoice.

req := testClient.NewPostCustomerInvoiceAttachmentV1Request()
req.SetPathParams(PostCustomerInvoiceAttachmentV1PathParams{
	InvoiceNumber: os.Getenv("TEST_CUSTOMER_INVOICE_NUMBER"),
})
req.SetBody(FileUploadBody{
	Files: []File{
		{
			Name:    "Test.txt",
			Content: []byte("Hello world"),
		},
	},
})
resp, err := req.Do()
if err != nil {
	fmt.Println("Error creating attachment:", err)
}
fmt.Println("Attachment created successfully:", resp.ResourceID())

CustomerInvoiceV2

POST /v2/customerinvoice

Create a new customer invoice.

req := testClient.NewPostCustomerInvoiceV2Request()
req.SetBody(RequestInvoice{
	CurrencyID:     NewStringValue("SEK"),
	CustomerNumber: NewStringValue(os.Getenv("TEST_CUSTOMER_CD")),
	InvoiceLines: 	&[]RequestInvoiceLine{
		{
			Operation:           	"Insert",
			ItemType:            	NewStringValue("Service"),
			BranchNumber:        	NewStringValue("1"),
			Description:         	NewStringValue("Test"),
			Quantity:            	NewFloatValue(1),
			UnitPriceInCurrency: 	NewFloatValue(100),
			AccountNumber:       	NewStringValue("3015"),
			Subaccount: 					&[]RequestSegment{
				{
					SegmentID:    1,
					SegmentValue: "00",
				},
				{
					SegmentID:    2,
					SegmentValue: "000",
				},
				{
					SegmentID:    3,
					SegmentValue: "00",
				},
			},
		},
	},
})
resp, err := req.Do()
if err != nil {
	fmt.Println("Error creating invoice:", err)
}
fmt.Println("Invoice created successfully:", resp.ResourceID())

Location

PUT /v1/location/{bAccountId}/{locationId}

Update an existing location with the specified ID for an account with the specified ID.

req := testClient.NewPutLocationV1Request()
req.SetPathParams(PutLocationV1PathParams{
	BAccountID: customerNumber,
	LocationID: "Main",
})
req.SetBody(RequestLocation{LocationName: vismanet.NewStringValue("My location")})
if err != nil {
	fmt.Println("Error updating location:", err)
}
fmt.Println("Location updated successfully:", resp.ResourceID())

Testing

Set up your testing environment by copying the contents of .env.template to a .env file. After that you can use godotenv to set the environment variables required for testing.

Run all tests:

$ godotenv go test

About

Golang library for the Visma Net Service API.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages