I am using a PromiseKit framework version 1.7.7 (I need to use this version due another framework that needs it).
So, in this framework that uses the PromiseKit framework has this method:
- (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];
});
}
And it shows this example on how to use it:
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);
});
Well, how I am using Swift instead of Object-C, I am trying to use it this way:
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)")
}
})
And it is showing me this error:
Ambiguous reference to member 'then()'
How could I fix it?
Try this code below. The promise thing should work. Modify it upon your needs.
gnEndpoints?.paymentToken(for: gnCreditCard).then({ token -> () in
if let token = token as? GNPaymentToken {
print(token)
}
}, { (error) -> () in
// catch your error
})