Tcl Requires Chilkat v11.0.0+
Tcl
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 Tcl Downloads
load ./chilkat.dll
set 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.
set http [new_CkHttp]
CkHttp_put_AuthToken $http "SANDBOX_ACCESS_TOKEN"
set resp [new_CkHttpResponse]
# 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.
set crypt [new_CkCrypt2]
set requestId [CkCrypt2_generateUuid $crypt]
CkHttp_SetRequestHeader $http "PayPal-Request-Id" $requestId
set body [new_CkJsonObject]
CkJsonObject_UpdateString $body "integration_mode" "LINK"
CkJsonObject_UpdateString $body "type" "BUY_NOW"
CkJsonObject_UpdateString $body "reusable" "MULTIPLE"
CkJsonObject_UpdateString $body "return_url" "https://merchant.example/thank-you"
CkJsonObject_UpdateString $body "line_items[0].name" "Annual inspection"
CkJsonObject_UpdateString $body "line_items[0].unit_amount.currency_code" "USD"
CkJsonObject_UpdateString $body "line_items[0].unit_amount.value" "90.00"
set success [CkHttp_HttpJson $http "POST" "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources" $body "application/json" $resp]
if {$success != 1} then {
puts "No HTTP response: [CkHttp_lastErrorText $http]"
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkCrypt2 $crypt
delete_CkJsonObject $body
exit
}
if {expr [[CkHttpResponse_get_StatusCode $resp] < 200] || [[CkHttpResponse_get_StatusCode $resp] >= 300]} then {
set errorInfo [new_CkJsonObject]
CkJsonObject_Load $errorInfo [CkHttpResponse_bodyStr $resp]
puts "HTTP [CkHttpResponse_get_StatusCode $resp] [CkJsonObject_stringOf $errorInfo name] [CkJsonObject_stringOf $errorInfo {details[0].issue}]"
puts "PayPal debug ID: [CkJsonObject_stringOf $errorInfo debug_id]"
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkCrypt2 $crypt
delete_CkJsonObject $body
delete_CkJsonObject $errorInfo
exit
}
set result [new_CkJsonObject]
set parsedResult [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$parsedResult != 1} then {
puts "Expected a JSON response; HTTP [CkHttpResponse_get_StatusCode $resp]"
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkCrypt2 $crypt
delete_CkJsonObject $body
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result
exit
}
puts "Catalog payment resource ID: [CkJsonObject_stringOf $result id]"
# Link order is not fixed. Find the customer-facing link to copy into an email.
set linkIndex 0
while {$linkIndex < [CkJsonObject_SizeOfArray $result "links"]} {
CkJsonObject_put_I $result $linkIndex
if {CkJsonObject_StringOfEquals $result "links[i].rel" "payment_link" 1} then {
puts "Share this payment link: [CkJsonObject_stringOf $result {links[i].href}]"
}
set linkIndex [expr $linkIndex + 1]
}
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkCrypt2 $crypt
delete_CkJsonObject $body
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result