PureBasic Requires Chilkat v11.0.0+
PureBasic
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.
Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkCrypt2.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
; 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.
crypt.i = CkCrypt2::ckCreate()
If crypt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
requestId.s = CkCrypt2::ckGenerateUuid(crypt)
CkHttp::ckSetRequestHeader(http,"PayPal-Request-Id",requestId)
CkHttp::ckSetRequestHeader(http,"Prefer","return=representation")
body.i = CkJsonObject::ckCreate()
If body.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(body,"intent","CAPTURE")
CkJsonObject::ckUpdateString(body,"purchase_units[0].amount.currency_code","USD")
CkJsonObject::ckUpdateString(body,"purchase_units[0].amount.value","30.00")
CkJsonObject::ckUpdateString(body,"payment_source.card.vault_id","SAVED_CARD_TOKEN")
CkJsonObject::ckUpdateString(body,"payment_source.card.stored_credential.payment_initiator","CUSTOMER")
CkJsonObject::ckUpdateString(body,"payment_source.card.stored_credential.payment_type","ONE_TIME")
CkJsonObject::ckUpdateString(body,"payment_source.card.stored_credential.usage","SUBSEQUENT")
CkJsonObject::ckUpdateString(body,"payment_source.card.experience_context.return_url","https://merchant.example/paypal/return")
CkJsonObject::ckUpdateString(body,"payment_source.card.experience_context.cancel_url","https://merchant.example/paypal/cancel")
success = CkHttp::ckHttpJson(http,"POST","https://api-m.sandbox.paypal.com/v2/checkout/orders",body,"application/json",resp)
If success <> 1
Debug "No HTTP response: " + CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkCrypt2::ckDispose(crypt)
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)
CkCrypt2::ckDispose(crypt)
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)
CkCrypt2::ckDispose(crypt)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(errorInfo)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndIf
Debug "ID: " + CkJsonObject::ckStringOf(result,"id")
Debug "Status: " + CkJsonObject::ckStringOf(result,"status")
; Link order is not fixed. Send the buyer to the matching URL in a browser.
linkIndex.i = 0
While (linkIndex < CkJsonObject::ckSizeOfArray(result,"links"))
CkJsonObject::setCkI(result, linkIndex)
If CkJsonObject::ckStringOfEquals(result,"links[i].rel","payer-action",1)
Debug "Approval URL: " + CkJsonObject::ckStringOf(result,"links[i].href")
EndIf
linkIndex = linkIndex + 1
Wend
; A purchase unit may have no captures yet, or more than one.
i.i = 0
While (i < CkJsonObject::ckSizeOfArray(result,"purchase_units"))
CkJsonObject::setCkI(result, i)
j.i = 0
While (j < CkJsonObject::ckSizeOfArray(result,"purchase_units[i].payments.captures"))
CkJsonObject::setCkJ(result, j)
Debug "Unit: " + CkJsonObject::ckStringOf(result,"purchase_units[i].reference_id") + " capture: " + CkJsonObject::ckStringOf(result,"purchase_units[i].payments.captures[j].id")
Debug " " + CkJsonObject::ckStringOf(result,"purchase_units[i].payments.captures[j].status") + " " + CkJsonObject::ckStringOf(result,"purchase_units[i].payments.captures[j].amount.value") + " " + CkJsonObject::ckStringOf(result,"purchase_units[i].payments.captures[j].amount.currency_code")
j = j + 1
Wend
i = i + 1
Wend
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkCrypt2::ckDispose(crypt)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(errorInfo)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndProcedure