Swift Requires Chilkat v11.0.0+
Swift
PayPal: Create an itemized draft invoice
See more PayPal Examples
Bill a sandbox recipient for two inspections at USD 45 each, due in 30 days. Request the invoice representation and save its ID for sending. Creating the draft does not notify the customer.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = 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.
let http = CkoHttp()!
http.authToken = "SANDBOX_ACCESS_TOKEN"
let resp = CkoHttpResponse()!
// Replace buyer@example.com with your sandbox buyer. PayPal uses the authenticated
// merchant as the invoicer.
http.setRequestHeader(name: "Prefer", value: "return=representation")
let body = CkoJsonObject()!
body.updateString(jsonPath: "detail.currency_code", value: "USD")
body.updateString(jsonPath: "detail.payment_term.term_type", value: "NET_30")
body.updateString(jsonPath: "primary_recipients[0].billing_info.email_address", value: "buyer@example.com")
body.updateString(jsonPath: "items[0].name", value: "Equipment inspection")
body.updateString(jsonPath: "items[0].quantity", value: "2")
body.updateString(jsonPath: "items[0].unit_amount.currency_code", value: "USD")
body.updateString(jsonPath: "items[0].unit_amount.value", value: "45.00")
success = http.httpJson(verb: "POST", url: "https://api-m.sandbox.paypal.com/v2/invoicing/invoices", json: body, contentType: "application/json", response: resp)
if success != true {
print("No HTTP response: \(http.lastErrorText!)")
return
}
if (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
let errorInfo = CkoJsonObject()!
errorInfo.load(json: resp.bodyStr)
print("HTTP \(resp.statusCode.intValue) \(errorInfo.string(of: "name")!) \(errorInfo.string(of: "details[0].issue")!)")
print("PayPal debug ID: \(errorInfo.string(of: "debug_id")!)")
return
}
let result = CkoJsonObject()!
var parsed: Bool = result.load(json: resp.bodyStr)
if parsed != true {
print("Expected a JSON response; HTTP \(resp.statusCode.intValue)")
return
}
print("ID: \(result.string(of: "id")!)")
print("Status: \(result.string(of: "status")!)")
print("Invoice number: \(result.string(of: "detail.invoice_number")!)")
print("Total: \(result.string(of: "amount.value")!)")
}