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

PayPal: Create a reusable payment link for a service

See more PayPal Examples

Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.

PayPal documentation

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkCrypt2W.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkCrypt2W crypt;
    const wchar_t *requestId;
    HCkJsonObjectW body;
    HCkJsonObjectW errorInfo;
    HCkJsonObjectW result;
    BOOL parsedResult;
    int linkIndex;

    success = FALSE;

    //  See Unlock Chilkat before making requests.
    //  For the merchant's internal backoffice application. Load the sandbox token
    //  from the merchant's credential store. Keep it out of publicly distributed apps.
    //  See Get a PayPal access token.
    http = CkHttpW_Create();
    CkHttpW_putAuthToken(http,L"SANDBOX_ACCESS_TOKEN");
    resp = CkHttpResponseW_Create();

    //  Replace the thank-you URL with your business website. The same link can be shared
    //  with several customers.
    //  Create and persist this key once per business operation. On a timeout,
    //  retry the same request with its saved key, not a newly generated one.
    crypt = CkCrypt2W_Create();
    requestId = CkCrypt2W_generateUuid(crypt);
    CkHttpW_SetRequestHeader(http,L"PayPal-Request-Id",requestId);
    body = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(body,L"integration_mode",L"LINK");
    CkJsonObjectW_UpdateString(body,L"type",L"BUY_NOW");
    CkJsonObjectW_UpdateString(body,L"reusable",L"MULTIPLE");
    CkJsonObjectW_UpdateString(body,L"return_url",L"https://merchant.example/thank-you");
    CkJsonObjectW_UpdateString(body,L"line_items[0].name",L"Annual inspection");
    CkJsonObjectW_UpdateString(body,L"line_items[0].unit_amount.currency_code",L"USD");
    CkJsonObjectW_UpdateString(body,L"line_items[0].unit_amount.value",L"90.00");

    success = CkHttpW_HttpJson(http,L"POST",L"https://api-m.sandbox.paypal.com/v1/checkout/payment-resources",body,L"application/json",resp);
    if (success != TRUE) {
        wprintf(L"No HTTP response: %s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkCrypt2W_Dispose(crypt);
        CkJsonObjectW_Dispose(body);
        return;
    }

    if ((CkHttpResponseW_getStatusCode(resp) < 200) || (CkHttpResponseW_getStatusCode(resp) >= 300)) {
        errorInfo = CkJsonObjectW_Create();
        CkJsonObjectW_Load(errorInfo,CkHttpResponseW_bodyStr(resp));
        wprintf(L"HTTP %d %s %s\n",CkHttpResponseW_getStatusCode(resp),CkJsonObjectW_stringOf(errorInfo,L"name"),CkJsonObjectW_stringOf(errorInfo,L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",CkJsonObjectW_stringOf(errorInfo,L"debug_id"));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkCrypt2W_Dispose(crypt);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(errorInfo);
        return;
    }

    result = CkJsonObjectW_Create();
    parsedResult = CkJsonObjectW_Load(result,CkHttpResponseW_bodyStr(resp));
    if (parsedResult != TRUE) {
        wprintf(L"Expected a JSON response; HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkCrypt2W_Dispose(crypt);
        CkJsonObjectW_Dispose(body);
        CkJsonObjectW_Dispose(errorInfo);
        CkJsonObjectW_Dispose(result);
        return;
    }

    wprintf(L"Catalog payment resource ID: %s\n",CkJsonObjectW_stringOf(result,L"id"));
    //  Link order is not fixed. Find the customer-facing link to copy into an email.
    linkIndex = 0;
    while ((linkIndex < CkJsonObjectW_SizeOfArray(result,L"links"))) {
        CkJsonObjectW_putI(result,linkIndex);
        if (CkJsonObjectW_StringOfEquals(result,L"links[i].rel",L"payment_link",TRUE)) {
            wprintf(L"Share this payment link: %s\n",CkJsonObjectW_stringOf(result,L"links[i].href"));
        }

        linkIndex = linkIndex + 1;
    }



    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkCrypt2W_Dispose(crypt);
    CkJsonObjectW_Dispose(body);
    CkJsonObjectW_Dispose(errorInfo);
    CkJsonObjectW_Dispose(result);

    }