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 <CkRest.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    CkRest rest;
    bool success;

    //  URL: https://api.stripe.com/v1/charges
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect("api.stripe.com",port,bTls,bAutoReconnect);
    if (success != true) {
        std::cout << "ConnectFailReason: " << rest.get_ConnectFailReason() << "\r\n";
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    rest.SetAuthBasic("STRIPE_SECRET_KEY","");

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

    const char *strResponseBody = rest.fullRequestFormUrlEncoded("POST","/v1/charges");
    if (rest.get_LastMethodSuccess() != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    CkJsonObject jsonResponse;
    jsonResponse.Load(strResponseBody);

    const char *id = 0;
    const char *object = 0;
    int amount;
    int amount_refunded;
    bool application;
    bool application_fee;
    const char *balance_transaction = 0;
    bool captured;
    int created;
    const char *currency = 0;
    bool customer;
    const char *description = 0;
    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 = 0;
    bool refundsHas_more;
    int refundsTotal_count;
    const char *refundsUrl = 0;
    bool review;
    bool shipping;
    const char *sourceId = 0;
    const char *sourceObject = 0;
    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 = 0;
    const char *sourceCountry = 0;
    bool sourceCustomer;
    bool sourceCvc_check;
    bool sourceDynamic_last4;
    int sourceExp_month;
    int sourceExp_year;
    const char *sourceFingerprint = 0;
    const char *sourceFunding = 0;
    const char *sourceLast4 = 0;
    bool sourceName;
    bool sourceTokenization_method;
    bool source_transfer;
    bool statement_descriptor;
    const char *status = 0;
    bool transfer_group;
    int i;
    int count_i;

    id = jsonResponse.stringOf("id");
    object = jsonResponse.stringOf("object");
    amount = jsonResponse.IntOf("amount");
    amount_refunded = jsonResponse.IntOf("amount_refunded");
    application = jsonResponse.IsNullOf("application");
    application_fee = jsonResponse.IsNullOf("application_fee");
    balance_transaction = jsonResponse.stringOf("balance_transaction");
    captured = jsonResponse.BoolOf("captured");
    created = jsonResponse.IntOf("created");
    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");
    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");
    sourceExp_year = jsonResponse.IntOf("source.exp_year");
    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");
    while (i < count_i) {
        jsonResponse.put_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
}