Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkCrypt2W.h>
#include <CkJsonObjectW.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.
CkHttpW http;
http.put_AuthToken(L"SANDBOX_ACCESS_TOKEN");
CkHttpResponseW resp;
// 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.
CkCrypt2W crypt;
const wchar_t *requestId = crypt.generateUuid();
http.SetRequestHeader(L"PayPal-Request-Id",requestId);
CkJsonObjectW body;
body.UpdateString(L"integration_mode",L"LINK");
body.UpdateString(L"type",L"BUY_NOW");
body.UpdateString(L"reusable",L"MULTIPLE");
body.UpdateString(L"return_url",L"https://merchant.example/thank-you");
body.UpdateString(L"line_items[0].name",L"Annual inspection");
body.UpdateString(L"line_items[0].unit_amount.currency_code",L"USD");
body.UpdateString(L"line_items[0].unit_amount.value",L"90.00");
success = http.HttpJson(L"POST",L"https://api-m.sandbox.paypal.com/v1/checkout/payment-resources",body,L"application/json",resp);
if (success != true) {
wprintf(L"No HTTP response: %s\n",http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObjectW errorInfo;
errorInfo.Load(resp.bodyStr());
wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),errorInfo.stringOf(L"name"),errorInfo.stringOf(L"details[0].issue"));
wprintf(L"PayPal debug ID: %s\n",errorInfo.stringOf(L"debug_id"));
return;
}
CkJsonObjectW result;
bool parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
wprintf(L"Expected a JSON response; HTTP %d\n",resp.get_StatusCode());
return;
}
wprintf(L"Catalog payment resource ID: %s\n",result.stringOf(L"id"));
// Link order is not fixed. Find the customer-facing link to copy into an email.
int linkIndex = 0;
while ((linkIndex < result.SizeOfArray(L"links"))) {
result.put_I(linkIndex);
if (result.StringOfEquals(L"links[i].rel",L"payment_link",true)) {
wprintf(L"Share this payment link: %s\n",result.stringOf(L"links[i].href"));
}
linkIndex = linkIndex + 1;
}
}