Apple Pay Access Detailed tutorial

Source: Internet
Author: User
Tags deprecated

Apple Pay Access Detailed tutorial

Source: Yasin's Pinterest

Links: http://www.jianshu.com/p/738aee78ba52#

Apple Pay Run Environment: IPhone6 above equipment, operating system minimum iOS9.0 above, some information settings need to iOS9.2 above. Enterprise Certificate Additions are not currently supported.

Environment set up can be run on the simulator above, xcode7.2.1+iphone6sp9.2 system, the system will be bound to several virtual bank cards, and several contacts, convenient debugging, payment will not happen real payment, really very convenient.

[TCO]

Preparatory work

Before you access Apple Pay, you must first apply for Merchantid and corresponding certificates.

Please take a tour of my application Merchantid and corresponding certificate detailed graphic tutorial

Project Setup

Bundleid settings

Enable Apple Pay permissions in capability, and select Merchantid.


After that the project will have one more ApplePay configuration file applepayyasin.entitlements


Libraries that need to be referenced


Xcode7.0 above does not need to manually add the need to refer to the library, only need to import the header file can be


#import <PassKit/PassKit.h>//user-bound bank card information

Display controls for #import <PassKit/PKPaymentAuthorizationViewController.h>//apple pay

#import <AddressBook/AddressBook.h>//user contact information related


Device ApplePay Permission Detection

if (![ Pkpaymentauthorizationviewcontroller class]) {

Pkpaymentauthorizationviewcontroller need iOS8.0 above support

NSLog (@ "Operating system does not support ApplePay, please upgrade to more than 9.0 version, and iPhone6 above equipment is supported");

Return

}

Check whether the current device can be paid

if (![ Pkpaymentauthorizationviewcontroller Canmakepayments]) {

Payment needs iOS9.0 above support

NSLog (@ "Device does not support ApplePay, please upgrade to more than 9.0 version, and iPhone6 above equipment is supported");

Return

}

Check whether the user can pay for some kind of card, whether to support Amex, MasterCard, Visa and UnionPay four kinds of cards, according to the needs of their own projects to detect

Nsarray *supportednetworks = @[pkpaymentnetworkamex, Pkpaymentnetworkmastercard,pkpaymentnetworkvisa, Pkpaymentnetworkchinaunionpay];

if (![ Pkpaymentauthorizationviewcontroller Canmakepaymentsusingnetworks:supportednetworks]) {

NSLog (@ "No payment card bundled");

Return

}


Create a payment request Pkpaymentrequest


    • Initialize Pkpaymentrequest


It is important to note that the currency code for RMB is CNY

Set up basic information such as currency, country code and merchant identifier

Pkpaymentrequest *payrequest = [[Pkpaymentrequest alloc]init];

Payrequest.countrycode = @ "CN"; Country code

Payrequest.currencycode = @ "CNY"; Currency code for RMB

Payrequest.merchantidentifier = @ "merchant.  Applepaydemoyasin "; Merchantid of application

Payrequest.supportednetworks = Supportednetworks; Bank cards that can be paid by the user

Payrequest.merchantcapabilities = pkmerchantcapability3ds|      PKMERCHANTCAPABILITYEMV; Set up a supported transaction processing protocol, 3DS must support, EMV is optional, current domestic words or use both bar

    • Set up the invoice delivery information and the delivery address information, the user can be set up after the agent callback agent to obtain information updates

Payrequest.requiredbillingaddressfields = Pkaddressfieldemail;

If you need to send a bill, you can choose to set it, default Pkaddressfieldnone (do not mail the bill)

Landlord feel the bill mailing address can be in advance to allow users to choose whether or not, otherwise it will increase the customer input trouble degree, experience is not good,

Payrequest.requiredshippingaddressfields = pkaddressfieldpostaladdress| pkaddressfieldphone| Pkaddressfieldname;

Shipping address information, here settings need address and contact and name, if need to set, default Pkaddressfieldnone (no shipping address)

Shipping Information page Show

    • Set up the delivery mode of the goods, do not need not configure

Set up two ways of shipping

Pkshippingmethod *freeshipping = [Pkshippingmethod summaryitemwithlabel:@ "Mail" Amount:[nsdecimalnumber Zero]];

Freeshipping.identifier = @ "Freeshipping";

Freeshipping.detail = @ "6-8 days delivery";

Pkshippingmethod *expressshipping = [Pkshippingmethod summaryitemwithlabel:@ "Fast delivery" Amount:[nsdecimalnumber decimalnumberwithstring:@ "10.00"];

Expressshipping.identifier = @ "expressshipping";

Expressshipping.detail = @ "2-3-hour delivery";

Payrequest.shippingmethods = @[freeshipping, expressshipping];

    • Setup of Billing information

    • Setup for each bill

The bill List uses Pkpaymentsummaryitem to add descriptions and prices, and the price uses Nsdecimalnumber.

Pkpaymentsummaryitem initialization:

Label is the product name or description, amount is the commodity price, the discount is negative, type is the bill for the final price or the estimated price (such as the taxi price estimate)


+ (Instancetype) Summaryitemwithlabel: (NSString *) label amount: (Nsdecimalnumber *) amount;

+ (Instancetype) Summaryitemwithlabel: (NSString *) label amount: (Nsdecimalnumber *) Amount Type: ( Pkpaymentsummaryitemtype) Type ns_available (NA, 9_0);

    • Nsdecimalnumber initialization:

Nsdecimalnumber can use numeric initialization, or strings can be used.

How to use the nsdecimalnumber--decimal number I wrote

    • Add a billing list:

Nsdecimalnumber *subtotalamount = [Nsdecimalnumber decimalnumberwithmantissa:1275 exponent:-2 IsNegative:NO]; 12.75

Pkpaymentsummaryitem *subtotal = [Pkpaymentsummaryitem summaryitemwithlabel:@ "Commodity price" amount:subtotalamount];

Nsdecimalnumber *discountamount = [Nsdecimalnumber decimalnumberwithstring:@ "-12.74"]; -12.74

Pkpaymentsummaryitem *discount = [pkpaymentsummaryitem summaryitemwithlabel:@ "discount" amount:discountamount];

Nsdecimalnumber *methodsamount = [Nsdecimalnumber zero];

Pkpaymentsummaryitem *methods = [Pkpaymentsummaryitem summaryitemwithlabel:@ "parcel post" amount:methodsamount];

Nsdecimalnumber *totalamount = [Nsdecimalnumber zero];

TotalAmount = [TotalAmount decimalnumberbyadding:subtotalamount];

TotalAmount = [TotalAmount decimalnumberbyadding:discountamount];

TotalAmount = [TotalAmount decimalnumberbyadding:methodsamount];

Pkpaymentsummaryitem *total = [Pkpaymentsummaryitem summaryitemwithlabel:@ "Yasin" amount:totalamount]; Finally this is paid to WHO. Haha, pay me!

Summaryitems = [Nsmutablearray arraywitharray:@[subtotal, Discount, methods, total]];

Summaryitems is a list of bills, and the type is Nsmutablearray, which is set as a member variable and can be adjusted for payment amounts in subsequent proxy callbacks.

Payrequest.paymentsummaryitems = Summaryitems;


Display shopping information and make payments

ApplePay controls

Pkpaymentauthorizationviewcontroller *view = [[Pkpaymentauthorizationviewcontroller alloc]initWithPaymentRequest: Payrequest];

View.delegate = self;

[Self Presentviewcontroller:view animated:yes completion:nil];


Pkpaymentauthorizationviewcontrollerdelegate Agent


    • Here are two more classes to introduce

    • Pkpayment Payment Success Information

Pkpaymenttoken *paytoken = Payment.token;

Payment credentials, issued to the server to verify that the payment is true and effective

Pkcontact *billingcontact = payment.billingcontact; Billing information

Pkcontact *shippingcontact = payment.shippingcontact; Shipping Information

Pkcontact *shippingmethod = Payment.shippingmethod; Delivery method

    • Pkcontact Contact information


Nspersonnamecomponents *name = contact.name; Name of contact person

Cnpostaladdress *postaladdress = contact.postaladdress; Contact person Address

NSString *emailaddress = contact.emailaddress; Contact Mailbox

Cnphonenumber *phonenumber = Contact.phonenumber; Contact phone

NSString *supplementarysublocality = contact.supplementarysublocality; Supplementary information, address detailed description, other notes, etc., iOS9.2 and above only

    • Agent Description


Delivery address Callback

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller

Didselectshippingcontact: (pkcontact *) Contact

Completion: (void (^) (pkpaymentauthorizationstatus, Nsarray<pkshippingmethod *> * _nonnull, NSArray< Pkpaymentsummaryitem *> * _nonnull)) completion{

Contact shipping address information, Pkcontact type

Shipping Information Selection callback, if you need to adjust the delivery method according to the delivery address, such as the general area of the Mail + express delivery, remote areas only pay the ordinary distribution, the amount of payment recalculated, you can implement the agent, return to the system: Shippingmethods distribution method, Summaryitems Bill List, if the shipping information is not supported return the desired pkpaymentauthorizationstatus

Completion (pkpaymentauthorizationstatussuccess, Shippingmethods, Summaryitems);

}


Delivery Method Callback

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller

Didselectshippingmethod: (Pkshippingmethod *) ShippingMethod

Completion: (void (^) (pkpaymentauthorizationstatus, Nsarray<pkpaymentsummaryitem *> * _nonnull)) completion{

Delivery method Callback, if you need to adjust the amount of payment according to different shipping methods, such as delivery and payment of expedited delivery, you can implement the agent

Pkshippingmethod *oldshippingmethod = [Summaryitems objectatindex:2];

Pkpaymentsummaryitem *total = [Summaryitems lastobject];

Total.amount = [Total.amount decimalNumberBySubtracting:oldShippingMethod.amount];

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller

Didselectshippingmethod: (Pkshippingmethod *) ShippingMethod

Completion: (void (^) (pkpaymentauthorizationstatus, Nsarray<pkpaymentsummaryitem *> * _nonnull)) completion{

Delivery method Callback, if you need to adjust the amount of payment according to different shipping methods, such as delivery and payment of expedited delivery, you can implement the agent

Pkshippingmethod *oldshippingmethod = [Summaryitems objectatindex:2];

Pkpaymentsummaryitem *total = [Summaryitems lastobject];

Total.amount = [Total.amount decimalNumberBySubtracting:oldShippingMethod.amount];

Total.amount = [Total.amount decimalNumberByAdding:shippingMethod.amount];

[Summaryitems Replaceobjectatindex:2 Withobject:shippingmethod];

[Summaryitems Replaceobjectatindex:3 withobject:total];

Completion (pkpaymentauthorizationstatussuccess, summaryitems);

} total.amount = [Total.amount decimalNumberByAdding:shippingMethod.amount];

[Summaryitems Replaceobjectatindex:2 Withobject:shippingmethod];

[Summaryitems Replaceobjectatindex:3 withobject:total];

Completion (pkpaymentauthorizationstatussuccess, summaryitems);

}


Payment Card Selection Callback

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller Didselectpaymentmethod: (Pkpaymentmethod *) PaymentMethod Completion: (void (^) (Nsarray<pkpaymentsummaryitem *> * _nonnull)) completion{

Pay the bank card callback, if you need to adjust the payment amount according to different banks, you can implement the agent

Completion (SUMMARYITEMS);

}


Delivery address callback, deprecated

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller Didselectshippingaddress: (ABRECORDREF) address completion: (void (^) (Pkpaymentauthorizationstatus, nsarray< Pkshippingmethod *> * _nonnull, Nsarray<pkpaymentsummaryitem *> * _nonnull)) completion{

Delivery address callback, deprecated

}

Payment successful Apple server return information callback, do server authentication

-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller

Didauthorizepayment: (pkpayment *) payment

Completion: (void (^) (pkpaymentauthorizationstatus status)) completion {

Pkpaymenttoken *paytoken = Payment.token;

Payment credentials, issued to the server to verify that the payment is true and effective

Pkcontact *billingcontact = payment.billingcontact; Billing information

Pkcontact *shippingcontact = payment.shippingcontact; Shipping Information

Pkcontact *shippingmethod = Payment.shippingmethod; Delivery method

Wait for the server to return results before making a system block call

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

Analog server communication

Completion (pkpaymentauthorizationstatussuccess);

});

}

Payment Completion Callback

-(void) Paymentauthorizationviewcontrollerdidfinish: (Pkpaymentauthorizationviewcontroller *) controller{

[Controller Dismissviewcontrolleranimated:yes Completion:nil];

}

Apple Pay Access Detailed tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.