PureBasic Requires Chilkat v11.0.0+
PureBasic
PayPal: Create a reusable payment link for a service
See more PayPal Examples
Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.
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
; Replace the thank-you URL with your business website. The same link can be shared
; with several customers.
; 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)
body.i = CkJsonObject::ckCreate()
If body.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(body,"integration_mode","LINK")
CkJsonObject::ckUpdateString(body,"type","BUY_NOW")
CkJsonObject::ckUpdateString(body,"reusable","MULTIPLE")
CkJsonObject::ckUpdateString(body,"return_url","https://merchant.example/thank-you")
CkJsonObject::ckUpdateString(body,"line_items[0].name","Annual inspection")
CkJsonObject::ckUpdateString(body,"line_items[0].unit_amount.currency_code","USD")
CkJsonObject::ckUpdateString(body,"line_items[0].unit_amount.value","90.00")
success = CkHttp::ckHttpJson(http,"POST","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources",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
parsedResult.i = CkJsonObject::ckLoad(result,CkHttpResponse::ckBodyStr(resp))
If parsedResult <> 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 "Catalog payment resource ID: " + CkJsonObject::ckStringOf(result,"id")
; Link order is not fixed. Find the customer-facing link to copy into an email.
linkIndex.i = 0
While (linkIndex < CkJsonObject::ckSizeOfArray(result,"links"))
CkJsonObject::setCkI(result, linkIndex)
If CkJsonObject::ckStringOfEquals(result,"links[i].rel","payment_link",1)
Debug "Share this payment link: " + CkJsonObject::ckStringOf(result,"links[i].href")
EndIf
linkIndex = linkIndex + 1
Wend
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkCrypt2::ckDispose(crypt)
CkJsonObject::ckDispose(body)
CkJsonObject::ckDispose(errorInfo)
CkJsonObject::ckDispose(result)
ProcedureReturn
EndProcedure