Swift Stripe: Retrieve a Customer

Back to Index

Retrieves the details of an existing customer. You need only supply the unique customer identifier that was returned upon customer creation.

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

CURL Command

curl https://api.stripe.com/v1/customers/cus_CBbgVLJqv487Oq \
   -u STRIPE_SECRET_KEY:

Swift Example


func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //  URL: https://api.stripe.com/v1/customers/cus_CBbgVLJqv487Oq
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.Connect("api.stripe.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.integerValue)")
        print("\(rest.LastErrorText)")
        return
    }

    rest.SetAuthBasic("STRIPE_SECRET_KEY", password: "")

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/v1/customers/cus_CBbgVLJqv487Oq", sb: sbResponseBody)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.LoadSb(sbResponseBody)

    var id: String?
    var object: String?
    var account_balance: Int
    var created: Int
    var currency: String?
    var default_source: Bool
    var delinquent: Bool
    var description: Bool
    var discount: Bool
    var email: Bool
    var livemode: Bool
    var shipping: Bool
    var sourcesObject: String?
    var sourcesHas_more: Bool
    var sourcesTotal_count: Int
    var sourcesUrl: String?
    var subscriptionsObject: String?
    var subscriptionsHas_more: Bool
    var subscriptionsTotal_count: Int
    var subscriptionsUrl: String?
    var i: Int
    var count_i: Int

    id = jsonResponse.StringOf("id")
    object = jsonResponse.StringOf("object")
    account_balance = jsonResponse.IntOf("account_balance").integerValue
    created = jsonResponse.IntOf("created").integerValue
    currency = jsonResponse.StringOf("currency")
    default_source = jsonResponse.IsNullOf("default_source")
    delinquent = jsonResponse.BoolOf("delinquent")
    description = jsonResponse.IsNullOf("description")
    discount = jsonResponse.IsNullOf("discount")
    email = jsonResponse.IsNullOf("email")
    livemode = jsonResponse.BoolOf("livemode")
    shipping = jsonResponse.IsNullOf("shipping")
    sourcesObject = jsonResponse.StringOf("sources.object")
    sourcesHas_more = jsonResponse.BoolOf("sources.has_more")
    sourcesTotal_count = jsonResponse.IntOf("sources.total_count").integerValue
    sourcesUrl = jsonResponse.StringOf("sources.url")
    subscriptionsObject = jsonResponse.StringOf("subscriptions.object")
    subscriptionsHas_more = jsonResponse.BoolOf("subscriptions.has_more")
    subscriptionsTotal_count = jsonResponse.IntOf("subscriptions.total_count").integerValue
    subscriptionsUrl = jsonResponse.StringOf("subscriptions.url")
    i = 0
    count_i = jsonResponse.SizeOfArray("sources.data").integerValue
    while i < count_i {
        jsonResponse.I = i
        i = i + 1
    }

    i = 0
    count_i = jsonResponse.SizeOfArray("subscriptions.data").integerValue
    while i < count_i {
        jsonResponse.I = i
        i = i + 1
    }


}

Sample JSON Response Body

{
  "id": "cus_CBbgVLJqv487Oq",
  "object": "customer",
  "account_balance": 0,
  "created": 1516662781,
  "currency": "usd",
  "default_source": null,
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "livemode": false,
  "metadata": {},
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [
    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_CBbgVLJqv487Oq/sources"
  },
  "subscriptions": {
    "object": "list",
    "data": [
    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_CBbgVLJqv487Oq/subscriptions"
  }
}