Swift Stripe: Create a Charge

Back to Index

To charge a credit card, you create a Charge object. If your API key is in test mode, the supplied payment source (e.g., card) won't actually be charged, though everything else will occur as if in live mode. (Stripe assumes that the charge would have completed successfully).

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

CURL Command

curl -X POST  https://api.stripe.com/v1/charges \
   -u STRIPE_SECRET_KEY: \
   -d amount=2000 \
   -d currency=usd \
   -d source=tok_visa \
   -d description="Charge for aiden.jones@example.com"

Swift Example


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

    //  URL: https://api.stripe.com/v1/charges
    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: "")

    rest.AddQueryParam("amount", value: "2000")
    rest.AddQueryParam("currency", value: "usd")
    rest.AddQueryParam("source", value: "tok_visa")
    rest.AddQueryParam("description", value: "Charge for aiden.jones@example.com")

    var strResponseBody: String? = rest.FullRequestFormUrlEncoded("POST", uriPath: "/v1/charges")
    if rest.LastMethodSuccess != true {
        print("\(rest.LastErrorText)")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.Load(strResponseBody)

    var id: String?
    var object: String?
    var amount: Int
    var amount_refunded: Int
    var application: Bool
    var application_fee: Bool
    var balance_transaction: String?
    var captured: Bool
    var created: Int
    var currency: String?
    var customer: Bool
    var description: String?
    var destination: Bool
    var dispute: Bool
    var failure_code: Bool
    var failure_message: Bool
    var invoice: Bool
    var livemode: Bool
    var on_behalf_of: Bool
    var order: Bool
    var outcome: Bool
    var paid: Bool
    var receipt_email: Bool
    var receipt_number: Bool
    var refunded: Bool
    var refundsObject: String?
    var refundsHas_more: Bool
    var refundsTotal_count: Int
    var refundsUrl: String?
    var review: Bool
    var shipping: Bool
    var sourceId: String?
    var sourceObject: String?
    var sourceAddress_city: Bool
    var sourceAddress_country: Bool
    var sourceAddress_line1: Bool
    var sourceAddress_line1_check: Bool
    var sourceAddress_line2: Bool
    var sourceAddress_state: Bool
    var sourceAddress_zip: Bool
    var sourceAddress_zip_check: Bool
    var sourceBrand: String?
    var sourceCountry: String?
    var sourceCustomer: Bool
    var sourceCvc_check: Bool
    var sourceDynamic_last4: Bool
    var sourceExp_month: Int
    var sourceExp_year: Int
    var sourceFingerprint: String?
    var sourceFunding: String?
    var sourceLast4: String?
    var sourceName: Bool
    var sourceTokenization_method: Bool
    var source_transfer: Bool
    var statement_descriptor: Bool
    var status: String?
    var transfer_group: Bool
    var i: Int
    var count_i: Int

    id = jsonResponse.StringOf("id")
    object = jsonResponse.StringOf("object")
    amount = jsonResponse.IntOf("amount").integerValue
    amount_refunded = jsonResponse.IntOf("amount_refunded").integerValue
    application = jsonResponse.IsNullOf("application")
    application_fee = jsonResponse.IsNullOf("application_fee")
    balance_transaction = jsonResponse.StringOf("balance_transaction")
    captured = jsonResponse.BoolOf("captured")
    created = jsonResponse.IntOf("created").integerValue
    currency = jsonResponse.StringOf("currency")
    customer = jsonResponse.IsNullOf("customer")
    description = jsonResponse.StringOf("description")
    destination = jsonResponse.IsNullOf("destination")
    dispute = jsonResponse.IsNullOf("dispute")
    failure_code = jsonResponse.IsNullOf("failure_code")
    failure_message = jsonResponse.IsNullOf("failure_message")
    invoice = jsonResponse.IsNullOf("invoice")
    livemode = jsonResponse.BoolOf("livemode")
    on_behalf_of = jsonResponse.IsNullOf("on_behalf_of")
    order = jsonResponse.IsNullOf("order")
    outcome = jsonResponse.IsNullOf("outcome")
    paid = jsonResponse.BoolOf("paid")
    receipt_email = jsonResponse.IsNullOf("receipt_email")
    receipt_number = jsonResponse.IsNullOf("receipt_number")
    refunded = jsonResponse.BoolOf("refunded")
    refundsObject = jsonResponse.StringOf("refunds.object")
    refundsHas_more = jsonResponse.BoolOf("refunds.has_more")
    refundsTotal_count = jsonResponse.IntOf("refunds.total_count").integerValue
    refundsUrl = jsonResponse.StringOf("refunds.url")
    review = jsonResponse.IsNullOf("review")
    shipping = jsonResponse.IsNullOf("shipping")
    sourceId = jsonResponse.StringOf("source.id")
    sourceObject = jsonResponse.StringOf("source.object")
    sourceAddress_city = jsonResponse.IsNullOf("source.address_city")
    sourceAddress_country = jsonResponse.IsNullOf("source.address_country")
    sourceAddress_line1 = jsonResponse.IsNullOf("source.address_line1")
    sourceAddress_line1_check = jsonResponse.IsNullOf("source.address_line1_check")
    sourceAddress_line2 = jsonResponse.IsNullOf("source.address_line2")
    sourceAddress_state = jsonResponse.IsNullOf("source.address_state")
    sourceAddress_zip = jsonResponse.IsNullOf("source.address_zip")
    sourceAddress_zip_check = jsonResponse.IsNullOf("source.address_zip_check")
    sourceBrand = jsonResponse.StringOf("source.brand")
    sourceCountry = jsonResponse.StringOf("source.country")
    sourceCustomer = jsonResponse.IsNullOf("source.customer")
    sourceCvc_check = jsonResponse.IsNullOf("source.cvc_check")
    sourceDynamic_last4 = jsonResponse.IsNullOf("source.dynamic_last4")
    sourceExp_month = jsonResponse.IntOf("source.exp_month").integerValue
    sourceExp_year = jsonResponse.IntOf("source.exp_year").integerValue
    sourceFingerprint = jsonResponse.StringOf("source.fingerprint")
    sourceFunding = jsonResponse.StringOf("source.funding")
    sourceLast4 = jsonResponse.StringOf("source.last4")
    sourceName = jsonResponse.IsNullOf("source.name")
    sourceTokenization_method = jsonResponse.IsNullOf("source.tokenization_method")
    source_transfer = jsonResponse.IsNullOf("source_transfer")
    statement_descriptor = jsonResponse.IsNullOf("statement_descriptor")
    status = jsonResponse.StringOf("status")
    transfer_group = jsonResponse.IsNullOf("transfer_group")
    i = 0
    count_i = jsonResponse.SizeOfArray("refunds.data").integerValue
    while i < count_i {
        jsonResponse.I = i
        i = i + 1
    }


}

Sample JSON Response Body

{
  "id": "ch_1BnETJGswQrCoh0XTs0EERBj",
  "object": "charge",
  "amount": 100,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "balance_transaction": "txn_1BnETJGswQrCoh0XxO2tGYr7",
  "captured": false,
  "created": 1516662781,
  "currency": "usd",
  "customer": null,
  "description": "My First Test Charge (created for API docs)",
  "destination": null,
  "dispute": null,
  "failure_code": null,
  "failure_message": null,
  "fraud_details": {},
  "invoice": null,
  "livemode": false,
  "metadata": {},
  "on_behalf_of": null,
  "order": null,
  "outcome": null,
  "paid": true,
  "receipt_email": null,
  "receipt_number": null,
  "refunded": false,
  "refunds": {
    "object": "list",
    "data": [
    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/charges/ch_1BnETJGswQrCoh0XTs0EERBj/refunds"
  },
  "review": null,
  "shipping": null,
  "source": {
    "id": "card_18ropuGswQrCoh0XjCJ5Zvma",
    "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": null,
    "cvc_check": null,
    "dynamic_last4": null,
    "exp_month": 8,
    "exp_year": 2017,
    "fingerprint": "F9mANtIt1TaukpRJ",
    "funding": "credit",
    "last4": "4242",
    "metadata": {},
    "name": null,
    "tokenization_method": null
  },
  "source_transfer": null,
  "statement_descriptor": null,
  "status": "succeeded",
  "transfer_group": null
}