https://github.com/asc-lab/azure-functions-billing
Azure Functions v2 with .NET Core - billing in serverless architecture.
https://github.com/asc-lab/azure-functions-billing
azure-blob azure-functions azure-queue azure-table billing blob cosmosdb cosmosdb-database csv insurance jsreport sendgrid serverless twilio
Last synced: about 2 months ago
JSON representation
Azure Functions v2 with .NET Core - billing in serverless architecture.
- Host: GitHub
- URL: https://github.com/asc-lab/azure-functions-billing
- Owner: asc-lab
- License: apache-2.0
- Created: 2018-09-26T07:42:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-21T08:14:07.000Z (over 6 years ago)
- Last Synced: 2025-04-01T22:46:55.406Z (2 months ago)
- Topics: azure-blob, azure-functions, azure-queue, azure-table, billing, blob, cosmosdb, cosmosdb-database, csv, insurance, jsreport, sendgrid, serverless, twilio
- Language: C#
- Homepage:
- Size: 992 KB
- Stars: 61
- Watchers: 9
- Forks: 39
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Azure Functions v2 with .NET Core
This example shows simplified billing system in serverless architecture.
Comprehensive guide describing exactly the architecture, applied design patterns and technologies can be found on our blog in article **[Azure Functions 2.0 – real world use case for serverless architecture](https://altkomsoftware.pl/en/blog/azure-functions/)**.
**We encourage you to read, because in this README there is only a substitute for all information.**
![]()
1. User uploads CSV file (with name structure ```CLIENTCODE_YEAR_MONTH_activeList.txt.```) with Beneficiaries (the sample file is located in the ```data-examples``` folder) to a specific data storage - ```active-lists``` Azure Blob Container.
2. The above action triggers a function (```GenerateBillingItemsFunc```) that is responsible for:
* generating billing items (using prices from an external database - CosmosDB ```crm``` database, ```prices``` collection) and saving them in the table ```billingItems```;
* sending message about the need to create a new invoice to ```invoice-generation-request```;3. When a new message appears on the queue ```invoice-generation-request```, next function is triggered (```GenerateInvoiceFunc```). This function creates domain object ```Invoice``` and save this object in database (CosmosDB ```crm``` database, ```invoices``` collection) and send message to queues: ```invoice-print-request``` and ```invoice-notification-request```.
4. When a new message appears on the queue ```invoice-print-request```, function ```PrintInvoiceFunc``` is triggered. This function uses external engine to PDF generation - JsReport and saves PDF file in BLOB storage.
5. When a new message appears on the queue ```invoice-notification-request```, function ```NotifyInvoiceFunc``` is triggered. This function uses two external systems - SendGrid to Email sending and Twilio to SMS sending.
## Tutorial from scratch to run locally
1. Install and run [Microsoft Azure Storage Emulator](https://docs.microsoft.com/en-us/azure/storage/common/storage-use-emulator).
2. Install and run [CosmosDB Emulator](https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator). Check this on ```https://localhost:8081/_explorer/index.html```.
3. Create in Emulator blob Container ```active-lists```.
4. Upload ```ASC_2018_02_activeLists.txt``` file from ```data-examples``` folder to ```active-lists``` blob.
5. Create CosmosDB database ```crm``` and in this database create collections: ```prices```, ```invoices```.
6. Add CosmosDB properties ```PriceDbUrl``` and ```PriceDbAuthKey``` to ```local.appsettings.json``` in ```PriceDbInitializator``` and ```GenerateBillingIemsFunc```. You can copy this properties from ```Azure CosmosDB Emulator``` - check point 2 (URI and Primary Key).
7. Run project ```PriceDbInitializator``` to init collection ```prices``` in ```crm``` database.
8. Add CosmosDB connection string as ```cosmosDb``` to ```local.settings.json``` in ```GenerateInvoiceFunc```. You can copy this string from ```Azure CosmosDB Emulator``` - check point 2 (Primary Connection String).
9. Create an account in [SendGrid](https://sendgrid.com/) and add property ```SendGridApiKey``` to ```local.settings.json``` in ```NotifyInvoiceFunc```.
10. Create an account in [Twilio](https://www.twilio.com/) and add properties ```TwilioAccountSid``` ```TwilioAuthToken``` to ```local.settings.json``` in ```NotifyInvoiceFunc```.
11. Run JsReport with Docker: ```docker run -p 5488:5488 jsreport/jsreport```. Check JsReport Studio on ```localhost:5488```.
12. Add JsReport url as ```JsReportUrl``` to ```local.settings.json``` in ```PrintInvoiceFunc``` project.
13. Add JsReport template with name ```INVOICE``` and content:
```html
Invoice {{invoiceNumber }}
Customer: {{ customer }}
Address: 00-101 Warszawa, Chłodna 21
Description: {{ description }}
Details:
Item
Price
{{#each lines}}
{{ itemName }}
{{ cost }}
{{/each}}
Total
{{ totalCost }}
```
Example JSON for INVOICE template:
```json
{
"customer": "ASC",
"invoiceNumber": "ASC/10/2018",
"description": "Invoice for insurance policies for 10/2018",
"lines": [
{
"itemName": "Policy A",
"cost": 2140.0
},
{
"itemName": "Policy B",
"cost": 1360.0
}
],
"totalCost": 3500.0
}
```All properties in one `local.appsettings.json`:
```
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"PriceDbUrl": "https://localhost:8081",
"PriceDbAuthKey": "AUTH_KEY",
"cosmosDb": "AccountEndpoint=https://localhost:8081/;AccountKey=AUTH_KEY",
"JsReportUrl": "http://localhost:5488",
"SendGridApiKey": "SEND_GRID_API_KEY",
"TwilioAccountSid ": "TWILIO_ACCOUNT_SID",
"TwilioAuthToken": "TWILIO_AUTH_TOKEN"
}
}
```## Monitoring examples
Application Map for all function in one project:
![]()
Application Map for functions in separated projects:
![]()
End-to-end transaction details:
![]()
## Tips & Tricks
1. CSV file is working for client code ```ASC``` (filename: ```ASC_2018_12_activeList.txt```). If you want run functions for another client code, you must simulate prices in database. Check project ```PriceDbInitializator```, file ```Program.cs```, method ```AddDoc```.
2. Remember that you must use **Twilio Test Credentials**.
![]()
3. **Microsoft Azure Storage Emulator** with all created storages:
![]()