主要的申请一个商品的ID,Xcode要支持Apple pay
//
// ViewController.m
// TestApplePay
//
// Created by yinbo on 2016/11/30.
// Copyright © 2016年 yinbo. All rights reserved.
//
#import "ViewController.h"
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
PKPaymentButton *payBtn=[PKPaymentButton buttonWithType:PKPaymentButtonTypePlain style:PKPaymentButtonStyleBlack];
payBtn.center=self.view.center;
[payBtn addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:payBtn];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)payAction:(PKPaymentButton *)btn{
PKPaymentRequest *request=[[PKPaymentRequest alloc]init];
request.countryCode=@"CN";
request.currencyCode=@"CNY";
request.supportedNetworks=@[PKPaymentNetworkAmex,PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa,PKPaymentNetworkMasterCard,PKPaymentNetworkDiscover,PKPaymentNetworkInterac,PKPaymentNetworkPrivateLabel];
request.merchantCapabilities=PKMerchantCapabilityDebit|PKMerchantCapabilityCredit|PKMerchantCapabilityEMV;
request.merchantIdentifier=@"merchant.com.zhuguangmama.TestApplePay";
request.requiredBillingAddressFields=PKAddressFieldAll;
request.requiredShippingAddressFields=PKAddressFieldAll;
NSDecimalNumber *shippingPrice=[NSDecimalNumber decimalNumberWithString:@"11.0"];
PKShippingMethod *method=[PKShippingMethod summaryItemWithLabel:@"快递公司" amount:shippingPrice];
method.detail=@"24小时送到!";
method.identifier=@"kuaidi";
request.shippingMethods=@[method];
request.shippingType=PKShippingTypeServicePickup;
NSString *str=@"商品ID:123456";
request.applicationData=[str dataUsingEncoding:NSUTF8StringEncoding];
PKPaymentSummaryItem *item1=[PKPaymentSummaryItem summaryItemWithLabel:@"会员" amount:[NSDecimalNumber decimalNumberWithString:@"11.0"]];
request.paymentSummaryItems=@[item1];
PKPaymentAuthorizationViewController *paymentPane=[[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:request];
paymentPane.delegate=self;
// [self presentViewController:paymentPane animated:YES completion:nil];
if([PKPaymentAuthorizationViewController canMakePayments]){
[self presentViewController:paymentPane animated:YES completion:nil];
}else{
NSLog(@"用户不支持Apple pay");
}
}
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{
PKPaymentToken *token=payment.token;
NSLog(@"获取TOKEN---%@",token);
NSString *address=payment.billingContact.postalAddress.city;
NSLog(@"获取地址:%@",address);
NSLog(@"验证通过后,需要开发者继续完成交易");
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
NSLog(@"取消或者交易成功");
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end