我使用的是PromiseKit框架版本1.7.7(我需要使用此版本,因为另一个框架需要它)。
因此,在这个使用PromiseKit框架的框架中,有以下方法:
- (PMKPromise *)paymentTokenForCreditCard:(GNCreditCard *)creditCard {
NSDictionary *cardDict = [creditCard paramsDicionary];
NSString *jsonCard = [self getJSONStringFromDictionary:cardDict];
return [self encryptData:jsonCard]
.then(^(NSString *encryptedData){
NSDictionary *params = @{@"data":encryptedData};
return [self request:kGNApiRouteSaveCard method:@"POST" params:params];
})
.then(^(NSDictionary *response){
return [[GNPaymentToken alloc] initWithDictionary:response];
});
}
它展示了如何使用它的示例:
GNConfig *gnConfig = [[GNConfig alloc] initWithAccountCode:@"YOUR_ACCOUNT_CODE" sandbox:YES];
GNApiEndpoints *gnApi = [[GNApiEndpoints alloc] initWithConfig:gnConfig];
GNCreditCard *creditCard = [[GNCreditCard alloc] init];
creditCard.number = @"4012001038443335";
creditCard.brand = kGNMethodBrandVisa;
creditCard.expirationMonth = @"05";
creditCard.expirationYear = @"2018";
creditCard.cvv = @"123";
[gnApi paymentTokenForCreditCard:creditCard]
.then(^(GNPaymentToken *paymentToken){
NSLog(@"%@", paymentToken.token);
})
.catch(^(GNError *error){
NSLog(@"An error occurred: %@", error.message);
});
好吧,我是如何使用swift而不是object-c的,我试着这样使用它:
let gnConfig = GNConfig(accountCode: "3f62976bea79971730b67cd62806c256", sandbox: true)
let gnEndpoints = GNApiEndpoints(config: gnConfig)
let gnCreditCard: GNCreditCard! = GNCreditCard(number: "4012001038443335", brand: kGNMethodBrandVisa, expirationMonth: "05", expirationYear: "2018", cvv: "123")
gnEndpoints?.paymentToken(for: gnCreditCard).then({ tokenPagamento in
if let aToken = tokenPagamento?.token {
print("\(aToken)")
}
}).catch({ error in
if let aMessage = error?.message {
print("An error occurred: \(aMessage)")
}
})
它显示了这个错误:
对成员“then()”的引用不明确
我怎么能修好它?