0

Square 最近删除了对 OAuth 商家授权的要求,如“从您应用程序发起 Square 注册交易”部分中所述。

当我的应用程序弹出到 Square Register 应用程序时,Square 应用程序中会出现一个警报弹出窗口,上面写着:

“API 错误:未指定客户端 ID”

尽管当我打电话给 open square 时,我在我的应用程序中清楚地列出了我的“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";
}

编码字符串的 NSLog 是: 百分比编码字符串

而且,作为说明,我输入的“CALLBACK_URL”和“CLIENT_ID”是占位符,因为我不想放入实际值。

谢谢!

4

1 回答 1

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");
    }
}
于 2016-12-17T20:59:40.973 回答