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

PayPal: Pay with a previously saved card

See more PayPal Examples

Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.

When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.

PayPal documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Crypt
string ls_RequestId
oleobject loo_Body
oleobject loo_ErrorInfo
oleobject loo_Result
long li_Parsed
long li_LinkIndex
long i
long j

li_Success = 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.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.AuthToken = "SANDBOX_ACCESS_TOKEN"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

//  Resolve the saved token under this signed-in buyer. A vault ID from a different
//  customer must never be accepted from the browser.

//  Create and persist this key once per business operation. On a timeout,
//  retry the same request with its saved key, not a newly generated one.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

ls_RequestId = loo_Crypt.GenerateUuid()
loo_Http.SetRequestHeader("PayPal-Request-Id",ls_RequestId)
loo_Http.SetRequestHeader("Prefer","return=representation")
loo_Body = create oleobject
li_rc = loo_Body.ConnectToNewObject("Chilkat.JsonObject")

loo_Body.UpdateString("intent","CAPTURE")
loo_Body.UpdateString("purchase_units[0].amount.currency_code","USD")
loo_Body.UpdateString("purchase_units[0].amount.value","30.00")
loo_Body.UpdateString("payment_source.card.vault_id","SAVED_CARD_TOKEN")
loo_Body.UpdateString("payment_source.card.stored_credential.payment_initiator","CUSTOMER")
loo_Body.UpdateString("payment_source.card.stored_credential.payment_type","ONE_TIME")
loo_Body.UpdateString("payment_source.card.stored_credential.usage","SUBSEQUENT")
loo_Body.UpdateString("payment_source.card.experience_context.return_url","https://merchant.example/paypal/return")
loo_Body.UpdateString("payment_source.card.experience_context.cancel_url","https://merchant.example/paypal/cancel")

li_Success = loo_Http.HttpJson("POST","https://api-m.sandbox.paypal.com/v2/checkout/orders",loo_Body,"application/json",loo_Resp)
if li_Success <> 1 then
    Write-Debug "No HTTP response: " + loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Crypt
    destroy loo_Body
    return
end if

if (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    loo_ErrorInfo = create oleobject
    li_rc = loo_ErrorInfo.ConnectToNewObject("Chilkat.JsonObject")

    loo_ErrorInfo.Load(loo_Resp.BodyStr)
    Write-Debug "HTTP " + string(loo_Resp.StatusCode) + " " + loo_ErrorInfo.StringOf("name") + " " + loo_ErrorInfo.StringOf("details[0].issue")
    Write-Debug "PayPal debug ID: " + loo_ErrorInfo.StringOf("debug_id")
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Crypt
    destroy loo_Body
    destroy loo_ErrorInfo
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Result.Load(loo_Resp.BodyStr)
if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response; HTTP " + string(loo_Resp.StatusCode)
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Crypt
    destroy loo_Body
    destroy loo_ErrorInfo
    destroy loo_Result
    return
end if

Write-Debug "ID: " + loo_Result.StringOf("id")
Write-Debug "Status: " + loo_Result.StringOf("status")
//  Link order is not fixed. Send the buyer to the matching URL in a browser.
li_LinkIndex = 0
do while (li_LinkIndex < loo_Result.SizeOfArray("links"))
    loo_Result.I = li_LinkIndex
    if loo_Result.StringOfEquals("links[i].rel","payer-action",1) then
        Write-Debug "Approval URL: " + loo_Result.StringOf("links[i].href")
    end if

    li_LinkIndex = li_LinkIndex + 1
loop
//  A purchase unit may have no captures yet, or more than one.
i = 0
do while (i < loo_Result.SizeOfArray("purchase_units"))
    loo_Result.I = i
    j = 0
    do while (j < loo_Result.SizeOfArray("purchase_units[i].payments.captures"))
        loo_Result.J = j
        Write-Debug "Unit: " + loo_Result.StringOf("purchase_units[i].reference_id") + "  capture: " + loo_Result.StringOf("purchase_units[i].payments.captures[j].id")
        Write-Debug "  " + loo_Result.StringOf("purchase_units[i].payments.captures[j].status") + "  " + loo_Result.StringOf("purchase_units[i].payments.captures[j].amount.value") + " " + loo_Result.StringOf("purchase_units[i].payments.captures[j].amount.currency_code")
        j = j + 1
    loop
    i = i + 1
loop


destroy loo_Http
destroy loo_Resp
destroy loo_Crypt
destroy loo_Body
destroy loo_ErrorInfo
destroy loo_Result