DocuSign付款项目正在通过API工作,但无法输入付款说明

时间:2017-10-10 20:01:06

标签: c# xml docusignapi

这是我迄今为止所取得的成就...... 1.使用他们的Web应用程序创建DocuSign模板 2.在模板中添加了Payment item(Stripe是我们的支付网关) 3.创建了DocuSign API信封请求,该请求还添加了付款金额(见下文)。

            string requestBody = 
                "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" +
                    "<status>sent</status>" +
                    "<emailSubject>Just A Test</emailSubject>" +
                    "<templateId>" + templateId + "</templateId>" +
                    "<templateRoles>" +
                        "<templateRole>" +
                            "<name>" + custName + "</name>" +
                            "<email>" + custEmail + "</email>" +
                            "<roleName>Customer</roleName>" +
                            "<tabs>" +
                                "<textTabs>" +
                                    "<text>" +
                                        "<tabLabel>\\*txtPropAddr</tabLabel>" +
                                        "<value>" + txtPropAddr + "</value>" +
                                    "</text>" +
                                    "<text>" +
                                        "<tabLabel>\\*txtPropCity</tabLabel>" +
                                        "<value>" + txtPropCity.TrimEnd() + "</value>" +
                                    "</text>" +
                                    "<text>" +
                                        "<tabLabel>\\*txtPropZip</tabLabel>" +
                                        "<value>" + txtPropZip + "</value>" +
                                    "</text>" +
                                "</textTabs>" +
                                "<numberTabs>" +
                                    "<number>" +
                                        "<fontColor>DarkRed</fontColor>" +
                                        "<locked>true</locked>" +
                                        "<tabLabel>PaymentCustom</tabLabel>" +
                                        "<value>" + txtPayment + "</value>" +
                                    "</number>" +
                                "</numberTabs>" +
                            "</tabs>" +
                        "</templateRole>" +
                        "<templateRole>" +
                            "<name>" + repName + "</name>" +
                            "<email>" + repEmail + "</email>" +
                            "<roleName>SalesRep</roleName>" +
                        "</templateRole>" +
                    "</templateRoles>" +
                "</envelopeDefinition>";

这一切都很棒! 我现在想做的是传递一些额外的信息以及Stripe付款 我们可以使用的模板Payment item有3个属性 这三个都位于Payment Description部分see example 1.项目名称
2.物品代码/ SKU
3.项目细节
如果我在模板中对这三个项目进行硬编码并发送付款,则附加信息会显示在条带付款see exampleMetadata部分下方。

有谁知道如何通过API实现这一目标?

我尝试在HTML中添加以下几种变体而没有运气。

    "<paymentDetails>" +
        "<lineItems>" +
            "<paymentLineItem>" +
                "<amountReference>PaymentCustom</amountReference>" + 
                "<description>test string 1</description>" +
                "<itemCode>test string 2</itemCode>" +
                "<name>test string 3</name>" +
            "</paymentLineItem>" +
        "</lineItems>" +   
    "</paymentDetails>" +

2 个答案:

答案 0 :(得分:0)

我会使用xml linq作为xml。见下面的代码。我使用了一些不同的技术来生成xml。我不知道在哪里添加付款细节。如有必要,我可以修改。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int templateId = 123;
            string custName = "john";
            string custEmail = "abc@def.com";
            string txtPropAddr = "123 main st.";
            string txtPropCity = "city";
            int txtPropZip = 12345;
            decimal txtPayment = 1.00M;
            string repName = "john";
            string repEmail = "abc@def.com";

            string requestBody = "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\"></envelopeDefinition>";

            XElement requestBodyElement = XElement.Parse(requestBody);
            XNamespace ns = requestBodyElement.GetDefaultNamespace();

            requestBodyElement.Add(new object[] {
                new XElement(ns + "status", "sent"),
                new XElement(ns + "emailSubject", "Just A Test"),
                new XElement(ns + "templateId", templateId),
                new XElement(ns + "templateRoles", new object[] {
                    new XElement(ns + "templateRole", new object[] {
                        new XElement(ns + "name", custName), 
                        new XElement(ns + "email", custEmail),
                        new XElement(ns + "roleName", "Customer"),
                        new XElement(ns + "tabs", new object[] {
                            new XElement(ns + "textTabs", new object[] {
                                new XElement(ns + "text", new object[] {
                                    new XElement(ns + "tabLabel", "\\*txtPropAddr"),
                                    new XElement(ns + "value", txtPropAddr)
                                }),
                                new XElement(ns + "text", new object[] {
                                    new XElement(ns + "tabLabel", "\\*txtPropCity"),
                                    new XElement(ns + "value", txtPropCity.TrimEnd())
                                }),
                                new XElement(ns + "text", new object[] {
                                    new XElement(ns + "tabLabel", "\\*txtPropZip"),
                                    new XElement(ns + "value", txtPropZip)
                                })
                            }),
                            new XElement(ns + "numberTabs",
                                new XElement(ns + "number", new object[] {
                                    new XElement(ns + "fontColor", "DarkRed"),
                                    new XElement(ns + "locked", true),
                                    new XElement(ns + "tabLabel", "PaymentCustom"),
                                    new XElement(ns + "value", txtPayment)
                                })
                            })
                        })
                    }),
                    new XElement(ns + "templateRole", new object[] {
                        new XElement(ns + "name", repName), 
                        new XElement(ns + "email", repEmail),
                        new XElement(ns + "roleName", "SalesRep")
                    })
                })
            });

            XElement paymentDetails = new XElement(ns + "paymentDetails",
                new XElement(ns + "paymentDetails",
                    new XElement(ns + "lineItems",
                        new XElement(ns + "paymentLineItem", new object[] {
                            new XElement(ns + "amountReference", "PaymentCustom"), 
                            new XElement(ns + "description", "test string 1"),
                            new XElement(ns + "itemCode", "test string 2"),
                            new XElement(ns + "name", "test string 3")
                        })
                    )
               )
            );

            requestBodyElement.Add(paymentDetails);


        }

    }
}

答案 1 :(得分:0)

  1. 通过网站创建DocuSign模板。
  2. 向模板添加text字段,设置数据标签,将验证设置为数字。将验证设置为数字实际上将字段变为数字选项卡。
  3. 在模板中添加payment item字段,选择付款网关,将付款金额更改为公式,编辑公式并从步骤2中选择text字段数据标签。实际上,创建两个隐藏formulaTabs在模板中。
  4. GetRecipientId() - 捕获更新数字标签时使用的模板角色收件人ID号。
  5. CreateEnvelope() - 更新模板上的templateRolestextTabsnumberTabs
  6. GetFormulaTabIds() - 捕获formulaTab个ID号,用于更新在步骤3中创建的两个隐藏公式选项卡。
  7. UpdateEnvelopeFormulas() - 添加付款说明。
  8. UpdateEnvelopeStatus() - 发送DocuSign电子邮件。
  9. 我知道有更好的方法来创建XML字符串,但是,我觉得它过于复杂。我只是使用GetEnvelopeRecipientTabs()来创建XML文档,然后将所需内容剪切并粘贴到我的代码中,非常简单......

        // Enter your credentials
        public static string username = "guid";
        public static string password = "string";
        public static string integratorKey = "guid";
        public static string loginEndpoint = "https://demo.docusign.net/restapi";// for production change to www.docusign.net/restapi
        public static string gatewayAccountId = "guid";
        public static string recipId = null;
        public static string PaymentReceiptTabId = null;
        public static string PaymentFormulaTabId = null;
        public static string templateId = "guid";
        public static string baseURL = "https://demo.docusign.net/restapi/v2/accounts/#######"
    
        GetRecipientId();
    
        public static string CreateEnvelope()
        {
            string RV = null;
            try
            {
                string url = baseURL + "/envelopes";
                string xmlRequestBody =
                    "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" +
                        "<status>created</status>" +
                          "<emailSubject>Turner Pest Control - Termite Bond Quote</emailSubject>" +
                          "<templateId>" + templateId + "</templateId>" +
                          "<templateRoles>" +
                            "<templateRole>" +
                              "<name>" + custName + "</name>" +
                              "<email>" + custEmail + "</email>" +
                              "<roleName>Customer</roleName>" +
                              "<tabs>" +
                                "<textTabs>" +
                                  "<text>" +
                                    "<tabLabel>txtPropAddr</tabLabel>" +
                                    "<value>" + txtPropAddr.TrimEnd() + "</value>" +
                                  "</text>" +
                                  "<text>" +
                                    "<tabLabel>txtPropZip</tabLabel>" +
                                    "<value>" + txtPropZip + "</value>" +
                                  "</text>" +
                                "</textTabs>" +
                                "<numberTabs>" +
                                    "<number>" +
                                        "<documentId>1</documentId>" +
                                        "<pageNumber>1</pageNumber>" +
                                        "<recipientId>" + recipId + "</recipientId>" +
                                        "<tabLabel>TPCPaymentCustom</tabLabel>" +
                                        "<locked>true</locked>" +
                                        "<required>false</required>" +
                                        "<value>" + txtPayment + "</value>" +
                                        "<isPaymentAmount>false</isPaymentAmount>" +
                                    "</number>" +
                                "</numberTabs>" +
                              "</tabs>" +
                            "</templateRole>" +
                            "<templateRole>" +
                              "<name>" + repName + "</name>" +
                              "<email>" + repEmail + "</email>" +
                              "<roleName>SalesRep</roleName>" +
                            "</templateRole>" +
                        "</templateRoles>" +
                    "</envelopeDefinition>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlRequestBody);
                HttpWebRequest request = initializeRequest(url, "POST", doc.OuterXml, username, password, integratorKey);
                using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(resp.GetResponseStream());
                    xmldoc.Save("C:\\Temp\\DocuSign\\CreateEnvelopeResponse.xml");// FOR TESTING ONLY
                    XmlNodeList xnList = xmldoc.GetElementsByTagName("envelopeSummary");
                    foreach (XmlNode xn in xnList)
                    {
                        string status = xn["status"].InnerText;
                        if (status == "created")
                        {
                            string envelopeId = xn["envelopeId"].InnerText;
                            GetFormulaTabIds();
                            if (UpdateEnvelopeFormulas())
                            {
                                if (UpdateEnvelopeStatus())
                                {
                                    RV = envelopeId;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        XDocument doc = XDocument.Parse(text);
                        SmtpEmailSend(httpResponse.StatusCode);
                    }
                }
            }
            catch (Exception ex)
            {
                SmtpEmailSend("CreateEnvelope() Error!");
            }
            return RV;
        }
    
        public static bool UpdateEnvelopeFormulas()
        {
            bool RV = false;
            try
            {
                string url = baseURL + "/envelopes/" + envelopeId + "/recipients/1/tabs";
                string xmlRequestBody =
                    "<tabs xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.docusign.com/restapi\">" +
                        "<formulaTabs xmlns:a=\"http://schemas.datacontract.org/2004/07/API_REST.Models\">" +
                            "<a:formulaTab>" +
                                "<documentId>1</documentId>" +
                                "<pageNumber>1</pageNumber>" +
                                "<recipientId>1</recipientId>" +
                                "<tabId>" + PaymentReceiptTabId + "</tabId>" +
                                "<tabLabel>PaymentReceipt c6d38477-62a4-4a03-adc1-671456278ecf</tabLabel>" +
                                "<locked>true</locked>" +
                                "<required>true</required>" +
                                "<a:formula>([PaymentFormula 4354e223-fb2e-44ed-81e4-385b40eb81f6]) * 100</a:formula>" +
                                "<a:hidden>true</a:hidden>" +
                                "<a:isPaymentAmount>false</a:isPaymentAmount>" +
                                "<a:paymentDetails>" +
                                    "<currencyCode>USD</currencyCode>" +
                                    "<gatewayAccountId>" + gatewayAccountId + "</gatewayAccountId>" +
                                    "<gatewayDisplayName>Stripe</gatewayDisplayName>" +
                                    "<gatewayName>stripe</gatewayName>" +
                                    "<lineItems xmlns:b=\"http://schemas.datacontract.org/2004/07/API_REST.Models.v2.payments\">" +
                                        "<b:paymentLineItem>" +
                                            "<b:amountReference>PaymentFormula 4354e223-fb2e-44ed-81e4-385b40eb81f6</b:amountReference>" +
                                            "<b:description>CustNum[" + custNo + "]</b:description>" +
                                            "<b:itemCode>PolicyNum[" + policyNum + "]</b:itemCode>" +
                                            "<b:name>" + warrantyType + "</b:name>" +
                                        "</b:paymentLineItem>" +
                                    "</lineItems>" +
                                    "<status>new</status>" +
                                "</a:paymentDetails>" +
                                "<a:roundDecimalPlaces>0</a:roundDecimalPlaces>" +
                            "</a:formulaTab>" +
                            "<a:formulaTab>" +
                                "<documentId>1</documentId>" +
                                "<pageNumber>1</pageNumber>" +
                                "<recipientId>1</recipientId>" +
                                "<tabId>" + PaymentFormulaTabId + "</tabId>" +
                                "<tabLabel>PaymentFormula 4354e223-fb2e-44ed-81e4-385b40eb81f6</tabLabel>" +
                                "<locked>true</locked>" +
                                "<required>true</required>" +
                                "<a:formula>[TPCPaymentCustom]</a:formula>" +
                                "<a:hidden>true</a:hidden>" +
                                "<a:isPaymentAmount>false</a:isPaymentAmount>" +
                                "<a:roundDecimalPlaces>2</a:roundDecimalPlaces>" +
                            "</a:formulaTab>" +
                        "</formulaTabs>" +
                    "</tabs>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlRequestBody);
                HttpWebRequest request = initializeRequest(url, "PUT", doc.OuterXml, username, password, integratorKey);
                using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                {
                    XmlDocument xmldoc = new XmlDocument();// FOR TESTING ONLY
                    xmldoc.Load(resp.GetResponseStream());// FOR TESTING ONLY
                    xmldoc.Save("C:\\Temp\\DocuSign\\RecipientTabUpdateResponse.xml");// FOR TESTING ONLY
                    RV = true;
                }
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        SmtpEmailSend(httpResponse.StatusCode);
                    }
                }
            }
            catch (Exception ex)
            {
                SmtpEmailSend("UpdateEnvelopeFormula() Error!");
            }
            return RV;
        }
    
        public static bool UpdateEnvelopeStatus()
        {
            bool RV = false;
            try
            {
                string url = baseURL + "/envelopes/" + envelopeId;
                string xmlRequestBody =
                    "<envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.docusign.com/restapi\">" +
                        "<status>sent</status>" +
                    "</envelope>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlRequestBody);
                HttpWebRequest request = initializeRequest(url, "PUT", doc.OuterXml, username, password, integratorKey);
                using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                {
                    XmlDocument xmldoc = new XmlDocument();// FOR TESTING ONLY
                    xmldoc.Load(resp.GetResponseStream());// FOR TESTING ONLY
                    xmldoc.Save("C:\\Temp\\DocuSign\\EnvelopeStatusUpdateResponse.xml");// FOR TESTING ONLY
                    RV = true;
                }
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        SmtpEmailSend(httpResponse.StatusCode);
                    }
                }
            }
            catch (Exception ex)
            {
                SmtpEmailSend("UpdateEnvelopeStatus() Error!");
            }
            return RV;
        }
    
        //***************************************************
        // --- HELPER FUNCTIONS ---
        //***************************************************
    
        public static HttpWebRequest initializeRequest(string url, string method, string body, string email, string password, string intKey)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            addRequestHeaders(request, email, password, intKey);
            if (body != null)
                addRequestBody(request, body);
            return request;
        }
    
        public static void addRequestHeaders(HttpWebRequest request, string email, string password, string intKey)
        {
            string authenticateStr =
                "<DocuSignCredentials>" +
                    "<Username>" + username + "</Username>" +
                    "<Password>" + password + "</Password>" +
                    "<IntegratorKey>" + integratorKey + "</IntegratorKey>" +
                    "</DocuSignCredentials>";
            request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
            request.Accept = "application/xml";
            request.ContentType = "application/xml";
        }
    
        public static void addRequestBody(HttpWebRequest request, string requestBody)
        {
            // create byte array out of request body and add to the request object
            byte[] body = System.Text.Encoding.UTF8.GetBytes(requestBody);
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(body, 0, requestBody.Length);
            dataStream.Close();
        }
    
        public static string getResponseBody(HttpWebRequest request)
        {
            // read the response stream into a local string
            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string responseText = sr.ReadToEnd();
            return responseText;
        }
    
        public static void GetEnvelopeRecipientTabs()
        {
            string url = baseURL + "/envelopes/" + envelopeId + "/recipients?include_tabs=true";
            HttpWebRequest request = initializeRequest(url, "GET", null, username, password, integratorKey);
            XmlDocument xmldoc = new XmlDocument();
            using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
            {
                xmldoc.Load(resp.GetResponseStream());
                xmldoc.Save("C:\\Temp\\DocuSign\\EnvelopeRecipientsTabs.xml");// FOR TESTING ONLY
            }
        }
    
        public static void GetRecipientId()
        {
            string url = baseURL + "/templates/" + templateId + "/recipients";
            HttpWebRequest request = initializeRequest(url, "GET", null, username, password, integratorKey);
            using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(resp.GetResponseStream());
                xmldoc.Save("C:\\Temp\\DocuSign\\TemplateRecipients.xml");// FOR TESTING ONLY
                XmlNodeList xnList = xmldoc.GetElementsByTagName("signer");
                foreach (XmlNode xn in xnList)
                {
                    string roleName = xn["roleName"].InnerText;
                    if (roleName == "Customer")
                    {
                        // Get customer's recipientId
                        recipId = xn["recipientId"].InnerText;
                        break;
                    }
                }
            }
        }
    
        public static void GetFormulaTabIds()
        {
            string url = baseURL + "/envelopes/" + envelopeId + "/recipients/1/tabs";
            HttpWebRequest request = initializeRequest(url, "GET", null, username, password, integratorKey);
            using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(resp.GetResponseStream());
                xmldoc.Save("C:\\Temp\\DocuSign\\RecipientOneTabs.xml");// FOR TESTING ONLY
                XmlNodeList xnList = xmldoc.GetElementsByTagName("a:formulaTab");
                foreach (XmlNode xn in xnList)
                {
                    // Gets the tabId's for hidden formula tabs, which get inserted into the envelope, 
                    // when a "Payment Item" is dropped on a template, and the Payment Amount is set to a Formula.
                    string tabLabel = xn["tabLabel"].InnerText;
                    switch (tabLabel.Substring(0, 14))
                    {
                        case "PaymentReceipt":
                            PaymentReceiptTabId = xn["tabId"].InnerText;
                            break;
                        case "PaymentFormula":
                            PaymentFormulaTabId = xn["tabId"].InnerText;
                            break;
                    }
                }
            }
        }
    

    巨大感谢Amit K Bist帮助我解决此问题!