C 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"

C Example

#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    const char *strResponseBody;
    HCkJsonObject jsonResponse;
    const char *id;
    const char *object;
    int amount;
    int amount_refunded;
    BOOL application;
    BOOL application_fee;
    const char *balance_transaction;
    BOOL captured;
    int created;
    const char *currency;
    BOOL customer;
    const char *description;
    BOOL destination;
    BOOL dispute;
    BOOL failure_code;
    BOOL failure_message;
    BOOL invoice;
    BOOL livemode;
    BOOL on_behalf_of;
    BOOL order;
    BOOL outcome;
    BOOL paid;
    BOOL receipt_email;
    BOOL receipt_number;
    BOOL refunded;
    const char *refundsObject;
    BOOL refundsHas_more;
    int refundsTotal_count;
    const char *refundsUrl;
    BOOL review;
    BOOL shipping;
    const char *sourceId;
    const char *sourceObject;
    BOOL sourceAddress_city;
    BOOL sourceAddress_country;
    BOOL sourceAddress_line1;
    BOOL sourceAddress_line1_check;
    BOOL sourceAddress_line2;
    BOOL sourceAddress_state;
    BOOL sourceAddress_zip;
    BOOL sourceAddress_zip_check;
    const char *sourceBrand;
    const char *sourceCountry;
    BOOL sourceCustomer;
    BOOL sourceCvc_check;
    BOOL sourceDynamic_last4;
    int sourceExp_month;
    int sourceExp_year;
    const char *sourceFingerprint;
    const char *sourceFunding;
    const char *sourceLast4;
    BOOL sourceName;
    BOOL sourceTokenization_method;
    BOOL source_transfer;
    BOOL statement_descriptor;
    const char *status;
    BOOL transfer_group;
    int i;
    int count_i;

    rest = CkRest_Create();

    //  URL: https://api.stripe.com/v1/charges
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"api.stripe.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    CkRest_SetAuthBasic(rest,"STRIPE_SECRET_KEY","");

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

    strResponseBody = CkRest_fullRequestFormUrlEncoded(rest,"POST","/v1/charges");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    jsonResponse = CkJsonObject_Create();
    CkJsonObject_Load(jsonResponse,strResponseBody);

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



    CkRest_Dispose(rest);
    CkJsonObject_Dispose(jsonResponse);

    }

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
}