ios Apple Pay 简单使用

配置
1.在开发者帐号中先添加一个 Merchant IDs
2.如果已经有apple id,编辑apple id 在其中打开apple pay 选项,这时候就可选择 刚才添加的 Merchant IDs
3.重新配置provisioning Profiles安装
4.在工程文件选择capatilities 大家app pay选项,然后程序就会自动关联之前设置的 Merchant IDs
5.在程序中导入头文件#import
同时要遵守一个协议
6.代码实现部分

 if([PKPaymentAuthorizationViewController canMakePayments])
    {
        NSLog(@"设备支持支付");
        
        PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
        
        PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"Widget1" amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];
        
        PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"Widget2" amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];
        
        PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"Grand Total" amount:[NSDecimalNumber decimalNumberWithString:@"0.02"]];

        
        request.paymentSummaryItems = @[item1,item2,total];  // 最后一个必须是总价格
        request.countryCode = @"CN";  // 国家
        request.currencyCode = @"CNY";  // 人民币
        
        request.supportedNetworks =@[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkAmex,PKPaymentNetworkDiscover,PKPaymentNetworkInterac, PKPaymentNetworkMasterCard,PKPaymentNetworkPrivateLabel,PKPaymentNetworkVisa];
        
        request.merchantIdentifier = @"merchant.com.testapa";
        request.merchantCapabilities = PKMerchantCapability3DS|PKMerchantCapabilityEMV;
        
        PKPaymentAuthorizationViewController *payvw = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:request];
        
        if (payvw) {
            
            payvw.delegate = self;
            
            [self presentViewController:payvw animated:YES completion:nil];
            
        }else
        {
            
            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil message:@"\n未绑定卡\n" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];

        }
        
        
    
    }else
    {
        
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil message:@"\n此设备不支持apple pay\n" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        NSLog(@"此设备不支持apple pay");
    }
7.实现协议以及回处理

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
    
    
    NSLog(@"payment was authoried %@ ",payment);
    
    PKPaymentToken *payToken = payment.token;
    
    NSLog(@"token = %@ ",payToken);
    
    //支付凭据,发给服务端进行验证支付是否真实有效
//    PKContact *billingContact = payment.billingContact;     //账单信息
//    PKContact *shippingContact = payment.shippingContact;   //送货信息
//    PKContact *shippingMethod = payment.shippingMethod;     //送货方式
    //等待服务器返回结果后再进行系统block调用
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //模拟服务器通信
        completion(PKPaymentAuthorizationStatusSuccess);
    });
    
    
}

- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
    
    NSLog(@"Finishing payment view controller");
    [controller dismissViewControllerAnimated:TRUE completion:nil];

}
8.付款成功后会返回一个token发送给服务器,服务器在收到 App 发送来的支付信息后,对数据进行解密操作,提取其中需要的信息,组织银联接口报文,调用银联的接口,完成扣款,会回调验证结果
9.要保证你的手机已经绑定了银行卡,如果没有去设置-Wallet与Apple Pay 绑定
否则可能会出现This device cannot make payments错误
10. 更多详细方法参考 http://www.cocoachina.com/ios/20160226/15443.html

你可能感兴趣的:(iOS开发)