webpay-token-ios is an iOS library for creating a WebPay token from a credit card.
There is a sample app using card.io
webpay-token-ios supports iOS 7 and above.
You can either install using cocoapods(recommended) or copying files manually.
In your Podfile, add a line
pod 'WebPay', '~> 2.0.3'
then, run pod install
.
- Clone this repository
- Add files under Webpay directory to your project
Add #import "Webpay.h"
in one of your files, and see if your target builds without error.
You need to create a bridging header and add #import "Webpay.h"
to the header to use the library in swift code.
For instruction on how to create a bridging header, please refer to Apple's documentation.
webpay-token-ios consists of 3 components.
- WPYPaymentViewController(view controller)
- WPYTokenizer(model that creates a token)
- WPYCardFormView(card form view)
If you want a view controller to drop in, WPYPaymentViewController is the way to go. WPYTokenizer is for developers planning to create their own view & view controller from scratch. It provides API for accessing WebPay API. WPYCardFormView offers a form with validation.
Initialization is required for using any components from this library.
// objective-c
#import "Webpay.h"
// replace test_public_YOUR_PUBLIC_KEY with your WebPay publishable key
[WPYTokenizer setPublicKey:@"test_public_YOUR_PUBLIC_KEY"];
// swift
// replace test_public_YOUR_PUBLIC_KEY with your WebPay publishable key
WPYTokenizer.setPublicKey("test_public_YOUR_PUBLIC_KEY")
If you just want a viewcontroller for pushViewController:animated
or presentViewController:animated:completion:
, this is what you want.
// objective-c
// version 2.x
WPYPaymentViewController *paymentViewController = [WPYPaymentViewController paymentViewControllerWithPriceTag:@"¥350" callback:^(WPYPaymentViewController *viewController, WPYToken *token, NSError *error) {
if (error)
{
NSLog(@"error:%@", [error localizedDescription]);
}
else
{
//post token to your server
// when transaction is complete
[viewController setPayButtonComplete]; // this will change the button color to green and its title to checkmark
[viewController dismissAfterDelay: 2.0f];
}
}];
[self.navigationController pushViewController:paymentViewController animated:YES];
// version 1.x
WPYPaymentViewController *paymentViewController = [[WPYPaymentViewController alloc] initWithPriceTag:@"¥350" callback:^(WPYPaymentViewController *viewController, WPYToken *token, NSError *error) {
if (error)
{
NSLog(@"error:%@", [error localizedDescription]);
}
else
{
//post token to your server
// when transaction is complete
[viewController setPayButtonComplete]; // this will change the button color to green and its title to checkmark
[viewController dismissAfterDelay: 2.0f];
}
}];
[self.navigationController pushViewController:paymentViewController animated:YES];
// swift
let paymentViewController = WPYPaymentViewController(priceTag: "¥350", callback: { viewController, token, error in
if let newError = error {
println("error:\(error.localizedDescription)")
} else {
//post token to your server
// when transaction is complete
viewController.setPayButtonComplete()
viewController.dismissAfterDelay(2.0)
}
})
self.navigationController?.pushViewController(paymentViewController, animated: true)
If you want the card form to be populated with card data, use initWithPriceTag:card:callback:
instead.
If you are creating your own view, create token using WPYTokenizer.
// objective-c
#import "Webpay.h"
// create a credit card model and populate with data
WPYCreditCard *card = [[WPYCreditCard alloc] init];
card.number = @"4242424242424242";
card.expiryYear = 2015;
card.expiryMonth = 12;
card.cvc = @"123";
card.name = @"TARO YAMADA";
// pass card instance and a callback
[WPYTokenizer createTokenFromCard:card
completionBlock:^(WPYToken *token, NSError *error) {
if (error)
{
NSLog(@"error:%@", [error localizedDescription]);
}
else
{
NSLog(@"token:%@", token.tokenId);
}
}];
// swift
// create a credit card model and populate with data
let card = WPYCreditCard()
card.number = "4242424242424242"
card.expiryYear = 2015
card.expiryMonth = WPYMonth.December
card.cvc = "123"
card.name = "TARO YAMADA"
// pass card instance and a callback
WPYTokenizer.createTokenFromCard(card, completionBlock: {token, error in
if let newError = error {
println("\(error)")
} else {
println("\(token.tokenId)")
}
})
WPYCardFormView is a credit card form view that calls its delegate method when the form is valid. It handles padding credit card number, masking security code, and validating each field.
// objective-c
// create view
WPYCreditCard *card = [[WPYCreditCard alloc] init];
WPYCardFormView *cardForm = [[WPYCardFormView alloc] initWithFrame:CGRectMake(0, 0, 320, 300) card:card];
cardForm.delegate = self;
[self.view addSubview: cardForm];
// WPYCardFormDelegate methods
- (void)validFormWithCard:(WPYCreditCard *)creditCard
{
// called when the form is valid.
}
// swift
// create view
let card = WPYCreditCard()
let form = WPYCardFormView(frame: CGRect(x: 0, y: 0, width: 320, height: 320), card: card)
form.delegate = self
self.view.addSubview(form)
// WPYCardFormDelegate methods
func validFormWithCard(creditCard: WPYCreditCard!) {
// called when the form is valid.
}
If you want more granular control, use subclasses of WPYAbstractCardField
.
WPYCreditCard offers various validation methods.
For validating the whole card, use - (BOOL)validate:
// objective-c
NSError *cardError = nil;
if (![card validate:&cardError])
{
NSLog(@"error:%@", [cardError localizedDescription]);
}
// swift
var cardError: NSError?
if !card.validate(&cardError) {
println("error:\(cardError.localizedDescription)")
}
For validating each property, use - (BOOL)validatePROPERTY:error:
// objective-c
NSString *number = @"4242424242424242";
NSError *cardError = nil;
WPYCreditCard *card = [[WPYCreditCard alloc] init];
if (![card validateNumber:&number error:&cardError])
{
NSLog(@"error:%@", [cardError localizedDescription]);
}
// swift
var number: AnyObject? = "4242424242424242"
var cardError: NSError?
let card = WPYCreditCard()
if !card.validateNumber(&number, error:&cardError) {
println("error:\(cardError.localizedDescription)")
}
For checking brand from partial numbers
// objective-c
[WPYCreditCard brandNameFromPartialNumber:@"42"];
// swift
WPYCreditCard.brandNameFromPartialNumber("42")
WPYToken holds token data returned from Webpay API.
This class defines all the errors originating from webpay-ios-token.