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

PayPal: Start a PayPal wallet checkout

See more PayPal Examples

Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.

Create a USD 30 digital order from a trusted cart. Store the order ID and redirect the buyer to the payer-action link. After approval, capture this same order from your merchant application.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkCrypt2W.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool 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.
    CkHttpW http;
    http.put_AuthToken(L"SANDBOX_ACCESS_TOKEN");
    CkHttpResponseW resp;

    //  Replace the two callback URLs with routes on your site. This order contains no
    //  physical shipment.
    //  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.
    CkCrypt2W crypt;
    const wchar_t *requestId = crypt.generateUuid();
    http.SetRequestHeader(L"PayPal-Request-Id",requestId);
    http.SetRequestHeader(L"Prefer",L"return=representation");
    CkJsonObjectW body;
    body.UpdateString(L"intent",L"CAPTURE");
    body.UpdateString(L"purchase_units[0].amount.currency_code",L"USD");
    body.UpdateString(L"purchase_units[0].amount.value",L"30.00");
    body.UpdateString(L"payment_source.paypal.experience_context.return_url",L"https://merchant.example/paypal/return");
    body.UpdateString(L"payment_source.paypal.experience_context.cancel_url",L"https://merchant.example/paypal/cancel");
    body.UpdateString(L"payment_source.paypal.experience_context.shipping_preference",L"NO_SHIPPING");

    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/checkout/orders",body,L"application/json",resp);
    if (success != true) {
        wprintf(L"No HTTP response: %s\n",http.lastErrorText());
        return;
    }

    if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        CkJsonObjectW errorInfo;
        errorInfo.Load(resp.bodyStr());
        wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),errorInfo.stringOf(L"name"),errorInfo.stringOf(L"details[0].issue"));
        wprintf(L"PayPal debug ID: %s\n",errorInfo.stringOf(L"debug_id"));
        return;
    }

    CkJsonObjectW result;
    bool parsed = result.Load(resp.bodyStr());
    if (parsed != true) {
        wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
        return;
    }

    wprintf(L"ID: %s\n",result.stringOf(L"id"));
    wprintf(L"Status: %s\n",result.stringOf(L"status"));
    //  Link order is not fixed. Send the buyer to the matching URL in a browser.
    int linkIndex = 0;
    while ((linkIndex < result.SizeOfArray(L"links"))) {
        result.put_I(linkIndex);
        if (result.StringOfEquals(L"links[i].rel",L"payer-action",true)) {
            wprintf(L"Approval URL: %s\n",result.stringOf(L"links[i].href"));
        }

        linkIndex = linkIndex + 1;
    }
    }