Chilkat Online Tools

Swift / New FreshBooks / Single Invoice w/ Payment Gateway

Back to Collection Items

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

    let http = CkoHttp()
    var success: Bool

    // 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": {
    //     "customerid": 123,
    //     "create_date": "2019-04-20",
    //     "lines": [
    //       {
    //         "type": 0,
    //         "description": "",
    //         "taxName1": "",
    //         "taxAmount1": 0,
    //         "name": "Climbing Helmet",
    //         "qty": 1,
    //         "taxName2": "",
    //         "taxAmount2": 0,
    //         "unit_cost": {
    //           "amount": "80.00",
    //           "code": "USD"
    //         }
    //       },
    //       {
    //         "type": 0,
    //         "description": "",
    //         "taxName1": "",
    //         "taxAmount1": 0,
    //         "name": "Belay Device",
    //         "qty": 1,
    //         "taxName2": "",
    //         "taxAmount2": 0,
    //         "unit_cost": {
    //           "amount": "100.00",
    //           "code": "USD"
    //         }
    //       }
    //     ],
    //     "presentation": {
    //       "image_logo_src": "{{image_logo_src}}",
    //       "theme_primary_color": "{{theme_primary_color}}"
    //     }
    //   }
    // }

    let json = CkoJsonObject()
    json.UpdateInt("invoice.customerid", value: 123)
    json.UpdateString("invoice.create_date", value: "2019-04-20")
    json.UpdateInt("invoice.lines[0].type", value: 0)
    json.UpdateString("invoice.lines[0].description", value: "")
    json.UpdateString("invoice.lines[0].taxName1", value: "")
    json.UpdateInt("invoice.lines[0].taxAmount1", value: 0)
    json.UpdateString("invoice.lines[0].name", value: "Climbing Helmet")
    json.UpdateInt("invoice.lines[0].qty", value: 1)
    json.UpdateString("invoice.lines[0].taxName2", value: "")
    json.UpdateInt("invoice.lines[0].taxAmount2", value: 0)
    json.UpdateString("invoice.lines[0].unit_cost.amount", value: "80.00")
    json.UpdateString("invoice.lines[0].unit_cost.code", value: "USD")
    json.UpdateInt("invoice.lines[1].type", value: 0)
    json.UpdateString("invoice.lines[1].description", value: "")
    json.UpdateString("invoice.lines[1].taxName1", value: "")
    json.UpdateInt("invoice.lines[1].taxAmount1", value: 0)
    json.UpdateString("invoice.lines[1].name", value: "Belay Device")
    json.UpdateInt("invoice.lines[1].qty", value: 1)
    json.UpdateString("invoice.lines[1].taxName2", value: "")
    json.UpdateInt("invoice.lines[1].taxAmount2", value: 0)
    json.UpdateString("invoice.lines[1].unit_cost.amount", value: "100.00")
    json.UpdateString("invoice.lines[1].unit_cost.code", value: "USD")
    json.UpdateString("invoice.presentation.image_logo_src", value: "{{image_logo_src}}")
    json.UpdateString("invoice.presentation.theme_primary_color", value: "{{theme_primary_color}}")

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

    var resp: CkoHttpResponse? = http.PostJson3("https://api.freshbooks.com/accounting/account/{{accountId}}/invoices/invoices/{{invoiceid}}?include%5B%5D=allowed_gateways", contentType: "application/json", json: json)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    let sbResponseBody = CkoStringBuilder()
    resp!.GetBodySb(sbResponseBody)

    let jResp = CkoJsonObject()
    jResp.LoadSb(sbResponseBody)
    jResp.EmitCompact = false

    print("Response Body:")
    print("\(jResp.Emit())")

    var respStatusCode: Int = resp!.StatusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.Header)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

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

    // {
    //   "response": {
    //     "result": {
    //       "invoice": {
    //         "accountid": "wkMd2g",
    //         "accounting_systemid": "wkMd2g",
    //         "address": "",
    //         "allowed_gateways": [
    //         ],
    //         "amount": {
    //           "amount": "180.00",
    //           "code": "USD"
    //         },
    //         "auto_bill": false,
    //         "autobill_status": null,
    //         "basecampid": 0,
    //         "city": "Toronto",
    //         "code": "M6P 3T1",
    //         "country": "",
    //         "create_date": "2019-04-20",
    //         "created_at": "2019-05-09 16:02:40",
    //         "currency_code": "USD",
    //         "current_organization": "Space Is Only Noise",
    //         "customerid": 92402,
    //         "date_paid": null,
    //         "deposit_amount": null,
    //         "deposit_percentage": null,
    //         "deposit_status": "none",
    //         "description": "",
    //         "discount_description": null,
    //         "discount_total": {
    //           "amount": "0.00",
    //           "code": "USD"
    //         },
    //         "discount_value": "0",
    //         "display_status": "draft",
    //         "dispute_status": null,
    //         "due_date": "2019-04-20",
    //         "due_offset_days": 0,
    //         "estimateid": 0,
    //         "ext_archive": 0,
    //         "fname": "Nicholas",
    //         "fulfillment_date": null,
    //         "generation_date": null,
    //         "gmail": false,
    //         "id": 74888,
    //         "invoice_number": "0000029",
    //         "invoiceid": 74888,
    //         "language": "en",
    //         "last_order_status": null,
    //         "lname": "Jaar",
    //         "notes": "",
    //         "organization": "Space Is Only Noise",
    //         "outstanding": {
    //           "amount": "180.00",
    //           "code": "USD"
    //         },
    //         "ownerid": 1,
    //         "paid": {
    //           "amount": "0.00",
    //           "code": "USD"
    //         },
    //         "parent": 0,
    //         "payment_details": "",
    //         "payment_status": "unpaid",
    //         "po_number": null,
    //         "province": "Ontario",
    //         "return_uri": null,
    //         "sentid": 1,
    //         "show_attachments": true,
    //         "status": 1,
    //         "street": "1655 Dupont St. W.",
    //         "street2": "",
    //         "template": "clean-grouped",
    //         "terms": "",
    //         "updated": "2019-05-09 16:02:41",
    //         "v3_status": "draft",
    //         "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

    let Date_paid = CkoDtObj()

    var Accountid: String? = jResp.StringOf("response.result.invoice.accountid")
    var Accounting_systemid: String? = jResp.StringOf("response.result.invoice.accounting_systemid")
    var Address: String? = jResp.StringOf("response.result.invoice.address")
    var Amount: String? = jResp.StringOf("response.result.invoice.amount.amount")
    var Code: String? = jResp.StringOf("response.result.invoice.amount.code")
    var Auto_bill: Bool = jResp.BoolOf("response.result.invoice.auto_bill")
    var Autobill_status: String? = jResp.StringOf("response.result.invoice.autobill_status")
    var Basecampid: Int = jResp.IntOf("response.result.invoice.basecampid").intValue
    var City: String? = jResp.StringOf("response.result.invoice.city")
    var InvoiceCode: String? = jResp.StringOf("response.result.invoice.code")
    var Country: String? = jResp.StringOf("response.result.invoice.country")
    var Create_date: String? = jResp.StringOf("response.result.invoice.create_date")
    var Created_at: String? = jResp.StringOf("response.result.invoice.created_at")
    var Currency_code: String? = jResp.StringOf("response.result.invoice.currency_code")
    var Current_organization: String? = jResp.StringOf("response.result.invoice.current_organization")
    var Customerid: Int = jResp.IntOf("response.result.invoice.customerid").intValue
    jResp.DtOf("response.result.invoice.date_paid", bLocal: false, dt: Date_paid)
    var Deposit_amount: String? = jResp.StringOf("response.result.invoice.deposit_amount")
    var Deposit_percentage: String? = jResp.StringOf("response.result.invoice.deposit_percentage")
    var Deposit_status: String? = jResp.StringOf("response.result.invoice.deposit_status")
    var Description: String? = jResp.StringOf("response.result.invoice.description")
    var Discount_description: String? = jResp.StringOf("response.result.invoice.discount_description")
    var Discount_totalAmount: String? = jResp.StringOf("response.result.invoice.discount_total.amount")
    var Discount_totalCode: String? = jResp.StringOf("response.result.invoice.discount_total.code")
    var Discount_value: String? = jResp.StringOf("response.result.invoice.discount_value")
    var Display_status: String? = jResp.StringOf("response.result.invoice.display_status")
    var Dispute_status: String? = jResp.StringOf("response.result.invoice.dispute_status")
    var Due_date: String? = jResp.StringOf("response.result.invoice.due_date")
    var Due_offset_days: Int = jResp.IntOf("response.result.invoice.due_offset_days").intValue
    var Estimateid: Int = jResp.IntOf("response.result.invoice.estimateid").intValue
    var Ext_archive: Int = jResp.IntOf("response.result.invoice.ext_archive").intValue
    var Fname: String? = jResp.StringOf("response.result.invoice.fname")
    var Fulfillment_date: String? = jResp.StringOf("response.result.invoice.fulfillment_date")
    var Generation_date: String? = jResp.StringOf("response.result.invoice.generation_date")
    var Gmail: Bool = jResp.BoolOf("response.result.invoice.gmail")
    var Id: Int = jResp.IntOf("response.result.invoice.id").intValue
    var Invoice_number: String? = jResp.StringOf("response.result.invoice.invoice_number")
    var Invoiceid: Int = jResp.IntOf("response.result.invoice.invoiceid").intValue
    var Language: String? = jResp.StringOf("response.result.invoice.language")
    var Last_order_status: String? = jResp.StringOf("response.result.invoice.last_order_status")
    var Lname: String? = jResp.StringOf("response.result.invoice.lname")
    var Notes: String? = jResp.StringOf("response.result.invoice.notes")
    var Organization: String? = jResp.StringOf("response.result.invoice.organization")
    var OutstandingAmount: String? = jResp.StringOf("response.result.invoice.outstanding.amount")
    var OutstandingCode: String? = jResp.StringOf("response.result.invoice.outstanding.code")
    var Ownerid: Int = jResp.IntOf("response.result.invoice.ownerid").intValue
    var PaidAmount: String? = jResp.StringOf("response.result.invoice.paid.amount")
    var PaidCode: String? = jResp.StringOf("response.result.invoice.paid.code")
    var Parent: Int = jResp.IntOf("response.result.invoice.parent").intValue
    var Payment_details: String? = jResp.StringOf("response.result.invoice.payment_details")
    var Payment_status: String? = jResp.StringOf("response.result.invoice.payment_status")
    var Po_number: String? = jResp.StringOf("response.result.invoice.po_number")
    var Province: String? = jResp.StringOf("response.result.invoice.province")
    var Return_uri: String? = jResp.StringOf("response.result.invoice.return_uri")
    var Sentid: Int = jResp.IntOf("response.result.invoice.sentid").intValue
    var Show_attachments: Bool = jResp.BoolOf("response.result.invoice.show_attachments")
    var Status: Int = jResp.IntOf("response.result.invoice.status").intValue
    var Street: String? = jResp.StringOf("response.result.invoice.street")
    var Street2: String? = jResp.StringOf("response.result.invoice.street2")
    var Template: String? = jResp.StringOf("response.result.invoice.template")
    var Terms: String? = jResp.StringOf("response.result.invoice.terms")
    var Updated: String? = jResp.StringOf("response.result.invoice.updated")
    var V3_status: String? = jResp.StringOf("response.result.invoice.v3_status")
    var Vat_name: String? = jResp.StringOf("response.result.invoice.vat_name")
    var Vat_number: String? = jResp.StringOf("response.result.invoice.vat_number")
    var Vis_state: Int = jResp.IntOf("response.result.invoice.vis_state").intValue
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("response.result.invoice.allowed_gateways").intValue
    while i < count_i {
        jResp.I = i
        i = i + 1
    }


}

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
	"invoice": {
		"customerid": {{customerId}},
		"create_date": "2019-04-20",
		"lines": [
	      {
	        "type": 0,
	        "description": "",
	        "taxName1": "",
	        "taxAmount1": 0,
	        "name": "Climbing Helmet",
	        "qty": 1,
	        "taxName2": "",
	        "taxAmount2": 0,
	        "unit_cost": {
	            "amount": "80.00",
	            "code": "USD"
	        }
	      },
	      {
	        "type": 0,
	        "description": "",
	        "taxName1": "",
	        "taxAmount1": 0,
	        "name": "Belay Device",
	        "qty": 1,
	        "taxName2": "",
	        "taxAmount2": 0,
	        "unit_cost": {
	            "amount": "100.00",
	            "code": "USD"
	        }
	      }
	   ],
		"presentation": {
			"image_logo_src": {{imgSrc}},
			 "theme_primary_color": {{themeColor}}
		}
	}
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/invoices/invoices/{{invoiceid}}?include%5B%5D=allowed_gateways

Postman Collection Item JSON

{
  "name": "Single Invoice w/ Payment Gateway",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"invoiceId\", jsonData.response.result.invoice.id)",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"invoice\": {\n\t\t\"customerid\": {{customerId}},\n\t\t\"create_date\": \"2019-04-20\",\n\t\t\"lines\": [\n\t      {\n\t        \"type\": 0,\n\t        \"description\": \"\",\n\t        \"taxName1\": \"\",\n\t        \"taxAmount1\": 0,\n\t        \"name\": \"Climbing Helmet\",\n\t        \"qty\": 1,\n\t        \"taxName2\": \"\",\n\t        \"taxAmount2\": 0,\n\t        \"unit_cost\": {\n\t            \"amount\": \"80.00\",\n\t            \"code\": \"USD\"\n\t        }\n\t      },\n\t      {\n\t        \"type\": 0,\n\t        \"description\": \"\",\n\t        \"taxName1\": \"\",\n\t        \"taxAmount1\": 0,\n\t        \"name\": \"Belay Device\",\n\t        \"qty\": 1,\n\t        \"taxName2\": \"\",\n\t        \"taxAmount2\": 0,\n\t        \"unit_cost\": {\n\t            \"amount\": \"100.00\",\n\t            \"code\": \"USD\"\n\t        }\n\t      }\n\t   ],\n\t\t\"presentation\": {\n\t\t\t\"image_logo_src\": {{imgSrc}},\n\t\t\t \"theme_primary_color\": {{themeColor}}\n\t\t}\n\t}\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoices/invoices/{{invoiceid}}?include%5B%5D=allowed_gateways",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "invoices",
        "invoices",
        "{{invoiceid}}"
      ],
      "query": [
        {
          "key": "include%5B%5D",
          "value": "allowed_gateways"
        }
      ]
    },
    "description": "Call an invoice and display the enabled gateway on that invoice.\n\nStripe = id: 26 |\nFBpay = id: 30 |\nPayPal = id: 7"
  },
  "response": [
    {
      "name": "Single Invoice w/ Payment Gateway",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"invoice\": {\n\t\t\"customerid\": {{customerId}},\n\t\t\"create_date\": \"2019-04-20\",\n\t\t\"lines\": [\n\t      {\n\t        \"type\": 0,\n\t        \"description\": \"\",\n\t        \"taxName1\": \"\",\n\t        \"taxAmount1\": 0,\n\t        \"name\": \"Climbing Helmet\",\n\t        \"qty\": 1,\n\t        \"taxName2\": \"\",\n\t        \"taxAmount2\": 0,\n\t        \"unit_cost\": {\n\t            \"amount\": \"80.00\",\n\t            \"code\": \"USD\"\n\t        }\n\t      },\n\t      {\n\t        \"type\": 0,\n\t        \"description\": \"\",\n\t        \"taxName1\": \"\",\n\t        \"taxAmount1\": 0,\n\t        \"name\": \"Belay Device\",\n\t        \"qty\": 1,\n\t        \"taxName2\": \"\",\n\t        \"taxAmount2\": 0,\n\t        \"unit_cost\": {\n\t            \"amount\": \"100.00\",\n\t            \"code\": \"USD\"\n\t        }\n\t      }\n\t   ],\n\t\t\"presentation\": {\n\t\t\t\"image_logo_src\": {{imgSrc}},\n\t\t\t \"theme_primary_color\": {{themeColor}}\n\t\t}\n\t}\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoices/invoices?include%5B%5D=allowed_gateways",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "invoices",
            "invoices"
          ],
          "query": [
            {
              "key": "include%5B%5D",
              "value": "allowed_gateways"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gNX0BcCwUGEQoLVhRXUVdXQEcVUR1RHVBWDgRRUVQKAAldWFIFAgNSSAAEBldKQVoHUApVDglQUwcGCVJXBAJASFBWDRUGZQ=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557432219"
        },
        {
          "key": "Retry-After",
          "value": "57"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Wed, 09 May 2018 20:02:41 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": "Thu, 09 May 2019 20:02:41 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17357-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\": {\n                \"accountid\": \"wkMd2g\",\n                \"accounting_systemid\": \"wkMd2g\",\n                \"address\": \"\",\n                \"allowed_gateways\": [],\n                \"amount\": {\n                    \"amount\": \"180.00\",\n                    \"code\": \"USD\"\n                },\n                \"auto_bill\": false,\n                \"autobill_status\": null,\n                \"basecampid\": 0,\n                \"city\": \"Toronto\",\n                \"code\": \"M6P 3T1\",\n                \"country\": \"\",\n                \"create_date\": \"2019-04-20\",\n                \"created_at\": \"2019-05-09 16:02:40\",\n                \"currency_code\": \"USD\",\n                \"current_organization\": \"Space Is Only Noise\",\n                \"customerid\": 92402,\n                \"date_paid\": null,\n                \"deposit_amount\": null,\n                \"deposit_percentage\": null,\n                \"deposit_status\": \"none\",\n                \"description\": \"\",\n                \"discount_description\": null,\n                \"discount_total\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"discount_value\": \"0\",\n                \"display_status\": \"draft\",\n                \"dispute_status\": null,\n                \"due_date\": \"2019-04-20\",\n                \"due_offset_days\": 0,\n                \"estimateid\": 0,\n                \"ext_archive\": 0,\n                \"fname\": \"Nicholas\",\n                \"fulfillment_date\": null,\n                \"generation_date\": null,\n                \"gmail\": false,\n                \"id\": 74888,\n                \"invoice_number\": \"0000029\",\n                \"invoiceid\": 74888,\n                \"language\": \"en\",\n                \"last_order_status\": null,\n                \"lname\": \"Jaar\",\n                \"notes\": \"\",\n                \"organization\": \"Space Is Only Noise\",\n                \"outstanding\": {\n                    \"amount\": \"180.00\",\n                    \"code\": \"USD\"\n                },\n                \"ownerid\": 1,\n                \"paid\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"parent\": 0,\n                \"payment_details\": \"\",\n                \"payment_status\": \"unpaid\",\n                \"po_number\": null,\n                \"province\": \"Ontario\",\n                \"return_uri\": null,\n                \"sentid\": 1,\n                \"show_attachments\": true,\n                \"status\": 1,\n                \"street\": \"1655 Dupont St. W.\",\n                \"street2\": \"\",\n                \"template\": \"clean-grouped\",\n                \"terms\": \"\",\n                \"updated\": \"2019-05-09 16:02:41\",\n                \"v3_status\": \"draft\",\n                \"vat_name\": \"\",\n                \"vat_number\": \"\",\n                \"vis_state\": 0\n            }\n        }\n    }\n}"
    }
  ]
}