PHP Requires Chilkat v11.0.0+
PHP
PayPal: Update the price on a reusable payment link
See more PayPal Examples
Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
// 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 = new Http();
$http->AuthToken = 'SANDBOX_ACCESS_TOKEN';
$resp = new HttpResponse();
$http->SetUrlVar('id', 'PAYMENT_RESOURCE_ID');
try {
$http->HttpNoBody('GET', 'https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}', $resp);
} catch (\Chilkat2\Exception $e) {
echo 'No HTTP response: ', $e->getMessage(), "\n";
exit;
}
if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
$errorInfo = new JsonObject();
$errorInfo->Load($resp->BodyStr);
echo 'HTTP ', $resp->StatusCode, ' ', $errorInfo->StringOf('name'), ' ', $errorInfo->StringOf('details[0].issue'), "\n";
echo 'PayPal debug ID: ', $errorInfo->StringOf('debug_id'), "\n";
exit;
}
$result = new JsonObject();
try {
$result->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
exit;
}
if (($result->SizeOfArray('line_items') != 1) || ((!$result->StringOfEquals('line_items[0].name', 'Annual inspection', true)) || (!$result->StringOfEquals('line_items[0].unit_amount.currency_code', 'USD', true)))) {
echo "Select the USD inspection link from your catalog.\n";
exit;
}
// Preserve line items, taxes, shipping and checkout settings from the retrieved link.
// Remove the resource metadata PayPal added to the GET response.
if ($result->HasMember('id')) {
$result->Delete('id');
}
if ($result->HasMember('create_time')) {
$result->Delete('create_time');
}
if ($result->HasMember('update_time')) {
$result->Delete('update_time');
}
if ($result->HasMember('status')) {
$result->Delete('status');
}
if ($result->HasMember('links')) {
$result->Delete('links');
}
if ($result->HasMember('payment_link')) {
$result->Delete('payment_link');
}
$result->UpdateString('line_items[0].unit_amount.value', '95.00');
try {
$http->HttpJson('PUT', 'https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}', $result, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
echo 'No HTTP response: ', $e->getMessage(), "\n";
exit;
}
if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
$updateError = new JsonObject();
$updateError->Load($resp->BodyStr);
echo 'HTTP ', $resp->StatusCode, ' ', $updateError->StringOf('name'), ' ', $updateError->StringOf('details[0].issue'), "\n";
echo 'PayPal debug ID: ', $updateError->StringOf('debug_id'), "\n";
exit;
}
echo "The inspection price is now USD 95.00.\n";