ApplePay测试开发心得

ApplePay测试开发心得


上周本身测试了一下ApplePay的开发. 今天面试的一个iOS开发, 我说ApplePay和3D-Touch, 那货居然一愣一愣的... 看来仍是有不少人不太会的, 分享一下吧, 让更多的人学习.html

不试不知道, 一试吓一跳, ApplePay开发居然这么简单....ios

苹果ApplePay开发文档 对于这种新技术, 最靠谱的就是苹果的开发文档了, 对染全是英文的, 可是看多了天然就能看懂了, 做为一名iOS程序猿, 我认为好好养成读苹果官方文档这个习惯仍是不错的.面试

第一步: 配置环境

Applepay配置环境 苹果官方写的这么详细, 我就不写了, 按照步骤一步一步来就好了.服务器

配置环境的过程就这么简单, 可是一步也不能少, 必须所有配置完.app

第二步: 上代码

Apple Pay,是苹果公司在2014苹果秋季新品发布会上发布的一种基于NFC的手机支付功能,于2014年10月20日在美国正式上线。 2014年秋季applepay面世, 2016.2.18 ApplePay正式登录中国, 说这些废话是想表达, ApplePay是个新东西, 因此它必定不会太难 ! 另外 苹果官方文档上面, 对代码部分也有很详细的教程.ide

如下代码就是唤出苹果支付的控制器, 可唤出控制器以前进行一些基本的设置. 详情请看注释, 也是很简单的, 咱们能够经过代码设置是否展现收货地址, 发票地址抬头等信息, 也能够设置默认的联系人, 付款信息等.post

本身能够根据本身项目须要进行设置. 代理方法中的didAuthorizePayment:将在用户受权成功后调用, 受权成功以后咱们能够经过代理方法参数中的payment拿到PKPaymentToken(用于去请求银联支付), 发票抬头, 收货地址, 收货人等等信息. 这些信息也能够经过PKPaymentAuthorizationViewControllerDelegate中其它的代理方法拿到.学习

- (IBAction)openApplePay:(UIButton *)sender {
    NSLog(@"打开苹果支付");
    
    //1.
    // Determine whether this device can process payment requests.
    // YES if the device is generally capable of making in-app payments.
    // NO if the device cannot make in-app payments or if the user is restricted from authorizing payments.
    if (![PKPaymentAuthorizationViewController canMakePayments]) {
        NSLog(@"设备不支持ApplePay");
        return;
    }
    
    //2.
    // Determine whether this device can process payment requests using specific payment network brands.
    // Your application should confirm that the user can make payments before attempting to authorize a payment.
    // Your application may also want to alter its appearance or behavior when the user is not allowed
    // to make payments.
    // YES if the user can authorize payments on this device using one of the payment networks supported
    // by the merchant.
    // NO if the user cannot authorize payments on these networks or if the user is restricted from
    if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa]]) {
        NSLog(@"不支持银联和visa");
        [[[PKPassLibrary alloc] init] openPaymentSetup]; //不支持指定的卡片, 去打开设置添加卡片
        return;
    }
    
    NSLog(@"进行支付...");
    
    // *建立支付请求
    PKPaymentRequest *payRequest = [[PKPaymentRequest alloc] init];
    // *设置商户id
    payRequest.merchantIdentifier = @"merchant.com.walden.Applepay";
    // *设置国家代码
    payRequest.countryCode = @"CN";
    // *设置支持的卡片类型
    payRequest.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa];
    // *设置商户支付标准
    payRequest.merchantCapabilities = PKMerchantCapability3DS;
    // *设置货币单位
    payRequest.currencyCode = @"CNY";
    // *设置商品
    PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"茅台" amount:[NSDecimalNumber decimalNumberWithString:@"859.3"] type:PKPaymentSummaryItemTypeFinal];
    PKPaymentSummaryItem *allItem = [PKPaymentSummaryItem summaryItemWithLabel:@"小蜜蜂公司" amount:[NSDecimalNumber decimalNumberWithString:@"858.3"]];
    payRequest.paymentSummaryItems = @[item1, allItem];
    
    // billing 收据, 设置收据/发票必填内容
    payRequest.requiredBillingAddressFields = PKAddressFieldAll;
    
    // shipping 邮寄,运送, 设置邮寄地址必填选项
    payRequest.requiredShippingAddressFields = PKAddressFieldAll;
    
    PKShippingMethod *shippingMethod1 = [PKShippingMethod summaryItemWithLabel:@"EMS快递" amount:[NSDecimalNumber decimalNumberWithString:@"8"]];
    shippingMethod1.identifier = @"ems";
    shippingMethod1.detail = @"24小时内送达";
    
    PKShippingMethod *shippingMethod2 = [PKShippingMethod summaryItemWithLabel:@"圆通快递" amount:[NSDecimalNumber decimalNumberWithString:@"9.9"]];
    shippingMethod2.identifier = @"yuantong";
    shippingMethod2.detail = @"环线内免费送";
    
    payRequest.shippingMethods = @[shippingMethod1, shippingMethod2];

    
    //If you have up-to-date billing and shipping contact information, you can set those on the payment request. Apple Pay uses this information by default; however, the user can still choose other contact information as part of the payment authorization process.
    // This Will be the defualt information
    PKContact *contact = [[PKContact alloc] init];
    NSPersonNameComponents *name = [[NSPersonNameComponents alloc] init];
    name.givenName = @"为";
    name.familyName = @"东方";
    
    contact.name = name;
    
    CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
    address.street = @"不知道 Laurel Street";
    address.city = @"上海Atlanta";
    address.state = @"GA";
    address.postalCode = @"30303";
    
    contact.postalAddress = address;
    
    payRequest.billingContact = contact;
    payRequest.shippingContact = contact;
    
    
    // 建立支付的控制器, 并madal出来这个控制器
    PKPaymentAuthorizationViewController *payVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payRequest];
    payVC.delegate = self;
    [self presentViewController:payVC animated:YES completion:nil];
    
    
    //PKPaymentButton
}


 #pragma mark - PKPaymentAuthorizationViewControllerDelegate-------------
// 受权支付后调用这个方法, 可以拿到受权码, 能够去服务器进行支付
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
                       didAuthorizePayment:(PKPayment *)payment
                                completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
    NSLog(@"%s", __func__);
    // 把支付信息发送给服务器进行处理
    
    // 根据服务器返回的支付成功与否  进行不一样的显示(调用block传不一样的枚举)
    completion(PKPaymentAuthorizationStatusSuccess);
}

// 支付成功或者失败以后, 调用这个方法会取消弹出的控制器
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
    NSLog(@"%s", __func__);
    [self dismissViewControllerAnimated:YES completion:nil];
}
相关文章
相关标签/搜索