DataFlex
DataFlex
PayPal - Update Invoice
See more PayPal Examples
Update a PayPal invoice.Note: This example requires Chilkat v9.5.0.65 or greater.
See also PayPal Update Invoice REST API Reference
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoJsonToken
Handle hoSbAuth
Handle hoRest
Boolean iBAutoReconnect
Handle hoJson
String sInvoiceId
Handle hoSbPath
Variant vSbJsonRequest
Handle hoSbJsonRequest
Variant vSbJsonResponse
Handle hoSbJsonResponse
Integer iNumItems
Integer i
String sTemp1
Integer iTemp1
Move False To iSuccess
// Note: Requires Chilkat v9.5.0.65 or greater.
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load our previously obtained access token. (see PayPal OAuth2 Access Token)
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
If (Not(IsComObjectCreated(hoJsonToken))) Begin
Send CreateComObject of hoJsonToken
End
Get ComLoadFile Of hoJsonToken "qa_data/tokens/paypal.json" To iSuccess
// Build the Authorization request header field value.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbAuth
If (Not(IsComObjectCreated(hoSbAuth))) Begin
Send CreateComObject of hoSbAuth
End
// token_type should be "Bearer"
Get ComStringOf Of hoJsonToken "token_type" To sTemp1
Get ComAppend Of hoSbAuth sTemp1 To iSuccess
Get ComAppend Of hoSbAuth " " To iSuccess
Get ComStringOf Of hoJsonToken "access_token" To sTemp1
Get ComAppend Of hoSbAuth sTemp1 To iSuccess
// Make the initial connection.
// A single REST object, once connected, can be used for many PayPal REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Move True To iBAutoReconnect
Get ComConnect Of hoRest "api.sandbox.paypal.com" 443 True iBAutoReconnect To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// ----------------------------------------------------------------------------------------------
// The code above this comment could be placed inside a function/subroutine within the application
// because the connection does not need to be made for every request. Once the connection is made
// the app may send many requests..
// ----------------------------------------------------------------------------------------------
// First create the JSON with updated information:
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Set ComEmitCompact Of hoJson To False
Get ComUpdateString Of hoJson "merchant_info.email" "smith-facilitator@chilkatsoft.com" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.first_name" "Joe" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.last_name" "Facilitator" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.business_name" "Medical Professionals, LLC" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.phone.country_code" "001" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.phone.national_number" "5032141716" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.address.line1" "1234 Main St." To iSuccess
Get ComUpdateString Of hoJson "merchant_info.address.city" "Portland" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.address.state" "OR" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.address.postal_code" "97217" To iSuccess
Get ComUpdateString Of hoJson "merchant_info.address.country_code" "US" To iSuccess
Get ComUpdateString Of hoJson "billing_info[0].email" "smith-buyer@chilkatsoft.com" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.first_name" "Sally" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.last_name" "Patient" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.business_name" "Not applicable" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.phone.country_code" "001" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.phone.national_number" "5039871234" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.address.line1" "1234 Broad St." To iSuccess
Get ComUpdateString Of hoJson "shipping_info.address.city" "Portland" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.address.state" "OR" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.address.postal_code" "97216" To iSuccess
Get ComUpdateString Of hoJson "shipping_info.address.country_code" "US" To iSuccess
Get ComUpdateString Of hoJson "items[0].name" "Sutures" To iSuccess
// We are updating the quantity from 100.0 to 200.0
// Note: The UpdateNumber method is added in v9.5.0.65
Get ComUpdateNumber Of hoJson "items[0].quantity" "200.0" To iSuccess
Get ComUpdateString Of hoJson "items[0].unit_price.currency" "USD" To iSuccess
Get ComUpdateString Of hoJson "items[0].unit_price.value" "5.00" To iSuccess
Get ComUpdateString Of hoJson "payment_term.term_type" "NET_45" To iSuccess
Get ComUpdateString Of hoJson "payment_term.due_date" "2016-12-30 PST" To iSuccess
Get ComUpdateString Of hoJson "note" "Medical Invoice 16 Jul, 2013 PST" To iSuccess
// The JSON created by the above code is this:
// {
// "merchant_info": {
// "email": "smith-facilitator@chilkatsoft.com",
// "first_name": "Joe",
// "last_name": "Facilitator",
// "business_name": "Medical Professionals, LLC",
// "phone": {
// "country_code": "001",
// "national_number": "5032141716"
// },
// "address": {
// "line1": "1234 Main St.",
// "city": "Portland",
// "state": "OR",
// "postal_code": "97217",
// "country_code": "US"
// }
// },
// "billing_info": [
// {
// "email": "smith-buyer@chilkatsoft.com"
// }
// ],
// "shipping_info": {
// "first_name": "Sally",
// "last_name": "Patient",
// "business_name": "Not applicable",
// "phone": {
// "country_code": "001",
// "national_number": "5039871234"
// },
// "address": {
// "line1": "1234 Broad St.",
// "city": "Portland",
// "state": "OR",
// "postal_code": "97216",
// "country_code": "US"
// }
// },
// "items": [
// {
// "name": "Sutures",
// "quantity": 200.0,
// "unit_price": {
// "currency": "USD",
// "value": "5.00"
// }
// }
// ],
// "payment_term": {
// "term_type": "NET_45",
// "due_date": "2016-12-30 PST"
// },
// "note": "Medical Invoice 16 Jul, 2013 PST"
// }
Get ComGetAsString Of hoSbAuth To sTemp1
Get ComAddHeader Of hoRest "Authorization" sTemp1 To iSuccess
Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess
Move "INV2-XV4B-736P-PLVN-SZCE" To sInvoiceId
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
If (Not(IsComObjectCreated(hoSbPath))) Begin
Send CreateComObject of hoSbPath
End
Get ComAppend Of hoSbPath "/v1/invoicing/invoices/" To iSuccess
Get ComAppend Of hoSbPath sInvoiceId To iSuccess
// Send the PUT request containign the JSON in the request body, and get the JSON response.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJsonRequest
If (Not(IsComObjectCreated(hoSbJsonRequest))) Begin
Send CreateComObject of hoSbJsonRequest
End
Get pvComObject of hoSbJsonRequest to vSbJsonRequest
Get ComEmitSb Of hoJson vSbJsonRequest To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJsonResponse
If (Not(IsComObjectCreated(hoSbJsonResponse))) Begin
Send CreateComObject of hoSbJsonResponse
End
Get ComGetAsString Of hoSbPath To sTemp1
Get pvComObject of hoSbJsonRequest to vSbJsonRequest
Get pvComObject of hoSbJsonResponse to vSbJsonResponse
Get ComFullRequestSb Of hoRest "PUT" sTemp1 vSbJsonRequest vSbJsonResponse To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbJsonResponse to vSbJsonResponse
Get ComLoadSb Of hoJson vSbJsonResponse To iSuccess
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "Response Status Code = " iTemp1
// Did we get a 200 success response?
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
Showln "Failed."
Procedure_Return
End
// Sample response JSON is shown below.
// Get some information..
Get ComStringOf Of hoJson "merchant_info.email" To sTemp1
Showln "email: " sTemp1
Get ComStringOf Of hoJson "merchant_info.business_name" To sTemp1
Showln "business_name: " sTemp1
Get ComSizeOfArray Of hoJson "items" To iNumItems
Move 0 To i
While (i < iNumItems)
Set ComI Of hoJson To i
Get ComStringOf Of hoJson "items[i].name" To sTemp1
Showln "item name: " sTemp1
Get ComStringOf Of hoJson "items[i].quantity" To sTemp1
Showln "item quantity: " sTemp1
Get ComStringOf Of hoJson "items[i].unit_price.currency" To sTemp1
Showln "item currency: " sTemp1
Get ComStringOf Of hoJson "items[i].unit_price.value" To sTemp1
Showln "item value: " sTemp1
Showln "----"
Move (i + 1) To i
Loop
Showln "Success."
End_Procedure