Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

PayPal: Revise a draft invoice from its full billing record

See more PayPal Examples

PUT replaces the invoice. This example revises the quantity on the simple two-inspection draft to three. Rebuild the complete editable document from your billing record; omitted items, terms or recipients can be lost. Do not use this body for an unrelated invoice.

PayPal documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, "SANDBOX_ACCESS_TOKEN")
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Use only the ID of the matching draft from the create example, while it is still a
    ;  draft. Preserve all of your real draft fields, including any custom invoice number.
    CkHttp::ckSetUrlVar(http,"id","INVOICE_ID")
    CkHttp::ckSetRequestHeader(http,"Prefer","return=representation")
    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"detail.currency_code","USD")
    CkJsonObject::ckUpdateString(body,"detail.payment_term.term_type","NET_30")
    CkJsonObject::ckUpdateString(body,"primary_recipients[0].billing_info.email_address","buyer@example.com")
    CkJsonObject::ckUpdateString(body,"items[0].name","Equipment inspection")
    CkJsonObject::ckUpdateString(body,"items[0].quantity","3")
    CkJsonObject::ckUpdateString(body,"items[0].unit_amount.currency_code","USD")
    CkJsonObject::ckUpdateString(body,"items[0].unit_amount.value","45.00")

    success = CkHttp::ckHttpJson(http,"PUT","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",body,"application/json",resp)
    If success <> 1
        Debug "No HTTP response: " + CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        ProcedureReturn
    EndIf

    If (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
        errorInfo.i = CkJsonObject::ckCreate()
        If errorInfo.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        CkJsonObject::ckLoad(errorInfo,CkHttpResponse::ckBodyStr(resp))
        Debug "HTTP " + Str(CkHttpResponse::ckStatusCode(resp)) + " " + CkJsonObject::ckStringOf(errorInfo,"name") + " " + CkJsonObject::ckStringOf(errorInfo,"details[0].issue")
        Debug "PayPal debug ID: " + CkJsonObject::ckStringOf(errorInfo,"debug_id")
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(errorInfo)
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    parsed.i = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
    If parsed <> 1
        Debug "Expected a JSON response; HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    Debug "ID: " + CkJsonObject::ckStringOf(result,"id")
    Debug "Status: " + CkJsonObject::ckStringOf(result,"status")
    Debug "New total: " + CkJsonObject::ckStringOf(result,"amount.value")
    Debug "Currency: " + CkJsonObject::ckStringOf(result,"amount.currency_code")


    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)
    CkJsonObject::ckDispose(body)
    CkJsonObject::ckDispose(errorInfo)
    CkJsonObject::ckDispose(result)


    ProcedureReturn
EndProcedure