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

PayPal: Record an external refund against an invoice

See more PayPal Examples

Record USD 5 already returned by bank transfer for a paid invoice. This call does not move money. To refund a PayPal capture, use the Payments v2 capture refund example instead.

PayPal documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.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

    ;  Record only money already returned outside PayPal. Reconcile first if a previous
    ;  attempt timed out.

    CkHttp::ckSetUrlVar(http,"id","INVOICE_ID")
    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(body,"method","BANK_TRANSFER")
    CkJsonObject::ckUpdateString(body,"amount.currency_code","USD")
    CkJsonObject::ckUpdateString(body,"amount.value","5.00")

    ;  Use the actual date of the transfer; this example records one made today.
    date.i = CkDateTime::ckCreate()
    If date.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(date)
    CkJsonObject::ckUpdateString(body,"refund_date",CkDateTime::ckGetAsIso8601(date,"YYYY-MM-DD",0))

    success = CkHttp::ckHttpJson(http,"POST","https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/refunds",body,"application/json",resp)
    If success <> 1
        Debug "No HTTP response: " + CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        CkJsonObject::ckDispose(body)
        CkDateTime::ckDispose(date)
        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)
        CkDateTime::ckDispose(date)
        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)
        CkDateTime::ckDispose(date)
        CkJsonObject::ckDispose(errorInfo)
        CkJsonObject::ckDispose(result)
        ProcedureReturn
    EndIf

    Debug "Record ID: " + CkJsonObject::ckStringOf(result,"refund_id")


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


    ProcedureReturn
EndProcedure