Objective-C Requires Chilkat v11.0.0+
Objective-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 Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <NSString.h>
BOOL success = NO;
// 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.
CkoHttp *http = [[CkoHttp alloc] init];
http.AuthToken = @"SANDBOX_ACCESS_TOKEN";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
[http SetUrlVar: @"id" value: @"INVOICE_ID"];
success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}" response: resp];
if (success != YES) {
NSLog(@"%@%@",@"No HTTP response: ",http.LastErrorText);
return;
}
if (([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
CkoJsonObject *errorInfo = [[CkoJsonObject alloc] init];
[errorInfo Load: resp.BodyStr];
NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[errorInfo StringOf: @"name"],@" ",[errorInfo StringOf: @"details[0].issue"]);
NSLog(@"%@%@",@"PayPal debug ID: ",[errorInfo StringOf: @"debug_id"]);
return;
}
CkoJsonObject *result = [[CkoJsonObject alloc] init];
BOOL parsedResult = [result Load: resp.BodyStr];
if (parsedResult != YES) {
NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
return;
}
NSString *paymentId = @"EXTERNAL_PAYMENT_ID";
BOOL matched = NO;
int i = 0;
while ((i < [[result SizeOfArray: @"payments.transactions"] intValue])) {
result.I = [NSNumber numberWithInt: i];
if (([result StringOfEquals: @"payments.transactions[i].payment_id" value: paymentId caseSensitive: YES] == YES) && ([result StringOfEquals: @"payments.transactions[i].type" value: @"EXTERNAL" caseSensitive: YES] == YES)) {
matched = YES;
}
i = i + 1;
}
if (matched != YES) {
NSLog(@"%@",@"This invoice has no matching external payment record.");
return;
}
[http SetUrlVar: @"paymentId" value: paymentId];
success = [http HttpNoBody: @"DELETE" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments/{$paymentId}" response: resp];
if (success != YES) {
NSLog(@"%@%@",@"No HTTP response: ",http.LastErrorText);
return;
}
if (([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
CkoJsonObject *removeError = [[CkoJsonObject alloc] init];
[removeError Load: resp.BodyStr];
NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[removeError StringOf: @"name"],@" ",[removeError StringOf: @"details[0].issue"]);
NSLog(@"%@%@",@"PayPal debug ID: ",[removeError StringOf: @"debug_id"]);
return;
}
NSLog(@"%@",@"External payment entry removed.");