PaymentKit is a utility library for writing payment forms in iOS apps.
Just add PTKView
to your application, and it'll take care accepting card numbers, expiry and cvc.
Alternatively, we've provided a bunch of classes that you can use yourself to add formatting, validation and restricting input of UITextField
s.
In short, PaymentKit should greatly simplify your life when dealing with iOS payments.
Note: If you also want to send the card data to Stripe, check out our iOS bindings, which are built on PaymentKit.
For purchases that are consumed within the app (such as extra content or features), Apple's TOS require that you use their In-App Purchase API. PaymentKit is for everything else.
CocoaPods is a library dependency management tool for Objective-C. To use PaymentKit with CocoaPods, simply add the following to your Podfile and run pod install:
pod 'PaymentKit', :git => 'https://github.com/stripe/PaymentKit.git'
- Clone this repository
- In the menubar, click on 'File' then 'Add files to "Project"...'
- Select the 'PaymentKit' directory in your cloned PaymentKit repository
- Make sure "Copy items into destination group's folder (if needed)" is checked"
- Click "Add"
1) Add the QuartzCore
framework to your application.
2) Create a new ViewController
, for example PaymentViewController
.
#import <UIKit/UIKit.h>
#import "PTKView.h"
@interface PaymentViewController : UIViewController <PTKViewDelegate>
@property IBOutlet PTKView* paymentView;
@end
Notice we're importing PTKView.h
, the class conforms to PTKViewDelegate
, and lastly we have a paymentView
property of type PTKView
.
3) Instantiate and add PTKView
. We recommend you use the same frame.
- (void)viewDidLoad
{
[super viewDidLoad];
self.paymentView = [[PTKView alloc] initWithFrame:CGRectMake(15, 25, 290, 55)];
self.paymentView.delegate = self;
[self.view addSubview:self.paymentView];
}
4) Implement PTKViewDelegate
method paymentView:withCard:isValid:
. This gets passed a PTKCard
instance, and a BOOL
indicating whether the card is valid. You can enable or disable a navigational button depending on the value of valid
, for example:
- (void) paymentView:(PTKView*)paymentView withCard:(PTKCard *)card isValid:(BOOL)valid
{
NSLog(@"Card number: %@", card.number);
NSLog(@"Card expiry: %lu/%lu", (unsigned long)card.expMonth, (unsigned long)card.expYear);
NSLog(@"Card cvc: %@", card.cvc);
NSLog(@"Address zip: %@", card.addressZip);
// self.navigationItem.rightBarButtonItem.enabled = valid;
}
That's all! No further reading is required, unless you want more flexibility by using the raw API. For more, please see the included example.
// Format a card number
[[PTKCardNumber cardNumberWithString:@"4242424242424242"] formattedString]; //=> '4242 4242 4242 4242'
[[PTKCardNumber cardNumberWithString:@"4242424242"] formattedString]; //=> '4242 4242 42'
// Amex support
[[PTKCardNumber cardNumberWithString:@"378282246310005"] formattedString]; //=> '3782 822463 10005'
[[PTKCardNumber cardNumberWithString:@"378282246310005"] cardType] == PTKCardTypeAmex; //=> YES
// Check a card number is valid using the Luhn algorithm
[[PTKCardNumber cardNumberWithString:@"4242424242424242"] isValid]; //=> YES
[[PTKCardNumber cardNumberWithString:@"4242424242424243"] isValid]; //=> NO
// Check to see if a card expiry is valid
[[PTKCardExpiry cardExpiryWithString:@"05 / 20"] isValid]; //=> YES
[[PTKCardExpiry cardExpiryWithString:@"05 / 02"] isValid]; //=> NO
// Return a card expiry's month
[[PTKCardExpiry cardExpiryWithString:@"05 / 02"] month]; //=> 5
Included are a number of UITextFieldDelegate
delegates: PTKCardCVCDelegate
, PTKCardExpiryDelegate
and PTKCardNumberDelegate
. You can set these as the delegates of UITextField
inputs, which ensures that input is limited and formatted.
You can localize the placeholders shown in the form by adding a PaymentKit.strings
file to your project:
"placeholder.card_number" = "1234 5678 9012 3456";
"placeholder.card_expiry" = "MM/YY";
"placeholder.card_cvc" = "CVC";
Create a PTKCardNumber
object, passing a NSString
representing the card number. For example:
PTKCardNumber* cardNumber = [PTKCardNumber cardNumberWithString:@"4242424242424242"];
Returns a PTKCardType
representing the card type (Visa, Amex etc).
PTKCardType cardType = [[PTKCardNumber cardNumberWithString:@"4242424242424242"] cardType];
if (cardType == PTKCardTypeAmex) {
}
Available types are:
PTKCardTypeVisa
PTKCardTypeMasterCard
PTKCardTypeAmex
PTKCardTypeDiscover
PTKCardTypeJCB
PTKCardTypeDinersClub
PTKCardTypeUnknown
Returns the card number as a string.
Returns a formatted card number, in the same space format as it appears on the card.
NSString* number = [[PTKCardNumber cardNumberWithString:@"4242424242424242"] formattedString];
number //=> '4242 4242 4242 4242'
Returns a formatted card number with a trailing space, if appropriate. Useful for formatting UITextField
input.
Helper method which calls isValidLength
and isValidLuhn
.
Returns a BOOL
depending on whether the card number is a valid length. Takes into account the different lengths of Amex and Visa, for example.
Returns a BOOL
indicating whether the number passed a Luhn check.
Returns a BOOL
indicating whether the number is too long or not.
Returns a PTKCardCVC
instance, representing the card CVC. For example:
PTKCardCVC* cardCVC = [PTKCardCVC cardCVCWithString:@"123"];
Returns the CVC as a string.
Returns a BOOL
indicating whether the CVC is valid universally.
Returns a BOOL
indicating whether the CVC is valid for a particular card type.
Returns a BOOL
indicating whether the cvc is too long or not.
Create a PTKCardExpiry
object, passing a NSString
representing the card expiry. For example:
PTKCardExpiry* cardExpiry = [PTKCardExpiry cardExpiryWithString:@"10 / 2015"];
Returns a formatted representation of the card expiry. For example:
[[PTKCardExpiry cardExpiryWithString:@"10/2015"] formattedString]; //=> "10 / 2015"
Returns a formatted representation of the card expiry, with a trailing slash if appropriate. Useful for formatting UITextField
inputs.
Returns a BOOL
if the expiry has a valid month, a valid year and is in the future.
Returns an integer representing the expiry's month. Returns 0
if the month can't be determined.
Returns an integer representing the expiry's year. Returns 0
if the year can't be determined.