zoukankan      html  css  js  c++  java
  • ApplePay

      ApplePay要在项目有里配置,,配置好项目之后,就剩下编码了,做ApplePay首先要检查设备是否支持ApplePay,支持 ApplePay的设备在 iPhone6及以后,  PKPaymentButton按钮在 iOS 8.3以后可以使用,中国银联卡在 iOS 9.2及以后可以使用。

    代码:

    ViewController
    
    #import "ViewController.h"
    #import <PassKit/PassKit.h>
    #import "UIView+ShowAlert.h"
    
    #define kDevice_SystemVersion [[UIDevice currentDevice].systemVersion floatValue]
    
    //用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa
    #define SupportApplePayNetworks @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa]
    
    //用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa与银联四种卡
    #define SupportApplePayNetworksAndChina @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]
    
    @interface ViewController () <PKPaymentAuthorizationViewControllerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // PKPaymentButton 按钮 8.3 之后
        if (kDevice_SystemVersion >= 8.3) {
            PKPaymentButton *payButton = [[PKPaymentButton alloc] initWithPaymentButtonType:PKPaymentButtonTypeBuy paymentButtonStyle:PKPaymentButtonStyleBlack];
            payButton.center = self.view.center;
            [self.view addSubview:payButton];
    
            [payButton addTarget:self action:@selector(pay:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    - (IBAction)pay:(id)sender {
    
        // 苹果支付 8.3 版本之后可以用,如果在中国是 9.2 之后可以
        if (![PKPaymentAuthorizationViewController class] || kDevice_SystemVersion < 8.3) {
            [self.view myShowAlertView:@"OS does not support Apple Pay, please upgrade to version 8.3 or above. Only iPhone6 or above devices are supported."];
            return;
        }
    
        //检查当前设备是否可以支付
        if(![PKPaymentAuthorizationViewController canMakePayments]) {
            [self.view myShowAlertView:@"The device does not support Apple Pay, please upgrade to version 8.3 or above. Only iPhone6 or above devices are supported."];
            return;
        }
    
        //检查用户是否可进行某种卡的支付,银联卡9.2之后才支持。 根据自己项目的需要进行检测
        NSArray *supportedNetworks = kDevice_SystemVersion >= 9.2 ? SupportApplePayNetworksAndChina:SupportApplePayNetworks;
        if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworks]) {
            [self.view myShowAlertView:@"No payment card binding."];
            return;
        }
    
    
        PKPaymentRequest *paymentRequest = [[PKPaymentRequest alloc] init];
    
        PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"ROMWE" amount:[NSDecimalNumber decimalNumberWithString:@"33.99"]];
    
        paymentRequest.paymentSummaryItems = @[total];
    
        // currencyCode 设置币种
    //    paymentRequest.currencyCode = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];//用户当前地区的币种
        //    // 人民币
            paymentRequest.currencyCode = @"CNY";
    
        // countryCode 设置国家
        paymentRequest.countryCode = @"CN";  // 中国    注意:currencyCode 和 countryCode 应该是对应的额
    
        // 在 developer.apple.com member center 里设置的 merchantID
        paymentRequest.merchantIdentifier = @"merchant.com.romwe.dotfashion";
    
        // PKMerchantCapabilityCredit 在真机上无法回调 `didAuthorizePayment` 方法
        paymentRequest.merchantCapabilities = PKMerchantCapability3DS;
    
        // 支持哪种结算网关
        paymentRequest.supportedNetworks = supportedNetworks;
    
    
        PKPaymentAuthorizationViewController *paymentVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:paymentRequest];
        if (paymentVC) {
            paymentVC.delegate = self;
            [self presentViewController:paymentVC animated:YES completion:NULL];
        } else {
            [self.view myShowAlertView:@"You can only use debit or credit card provided by:American express, Visa, Mastercard, Union pay"];
        }
    
    }
    
    #pragma mark - Payment delegate
    //支付卡选择回调
    - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectPaymentMethod:(PKPaymentMethod *)paymentMethod completion:(void (^)(NSArray<PKPaymentSummaryItem *> * _Nonnull))completion {
        NSLog(@"didSelectPaymentMethod");
        completion(@[]);
    }
    
    //送货地址回调
    -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingContact:(PKContact *)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion {
        NSLog(@"didSelectShippingContact");
    }
    
    //送货方式回调
    - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingMethod:(PKShippingMethod *)shippingMethod completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion {
        NSLog(@"didSelectShippingMethod");
    }
    
    //付款成功苹果服务器返回信息回调,做服务器验证
    - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion {
        NSLog(@"---------------did authorize payment token: %@, %@", payment.token, payment.token.transactionIdentifier);
    
        // 发送网络 请求  做服务器验证
        BOOL seccess; // 服务器返回成功
        if (/* 请求成功 */seccess) {
            completion(PKPaymentAuthorizationStatusSuccess);
        } else {
            completion(PKPaymentAuthorizationStatusFailure);
        }
    }
    
    //支付完成回调
    - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
        [controller dismissViewControllerAnimated:controller completion:NULL];
    
        // 支付完成之后可以在这里做一些逻辑,比如跳转到订单详情
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor redColor];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    @end

    Demo:http://files.cnblogs.com/files/10-19-92/ApplePayDemo.zip

  • 相关阅读:
    虚拟机安装 ubuntu 后,更新源无效,以及无法联网安装软件的问题
    使用IE滤镜实现css3中rgba让背景色透明的效果
    C# 代理应用
    通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页
    ICMP:internet 控制报文协议
    多模板支持
    C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
    IOS中KVO模式的解析与应用
    ASP.NET MVC实现仪表程序
    spring和redis的整合
  • 原文地址:https://www.cnblogs.com/10-19-92/p/5205899.html
Copyright © 2011-2022 走看看