C# Requires Chilkat v11.0.0+
C#
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.
Chilkat C# Downloads
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.
Chilkat.Http http = new Chilkat.Http();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
http.SetUrlVar("id","INVOICE_ID");
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",resp);
if (success != true) {
Debug.WriteLine("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
Chilkat.JsonObject errorInfo = new Chilkat.JsonObject();
errorInfo.Load(resp.BodyStr);
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
Debug.WriteLine("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
return;
}
Chilkat.JsonObject result = new Chilkat.JsonObject();
bool parsedResult = result.Load(resp.BodyStr);
if (parsedResult != true) {
Debug.WriteLine("Expected a JSON response; HTTP " + Convert.ToString(resp.StatusCode));
return;
}
if (result.StringOfEquals("status","DRAFT",true) != true) {
Debug.WriteLine("Select an unsent draft.");
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
Chilkat.CkDateTime now = new Chilkat.CkDateTime();
now.SetFromCurrentSystemTime();
Chilkat.StringBuilder stamp = new Chilkat.StringBuilder();
stamp.Append(result.StringOf("detail.invoice_date"));
stamp.Append("T00:00:00Z");
Chilkat.CkDateTime issue = new Chilkat.CkDateTime();
bool validDate = issue.SetFromTimestamp(stamp.GetAsString());
if ((validDate != true) || (now.DiffSeconds(issue) < 0)) {
Debug.WriteLine("Select a draft with an issue date of today or earlier.");
return;
}
Chilkat.JsonObject body = new Chilkat.JsonObject();
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) {
Debug.WriteLine("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
Chilkat.JsonObject sendError = new Chilkat.JsonObject();
sendError.Load(resp.BodyStr);
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + " " + sendError.StringOf("name") + " " + sendError.StringOf("details[0].issue"));
Debug.WriteLine("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) {
Debug.WriteLine("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
Chilkat.JsonObject invoiceError = new Chilkat.JsonObject();
invoiceError.Load(resp.BodyStr);
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + " " + invoiceError.StringOf("name") + " " + invoiceError.StringOf("details[0].issue"));
Debug.WriteLine("PayPal debug ID: " + invoiceError.StringOf("debug_id"));
return;
}
Chilkat.JsonObject invoice = new Chilkat.JsonObject();
bool parsedInvoice = invoice.Load(resp.BodyStr);
if (parsedInvoice != true) {
Debug.WriteLine("Expected a JSON response; HTTP " + Convert.ToString(resp.StatusCode));
return;
}
if (((invoice.StringOfEquals("status","SENT",true) == true) || (invoice.StringOfEquals("status","UNPAID",true) == true) || (invoice.StringOfEquals("status","SHARED",true) == true)) && (invoice.HasMember("detail.metadata.recipient_view_url") == true)) {
Debug.WriteLine("Customer payment page: " + invoice.StringOf("detail.metadata.recipient_view_url"));
return;
}
Debug.WriteLine("Invoice is " + invoice.StringOf("status") + ". Retrieve it again when payable.");