Friday, December 26, 2014

Apple Pay - Doing the Programming

The coding is fairly simple and it is like below


#import

We have to include the PassKit Framework and do the import below

Add a button and on the action do the below

-(IBAction) payButtonPressed:(id)sender
{
    if([PKPaymentAuthorizationViewController canMakePayments])
    {
        PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
        request.countryCode = @"US";
        request.currencyCode = @"USD";
        request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
        request.merchantCapabilities = PKMerchantCapabilityEMV;
        request.merchantIdentifier = @"merchant.com.mportal.applepaytest";
        PKPaymentSummaryItem *amount = [PKPaymentSummaryItem summaryItemWithLabel:@"amount" amount:[NSDecimalNumber decimalNumberWithString:@"0.99"]];
        PKPaymentSummaryItem *tax = [PKPaymentSummaryItem summaryItemWithLabel:@"tax" amount:[NSDecimalNumber decimalNumberWithString:@"1.00"]];
        PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"Grand Total" amount:[NSDecimalNumber decimalNumberWithString:@"1.99"]];
        request.paymentSummaryItems = @[amount, tax, total];
        
        self.paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
        if(self.paymentPane != nil)
        {
            paymentPane.delegate = self;
            [self presentViewController:paymentPane animated:TRUE completion:nil];
        }
        else
        {
            [self showMessage:@"Error while creating payment pane"];
        }
    }
    else
    {
        [self showMessage:@"Apple Pay Not supported"];
    }
    

}


Below are the call backs for the 

// Sent to the delegate after the user has acted on the payment request.  The application
// should inspect the payment to determine whether the payment request was authorized.
//
// If the application requested a shipping address then the full addresses is now part of the payment.
//
// The delegate must call completion with an appropriate authorization status, as may be determined
// by submitting the payment credential to a processing gateway for payment authorization.
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
    [self showMessage:@"didAuthorizePayment called back"];
}

// Sent to the delegate when payment authorization is finished.  This may occur when
// the user cancels the request, or after the PKPaymentAuthorizationStatus parameter of the
// paymentAuthorizationViewController:didAuthorizePayment:completion: has been shown to the user.
//
// The delegate is responsible for dismissing the view controller in this method.
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
    [self showMessage:@"paymentAuthorizationViewControllerDidFinish called back"];
}

/ Sent when the user has selected a new shipping method.  The delegate should determine
// shipping costs based on the shipping method and either the shipping address supplied in the original
// PKPaymentRequest or the address fragment provided by the last call to paymentAuthorizationViewController:
// didSelectShippingAddress:completion:.
//
// The delegate must invoke the completion block with an updated array of PKPaymentSummaryItem objects.
//
// The delegate will receive no further callbacks except paymentAuthorizationViewControllerDidFinish:
// until it has invoked the completion block.
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                   didSelectShippingMethod:(PKShippingMethod *)shippingMethod
                                completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *summaryItems))completion
{
    [self showMessage:@"paymentAuthorizationViewController called back"];
}

// Sent when the user has selected a new shipping address.  The delegate should inspect the
// address and must invoke the completion block with an updated array of PKPaymentSummaryItem objects.
//
// The delegate will receive no further callbacks except paymentAuthorizationViewControllerDidFinish:
// until it has invoked the completion block.
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                  didSelectShippingAddress:(ABRecordRef)address
                                completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *shippingMethods, NSArray *summaryItems))completion
{
    [self showMessage:@"didSelectShippingAddress called back"];
}

References:

No comments:

Post a Comment