Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

Prefill form with existing card details #45

Open
jflinter opened this issue Sep 4, 2014 · 3 comments
Open

Prefill form with existing card details #45

jflinter opened this issue Sep 4, 2014 · 3 comments

Comments

@jflinter
Copy link

jflinter commented Sep 4, 2014

This issue was migrated from stripe-ios: stripe/stripe-ios#17

@cannyboy
Copy link

cannyboy commented Oct 8, 2014

this would be useful in conjunction with https://github.com/card-io/card.io-iOS-SDK

@cannyboy
Copy link

cannyboy commented Oct 9, 2014

I just wanted to fill in the card number from the number returned from card-io. Not the CVC or expiry.

I could do this separating a card number string into an array of single character NSStrings, and iterating through the array, and pasting the string up to that point into the cardNumberField

UIPasteboard *pb = [UIPasteboard generalPasteboard];
NSString *originalPasteboard = pb.string;
NSMutableArray *numberArray = [NSMutableArray array];
NSString *str = info.cardNumber;
for (int i = 0; i < [str length]; i++) {
    NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
    [numberArray addObject:ch];
}
[cell.paymentView.cardNumberField becomeFirstResponder];
cell.paymentView.cardNumberField.text = @"";
for (int i = 0; i < [numberArray count]; i++) {
    float f = (float)i * 0.02;
    NSString *number = [numberArray objectAtIndex:i];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableString *newString = [cell.paymentView.cardNumberField.text mutableCopy];
        [newString appendString:number];
        pb.string = number;
        [cell.paymentView.cardNumberField paste:self];
        pb.string = @"";
        if (i == (numberArray.count-1)) pb.string = originalPasteboard;
    });
}

@zuthan
Copy link

zuthan commented Feb 23, 2015

I solved this by creating a category for PTKView that provides a setCardNumber method and calls the textField:shouldChangeCharactersInRange:replacementString method like so:

PTKView+Programmable.h

@interface PTKView (Programmable)
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString;

    - (void)setCardNumber:(NSString *)cardNumber;
@end

PTKView+Programmable.m

@implementation PTKView (Programmable)
    - (void)setCardNumber:(NSString *)cardNumber {
        NSRange range = NSMakeRange(0, [self cardNumberField].text.length);
        [self textField:[self cardNumberField] shouldChangeCharactersInRange:range replacementString:cardNumber];
    }
@end

Since I'm using swift, I also had to add #import "PTKView+Programmable.h" to my Bridging-Header.h file.

Then I could simply call self.pkView.setCardNumber(cardNumber) in my PaymentViewController.

Since there are obvious use cases for setting the card fields programmatically (such as Card.io), please add this feature natively.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants