Tcl Stripe: Create a Card

Back to Index

Creates a new credit card belonging to a customer.

Documentation: https://stripe.com/docs/api/curl#create_card

CURL Command

curl https://api.stripe.com/v1/customers/cus_CBbg3iRMzWBjoe/sources \
   -u STRIPE_SECRET_KEY: \
   -d source=tok_amex \
   -X POST

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://api.stripe.com/v1/customers/cus_CBbg3iRMzWBjoe/sources
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "api.stripe.com" $port $bTls $bAutoReconnect]
if {[expr $success != 1]} then {
    puts "ConnectFailReason: [CkRest_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

CkRest_SetAuthBasic $rest "STRIPE_SECRET_KEY" ""

CkRest_AddQueryParam $rest "source" "tok_amex"

set strResponseBody [CkRest_fullRequestFormUrlEncoded $rest "POST" "/v1/customers/cus_CBbg3iRMzWBjoe/sources"]
if {[expr [CkRest_LastMethodSuccess $rest] != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

set jsonResponse [new_CkJsonObject]

CkJsonObject_Load $jsonResponse $strResponseBody

set id [CkJsonObject_stringOf $jsonResponse "id"]
set object [CkJsonObject_stringOf $jsonResponse "object"]
set address_city [CkJsonObject_IsNullOf $jsonResponse "address_city"]
set address_country [CkJsonObject_IsNullOf $jsonResponse "address_country"]
set address_line1 [CkJsonObject_IsNullOf $jsonResponse "address_line1"]
set address_line1_check [CkJsonObject_IsNullOf $jsonResponse "address_line1_check"]
set address_line2 [CkJsonObject_IsNullOf $jsonResponse "address_line2"]
set address_state [CkJsonObject_IsNullOf $jsonResponse "address_state"]
set address_zip [CkJsonObject_IsNullOf $jsonResponse "address_zip"]
set address_zip_check [CkJsonObject_IsNullOf $jsonResponse "address_zip_check"]
set brand [CkJsonObject_stringOf $jsonResponse "brand"]
set country [CkJsonObject_stringOf $jsonResponse "country"]
set customer [CkJsonObject_stringOf $jsonResponse "customer"]
set cvc_check [CkJsonObject_IsNullOf $jsonResponse "cvc_check"]
set dynamic_last4 [CkJsonObject_IsNullOf $jsonResponse "dynamic_last4"]
set exp_month [CkJsonObject_IntOf $jsonResponse "exp_month"]
set exp_year [CkJsonObject_IntOf $jsonResponse "exp_year"]
set fingerprint [CkJsonObject_stringOf $jsonResponse "fingerprint"]
set funding [CkJsonObject_stringOf $jsonResponse "funding"]
set last4 [CkJsonObject_stringOf $jsonResponse "last4"]
set name [CkJsonObject_IsNullOf $jsonResponse "name"]
set tokenization_method [CkJsonObject_IsNullOf $jsonResponse "tokenization_method"]

delete_CkRest $rest
delete_CkJsonObject $jsonResponse

Sample JSON Response Body

{
  "id": "card_1BnETKGswQrCoh0Xhu1A6BfL",
  "object": "card",
  "address_city": null,
  "address_country": null,
  "address_line1": null,
  "address_line1_check": null,
  "address_line2": null,
  "address_state": null,
  "address_zip": null,
  "address_zip_check": null,
  "brand": "Visa",
  "country": "US",
  "customer": "cus_CBbg3iRMzWBjoe",
  "cvc_check": null,
  "dynamic_last4": null,
  "exp_month": 8,
  "exp_year": 2019,
  "fingerprint": "F9mANtIt1TaukpRJ",
  "funding": "credit",
  "last4": "4242",
  "metadata": {},
  "name": null,
  "tokenization_method": null
}