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

PayPal: Record a bank transfer against an invoice

See more PayPal Examples

Record USD 45 already received by bank transfer. The invoice may still have a balance. This makes a bookkeeping entry; it does not debit the customer. Save payment_id for reconciliation.

PayPal documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.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;

    //  Record only a bank transfer you have already received. Reconcile first if a previous
    //  attempt timed out.

    http.SetUrlVar(L"id",L"INVOICE_ID");
    CkJsonObjectW body;
    body.UpdateString(L"method",L"BANK_TRANSFER");
    body.UpdateString(L"amount.currency_code",L"USD");
    body.UpdateString(L"amount.value",L"45.00");

    //  Use the actual date of the transfer; this example records one made today.
    CkDateTimeW date;
    date.SetFromCurrentSystemTime();
    body.UpdateString(L"payment_date",date.getAsIso8601(L"YYYY-MM-DD",false));

    success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments",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"Record ID: %s\n",result.stringOf(L"payment_id"));
    }