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

Ruby
require 'chilkat'

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

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

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

if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
    errorInfo = Chilkat::CkJsonObject.new()
    errorInfo.Load(resp.bodyStr())
    print "HTTP " + resp.get_StatusCode().to_s() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue") + "\n";
    print "PayPal debug ID: " + errorInfo.stringOf("debug_id") + "\n";
    exit
end

result = Chilkat::CkJsonObject.new()
parsedResult = result.Load(resp.bodyStr())
if (parsedResult != true)
    print "Expected a JSON response; HTTP " + resp.get_StatusCode().to_s() + "\n";
    exit
end

if (result.StringOfEquals("status","DRAFT",true) != true)
    print "Select an unsent draft." + "\n";
    exit
end

#  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
now = Chilkat::CkDateTime.new()
now.SetFromCurrentSystemTime()
stamp = Chilkat::CkStringBuilder.new()
stamp.Append(result.stringOf("detail.invoice_date"))
stamp.Append("T00:00:00Z")
issue = Chilkat::CkDateTime.new()
validDate = issue.SetFromTimestamp(stamp.getAsString())
if ((validDate != true) or (now.DiffSeconds(issue) < 0))
    print "Select a draft with an issue date of today or earlier." + "\n";
    exit
end

body = Chilkat::CkJsonObject.new()
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)
    print "No HTTP response: " + http.lastErrorText() + "\n";
    exit
end

if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
    sendError = Chilkat::CkJsonObject.new()
    sendError.Load(resp.bodyStr())
    print "HTTP " + resp.get_StatusCode().to_s() + " " + sendError.stringOf("name") + " " + sendError.stringOf("details[0].issue") + "\n";
    print "PayPal debug ID: " + sendError.stringOf("debug_id") + "\n";
    exit
end

#  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)
    print "No HTTP response: " + http.lastErrorText() + "\n";
    exit
end

if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
    invoiceError = Chilkat::CkJsonObject.new()
    invoiceError.Load(resp.bodyStr())
    print "HTTP " + resp.get_StatusCode().to_s() + " " + invoiceError.stringOf("name") + " " + invoiceError.stringOf("details[0].issue") + "\n";
    print "PayPal debug ID: " + invoiceError.stringOf("debug_id") + "\n";
    exit
end

invoice = Chilkat::CkJsonObject.new()
parsedInvoice = invoice.Load(resp.bodyStr())
if (parsedInvoice != true)
    print "Expected a JSON response; HTTP " + resp.get_StatusCode().to_s() + "\n";
    exit
end

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))
    print "Customer payment page: " + invoice.stringOf("detail.metadata.recipient_view_url") + "\n";
    exit
end

print "Invoice is " + invoice.stringOf("status") + ". Retrieve it again when payable." + "\n";