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

PayPal: Capture a buyer-approved order

See more PayPal Examples

After approval, match the order to the checkout in your merchant records. Read each capture ID and status. Fulfill only when the expected amount and currency are captured and the capture is COMPLETED.

PayPal documentation

Chilkat Tcl Downloads

Tcl

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]

#  Use the order ID you stored before redirecting the buyer, associated with this cart
#  and session.

#  Use the approved order created with intent CAPTURE.
CkHttp_SetUrlVar $http "id" "ORDER_ID"
#  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
CkHttp_SetRequestHeader $http "Prefer" "return=representation"
set body [new_CkJsonObject]

set success [CkHttp_HttpJson $http "POST" "https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/capture" $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 parsed [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$parsed != 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 "ID: [CkJsonObject_stringOf $result id]"
puts "Status: [CkJsonObject_stringOf $result status]"
#  A purchase unit may have no captures yet, or more than one.
set i 0
while {$i < [CkJsonObject_SizeOfArray $result "purchase_units"]} {
    CkJsonObject_put_I $result $i
    set j 0
    while {$j < [CkJsonObject_SizeOfArray $result "purchase_units[i].payments.captures"]} {
        CkJsonObject_put_J $result $j
        puts "Unit: [CkJsonObject_stringOf $result {purchase_units[i].reference_id}]  capture: [CkJsonObject_stringOf $result {purchase_units[i].payments.captures[j].id}]"
        puts "  [CkJsonObject_stringOf $result {purchase_units[i].payments.captures[j].status}]  [CkJsonObject_stringOf $result {purchase_units[i].payments.captures[j].amount.value}] [CkJsonObject_stringOf $result {purchase_units[i].payments.captures[j].amount.currency_code}]"
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}
#  When a buyer opted to save a card, the token may arrive separately from capture.
if {CkJsonObject_StringOfEquals $result "payment_source.card.attributes.vault.status" "VAULTED" 1} then {
    set cardToken [CkJsonObject_stringOf $result "payment_source.card.attributes.vault.id"]
    set customerId [CkJsonObject_stringOf $result "payment_source.card.attributes.vault.customer.id"]
    #  Store cardToken and customerId with this buyer in the merchant credential store.
}

if {CkJsonObject_StringOfEquals $result "payment_source.card.attributes.vault.status" "APPROVED" 1} then {
    puts "Await the verified VAULT.PAYMENT-TOKEN.CREATED webhook for the token."
}


delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkCrypt2 $crypt
delete_CkJsonObject $body
delete_CkJsonObject $errorInfo
delete_CkJsonObject $result