C++ Requires Chilkat v11.0.0+
C++
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 C++ Downloads
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// 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.
CkHttp http;
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse resp;
http.SetUrlVar("id","PAYMENT_RESOURCE_ID");
success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}",resp);
if (success != true) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo;
errorInfo.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << errorInfo.stringOf("name") << " " << errorInfo.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << errorInfo.stringOf("debug_id") << "\r\n";
return;
}
CkJsonObject result;
bool parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
std::cout << "Expected a JSON response; HTTP " << resp.get_StatusCode() << "\r\n";
return;
}
if ((result.SizeOfArray("line_items") != 1) || (result.StringOfEquals("line_items[0].name","Annual inspection",true) != true) || (result.StringOfEquals("line_items[0].unit_amount.currency_code","USD",true) != true)) {
std::cout << "Select the USD inspection link from your catalog." << "\r\n";
return;
}
// 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") == true) {
result.Delete("id");
}
if (result.HasMember("create_time") == true) {
result.Delete("create_time");
}
if (result.HasMember("update_time") == true) {
result.Delete("update_time");
}
if (result.HasMember("status") == true) {
result.Delete("status");
}
if (result.HasMember("links") == true) {
result.Delete("links");
}
if (result.HasMember("payment_link") == true) {
result.Delete("payment_link");
}
result.UpdateString("line_items[0].unit_amount.value","95.00");
success = http.HttpJson("PUT","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}",result,"application/json",resp);
if (success != true) {
std::cout << "No HTTP response: " << http.lastErrorText() << "\r\n";
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject updateError;
updateError.Load(resp.bodyStr());
std::cout << "HTTP " << resp.get_StatusCode() << " " << updateError.stringOf("name") << " " << updateError.stringOf("details[0].issue") << "\r\n";
std::cout << "PayPal debug ID: " << updateError.stringOf("debug_id") << "\r\n";
return;
}
std::cout << "The inspection price is now USD 95.00." << "\r\n";
}