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

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

PayPal documentation

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
#include <C_CkDateTime.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkHttpResponse resp;
    HCkJsonObject errorInfo;
    HCkJsonObject result;
    BOOL parsedResult;
    HCkDateTime now;
    HCkStringBuilder stamp;
    HCkDateTime issue;
    BOOL validDate;
    HCkJsonObject body;
    HCkJsonObject sendError;
    HCkJsonObject invoiceError;
    HCkJsonObject invoice;
    BOOL parsedInvoice;

    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 = CkHttp_Create();
    CkHttp_putAuthToken(http,"SANDBOX_ACCESS_TOKEN");
    resp = CkHttpResponse_Create();

    CkHttp_SetUrlVar(http,"id","INVOICE_ID");

    success = CkHttp_HttpNoBody(http,"GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != TRUE) {
        printf("No HTTP response: %s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    if ((CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
        errorInfo = CkJsonObject_Create();
        CkJsonObject_Load(errorInfo,CkHttpResponse_bodyStr(resp));
        printf("HTTP %d %s %s\n",CkHttpResponse_getStatusCode(resp),CkJsonObject_stringOf(errorInfo,"name"),CkJsonObject_stringOf(errorInfo,"details[0].issue"));
        printf("PayPal debug ID: %s\n",CkJsonObject_stringOf(errorInfo,"debug_id"));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        return;
    }

    result = CkJsonObject_Create();
    parsedResult = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
    if (parsedResult != TRUE) {
        printf("Expected a JSON response; HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        return;
    }

    if (CkJsonObject_StringOfEquals(result,"status","DRAFT",TRUE) != TRUE) {
        printf("Select an unsent draft.\n");
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        return;
    }

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    now = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(now);
    stamp = CkStringBuilder_Create();
    CkStringBuilder_Append(stamp,CkJsonObject_stringOf(result,"detail.invoice_date"));
    CkStringBuilder_Append(stamp,"T00:00:00Z");
    issue = CkDateTime_Create();
    validDate = CkDateTime_SetFromTimestamp(issue,CkStringBuilder_getAsString(stamp));
    if ((validDate != TRUE) || (CkDateTime_DiffSeconds(now,issue) < 0)) {
        printf("Select a draft with an issue date of today or earlier.\n");
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        return;
    }

    body = CkJsonObject_Create();
    CkJsonObject_UpdateBool(body,"send_to_recipient",FALSE);
    CkJsonObject_UpdateBool(body,"send_to_invoicer",FALSE);
    success = CkHttp_HttpJson(http,"POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp);
    if (success != TRUE) {
        printf("No HTTP response: %s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        return;
    }

    if ((CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
        sendError = CkJsonObject_Create();
        CkJsonObject_Load(sendError,CkHttpResponse_bodyStr(resp));
        printf("HTTP %d %s %s\n",CkHttpResponse_getStatusCode(resp),CkJsonObject_stringOf(sendError,"name"),CkJsonObject_stringOf(sendError,"details[0].issue"));
        printf("PayPal debug ID: %s\n",CkJsonObject_stringOf(sendError,"debug_id"));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        CkJsonObject_Dispose(sendError);
        return;
    }

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    success = CkHttp_HttpNoBody(http,"GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != TRUE) {
        printf("No HTTP response: %s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        CkJsonObject_Dispose(sendError);
        return;
    }

    if ((CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
        invoiceError = CkJsonObject_Create();
        CkJsonObject_Load(invoiceError,CkHttpResponse_bodyStr(resp));
        printf("HTTP %d %s %s\n",CkHttpResponse_getStatusCode(resp),CkJsonObject_stringOf(invoiceError,"name"),CkJsonObject_stringOf(invoiceError,"details[0].issue"));
        printf("PayPal debug ID: %s\n",CkJsonObject_stringOf(invoiceError,"debug_id"));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        CkJsonObject_Dispose(sendError);
        CkJsonObject_Dispose(invoiceError);
        return;
    }

    invoice = CkJsonObject_Create();
    parsedInvoice = CkJsonObject_Load(invoice,CkHttpResponse_bodyStr(resp));
    if (parsedInvoice != TRUE) {
        printf("Expected a JSON response; HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        CkJsonObject_Dispose(sendError);
        CkJsonObject_Dispose(invoiceError);
        CkJsonObject_Dispose(invoice);
        return;
    }

    if (((CkJsonObject_StringOfEquals(invoice,"status","SENT",TRUE) == TRUE) || (CkJsonObject_StringOfEquals(invoice,"status","UNPAID",TRUE) == TRUE) || (CkJsonObject_StringOfEquals(invoice,"status","SHARED",TRUE) == TRUE)) && (CkJsonObject_HasMember(invoice,"detail.metadata.recipient_view_url") == TRUE)) {
        printf("Customer payment page: %s\n",CkJsonObject_stringOf(invoice,"detail.metadata.recipient_view_url"));
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(errorInfo);
        CkJsonObject_Dispose(result);
        CkDateTime_Dispose(now);
        CkStringBuilder_Dispose(stamp);
        CkDateTime_Dispose(issue);
        CkJsonObject_Dispose(body);
        CkJsonObject_Dispose(sendError);
        CkJsonObject_Dispose(invoiceError);
        CkJsonObject_Dispose(invoice);
        return;
    }

    printf("Invoice is %s. Retrieve it again when payable.\n",CkJsonObject_stringOf(invoice,"status"));


    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(errorInfo);
    CkJsonObject_Dispose(result);
    CkDateTime_Dispose(now);
    CkStringBuilder_Dispose(stamp);
    CkDateTime_Dispose(issue);
    CkJsonObject_Dispose(body);
    CkJsonObject_Dispose(sendError);
    CkJsonObject_Dispose(invoiceError);
    CkJsonObject_Dispose(invoice);

    }