Chilkat Online Tools

SQL Server / New FreshBooks / 1. [FBPAY] - Create Payment Method

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.

    -- {
    --   "cc_info": {
    --     "name": "John Johnston",
    --     "card_number": "4500123456789012",
    --     "expiry_month": "10",
    --     "expiry_year": "2024",
    --     "email": "mjohnston@freshbooks.com",
    --     "cvv": "123",
    --     "postal_code": "M6P3T1",
    --     "country": "CA"
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.name', 'John Johnston'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.card_number', '4500123456789012'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.expiry_month', '10'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.expiry_year', '2024'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.email', 'mjohnston@freshbooks.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.cvv', '123'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.postal_code', 'M6P3T1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'cc_info.country', 'CA'

    -- 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://paid.freshbooks.com/gateway/fbpay/tokenize', '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)

    -- {
    --   "cc_token": "1122334455"
    -- }

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

    DECLARE @cc_token nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @cc_token OUT, 'cc_token'

    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 '{
    "cc_info": {
        "name": "John Johnston", 
        "card_number": "4500123456789012", 
        "expiry_month": "10", 
        "expiry_year": "2024",
        "email": "mjohnston@freshbooks.com", 
        "cvv": "123",
        "postal_code": "M6P3T1",
        "country": "CA"
    }
}'
https://paid.freshbooks.com/gateway/fbpay/tokenize

Postman Collection Item JSON

{
  "name": "1. [FBPAY] - Create Payment Method",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"cc_info\": {\n        \"name\": \"John Johnston\", \n        \"card_number\": \"4500123456789012\", \n        \"expiry_month\": \"10\", \n        \"expiry_year\": \"2024\",\n        \"email\": \"mjohnston@freshbooks.com\", \n        \"cvv\": \"123\",\n        \"postal_code\": \"M6P3T1\",\n        \"country\": \"CA\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://paid.freshbooks.com/gateway/fbpay/tokenize",
      "protocol": "https",
      "host": [
        "paid",
        "freshbooks",
        "com"
      ],
      "path": [
        "gateway",
        "fbpay",
        "tokenize"
      ]
    }
  },
  "response": [
    {
      "name": "1. [FBPAY] - Create Payment Method",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"cc_info\": {\n        \"name\": \"John Johnston\", \n        \"card_number\": \"4500123456789012\", \n        \"expiry_month\": \"10\", \n        \"expiry_year\": \"2024\",\n        \"email\": \"mjohnston@freshbooks.com\", \n        \"cvv\": \"123\",\n        \"postal_code\": \"M6P3T1\",\n        \"country\": \"CA\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://paid.freshbooks.com/gateway/fbpay/tokenize",
          "protocol": "https",
          "host": [
            "paid",
            "freshbooks",
            "com"
          ],
          "path": [
            "gateway",
            "fbpay",
            "tokenize"
          ]
        }
      },
      "status": "CREATED",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "Content-Length",
          "value": "26"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Date",
          "value": "Mon, 10 Aug 2020 18:48:08 GMT"
        },
        {
          "key": "Server",
          "value": "gunicorn/19.9.0"
        },
        {
          "key": "Access-Control-Allow-Methods",
          "value": "POST"
        },
        {
          "key": "Access-Control-Allow-Headers",
          "value": "Content-Type, X-CSRF-Token"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "None"
        },
        {
          "key": "X-Version",
          "value": "0.4.0"
        },
        {
          "key": "X-FB-Backend",
          "value": "U5"
        },
        {
          "key": "Content-Security-Policy",
          "value": "frame-ancestors https://*.freshbooks.com https://*.freshenv.com http://localhost:4200 https://*.billingarm.com"
        },
        {
          "key": "X-Cache",
          "value": "Miss from cloudfront"
        },
        {
          "key": "Via",
          "value": "1.1 98aedae6661e3904540676966998ed89.cloudfront.net (CloudFront)"
        },
        {
          "key": "X-Amz-Cf-Pop",
          "value": "SEA19-C2"
        },
        {
          "key": "X-Amz-Cf-Id",
          "value": "VkEJyM9NHIFX2cXfSiC6nVzflt1xmKXsN2kfTozCtT80XmkilZ-YcQ=="
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"cc_token\": \"1122334455\"\n}"
    },
    {
      "name": "1. [FBPAY] - Create Payment Method",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"cc_info\": {\n        \"name\": \"John Johnston\", \n        \"card_number\": \"4500123456789012\", \n        \"expiry_month\": \"10\", \n        \"expiry_year\": \"2024\",\n        \"email\": \"mjohnston@freshbooks.com\", \n        \"cvv\": \"123\",\n        \"postal_code\": \"M6P3T1\",\n        \"country\": \"CA\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://paid.freshbooks.com/gateway/fbpay/tokenize",
          "protocol": "https",
          "host": [
            "paid",
            "freshbooks",
            "com"
          ],
          "path": [
            "gateway",
            "fbpay",
            "tokenize"
          ]
        }
      },
      "status": "CREATED",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "Content-Length",
          "value": "26"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Date",
          "value": "Mon, 10 Aug 2020 18:48:08 GMT"
        },
        {
          "key": "Server",
          "value": "gunicorn/19.9.0"
        },
        {
          "key": "Access-Control-Allow-Methods",
          "value": "POST"
        },
        {
          "key": "Access-Control-Allow-Headers",
          "value": "Content-Type, X-CSRF-Token"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "None"
        },
        {
          "key": "X-Version",
          "value": "0.4.0"
        },
        {
          "key": "X-FB-Backend",
          "value": "U5"
        },
        {
          "key": "Content-Security-Policy",
          "value": "frame-ancestors https://*.freshbooks.com https://*.freshenv.com http://localhost:4200 https://*.billingarm.com"
        },
        {
          "key": "X-Cache",
          "value": "Miss from cloudfront"
        },
        {
          "key": "Via",
          "value": "1.1 98aedae6661e3904540676966998ed89.cloudfront.net (CloudFront)"
        },
        {
          "key": "X-Amz-Cf-Pop",
          "value": "SEA19-C2"
        },
        {
          "key": "X-Amz-Cf-Id",
          "value": "VkEJyM9NHIFX2cXfSiC6nVzflt1xmKXsN2kfTozCtT80XmkilZ-YcQ=="
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"cc_token\": \"11223344556677\"\n}"
    }
  ]
}