Chilkat Online Tools

SQL Server / New FreshBooks / Delete Credit

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.

    -- {
    --   "credit_note": {
    --     "vis_state": 1
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'credit_note.vis_state', 1

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

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

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'PUT', 'https://api.freshbooks.com/accounting/account/{{accountId}}/credit_notes/credit_notes/{{creditId}}', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    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
        EXEC @hr = sp_OADestroy @sbRequestBody
        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 @sbRequestBody
        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": {
    --       "credit_note": {
    --         "accounting_systemid": "E86Qp",
    --         "amount": {
    --           "amount": "150.00",
    --           "code": "CAD"
    --         },
    --         "city": "",
    --         "clientid": 599843,
    --         "code": "",
    --         "country": "",
    --         "create_date": "2020-07-31",
    --         "credit_number": "0000069",
    --         "credit_type": "goodwill",
    --         "creditid": 30947,
    --         "currency_code": "CAD",
    --         "current_organization": "Squarespace",
    --         "description": "Goodwill Credit",
    --         "display_status": "created",
    --         "dispute_status": null,
    --         "ext_archive": 0,
    --         "fname": "",
    --         "id": 30947,
    --         "language": "en",
    --         "last_order_status": null,
    --         "lname": "",
    --         "notes": "Free $150 voucher",
    --         "organization": "Squarespace",
    --         "paid": {
    --           "amount": "150.00",
    --           "code": "CAD"
    --         },
    --         "payment_status": "paid",
    --         "province": "",
    --         "sentid": 1,
    --         "status": "created",
    --         "street": "",
    --         "street2": "",
    --         "template": "clean-grouped",
    --         "terms": "Will be applied to future invoice",
    --         "vat_name": "",
    --         "vat_number": "",
    --         "vis_state": 1
    --       }
    --     }
    --   }
    -- }

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

    DECLARE @Accounting_systemid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Accounting_systemid OUT, 'response.result.credit_note.accounting_systemid'
    DECLARE @Amount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.credit_note.amount.amount'
    DECLARE @Code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.credit_note.amount.code'
    DECLARE @City nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @City OUT, 'response.result.credit_note.city'
    DECLARE @Clientid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Clientid OUT, 'response.result.credit_note.clientid'
    DECLARE @Credit_noteCode nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Credit_noteCode OUT, 'response.result.credit_note.code'
    DECLARE @Country nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Country OUT, 'response.result.credit_note.country'
    DECLARE @Create_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Create_date OUT, 'response.result.credit_note.create_date'
    DECLARE @Credit_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Credit_number OUT, 'response.result.credit_note.credit_number'
    DECLARE @Credit_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Credit_type OUT, 'response.result.credit_note.credit_type'
    DECLARE @Creditid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Creditid OUT, 'response.result.credit_note.creditid'
    DECLARE @Currency_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Currency_code OUT, 'response.result.credit_note.currency_code'
    DECLARE @Current_organization nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Current_organization OUT, 'response.result.credit_note.current_organization'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'response.result.credit_note.description'
    DECLARE @Display_status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Display_status OUT, 'response.result.credit_note.display_status'
    DECLARE @Dispute_status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Dispute_status OUT, 'response.result.credit_note.dispute_status'
    DECLARE @Ext_archive int
    EXEC sp_OAMethod @jResp, 'IntOf', @Ext_archive OUT, 'response.result.credit_note.ext_archive'
    DECLARE @Fname nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Fname OUT, 'response.result.credit_note.fname'
    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'response.result.credit_note.id'
    DECLARE @Language nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Language OUT, 'response.result.credit_note.language'
    DECLARE @Last_order_status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Last_order_status OUT, 'response.result.credit_note.last_order_status'
    DECLARE @Lname nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Lname OUT, 'response.result.credit_note.lname'
    DECLARE @Notes nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Notes OUT, 'response.result.credit_note.notes'
    DECLARE @Organization nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Organization OUT, 'response.result.credit_note.organization'
    DECLARE @PaidAmount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PaidAmount OUT, 'response.result.credit_note.paid.amount'
    DECLARE @PaidCode nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PaidCode OUT, 'response.result.credit_note.paid.code'
    DECLARE @Payment_status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Payment_status OUT, 'response.result.credit_note.payment_status'
    DECLARE @Province nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Province OUT, 'response.result.credit_note.province'
    DECLARE @Sentid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Sentid OUT, 'response.result.credit_note.sentid'
    DECLARE @Status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Status OUT, 'response.result.credit_note.status'
    DECLARE @Street nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Street OUT, 'response.result.credit_note.street'
    DECLARE @Street2 nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Street2 OUT, 'response.result.credit_note.street2'
    DECLARE @Template nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Template OUT, 'response.result.credit_note.template'
    DECLARE @Terms nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Terms OUT, 'response.result.credit_note.terms'
    DECLARE @Vat_name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Vat_name OUT, 'response.result.credit_note.vat_name'
    DECLARE @Vat_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Vat_number OUT, 'response.result.credit_note.vat_number'
    DECLARE @Vis_state int
    EXEC sp_OAMethod @jResp, 'IntOf', @Vis_state OUT, 'response.result.credit_note.vis_state'

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


END
GO

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-d '{
  "credit_note": {
    "vis_state": 1
  }
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/credit_notes/credit_notes/{{creditId}}

Postman Collection Item JSON

{
  "name": "Delete Credit",
  "request": {
    "method": "PUT",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"credit_note\": {\n    \"vis_state\": 1\n  }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/credit_notes/credit_notes/{{creditId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "credit_notes",
        "credit_notes",
        "{{creditId}}"
      ]
    }
  },
  "response": [
    {
      "name": "Delete Credit",
      "originalRequest": {
        "method": "PUT",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"credit_note\": {\n    \"vis_state\": 1\n  }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/credit_notes/credit_notes/{{creditId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "credit_notes",
            "credit_notes",
            "{{creditId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Date",
          "value": "Fri, 11 Sep 2020 14:41:22 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gHQ1NXCxI8DF8WXRECW0ZXVwxND14VVkBIBhtRSFALCwZUV1ECBgJRUAUAAVtKV1ccQAAGDlsEBVYECwAGAAsBUwYVTQACVEBVOQ=="
        },
        {
          "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": "595"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1599835343"
        },
        {
          "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            \"credit_note\": {\n                \"accounting_systemid\": \"E86Qp\",\n                \"amount\": {\n                    \"amount\": \"150.00\",\n                    \"code\": \"CAD\"\n                },\n                \"city\": \"\",\n                \"clientid\": 599843,\n                \"code\": \"\",\n                \"country\": \"\",\n                \"create_date\": \"2020-07-31\",\n                \"credit_number\": \"0000069\",\n                \"credit_type\": \"goodwill\",\n                \"creditid\": 30947,\n                \"currency_code\": \"CAD\",\n                \"current_organization\": \"Squarespace\",\n                \"description\": \"Goodwill Credit\",\n                \"display_status\": \"created\",\n                \"dispute_status\": null,\n                \"ext_archive\": 0,\n                \"fname\": \"\",\n                \"id\": 30947,\n                \"language\": \"en\",\n                \"last_order_status\": null,\n                \"lname\": \"\",\n                \"notes\": \"Free $150 voucher\",\n                \"organization\": \"Squarespace\",\n                \"paid\": {\n                    \"amount\": \"150.00\",\n                    \"code\": \"CAD\"\n                },\n                \"payment_status\": \"paid\",\n                \"province\": \"\",\n                \"sentid\": 1,\n                \"status\": \"created\",\n                \"street\": \"\",\n                \"street2\": \"\",\n                \"template\": \"clean-grouped\",\n                \"terms\": \"Will be applied to future invoice\",\n                \"vat_name\": \"\",\n                \"vat_number\": \"\",\n                \"vis_state\": 1\n            }\n        }\n    }\n}"
    }
  ]
}