PaymentKit is a utility library for writing payment forms in iOS apps.
Just add PKView
to your application, and it'll take care accepting card numbers, expiry, cvc and zip.
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.
For purchases related to the app, such as premium features, Apple's TOS require that you use their native In-App Purchase API. PaymentKits is only for purchasing products or services outside the app.
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 "PKView.h"
@interface PaymentViewController : UIViewController <PKViewDelegate>
@property IBOutlet PKView* paymentView;
@end
Notice we're importing PKView.h
, the class conforms to PKViewDelegate
, and lastly we have a paymentView
property of type PKView
.
3) Instantiate and add PKView
. We recommend you use the same frame.
- (void)viewDidLoad
{
[super viewDidLoad];
self.paymentView = [[PKView alloc] initWithFrame:CGRectMake(15, 25, 290, 55)];
self.paymentView.delegate = self;
[self.view addSubview:self.paymentView];
}
4) Implement PKViewDelegate
method paymentView:withCard:isValid:
. This gets passed a PKCard
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:(PKView*)paymentView withCard:(PKCard *)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
[[PKCardNumber cardNumberWithString:@"4242424242424242"] formattedString]; //=> '4242 4242 4242 4242'
[[PKCardNumber cardNumberWithString:@"4242424242"] formattedString]; //=> '4242 4242 42'
// Amex support
[[PKCardNumber cardNumberWithString:@"378282246310005"] formattedString]; //=> '3782 822463 10005'
[[PKCardNumber cardNumberWithString:@"378282246310005"] cardType] == PKCardTypeAmex; //=> YES
// Check a card number is valid using the Luhn algorithm
[[PKCardNumber cardNumberWithString:@"4242424242424242"] isValid]; //=> YES
[[PKCardNumber cardNumberWithString:@"4242424242424243"] isValid]; //=> NO
// Check to see if a card expiry is valid
[[PKCardExpiry cardExpiryWithString:@"05 / 20"] isValid]; //=> YES
[[PKCardExpiry cardExpiryWithString:@"05 / 02"] isValid]; //=> NO
// Return a card expiry's month
[[PKCardExpiry cardExpiryWithString:@"05 / 02"] month]; //=> 5
Included are a number of UITextFieldDelegate
delegates: PKCardCVCDelegate
, PKCardExpiryDelegate
and PKCardNumberDelegate
. You can set these as the delegates of UITextField
inputs, which ensures that input is limited and formatted.
Create a PKCardNumber
object, passing a NSString
representing the card number. For example:
PKCardNumber* cardNumber = [PKCardNumber cardNumberWithString:@"4242424242424242"];
Returns a PKCardType
representing the card type (Visa, Amex etc).
PKCardType cardType = [[PKCardNumber cardNumberWithString:@"4242424242424242"] cardType];
if (cardType == PKCardTypeAmex) {
}
Available types are:
PKCardTypeVisa
PKCardTypeMasterCard
PKCardTypeAmex
PKCardTypeDiscover
PKCardTypeJCB
PKCardTypeDinersClub
PKCardTypeUnknown
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 = [[PKCardNumber 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 PKCardCVC
instance, representing the card CVC. For example:
PKCardCVC* cardCVC = [PKCardCVC 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 PKCardExpiry
object, passing a NSString
representing the card expiry. For example:
PKCardExpiry* cardExpiry = [PKCardExpiry cardExpiryWithString:@"10 / 2015"];
Returns a formatted representation of the card expiry. For example:
[[PKCardExpiry 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.