You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Logic App数据传至Azure Function后调用Fortnox API创建凭证的实现问题

完整业务流程实现方案

1. Logic Apps 配置

  • 在存储目标JSON数据的Logic Apps工作流中,添加「调用Azure函数」操作,选择你提前创建好的HTTP触发类型的C#函数。
  • 请求方法设置为POST,请求体直接传入你持有的完整JSON数据即可。
  • 额外添加请求头Content-Type,值设为application/json,配置完成后保存发布工作流。

2. Azure Function 开发配置

2.1 提前准备依赖

通过NuGet安装两个必要包:Fortnox官方SDK、JSON序列化包(System.Text.Json或Newtonsoft.Json均可),同时提前获取你本人的Fortnox API访问令牌,用于SDK初始化授权。

2.2 完整函数代码示例

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using System;
using System.Collections.Generic;
using System.Text.Json;

// 入参实体类
public class InputData
{
    public string amount { get; set; }
    public string date { get; set; }
    public string fee { get; set; }
}

public static class CreateFortnoxVoucherFunction
{
    [FunctionName("CreateFortnoxVoucher")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
    {
        // 1. 解析Logic Apps传入的JSON参数
        string requestBody = new System.IO.StreamReader(req.Body).ReadToEndAsync().Result;
        InputData input = JsonSerializer.Deserialize<InputData>(requestBody);
        
        // 基础入参校验
        if (input == null || string.IsNullOrEmpty(input.amount) || string.IsNullOrEmpty(input.date) || string.IsNullOrEmpty(input.fee))
        {
            return new BadRequestObjectResult("缺少必要的参数字段");
        }

        try
        {
            // 计算总金额
            decimal amountVal = decimal.Parse(input.amount);
            decimal feeVal = decimal.Parse(input.fee);
            decimal totalCreditVal = amountVal + feeVal;

            // 2. 初始化Fortnox连接器,需替换为你自己的API访问令牌
            VoucherConnector connector = new VoucherConnector();
            // SDK初始化时需要配置AccessToken,具体配置方式参考SDK内置说明
            // connector.AccessToken = "你的Fortnox访问令牌";

            List<VoucherRow> rows = new List<VoucherRow>();

            rows.Add(new VoucherRow()
            {
                Account = "2480",
                Description = "Checkräkningskredit, kortfristig",
                Debit = input.amount,
                Credit = "0"
            });

            rows.Add(new VoucherRow()
            {
                Account = "6092",
                Description = "Shopify Fee",
                Debit = input.fee,
                Credit = "0"
            });

            rows.Add(new VoucherRow()
            {
                Account = "1509",
                Description = "Shopify fordran",
                Debit = "0",
                Credit = totalCreditVal.ToString()
            });

            Voucher result = connector.Create(new Voucher()
            {
                TransactionDate = input.date,
                Description = "Inbetalning Shopify",
                VoucherRows = rows,
                VoucherSeries = "A"
            });

            // 返回创建成功的凭证信息
            return new OkObjectResult(result);
        }
        catch (Exception ex)
        {
            return new ObjectResult($"创建凭证失败:{ex.Message}")
            {
                StatusCode = StatusCodes.Status500InternalServerError
            };
        }
    }
}

3. 联调测试

  1. 本地启动Azure Function,用示例JSON作为入参调用函数,验证Fortnox凭证创建逻辑正常。
  2. 发布Azure Function到云端,触发Logic Apps工作流验证整条链路数据流转正常。

内容的提问来源于stack exchange,提问作者ronzyintegration

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 00:36:03