iOS Square Register Popover OpenURL问题

时间:2016-12-15 03:43:25

标签: ios encoding openurl square-connect percent-encoding

Square最近删除了OAuth商家授权的要求,如您在应用程序"发起广场注册交易中所述[{3}}所述"部分。

当我的应用程序弹出到Square Register应用程序时,Square应用程序中会出现一个警告弹出窗口:

  

" API错误:未指定客户ID"

尽管我清楚地列出了我的" client_id"在我的应用程序中,当我打电话给广场。我的代码如下。 有任何想法如何解决?

-(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];
}

jsonString的NSLog是:

{
"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";
}

encodedString的NSLog是: here

而且,作为一个注释,&#39; CALLBACK_URL&#39;和&#39; CLIENT_ID&#39;我输入的是占位符,因为我不想把真正的值放在。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是我使用过的代码:

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");
    }
}