Apple Pay 初探之Objective-C/Swift

2016-02-19  本文已影响568人  Jason王

2.18日 Apple Pay正式登陆中国了,18号就开始绑定绑定卡片,上午坑次坑次的开始试,试了N多次都不行,可能是因为是苹果的Pay,大家都这么激动,CocoaChina 第一时间体验Apple Pay,能秒微信、支付宝?

首先,准备工作,在开发者中心建Merchant IDs 和Apple Pay证书。Merchant IDs的ID在代码中还是会用到的,建议域名的反向解析。

Apple Pay证书新建的时候要注意:创建一个CSR文件(在Mac上的应用程序文件夹,打开Utilities文件夹并启动钥匙串访问。在钥匙串访问下拉菜单中选择钥匙串访问>证书助理>请求从证书颁发机构颁发的证书。)

勾选上让我指定密钥对信息 苹果上规定是用ECC算法大小256位

生成CSR文件,然后上传,成功后,下载,双击。

如果KeyChain Access中显示了警示信息,表示未知授权签发证书或者无效证书发行人,那么要确保你已经在钥匙链中安装了Worldwide Developer Relations-G2和Apple Root CA-G2。你可以在这个地方下载这些东西:apple.com/certificateauthority.

证书 Xcode里边的设置

Xcode的设置,Gapabilities-->Apple Pay变成 ON,勾选Merchant IDs的ID,还要导入PassKit包

PassKit

基本的配置也就这么多了,好了,下边是代码时间,Objective-C与Swift:

Objective-C:

#import <PassKit/Passkit.h>和PKPaymentAuthorizationViewControllerDelegate 

//是否支持ApplePay系统版本,硬件ParentControl或者是否因为家长控制而不能支付

BOOLisSupportPay = [PKPaymentAuthorizationViewControllercanMakePayments];

if(!isSupportPay) {

return;

}else{

// do something

}

//是否支持这些支持方式(可能没有绑定卡)判断用户是否能够使用你提供的支付网络进行支付

NSArray*netwotks =@[PKPaymentNetworkPrivateLabel,PKPaymentNetworkVisa,PKPaymentNetworkMasterCard];

BOOLcanPay = [PKPaymentAuthorizationViewControllercanMakePaymentsUsingNetworks:netwotks];

if(!canPay) {

//setup

PKPaymentButton*setupButton = [PKPaymentButtonbuttonWithType:PKPaymentButtonTypeSetUpstyle:PKPaymentButtonStyleBlack];

[setupButtonaddTarget:selfaction:@selector(applePaySetupButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:setupButton];

setupButton.center=CGPointMake(self.view.frame.size.width/2,100);

}else{

//发起支付请求

//PKPaymentRequest

PKPaymentRequest*paymentRequest = [PKPaymentRequestnew];

paymentRequest.currencyCode=@"CNY";

paymentRequest.countryCode=@"CN";

paymentRequest.merchantIdentifier=@"merchant.com.hunk.assistants";

//构造金额

// 2.01 subtotal标签文本是一个用户可阅读的摘要项目描述信息,数额是相对应的支付数额

NSDecimalNumber*subtotalAmount = [NSDecimalNumberdecimalNumberWithMantissa:201exponent:-2isNegative:NO];

PKPaymentSummaryItem*subtotal = [PKPaymentSummaryItemsummaryItemWithLabel:@"Subtotal"amount:subtotalAmount];

// 2.00 discount对于折扣或优惠券,则需要把数额设成负数。

NSDecimalNumber*discountAmount = [NSDecimalNumberdecimalNumberWithMantissa:200exponent:-2isNegative:YES];

PKPaymentSummaryItem*discount = [PKPaymentSummaryItemsummaryItemWithLabel:@"Discount"amount:discountAmount];

// 0.01 grand total总计金额应该使用公司的名称做为其标签,使用所有其它项目的金额总和做为金额

NSDecimalNumber*totalAmount = [NSDecimalNumberzero];

totalAmount = [totalAmountdecimalNumberByAdding:subtotalAmount];

totalAmount = [totalAmountdecimalNumberByAdding:discountAmount];

PKPaymentSummaryItem*total = [PKPaymentSummaryItemsummaryItemWithLabel:@"Gou Min Company"amount:totalAmount];

NSArray*summaryItems =@[subtotal, discount, total];

paymentRequest.paymentSummaryItems= summaryItems;

// Shipping Method (skip now)

//支付标准

paymentRequest.supportedNetworks=@[PKPaymentNetworkPrivateLabel,PKPaymentNetworkMasterCard,PKPaymentNetworkVisa];

// Supports 3DS only还可以设置支持其他的

paymentRequest.merchantCapabilities=PKMerchantCapability3DS;

//配送信息以及mail地址

paymentRequest.requiredBillingAddressFields=PKAddressFieldEmail;

paymentRequest.requiredBillingAddressFields=PKAddressFieldEmail|PKAddressFieldPostalAddress;

PKContact*contact = [[PKContactalloc]init];

NSPersonNameComponents*name = [[NSPersonNameComponentsalloc]init];

name.givenName=@"John";

name.familyName=@"Appleseed";

contact.name= name;

CNMutablePostalAddress*address = [[CNMutablePostalAddressalloc]init];

address.street=@"1234 Laurel Street";

address.city=@"Atlanta";

address.state=@"GA";

address.postalCode=@"30303";

contact.postalAddress= address;

paymentRequest.shippingContact= contact;

// Storing Additional Information

// request.applicationData =

// Authorizing Payment show

PKPaymentAuthorizationViewController*viewController = [[PKPaymentAuthorizationViewControlleralloc]initWithPaymentRequest:paymentRequest];

if(!viewController) {

/* ... Handle error ... */

}else{

viewController.delegate =self;

[selfpresentViewController:viewController animated:YEScompletion:nil];

}

}

Swift:

import passkit 和PKPaymentAuthorizationViewControllerDelegate

//

letisSupportPay:Bool=PKPaymentAuthorizationViewController.canMakePayments()

if!isSupportPay {

return;

}else{

//do something

}

letnetWork:Array= [PKPaymentNetworkPrivateLabel,PKPaymentNetworkVisa,PKPaymentNetworkMasterCard]

letcanPay:Bool=PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks(netWork)

if!canPay {

//setup

letsetupButton =PKPaymentButton(type:PKPaymentButtonType.SetUp, style:PKPaymentButtonStyle.Black)

setupButton.addTarget(self, action:"applePaySetupButtonPressed:", forControlEvents:UIControlEvents.TouchUpInside)

self.view.addSubview(setupButton)

setupButton.center=CGPointMake(UIScreen.mainScreen().bounds.width/2,150)

}else{

//PKPaymentRequest

letpaymentRequest =PKPaymentRequest()

paymentRequest.currencyCode="CNY"

paymentRequest.countryCode="CN"

// merchantIdentifier

paymentRequest.merchantIdentifier="merchant.com.hunk.assistants"

paymentRequest.merchantCapabilities= [PKMerchantCapability.Capability3DS,PKMerchantCapability.CapabilityEMV]

paymentRequest.requiredShippingAddressFields=PKAddressField.All

//support Networks

paymentRequest.supportedNetworks= [PKPaymentNetworkVisa,PKPaymentNetworkMasterCard,PKPaymentNetworkAmex]

//subtotal

letsubTotal =PKPaymentSummaryItem(label:"Subtotal", amount:NSDecimalNumber(string:"101.00"))

//discount

letdicount =PKPaymentSummaryItem(label:"Discount", amount:NSDecimalNumber(string:"100.00"))

//tax

lettax =PKPaymentSummaryItem(label:"Tax", amount:NSDecimalNumber(string:"2.00"))

paymentRequest.paymentSummaryItems= [subTotal,dicount,tax]

//show the apple pay controller

letpayAuth =PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)

payAuth.delegate=self

self.presentViewController(payAuth, animated:true, completion:nil)

}

代码可以直接复制粘贴,但是配置的还需要亲力亲为。

参照:

官方:https://developer.apple.com/apple-pay/

http://www.open-open.com/lib/view/open1422324034345.html

Demo:

Swift :https://github.com/Billerlin/Swift_ApplePayDemo

OC:https://github.com/Billerlin/OCApplePayDemo

上一篇下一篇

猜你喜欢

热点阅读