Chilkat Online Tools

C / New FreshBooks / 2. [FBPAY] - Create Setup Intent Using Payment Method Key

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    int intVal;
    const char *id;
    const char *gateway_name;
    const char *Credit_card_uuid;
    int Card_holder_user_id;
    const char *Saved_to_system_id;
    const char *Card_type;
    const char *Expiry_year;
    const char *Expiry_month;
    const char *Last_four_digits;
    const char *Card_holder_name;
    BOOL System;
    BOOL Client;
    int i;
    int count_i;

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

    http = CkHttp_Create();

    // 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_card": {
    //     "saved_to_system_id": "yDDAb0",
    //     "card_holder_user_id": 146211,
    //     "access": {
    //       "system": false,
    //       "client": false,
    //       "invoice_profiles": [
    //         33
    //       ]
    //     },
    //     "credit_card_tokens": [
    //       {
    //         "token": "token_123",
    //         "gateway_name": "fbpay",
    //         "is_primary": true
    //       }
    //     ]
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"credit_card.saved_to_system_id","yDDAb0");
    CkJsonObject_UpdateInt(json,"credit_card.card_holder_user_id",146211);
    CkJsonObject_UpdateBool(json,"credit_card.access.system",FALSE);
    CkJsonObject_UpdateBool(json,"credit_card.access.client",FALSE);
    CkJsonObject_UpdateInt(json,"credit_card.access.invoice_profiles[0]",33);
    CkJsonObject_UpdateString(json,"credit_card.credit_card_tokens[0].token","token_123");
    CkJsonObject_UpdateString(json,"credit_card.credit_card_tokens[0].gateway_name","fbpay");
    CkJsonObject_UpdateBool(json,"credit_card.credit_card_tokens[0].is_primary",TRUE);

    // Adds the "Authorization: Bearer <access_token>" header.
    CkHttp_putAuthToken(http,"<access_token>");

    resp = CkHttp_PostJson3(http,"https://api.freshbooks.com/payments/account/{{accountid}}/credit-card","application/json",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "credit_card": {
    //     "credit_card_uuid": "7c8d965555554d40ddde59sadasdc7180asda7ba",
    //     "card_holder_user_id": 123456,
    //     "saved_to_system_id": "E86Qd",
    //     "card_type": "visa",
    //     "expiry_year": "2024",
    //     "expiry_month": "9",
    //     "last_four_digits": "0234",
    //     "card_holder_name": "John Johnston",
    //     "access": {
    //       "system": true,
    //       "client": false,
    //       "invoice_profiles": [
    //         121212
    //       ]
    //     },
    //     "credit_card_tokens": [
    //       {
    //         "id": "fbpay+akdjhadkjashckJNCksajncasc",
    //         "gateway_name": "fbpay"
    //       }
    //     ]
    //   }
    // }

    // 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.

    Credit_card_uuid = CkJsonObject_stringOf(jResp,"credit_card.credit_card_uuid");
    Card_holder_user_id = CkJsonObject_IntOf(jResp,"credit_card.card_holder_user_id");
    Saved_to_system_id = CkJsonObject_stringOf(jResp,"credit_card.saved_to_system_id");
    Card_type = CkJsonObject_stringOf(jResp,"credit_card.card_type");
    Expiry_year = CkJsonObject_stringOf(jResp,"credit_card.expiry_year");
    Expiry_month = CkJsonObject_stringOf(jResp,"credit_card.expiry_month");
    Last_four_digits = CkJsonObject_stringOf(jResp,"credit_card.last_four_digits");
    Card_holder_name = CkJsonObject_stringOf(jResp,"credit_card.card_holder_name");
    System = CkJsonObject_BoolOf(jResp,"credit_card.access.system");
    Client = CkJsonObject_BoolOf(jResp,"credit_card.access.client");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"credit_card.access.invoice_profiles");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        intVal = CkJsonObject_IntOf(jResp,"credit_card.access.invoice_profiles[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"credit_card.credit_card_tokens");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"credit_card.credit_card_tokens[i].id");
        gateway_name = CkJsonObject_stringOf(jResp,"credit_card.credit_card_tokens[i].gateway_name");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "credit_card": {
        "saved_to_system_id": "yDDAb0",
        "card_holder_user_id": 146211,
        "access": {
            "system": false,
            "client": false,
            "invoice_profiles": [
                33
            ]
        },
        "credit_card_tokens": [
            {
                "token": "token_123",
                "gateway_name": "fbpay",
                "is_primary": true
            }
        ]
    }
}'
https://api.freshbooks.com/payments/account/{{accountid}}/credit-card

Postman Collection Item JSON

{
  "name": "2. [FBPAY] - Create Setup Intent Using Payment Method Key",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"credit_card\": {\n        \"saved_to_system_id\": \"yDDAb0\",\n        \"card_holder_user_id\": 146211,\n        \"access\": {\n            \"system\": false,\n            \"client\": false,\n            \"invoice_profiles\": [\n                33\n            ]\n        },\n        \"credit_card_tokens\": [\n            {\n                \"token\": \"token_123\",\n                \"gateway_name\": \"fbpay\",\n                \"is_primary\": true\n            }\n        ]\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.freshbooks.com/payments/account/{{accountid}}/credit-card",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "payments",
        "account",
        "{{accountid}}",
        "credit-card"
      ]
    }
  },
  "response": [
    {
      "name": "2. [FBPAY] - Create Setup Intent Using Payment Method Key",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"credit_card\": {\n        \"saved_to_system_id\": \"E86Qp\",\n        \"card_holder_user_id\": 590529,\n        \"access\": {\n            \"system\": true,\n            \"client\": false,\n            \"invoice_profiles\": [\n                26627\n            ]\n        },\n        \"credit_card_tokens\": [\n            {\n                \"token\": \"1111111\",\n                \"gateway_name\": \"fbpay\",\n                \"is_primary\": true\n            }\n        ]\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.freshbooks.com/payments/account/E86Qp/credit-card",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "payments",
            "account",
            "E86Qp",
            "credit-card"
          ]
        }
      },
      "status": "CREATED",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Date",
          "value": "Fri, 31 Jul 2020 20:33:18 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "Content-Length",
          "value": "438"
        },
        {
          "key": "X-CardApp-Version",
          "value": "2007.68.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABV1FUDwYGV1QTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FAkpXURQRTEVURAkRQ1VWEUgAEFUGURZnW1VAV0tLBEIORhAHUw0CFAZcWkQHABBTQ1IVC0REUAdET1IcURZQCQEBBgBcClIJVwtWVA4bVVNTFBEFVVJRUwBUX1MHAgAAVFFQEk5eA1RLUW8="
        },
        {
          "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": "598"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1596227656"
        },
        {
          "key": "Retry-After",
          "value": "57"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "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\""
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"credit_card\": {\n        \"credit_card_uuid\": \"7c8d965555554d40ddde59sadasdc7180asda7ba\",\n        \"card_holder_user_id\": 123456,\n        \"saved_to_system_id\": \"E86Qd\",\n        \"card_type\": \"visa\",\n        \"expiry_year\": \"2024\",\n        \"expiry_month\": \"9\",\n        \"last_four_digits\": \"0234\",\n        \"card_holder_name\": \"John Johnston\",\n        \"access\": {\n            \"system\": true,\n            \"client\": false,\n            \"invoice_profiles\": [121212]\n        },\n        \"credit_card_tokens\": [\n            {\n                \"id\": \"fbpay+akdjhadkjashckJNCksajncasc\",\n                \"gateway_name\": \"fbpay\"\n            }\n        ]\n    }\n}"
    }
  ]
}