iOS Square Register Popover OpenURL Issue

149 views Asked by At

Square recently removed the requirement for OAuth merchant authorization as outlined here in the "Initiating a Square Register transaction from your app" section.

When my app pops over to the Square Register app, an alert popup appears in the Square app saying:

"API Error: No client ID specified"

despite the fact that I clearly list my "client_id" in my app when I call to open square. My code is below. Any ideas how to fix it?

-(void)squarePaymentWithName:(NSString *)name{
//Specify amount of money to charge
float orderPriceFloat = [Order orderTotalPrice];
float orderPriceFloatCents = orderPriceFloat * 100;
NSInteger orderPriceFloatCentsInteger = [[NSNumber numberWithFloat:orderPriceFloatCents] integerValue];
NSString *amountString = [NSString stringWithFormat:@"%ld", (long)orderPriceFloatCentsInteger];

NSDictionary *squareDictionary = @{@"callback_url": @"<CALLBACK_URL>",
                                   @"client_id": @"<CLIENT_ID>",
                                   @"version": @"1.2",
                                   @"amount_money":
                                       @{@"amount": amountString,
                                         @"currency_mode":@"USD"
                                         },
                                   @"options":
                                       @{@"supported_tender_types": @[@"CREDIT_CARD", @"SQUARE_GIFT_CARD"],
                                         @"auto_return": @"true"
                                         }
                                   };
NSString *jsonString = [NSString stringWithFormat:@"%@", squareDictionary];

NSString *encodedString = [jsonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //encode the string
NSString *scheme = [NSString stringWithFormat:@"square-commerce-v1://payment/create?data=%@", encodedString]; //input the string to the url

UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
BOOL canOpen = [application canOpenURL:URL]; //open the url
[application openURL:URL];
}

The NSLog of jsonString is:

{
"amount_money" =     {
    amount = 250;
    "currency_mode" = USD;
};
"callback_url" = CALLBACK_URL;
"client_id" = CLIENT_ID;
options =     {
    "auto_return" = true;
    "supported_tender_types" =         (
        "CREDIT_CARD",
        "SQUARE_GIFT_CARD"
    );
};
version = "1.2";
}

The NSLog of encodedString is: Percent-encoded String

And, as a note, the 'CALLBACK_URL' and 'CLIENT_ID' I input are placeholders since I didn't want to put the real values in.

Thanks!

1

There are 1 answers

0
mdimarca On BEST ANSWER

Here's the code I used that worked:

NSDictionary *squareDictionary = @{@"callback_url": CALLBACK_URL,
                                   @"client_id": CLIENTID,
                                   @"version": @"1.2",
                                   @"notes": notes,
                                   @"state": name,
                                   @"amount_money":
                                       @{@"amount": amountString,
                                         @"currency_code":@"USD"
                                         },
                                   @"options":
                                       @{@"supported_tender_types": @[@"CREDIT_CARD"],
                                         @"auto_return": @"true"
                                         }
                                   };

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:squareDictionary
                                                  options:NSJSONWritingPrettyPrinted
                                                    error:&error]; // Pass 0 if you don't care about the readability of the generated string

NSString *jsonString;
if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSString *encodedString = [jsonString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //encode the string

    NSString *scheme = [NSString stringWithFormat:@"square-commerce-v1://payment/create?data=%@", encodedString]; //input the string to the url

    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:scheme];
    BOOL canOpen = [application canOpenURL:URL]; //open the url
    if (canOpen) {
        [application openURL:URL];
    } else {
        NSLog(@"couldn't open app");
    }
}