Apple Pay

主要的申请一个商品的ID,Xcode要支持Apple pay

//
//  ViewController.m
//  TestApplePay
//
//  Created by yinbo on 2016/11/30.
//  Copyright © 2016年 yinbo. All rights reserved.
//

#import "ViewController.h"
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    PKPaymentButton *payBtn=[PKPaymentButton  buttonWithType:PKPaymentButtonTypePlain style:PKPaymentButtonStyleBlack];
    payBtn.center=self.view.center;
    [payBtn addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payBtn];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)payAction:(PKPaymentButton *)btn{
    PKPaymentRequest *request=[[PKPaymentRequest alloc]init];
    request.countryCode=@"CN";
    request.currencyCode=@"CNY";
    request.supportedNetworks=@[PKPaymentNetworkAmex,PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa,PKPaymentNetworkMasterCard,PKPaymentNetworkDiscover,PKPaymentNetworkInterac,PKPaymentNetworkPrivateLabel];
    request.merchantCapabilities=PKMerchantCapabilityDebit|PKMerchantCapabilityCredit|PKMerchantCapabilityEMV;
    request.merchantIdentifier=@"merchant.com.zhuguangmama.TestApplePay";
    request.requiredBillingAddressFields=PKAddressFieldAll;
    request.requiredShippingAddressFields=PKAddressFieldAll;
    NSDecimalNumber *shippingPrice=[NSDecimalNumber decimalNumberWithString:@"11.0"];
    PKShippingMethod *method=[PKShippingMethod summaryItemWithLabel:@"快递公司" amount:shippingPrice];
    method.detail=@"24小时送到!";
    method.identifier=@"kuaidi";
    request.shippingMethods=@[method];
    request.shippingType=PKShippingTypeServicePickup;
    NSString *str=@"商品ID:123456";
    request.applicationData=[str dataUsingEncoding:NSUTF8StringEncoding];
    PKPaymentSummaryItem *item1=[PKPaymentSummaryItem summaryItemWithLabel:@"会员" amount:[NSDecimalNumber decimalNumberWithString:@"11.0"]];
    request.paymentSummaryItems=@[item1];
    PKPaymentAuthorizationViewController *paymentPane=[[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:request];
    paymentPane.delegate=self;
//    [self presentViewController:paymentPane animated:YES completion:nil];
    if([PKPaymentAuthorizationViewController canMakePayments]){
        [self presentViewController:paymentPane animated:YES completion:nil];
    }else{
        NSLog(@"用户不支持Apple pay");
    }
}
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{

    PKPaymentToken *token=payment.token;
    NSLog(@"获取TOKEN---%@",token);
    NSString *address=payment.billingContact.postalAddress.city;
    NSLog(@"获取地址:%@",address);
    NSLog(@"验证通过后,需要开发者继续完成交易");
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
    NSLog(@"取消或者交易成功");
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值