Chilkat Online Tools

C++ / New FreshBooks / Create Single Invoice Profile

Back to Collection Items

#include <CkHttp.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http;
    bool success;

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "invoice_profile": {
    //     "customerid": 123,
    //     "create_date": "2019-04-25",
    //     "frequency": "m",
    //     "numberRecurring": 3,
    //     "lines": [
    //       {
    //         "name": "Test Item",
    //         "qty": "1",
    //         "unit_cost": {
    //           "amount": "27.00"
    //         }
    //       }
    //     ]
    //   }
    // }

    CkJsonObject json;
    json.UpdateInt("invoice_profile.customerid",123);
    json.UpdateString("invoice_profile.create_date","2019-04-25");
    json.UpdateString("invoice_profile.frequency","m");
    json.UpdateInt("invoice_profile.numberRecurring",3);
    json.UpdateString("invoice_profile.lines[0].name","Test Item");
    json.UpdateString("invoice_profile.lines[0].qty","1");
    json.UpdateString("invoice_profile.lines[0].unit_cost.amount","27.00");

    // Adds the "Authorization: Bearer <access_token>" header.
    http.put_AuthToken("<access_token>");
    http.SetRequestHeader("Content-Type","application/json");

    CkHttpResponse *resp = http.PostJson3("https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        std::cout << http.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbResponseBody;
    resp->GetBodySb(sbResponseBody);

    CkJsonObject jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    std::cout << "Response Body:" << "\r\n";
    std::cout << jResp.emit() << "\r\n";

    int respStatusCode = resp->get_StatusCode();
    std::cout << "Response Status Code = " << respStatusCode << "\r\n";
    if (respStatusCode >= 400) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << resp->header() << "\r\n";
        std::cout << "Failed." << "\r\n";
        delete resp;
        return;
    }

    delete resp;

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "response": {
    //     "result": {
    //       "invoice_profile": {
    //         "accounting_systemid": "wkMd2g",
    //         "address": "",
    //         "amount": {
    //           "amount": "0.00",
    //           "code": "USD"
    //         },
    //         "auto_bill": false,
    //         "bill_gateway": null,
    //         "city": "",
    //         "code": "",
    //         "country": "Canada",
    //         "create_date": "2019-04-25",
    //         "currency_code": "USD",
    //         "customerid": 31006,
    //         "description": "",
    //         "disable": false,
    //         "discount_total": {
    //           "amount": "0.00",
    //           "code": "USD"
    //         },
    //         "discount_value": "0",
    //         "due_offset_days": 0,
    //         "ext_archive": null,
    //         "fname": "Nicolas",
    //         "frequency": "m",
    //         "id": 488,
    //         "include_unbilled_time": false,
    //         "language": "en",
    //         "lname": "one",
    //         "notes": "",
    //         "numberRecurring": 3,
    //         "occurrences_to_date": 0,
    //         "organization": "first company",
    //         "ownerid": 1,
    //         "payment_details": "",
    //         "po_number": null,
    //         "profileid": 488,
    //         "province": "",
    //         "require_auto_bill": false,
    //         "retainer_id": null,
    //         "send_email": true,
    //         "send_gmail": false,
    //         "street": "",
    //         "street2": "",
    //         "terms": null,
    //         "total_accrued_revenue": null,
    //         "updated": "2019-04-24 14:17:14",
    //         "vat_name": "",
    //         "vat_number": "",
    //         "vis_state": 0
    //       }
    //     }
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    const char *Accounting_systemid = jResp.stringOf("response.result.invoice_profile.accounting_systemid");
    const char *Address = jResp.stringOf("response.result.invoice_profile.address");
    const char *Amount = jResp.stringOf("response.result.invoice_profile.amount.amount");
    const char *Code = jResp.stringOf("response.result.invoice_profile.amount.code");
    bool Auto_bill = jResp.BoolOf("response.result.invoice_profile.auto_bill");
    const char *Bill_gateway = jResp.stringOf("response.result.invoice_profile.bill_gateway");
    const char *City = jResp.stringOf("response.result.invoice_profile.city");
    const char *Invoice_profileCode = jResp.stringOf("response.result.invoice_profile.code");
    const char *Country = jResp.stringOf("response.result.invoice_profile.country");
    const char *Create_date = jResp.stringOf("response.result.invoice_profile.create_date");
    const char *Currency_code = jResp.stringOf("response.result.invoice_profile.currency_code");
    int Customerid = jResp.IntOf("response.result.invoice_profile.customerid");
    const char *Description = jResp.stringOf("response.result.invoice_profile.description");
    bool Disable = jResp.BoolOf("response.result.invoice_profile.disable");
    const char *Discount_totalAmount = jResp.stringOf("response.result.invoice_profile.discount_total.amount");
    const char *Discount_totalCode = jResp.stringOf("response.result.invoice_profile.discount_total.code");
    const char *Discount_value = jResp.stringOf("response.result.invoice_profile.discount_value");
    int Due_offset_days = jResp.IntOf("response.result.invoice_profile.due_offset_days");
    const char *Ext_archive = jResp.stringOf("response.result.invoice_profile.ext_archive");
    const char *Fname = jResp.stringOf("response.result.invoice_profile.fname");
    const char *Frequency = jResp.stringOf("response.result.invoice_profile.frequency");
    int Id = jResp.IntOf("response.result.invoice_profile.id");
    bool Include_unbilled_time = jResp.BoolOf("response.result.invoice_profile.include_unbilled_time");
    const char *Language = jResp.stringOf("response.result.invoice_profile.language");
    const char *Lname = jResp.stringOf("response.result.invoice_profile.lname");
    const char *Notes = jResp.stringOf("response.result.invoice_profile.notes");
    int NumberRecurring = jResp.IntOf("response.result.invoice_profile.numberRecurring");
    int Occurrences_to_date = jResp.IntOf("response.result.invoice_profile.occurrences_to_date");
    const char *Organization = jResp.stringOf("response.result.invoice_profile.organization");
    int Ownerid = jResp.IntOf("response.result.invoice_profile.ownerid");
    const char *Payment_details = jResp.stringOf("response.result.invoice_profile.payment_details");
    const char *Po_number = jResp.stringOf("response.result.invoice_profile.po_number");
    int Profileid = jResp.IntOf("response.result.invoice_profile.profileid");
    const char *Province = jResp.stringOf("response.result.invoice_profile.province");
    bool Require_auto_bill = jResp.BoolOf("response.result.invoice_profile.require_auto_bill");
    const char *Retainer_id = jResp.stringOf("response.result.invoice_profile.retainer_id");
    bool Send_email = jResp.BoolOf("response.result.invoice_profile.send_email");
    bool Send_gmail = jResp.BoolOf("response.result.invoice_profile.send_gmail");
    const char *Street = jResp.stringOf("response.result.invoice_profile.street");
    const char *Street2 = jResp.stringOf("response.result.invoice_profile.street2");
    const char *Terms = jResp.stringOf("response.result.invoice_profile.terms");
    const char *Total_accrued_revenue = jResp.stringOf("response.result.invoice_profile.total_accrued_revenue");
    const char *Updated = jResp.stringOf("response.result.invoice_profile.updated");
    const char *Vat_name = jResp.stringOf("response.result.invoice_profile.vat_name");
    const char *Vat_number = jResp.stringOf("response.result.invoice_profile.vat_number");
    int Vis_state = jResp.IntOf("response.result.invoice_profile.vis_state");
    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "invoice_profile": {
    "customerid": {{customerId}},
    "create_date": "2019-04-25",
    "frequency": "m", 
    "numberRecurring": 3,
    "lines": [
      {
        "name": "Test Item",
        "qty": "1",
        "unit_cost": {
          "amount": "27.00"
        }
      }
     ]
   }
 }'
https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles

Postman Collection Item JSON

{
  "name": "Create Single Invoice Profile",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"customerId\", jsonData.response.result.invoice_profile[0].customerid);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"invoice_profile\": {\n    \"customerid\": {{customerId}},\n    \"create_date\": \"2019-04-25\",\n    \"frequency\": \"m\", \n    \"numberRecurring\": 3,\n    \"lines\": [\n      {\n        \"name\": \"Test Item\",\n        \"qty\": \"1\",\n        \"unit_cost\": {\n          \"amount\": \"27.00\"\n        }\n      }\n     ]\n   }\n }\n"
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "invoice_profiles",
        "invoice_profiles"
      ]
    },
    "description": "Creates a monthly recurring invoice that sends 3 invoices in total."
  },
  "response": [
    {
      "name": "Create Single Invoice Profile - Invalid Date",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_profile\": {\n    \"customerid\": {{customerId}},\n    \"create_date\": \"2019-04-20\",\n    \"frequency\": \"m\", \n    \"numberRecurring\": 3\n   }\n }"
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "invoice_profiles",
            "invoice_profiles"
          ]
        }
      },
      "status": "UNPROCESSABLE ENTITY",
      "code": 422,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gNX0BcCwUGPUAQVwRRVFFBCQxXF14IUAcURFgHDw9dQBJIUU4HHwZXUAIPC1ZRWlYGUw5UAQEGHgJWAU0TUgEAAgYGU1IFCgVVU1EGAhMbAAVdRVY/"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "303"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:16:19 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"errors\": [\n            {\n                \"errno\": 16003,\n                \"field\": \"create_date\",\n                \"message\": \"'Create Date' is an invalid value: The date for a new recurring profile must be today or later.\",\n                \"object\": \"invoice_profile\",\n                \"value\": \"2019-04-20\"\n            }\n        ]\n    }\n}"
    },
    {
      "name": "Create Single Invoice Profile",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_profile\": {\n    \"customerid\": {{customerId}},\n    \"create_date\": \"2019-04-25\",\n    \"frequency\": \"m\", \n    \"numberRecurring\": 3\n   }\n }"
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "invoice_profiles",
            "invoice_profiles"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gNX0BcCwUGPUAQVwRRVFFBCQxXF14IUAcURFgHDw9dQBJIUU4HHwZfXAICBlRQUlMGUQtRCAgMHgJWAU0TVARRBlVWVV5VDVcEAQQGBBMbAAVdRVY/"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:17:14 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:17:14 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"invoice_profile\": {\n                \"accounting_systemid\": \"wkMd2g\",\n                \"address\": \"\",\n                \"amount\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"auto_bill\": false,\n                \"bill_gateway\": null,\n                \"city\": \"\",\n                \"code\": \"\",\n                \"country\": \"Canada\",\n                \"create_date\": \"2019-04-25\",\n                \"currency_code\": \"USD\",\n                \"customerid\": 31006,\n                \"description\": \"\",\n                \"disable\": false,\n                \"discount_total\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"discount_value\": \"0\",\n                \"due_offset_days\": 0,\n                \"ext_archive\": null,\n                \"fname\": \"Nicolas\",\n                \"frequency\": \"m\",\n                \"id\": 488,\n                \"include_unbilled_time\": false,\n                \"language\": \"en\",\n                \"lname\": \"one\",\n                \"notes\": \"\",\n                \"numberRecurring\": 3,\n                \"occurrences_to_date\": 0,\n                \"organization\": \"first company\",\n                \"ownerid\": 1,\n                \"payment_details\": \"\",\n                \"po_number\": null,\n                \"profileid\": 488,\n                \"province\": \"\",\n                \"require_auto_bill\": false,\n                \"retainer_id\": null,\n                \"send_email\": true,\n                \"send_gmail\": false,\n                \"street\": \"\",\n                \"street2\": \"\",\n                \"terms\": null,\n                \"total_accrued_revenue\": null,\n                \"updated\": \"2019-04-24 14:17:14\",\n                \"vat_name\": \"\",\n                \"vat_number\": \"\",\n                \"vis_state\": 0\n            }\n        }\n    }\n}"
    }
  ]
}