Sample code for 30+ languages & platforms
Android™ 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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean 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.
    CkHttp http = new CkHttp();
    http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
    CkHttpResponse resp = new CkHttpResponse();

    http.SetUrlVar("id","INVOICE_ID");

    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        Log.i(TAG, "No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
        CkJsonObject errorInfo = new CkJsonObject();
        errorInfo.Load(resp.bodyStr());
        Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
        Log.i(TAG, "PayPal debug ID: " + errorInfo.stringOf("debug_id"));
        return;
        }

    CkJsonObject result = new CkJsonObject();
    boolean parsedResult = result.Load(resp.bodyStr());
    if (parsedResult != true) {
        Log.i(TAG, "Expected a JSON response; HTTP " + String.valueOf(resp.get_StatusCode()));
        return;
        }

    if (result.StringOfEquals("status","DRAFT",true) != true) {
        Log.i(TAG, "Select an unsent draft.");
        return;
        }

    //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
    CkDateTime now = new CkDateTime();
    now.SetFromCurrentSystemTime();
    CkStringBuilder stamp = new CkStringBuilder();
    stamp.Append(result.stringOf("detail.invoice_date"));
    stamp.Append("T00:00:00Z");
    CkDateTime issue = new CkDateTime();
    boolean validDate = issue.SetFromTimestamp(stamp.getAsString());
    if ((validDate != true) or (now.DiffSeconds(issue) < 0)) {
        Log.i(TAG, "Select a draft with an issue date of today or earlier.");
        return;
        }

    CkJsonObject body = new CkJsonObject();
    body.UpdateBool("send_to_recipient",false);
    body.UpdateBool("send_to_invoicer",false);
    success = http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send",body,"application/json",resp);
    if (success != true) {
        Log.i(TAG, "No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
        CkJsonObject sendError = new CkJsonObject();
        sendError.Load(resp.bodyStr());
        Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + " " + sendError.stringOf("name") + " " + sendError.stringOf("details[0].issue"));
        Log.i(TAG, "PayPal debug ID: " + sendError.stringOf("debug_id"));
        return;
        }

    //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
    success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
    if (success != true) {
        Log.i(TAG, "No HTTP response: " + http.lastErrorText());
        return;
        }

    if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)) {
        CkJsonObject invoiceError = new CkJsonObject();
        invoiceError.Load(resp.bodyStr());
        Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + " " + invoiceError.stringOf("name") + " " + invoiceError.stringOf("details[0].issue"));
        Log.i(TAG, "PayPal debug ID: " + invoiceError.stringOf("debug_id"));
        return;
        }

    CkJsonObject invoice = new CkJsonObject();
    boolean parsedInvoice = invoice.Load(resp.bodyStr());
    if (parsedInvoice != true) {
        Log.i(TAG, "Expected a JSON response; HTTP " + String.valueOf(resp.get_StatusCode()));
        return;
        }

    if (((invoice.StringOfEquals("status","SENT",true) == true) or (invoice.StringOfEquals("status","UNPAID",true) == true) or (invoice.StringOfEquals("status","SHARED",true) == true)) and (invoice.HasMember("detail.metadata.recipient_view_url") == true)) {
        Log.i(TAG, "Customer payment page: " + invoice.stringOf("detail.metadata.recipient_view_url"));
        return;
        }

    Log.i(TAG, "Invoice is " + invoice.stringOf("status") + ". Retrieve it again when payable.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}