Sample code for 30+ languages & platforms
C Requires Chilkat v11.7.0+

Stripe: Identify the payment for a paid invoice

See more Stripe Examples

List the payments allocated to an invoice and identify a paid charge or PaymentIntent for refund.

Stripe documentation and cURL example

Chilkat C Downloads

C
#include <C_CkHttpCurl.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *apiKey;
    HCkHttpCurl curl;
    HCkStringBuilder cmd;
    HCkJsonObject json;
    int replaced;
    const char *invoiceId;
    HCkStringBuilder pageSuffix;
    BOOL more;
    const char *cursor;
    int i;
    int count;
    const char *next;

    success = FALSE;

    //  Requires an unlocked Chilkat bundle. See Global Unlock Sample.
    //  Supply a restricted sandbox key from merchant-controlled configuration.
    apiKey = "YOUR_STRIPE_TEST_KEY";

    curl = CkHttpCurl_Create();
    cmd = CkStringBuilder_Create();
    json = CkJsonObject_Create();

    invoiceId = "in_FROM_BILLING_RECORD";

    pageSuffix = CkStringBuilder_Create();
    more = TRUE;
    cursor = "";
    while ((more == TRUE)) {
        //  curl -X GET "https://api.stripe.com/v1/invoice_payments?invoice={invoiceId}&limit=100" \
        //    -u "$STRIPE_API_KEY:" \
        //    -H "Stripe-Version: 2026-10-28.endive"
        CkStringBuilder_SetString(cmd,"curl -X GET \"https://api.stripe.com/v1/invoice_payments?invoice=@ID@&limit=100@CURSOR@\"");
        CkStringBuilder_Append(cmd," -u \"@KEY@:\"");
        CkStringBuilder_Append(cmd," -H \"Stripe-Version: 2026-10-28.endive\"");
        replaced = CkStringBuilder_Replace(cmd,"@CURSOR@",CkStringBuilder_getAsString(pageSuffix));
        replaced = CkStringBuilder_Replace(cmd,"@ID@",invoiceId);
        replaced = CkStringBuilder_Replace(cmd,"@KEY@",apiKey);
        success = CkHttpCurl_DoYourThing(curl,CkStringBuilder_getAsString(cmd));
        if (success != TRUE) {
            printf("No HTTP response. Chilkat failure code: %d\n",CkHttpCurl_getFailReason(curl));
            CkHttpCurl_Dispose(curl);
            CkStringBuilder_Dispose(cmd);
            CkJsonObject_Dispose(json);
            CkStringBuilder_Dispose(pageSuffix);
            return;
        }

        success = CkHttpCurl_GetResponseJson(curl,json);
        if ((CkHttpCurl_getStatusCode(curl) != 200) || (success != TRUE)) {
            printf("Stripe HTTP %d  Request-Id: %s\n",CkHttpCurl_getStatusCode(curl),CkHttpCurl_getResponseHeaderField(curl,"Request-Id"));
            if (success == TRUE) {
                printf("%s\n",CkJsonObject_stringOf(json,"error.message"));
            }

            CkHttpCurl_Dispose(curl);
            CkStringBuilder_Dispose(cmd);
            CkJsonObject_Dispose(json);
            CkStringBuilder_Dispose(pageSuffix);
            return;
        }

        i = 0;
        count = CkJsonObject_SizeOfArray(json,"data");
        while ((i < count)) {
            CkJsonObject_putI(json,i);
            printf("%s | %s | %s | %s | %s\n",CkJsonObject_stringOf(json,"data[i].id"),CkJsonObject_stringOf(json,"data[i].status"),CkJsonObject_stringOf(json,"data[i].amount_paid")
                ,CkJsonObject_stringOf(json,"data[i].currency"),CkJsonObject_stringOf(json,"data[i].payment.type"));
            if (CkJsonObject_stringOf(json,"data[i].payment.type") == "payment_intent") {
                printf("  PaymentIntent: %s\n",CkJsonObject_stringOf(json,"data[i].payment.payment_intent"));
            }

            if (CkJsonObject_stringOf(json,"data[i].payment.type") == "charge") {
                printf("  Charge: %s\n",CkJsonObject_stringOf(json,"data[i].payment.charge"));
            }

            i = i + 1;
        }

        more = CkJsonObject_BoolOf(json,"has_more");
        if (more == TRUE) {
            next = CkJsonObject_stringOf(json,"data[i].id");
            if ((count < 1) || (next == "") || (next == cursor)) {
                printf("Invalid pagination cursor.\n");
                CkHttpCurl_Dispose(curl);
                CkStringBuilder_Dispose(cmd);
                CkJsonObject_Dispose(json);
                CkStringBuilder_Dispose(pageSuffix);
                return;
            }

            cursor = next;
            CkStringBuilder_SetString(pageSuffix,"&starting_after=");
            CkStringBuilder_Append(pageSuffix,cursor);
        }

    }

    //  Select the paid allocation belonging to the order before issuing its refund.


    CkHttpCurl_Dispose(curl);
    CkStringBuilder_Dispose(cmd);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(pageSuffix);

    }