Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
PayPal: Remove an external payment recorded on the wrong invoice
See more PayPal Examples
Use the saved external payment record ID. Verify that it belongs to the selected invoice, then remove the bookkeeping entry. The bank transfer itself is unaffected. Record it on the correct invoice as a separate step.
Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkHttpResponseW.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;
http.SetUrlVar(L"id",L"INVOICE_ID");
success = http.HttpNoBody(L"GET",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}",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;
}
const wchar_t *paymentId = L"EXTERNAL_PAYMENT_ID";
bool matched = false;
int i = 0;
while ((i < result.SizeOfArray(L"payments.transactions"))) {
result.put_I(i);
if ((result.StringOfEquals(L"payments.transactions[i].payment_id",paymentId,true) == true) && (result.StringOfEquals(L"payments.transactions[i].type",L"EXTERNAL",true) == true)) {
matched = true;
}
i = i + 1;
}
if (matched != true) {
wprintf(L"This invoice has no matching external payment record.\n");
return;
}
http.SetUrlVar(L"paymentId",paymentId);
success = http.HttpNoBody(L"DELETE",L"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}",resp);
if (success != true) {
wprintf(L"No HTTP response: %s\n",http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObjectW removeError;
removeError.Load(resp.bodyStr());
wprintf(L"HTTP %d %s %s\n",resp.get_StatusCode(),removeError.stringOf(L"name"),removeError.stringOf(L"details[0].issue"));
wprintf(L"PayPal debug ID: %s\n",removeError.stringOf(L"debug_id"));
return;
}
wprintf(L"External payment entry removed.\n");
}