Skip to content

Commit

Permalink
Added Apple Pay Support
Browse files Browse the repository at this point in the history
  • Loading branch information
eWAYTony committed Nov 26, 2015
1 parent c0c1acb commit 0603b5a
Show file tree
Hide file tree
Showing 28 changed files with 1,335 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

workspace 'eWAYPaymentsSDK.xcworkspace'
xcodeproj 'eWAYPaymentsSDK.xcodeproj'
xcodeproj 'eWAYPaymentsSDK+ApplePay.xcodeproj'

target 'eWAYPaymentsSDK_Example', :exclusive => true do
pod "eWAYPaymentsSDK", :path => "../"
xcodeproj 'eWAYPaymentsSDK.xcodeproj'
end


target 'eWAYPaymentsSDK_Tests', :exclusive => true do
pod "eWAYPaymentsSDK", :path => "../"

pod 'Specta'
pod 'Expecta'
pod 'FBSnapshotTestCase'
pod 'Expecta+Snapshots'
xcodeproj 'eWAYPaymentsSDK.xcodeproj'
end

target 'eWAYPaymentsSDK_ApplePay_Example', :exclusive => true do
pod "eWAYPaymentsSDK", :path => "../"
xcodeproj 'eWAYPaymentsSDK+ApplePay.xcodeproj'
end
415 changes: 415 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay/APSimpleViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// APSimpleViewController.h
// eWAYPaymentsSDK+ApplePay
//
// Created by lok on 16/11/2015.
// Copyright © 2015 eWAY. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface APSimpleViewController : UIViewController

- (IBAction)Pay:(id)sender;
@end
180 changes: 180 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay/APSimpleViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//
// APSimpleViewController.m
// eWAYPaymentsSDK+ApplePay
//
// Created by lok on 16/11/2015.
// Copyright © 2015 eWAY. All rights reserved.
//

#import "APSimpleViewController.h"
#import "RapidAPI+ApplePay.h"

@interface APSimpleViewController () <PKPaymentAuthorizationViewControllerDelegate>
{
NSString *merchantIdentifier;
NSString *countryCode;
NSArray *supportedNetworks;
PKMerchantCapability merchantCapabilities;
NSArray *paymentSummaryItems;
NSString *currencyCode;
PKAddressField requiredBillingAddressFields;
PKAddressField requiredShippingAddressFields;
NSArray *shippingMethods;
NSString *transactionID;
IBOutlet UITextField *tfTransactionID;
NSString *token;
IBOutlet UITextField *tfToken;
IBOutlet UILabel *lbResult;
TransactionType transactionType;
Method method;
}

@end

@implementation APSimpleViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Set this to your Apple Pay Merchant ID from Developer account
merchantIdentifier = @"You Merchant ID from Developer account";

countryCode = @"AU";

//Indicate which payment networks you support by populating the supportedNetworks property with an array of string constants.
supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];

//Indicate which payment processing protocols you support by setting a value for the merchantCapabilities property
merchantCapabilities = PKMerchantCapability3DS;

//Create an instance of PKShippingMethod for each available shipping method
PKShippingMethod *standard =
[PKShippingMethod summaryItemWithLabel:@"Standard Shipping" amount:[NSDecimalNumber decimalNumberWithString:@"0.05"]];
standard.detail = @"5 Business Days";
standard.identifier = @"standard";
PKShippingMethod *express =
[PKShippingMethod summaryItemWithLabel:@"Express Shipping" amount:[NSDecimalNumber decimalNumberWithString:@"0.10"]];
express.detail = @"Next Day";
express.identifier = @"next-day";

shippingMethods = @[standard, express];

//Payment summary items, represented by the PKPaymentSummaryItem class, describe the different parts of the payment request to the user.
PKPaymentSummaryItem *firstItem = [PKPaymentSummaryItem summaryItemWithLabel:@"iPad" amount:[NSDecimalNumber decimalNumberWithString:@"3.00"]];
PKPaymentSummaryItem *secondItem = [PKPaymentSummaryItem summaryItemWithLabel:@"iWatch" amount:[NSDecimalNumber decimalNumberWithString:@"1.00"]];
PKPaymentSummaryItem *shippingItem = [PKPaymentSummaryItem summaryItemWithLabel:standard.identifier amount:standard.amount];

//The last payment summary item in the list is the grand total. Calculate the grand total amount by adding the amounts of all the other summary items.
NSDecimalNumber *total = [firstItem.amount decimalNumberByAdding:[secondItem.amount decimalNumberByAdding:shippingItem.amount]];
PKPaymentSummaryItem *totalItem = [PKPaymentSummaryItem summaryItemWithLabel:@"Your Company Name" amount:total];

paymentSummaryItems = @[firstItem, secondItem, shippingItem,totalItem];

currencyCode = @"AUD";

//Populate the requiredBillingAddressFields and requiredShippingAddressFields properties of the payment authorization view controller to indicate what billing and shipping information is needed
requiredBillingAddressFields = PKAddressFieldPostalAddress | PKAddressFieldName;
requiredShippingAddressFields = PKAddressFieldPostalAddress;

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

- (IBAction)Pay:(id)sender {

transactionType = Purchase;
method = ProcessPayment;

//Create PKPaymentRequest
[RapidAPI CreateApplePayRequest:merchantIdentifier countryCode:countryCode supportedNetworks:supportedNetworks merchantCapabilities:merchantCapabilities paymentSummaryItems:paymentSummaryItems currencyCode:currencyCode requiredBillingAddressFields:requiredBillingAddressFields billingAddress:nil requiredShippingAddressFields:requiredShippingAddressFields shippingAddress:nil shippingMethods:shippingMethods onCompletion:^(PKPaymentRequest *paymentRequest, NSError *error) {
if (error) {
NSLog(@"Error creating payment request: %@", [error localizedDescription]);

UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: @"Error"
message: [error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
return;
}

//Bring up the PKPaymentAuthorizationViewController, pass the payment request to the view controller’s initializer
//Set a delegate for the view controller, and then present it.
[RapidAPI ShowApplePayAuthorizationView:paymentRequest withDelegateController:self];

}];

}

#pragma mark PaymentAuthorizationViewController Delegate Methods
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didSelectShippingAddress:(ABRecordRef)address
completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray<PKShippingMethod *> *shippingMethods, NSArray<PKPaymentSummaryItem *> *summaryItems))completion {

completion(PKPaymentAuthorizationStatusSuccess,nil,nil);

}


- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didSelectShippingMethod:(PKShippingMethod *)shippingMethod
completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKPaymentSummaryItem *> *summaryItems))completion {
completion(PKPaymentAuthorizationStatusSuccess, [self summaryItemsForShippingMethod:shippingMethod]);
}

- (NSArray *)summaryItemsForShippingMethod:(PKShippingMethod *)shippingMethod {
NSMutableArray *summaryItemsArray = [NSMutableArray new];

[summaryItemsArray addObject:(PKPaymentSummaryItem *)paymentSummaryItems[0]];
[summaryItemsArray addObject:(PKPaymentSummaryItem *)paymentSummaryItems[1]];

PKPaymentSummaryItem *shippingItem = [PKPaymentSummaryItem summaryItemWithLabel:shippingMethod.identifier amount:shippingMethod.amount];
NSDecimalNumber *total = [((PKPaymentSummaryItem *)paymentSummaryItems[0]).amount decimalNumberByAdding:[((PKPaymentSummaryItem *)paymentSummaryItems[1]).amount decimalNumberByAdding:shippingMethod.amount]];
PKPaymentSummaryItem *totalItem = [PKPaymentSummaryItem summaryItemWithLabel:@"Your Company Name" amount:total];

[summaryItemsArray addObject:shippingItem];
[summaryItemsArray addObject:totalItem];

return summaryItemsArray;
}

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {


[RapidAPI submitApplePay:payment transactionType:transactionType method:method completed:^(SubmitPaymentResponse *submitPaymentResponse) {
NSString *msg = [NSString stringWithFormat:@"%@",@{@"Errors":submitPaymentResponse.Errors,@"SubmissionID":submitPaymentResponse.SubmissionID, @"Status":[NSString stringWithFormat:@"%lu",(unsigned long)submitPaymentResponse.Status]}];
NSLog(@"Apple Pay Result %@", msg);
if (submitPaymentResponse.Status != Error && [submitPaymentResponse.SubmissionID length] > 0)
completion(PKPaymentAuthorizationStatusSuccess);
else
completion(PKPaymentAuthorizationStatusFailure);

}];
}

- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}


@end
17 changes: 17 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay/ApplePayDemoAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppDelegate.h
// eWAYPaymentsSDK+ApplePay
//
// Created by lok on 31/07/2015.
// Copyright (c) 2015 eWAY. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ApplePayDemoAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

50 changes: 50 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay/ApplePayDemoAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// AppDelegate.m
// eWAYPaymentsSDK+ApplePay
//
// Created by lok on 31/07/2015.
// Copyright (c) 2015 eWAY. All rights reserved.
//

#import "ApplePayDemoAppDelegate.h"
#import "RapidApi.h"

@interface ApplePayDemoAppDelegate ()

@end

@implementation ApplePayDemoAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


//To Live, be careful, the card will be charged when using this setting.
[[RapidAPI sharedManager] setRapidEndpoint:@"https://api.ewaypayments.com/ApRapid/"];
[[RapidAPI sharedManager] setPublicAPIKey:@"Your Public API Key from MYeWAY"];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
41 changes: 41 additions & 0 deletions Example/eWAYPaymentsSDK+ApplePay/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015 eWAY. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="441" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="eWAYPaymentsSDK+ApplePay" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="441" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="548" y="455"/>
</view>
</objects>
</document>
Loading

0 comments on commit 0603b5a

Please sign in to comment.