iOS-- ApplePay支付(仅代码)

今天只是整理了代码,配置支付环境改天再写

1.头文件; #import <PassKit/PassKit.h>

2.遵守协议 <PKPaymentAuthorizationViewControllerDelegate>

3.代码

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.判断当前设备是否支持ApplePay
    
    if(![PKPaymentAuthorizationViewController canMakePayments]){
    
    
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持ApplePay" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        
        // 点击知道了直接在这里面写
        
         self.view.hidden = YES;
        
        
    }];
    
    // 添加按钮 将按钮添加到UIAlertController对象上
    [alertController addAction:okAction];
    
    // 将UIAlertController模态出来 相当于UIAlertView show 的方法
    
    [self presentViewController:alertController animated:YES completion:nil];
       
        
    
    }else if(![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay] ]){//判断是否添加了银行卡
        //创建一个跳转按钮。当用户点击按钮时,跳转到添加银行卡的界面
        
        PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
        [button addTarget:self action:@selector(addBankCard) forControlEvents:UIControlEventTouchUpInside];
payButton.frame = CGRectMake(100, 100, 100, 30);
        [self.payView addSubview:button];
    
       }else{//创建支付按钮
    
        PKPaymentButton *payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleBlack];
        [payButton addTarget:self action:@selector(starPay) forControlEvents:UIControlEventTouchUpInside];
           payButton.frame = CGRectMake(100, 100, 100, 30);
           
        [self.view addSubview:payButton];
    
    }
 
}

//跳转到添加银行卡的界面
-(void)addBankCard
{
    
    PKPassLibrary *pl = [[PKPassLibrary alloc] init];
    [pl openPaymentSetup];


}
//购买按钮
-(void)starPay
{
//1创建一个支付请求
   
    PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
    //配置支付请求
    //商家ID
    request.merchantIdentifier = @"merchant.com.huizhaodao.ApplePay";
    //货币代码和国家代码
    request.countryCode = @"CN";
    request.currencyCode = @"CNY";
    
    //请求支持的支付网络
    request.supportedNetworks = @[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
    
    //商户的处理方式
    request.merchantCapabilities = PKMerchantCapability3DS;
    
    //购买商品列表
    NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"100.0"];
    PKPaymentSummaryItem *item = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone6" amount:price];
    
    NSDecimalNumber *price4 = [NSDecimalNumber decimalNumberWithString:@"100.0"];
    PKPaymentSummaryItem *item4 = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone6s" amount:price4];
    
    NSDecimalNumber *price5 = [NSDecimalNumber decimalNumberWithString:@"200.0"];
    PKPaymentSummaryItem *item5 = [PKPaymentSummaryItem summaryItemWithLabel:@"财务" amount:price5];
    
    request.paymentSummaryItems = @[item,item4,item5];
    
    //注意:支付列表最后一个,代表总汇
    
    
    
    //配置请求的附件项
    //发票收货地址
    request.requiredBillingAddressFields = PKAddressFieldAll;
    //快递地址
    request.requiredShippingAddressFields = PKAddressFieldAll;
    //配置快递方式
    NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"10.0"];
    PKShippingMethod *method = [PKShippingMethod summaryItemWithLabel:@"顺丰快递" amount:price1];
    method.detail = @"同城24小时送达";
    method.identifier = @"shunfeng";
    NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"18.0"];
    PKShippingMethod *method2 = [PKShippingMethod summaryItemWithLabel:@"韵达快递" amount:price2];
    method2.identifier = @"yunda";
    method2.detail = @"送货上门。。。。";
    request.shippingMethods = @[method,method2];
    
    //配置 快递类型
    request.shippingType= PKShippingTypeStorePickup;
    
    //添加一些附加数据
    request.applicationData = [@"buyID=1234" dataUsingEncoding:NSUTF8StringEncoding];
    
    
    
    //2 验证用户的支付授权
    PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    if (paymentPane == nil) {
        NSLog(@"授权控制器创建失败");
        return;
    }
    paymentPane.delegate = self;
    
    [self presentViewController:paymentPane animated:YES completion:nil];

    
    

}
#pragma mark PKPaymentAuthorizationViewControllerDelegate--当用户授权成功时调用

//参数一:授权控制器
//参数二:支付对象
//参数三:系统给定的一个代码回调块,我们需要执行这个代码块,来告诉系统当前是支付状态是否成功
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
    //拿到支付信息,发送给服务器,处理之后服务器会返回一个状态,告诉客户端是否支付成功,然后由客户端处理
    BOOL isSucess = YES;
    
    //苹果三方服务器 developer.apple.com/apple-pay/
    
    if(isSucess){
    
        completion(PKPaymentAuthorizationStatusSuccess);
    
    }else{
    
     completion(PKPaymentAuthorizationStatusFailure);
    
    }


}
#pragma mark PKPaymentAuthorizationViewControllerDelegate--当用户授权成功时调用或者取消授权是调用
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];

}

 

转载于:https://my.oschina.net/huangyn/blog/1506815

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值