ApplePay

https://developer.apple.com/apple-pay/get-started/cn/

  • 注册并配置商业标识符
    商业标识符可以多选或单选


    Paste_Image.png

    Merchant IDs 选中创建的ID
    点击edit.
    点击Create Certificate,
    需要在keychain创建证书请求文件,生成证书

Paste_Image.png
Paste_Image.png

创建支持的applePay的bundle ID,然后edit,选择刚才创建的MerchantID.
将bundleID绑定商业ID

Paste_Image.png

在xcode的ca'pcapabilities开启ApplePay

Paste_Image.png

此证书是未知机构颁发的解决方案(下载安装G2证书)
http://www.apple.com/certificateauthority/

Paste_Image.png
  • 代码实现

导入Passkit

import <PassKit/Passkit.h>


#import "ViewController.h"
#import <PassKit/Passkit.h>

@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *payView;

@end

@implementation ViewController
- (IBAction)payBtnClick:(id)sender {
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //1.判断当前是否支持applePay
    if (![PKPaymentAuthorizationController canMakePayments]) {
        NSLog(@"当前设备不支持");
        return;
    }
    //2.判断是否添加银行卡
    if (![PKPaymentAuthorizationController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]]) {
        //跳转到添加银行卡
        PKPaymentButton * setupBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
        [self.payView addSubview:setupBtn];
        [setupBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
        return;
    }
    
    //添加购买按钮
    PKPaymentButton * payBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
    [self.payView addSubview:payBtn];
    [payBtn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];

}

- (void)jump{
    //跳转到wallet界面
    PKPassLibrary *pl = [[PKPassLibrary alloc] init];
    [pl openPaymentSetup];
}

- (void)buy{
    //1.创建支付请求
    PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
    //商家ID
    request.merchantIdentifier = @"merchant.com.session.payDemo";
    //货币类型
    request.countryCode = @"CN";
    request.currencyCode = @"CNY";
    //支持的支付网络
    request.supportedNetworks = @[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
    //商家的请求方式
    request.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityEMV;
    //支付的商品
    PKPaymentSummaryItem *item = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone9" amount:[NSDecimalNumber decimalNumberWithString:@".1"]];
     PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhon10" amount:[NSDecimalNumber decimalNumberWithString:@".1"]];
     PKPaymentSummaryItem *item3 = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone11" amount:[NSDecimalNumber decimalNumberWithString:@".1"]];
     PKPaymentSummaryItem *item4 = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone12" amount:[NSDecimalNumber decimalNumberWithString:@".1"]];
     PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"总价" amount:[NSDecimalNumber decimalNumberWithString:@".11"]];//最后一个必须是前面的总价
    request.paymentSummaryItems = @[item,item2,item3,item4,total];
    
    //配置请求附加项
//    request.requiredBillingAddressFields = PKAddressFieldAll; //需要填写发票配送地址
//    request.requiredShippingAddressFields = PKAddressFieldAll;//需要填写快递地址
 
    PKShippingMethod * method1 = [PKShippingMethod summaryItemWithLabel:@"顺丰快递" amount:[NSDecimalNumber decimalNumberWithString:@"18"]];
    method1.identifier = @"shunfeng";
    method1.detail = @"次日达";
    PKShippingMethod *method2 = [PKShippingMethod summaryItemWithLabel:@"韵达快递" amount:[NSDecimalNumber decimalNumberWithString:@"12"]];
    method2.identifier = @"yunda";
    method2.detail = @"很慢";
    request.shippingMethods = @[method1,method2]; //快递方式
    /*PKShippingTypeShipping 选择送货方式
     PKShippingTypeDelivery 选择送货方式
     PKShippingTypeStorePickup 选择取货方式
     PKShippingTypeServicePickup 选择取货方式
     */
    request.shippingType = PKShippingTypeServicePickup; //文字展示样式
    
//    request.billingContact
    //附加数据
    request.applicationData = [@"BUYID = 111111" dataUsingEncoding:NSUTF8StringEncoding];
    
    //2.验证用户支付授权
    PKPaymentAuthorizationViewController *payVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    payVC.delegate = self;
    [self presentViewController:payVC animated:YES completion:nil];
    
}


#pragma mark - PKPaymentAuthorizationViewControllerDelegate
//授权成功后会来到这里
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{
    BOOL success = NO;
    
    //将payment参数发给自己的服务器或者第三方集成的服务器
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (success) {
            completion(PKPaymentAuthorizationStatusSuccess);
        }
        else{
            completion(PKPaymentAuthorizationStatusFailure);
        }
    });
  
    
}
//支付取消或完毕都会来到这里
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

@end

Apple Pay第三方服务器
https://developer.apple.com/apple-pay/ 网址底部

Paste_Image.png

银联开发平台
银联applePay资料
银联demo

推荐阅读更多精彩内容