苹果支付教程(2)

Apple pay --作为开发人员你必要了解的东西

字数596  阅读1259  评论2 

最近一个特别火的词组出现在我们的生活中Apple Pay,大家又对其了解多少呢?其实在 iOS8.1 之后 Apple Pay 功能已经开放了,只不过在大陆在今年 2 月才可以使用,Apple Pay 是一个基于 NFC 的支付系统,类似于你的银行卡,走到哪刷到哪,那么作为开发人员的我们该如何去开发和使用呢?

1、我们要去设置Merchant IDs


Merchat IDs 设置
在设置 Identifier 的时候要注意 ID 的格式

Merchat IDs identifier 设置

注册成功以后

点击Edit

点击Continue

Choose File

从证书颁发机构请求证书

选择

下载

2、到目前为止我们的基本设置已经设置好了,现在我们要去Xcode里面去配置相关信息

由于我们要在真机上测试,所以我们要选择描述文件

屏幕快照 2016-02-23 16.16.04.png
我们在Xcode里面把Apple pay打开

屏幕快照 2016-02-23 16.15.46.png

3、代码示范

遵循PKPaymentAuthorizationViewControllerDelegate

#pragma mark ----支付状态
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
    NSLog(@"花费: %@", payment);

    BOOL asyncSuccessful = FALSE;
    if(asyncSuccessful) {
        completion(PKPaymentAuthorizationStatusSuccess);

        NSLog(@"支付成功");

    } else {
        completion(PKPaymentAuthorizationStatusFailure);

        NSLog(@"支付失败");
    }
}

#pragma mark ----支付完成
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {

    // 支付完成后让支付页面消失
    [controller dismissViewControllerAnimated:YES completion:nil];
}
支付方法
#pragma mark -----点击支付
- (IBAction)checkOut:(id)sender
{
    if([PKPaymentAuthorizationViewController canMakePayments]) {

        NSLog(@"支持支付");

        // 我们后面创建出来的支付页面就是根据这个request
        PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
        // 商品目录
        PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"大展"
                                                                            amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];

        PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"小展"
                                                                            amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];

        PKPaymentSummaryItem *widget3 = [PKPaymentSummaryItem summaryItemWithLabel:@"2个大展"
                                                                            amount:[NSDecimalNumber decimalNumberWithString:@"0.02"]];

        PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@"总金额" amount:[NSDecimalNumber decimalNumberWithString:@"0.01"] type:PKPaymentSummaryItemTypeFinal];

        request.paymentSummaryItems = @[widget1, widget2, widget3, widget4];
        request.countryCode = @"CN";
        request.currencyCode = @"CNY";//人民币
        request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];

        // 这里填的是就是我们创建的merchat IDs
        request.merchantIdentifier = @"merchant.com.dazhan.applePayDemo";
        request.merchantCapabilities = PKMerchantCapabilityCredit;
        //增加邮箱及地址信息
        request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;

        // 根据request去创建支付页面
        PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];

        // 设置代理
        paymentPane.delegate = self;

        if (!paymentPane) {

            NSLog(@"出问题了");
        } else {

            // 模态推出页面
            [self presentViewController:paymentPane animated:YES         completion:nil];
        }     
    } else {

        NSLog(@"该设备不支持支付");
    }
}

效果图.gif

你可能感兴趣的:(支付)