Chilkat Online Tools

SQL Server / New FreshBooks / Add Payment to Bill

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- {
    --   "bill_payment": {
    --     "billid": "7",
    --     "amount": {
    --       "amount": "375",
    --       "code": "USD"
    --     },
    --     "payment_type": "Check",
    --     "paid_date": "2020-10-09",
    --     "note": "",
    --     "bill": null
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.billid', '7'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.amount.amount', '375'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.amount.code', 'USD'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.payment_type', 'Check'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.paid_date', '2020-10-09'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'bill_payment.note', ''
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'bill_payment.bill'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.freshbooks.com/accounting/account/{{accountId}}/bill_payments/bill_payments', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "response": {
    --     "result": {
    --       "bill_payment": {
    --         "amount": {
    --           "amount": "100.00",
    --           "code": "EUR"
    --         },
    --         "billid": 1141,
    --         "id": 909,
    --         "matched_with_expense": false,
    --         "note": "",
    --         "paid_date": "2021-01-30",
    --         "payment_type": "Check",
    --         "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

    DECLARE @Amount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.bill_payment.amount.amount'
    DECLARE @Code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.bill_payment.amount.code'
    DECLARE @Billid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Billid OUT, 'response.result.bill_payment.billid'
    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'response.result.bill_payment.id'
    DECLARE @Matched_with_expense int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Matched_with_expense OUT, 'response.result.bill_payment.matched_with_expense'
    DECLARE @Note nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Note OUT, 'response.result.bill_payment.note'
    DECLARE @Paid_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Paid_date OUT, 'response.result.bill_payment.paid_date'
    DECLARE @Payment_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Payment_type OUT, 'response.result.bill_payment.payment_type'
    DECLARE @Vis_state int
    EXEC sp_OAMethod @jResp, 'IntOf', @Vis_state OUT, 'response.result.bill_payment.vis_state'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "bill_payment": {
        "billid": "7",
        "amount": {
            "amount": "375",
            "code": "USD"
        },
        "payment_type": "Check",
        "paid_date": "2020-10-09",
        "note": "",
        "bill": null
    }
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/bill_payments/bill_payments

Postman Collection Item JSON

{
  "name": "Add Payment to Bill",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"billPaymentId\", jsonData.response.result.bill_payment.id)"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"bill_payment\": {\n        \"billid\": \"7\",\n        \"amount\": {\n            \"amount\": \"375\",\n            \"code\": \"USD\"\n        },\n        \"payment_type\": \"Check\",\n        \"paid_date\": \"2020-10-09\",\n        \"note\": \"\",\n        \"bill\": null\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/bill_payments/bill_payments",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "bill_payments",
        "bill_payments"
      ]
    }
  },
  "response": [
    {
      "name": "Add Payment to Bill",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"bill_payment\": {\n        \"billid\": {{billId}},\n        \"amount\": {\n            \"amount\": \"100\",\n            \"code\": \"EUR\"\n        },\n        \"payment_type\": \"Check\",\n        \"paid_date\": \"2021-01-30\",\n        \"note\": \"\",\n        \"bill\": null\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/bill_payments/bill_payments",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "bill_payments",
            "bill_payments"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Date",
          "value": "Thu, 28 Jan 2021 19:48:34 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gGWFpfPRYCG10HVhZLAlZbXwlJAEgMVgwQRRVNVk8IHQFTUlcOBAFQUgkOBlFRVlsITgpXCBQWBQBVDwcAAlFXAFIGUwcFDREcAgAORFRq"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Content-Security-Policy",
          "value": "default-src 'self'"
        },
        {
          "key": "X-Content-Security-Policy",
          "value": "default-src 'self'"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31556926; includeSubDomains; preload"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin-when-cross-origin"
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "600"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "597"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1611863375"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Alt-Svc",
          "value": "h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"bill_payment\": {\n                \"amount\": {\n                    \"amount\": \"100.00\",\n                    \"code\": \"EUR\"\n                },\n                \"billid\": 1141,\n                \"id\": 909,\n                \"matched_with_expense\": false,\n                \"note\": \"\",\n                \"paid_date\": \"2021-01-30\",\n                \"payment_type\": \"Check\",\n                \"vis_state\": 0\n            }\n        }\n    }\n}"
    }
  ]
}