Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

PayPal: Record an external refund against an invoice

See more PayPal Examples

Record USD 5 already returned by bank transfer for a paid invoice. This call does not move money. To refund a PayPal capture, use the Payments v2 capture refund example instead.

PayPal documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <CkoDateTime.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];

//  Record only money already returned outside PayPal. Reconcile first if a previous
//  attempt timed out.

[http SetUrlVar: @"id" value: @"INVOICE_ID"];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"method" value: @"BANK_TRANSFER"];
[body UpdateString: @"amount.currency_code" value: @"USD"];
[body UpdateString: @"amount.value" value: @"5.00"];

//  Use the actual date of the transfer; this example records one made today.
CkoDateTime *date = [[CkoDateTime alloc] init];
[date SetFromCurrentSystemTime];
[body UpdateString: @"refund_date" value: [date GetAsIso8601: @"YYYY-MM-DD" bLocal: NO]];

success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/refunds" json: body contentType: @"application/json" 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 parsed = [result Load: resp.BodyStr];
if (parsed != YES) {
    NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
    return;
}

NSLog(@"%@%@",@"Record ID: ",[result StringOf: @"refund_id"]);