Apple Pay is coming, what developers should do

Apple Pay is coming, what developers should do

"iOS8.1 already has this feature, but it was not as popular as it is now. The current trend is that it will become popular, so many e-commerce platforms B2B, P2P, C2C, and X2X may need this payment function. Let's do a simple study here."

The first step is to change the target to iOS 8.1. Of course, to support China, you need 9.2 or above and iPhone 6 or above. Then set Apple Pay to on in the project Capabilities. This will automatically import the required library files, then add a permissions file and set it, and finally modify or create your App ID.

As the image above shows, you may notice that there is no valid Merchant ID. We need to create one. Visit the Identifiers > Merchant ID page in the Apple iOS Developer Center. Then follow the instructions to create a Merchant ID and register it. This is similar to other certificates and I won't go into more detail here. The image below is a reference image that has already been created.

Download and install, then refresh the interface of the project just now, select the check mark, and you can see the following figure

Here is a brief description of the code:

In the .h file

  1. # import [UIKit/UIKit.h] (Due to recognition issues, the angle brackets are changed to square brackets here)
  2. # import [PassKit/PassKit.h]
  3. @interface ViewController : UIViewController @end  

.m file

  1. # import   "ViewController.h"  
  2.   
  3. @interface ViewController ()
  4.   
  5. @end  
  6.   
  7. @implementation ViewController
  8.   
  9.   
  10. - ( void )viewDidLoad {
  11. [ super viewDidLoad];
  12. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  13. btn.frame = CGRectMake( 60 , 100 , 200 , 50 );
  14. btn.center = self.view.center;
  15. [btn setBackgroundImage:[UIImage imageNamed:@ "ApplePayBTN_64pt__whiteLine_textLogo_" ] forState:UIControlStateNormal];
  16.   
  17. [btn addTarget:self action: @selector (ApplePay) forControlEvents:UIControlEventTouchUpInside];
  18. [self.view addSubview:btn];
  19.   
  20. }
  21.   
  22. #pragma mark ----payment status
  23. - ( void )paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
  24. didAuthorizePayment:(PKPayment *)payment
  25. completion:( void (^)(PKPaymentAuthorizationStatus status))completion
  26. {
  27. NSLog(@ "Payment was authorized: %@" , payment);
  28.   
  29. BOOL asyncSuccessful = FALSE;
  30. if (asyncSuccessful) {
  31. completion(PKPaymentAuthorizationStatusSuccess);
  32.   
  33. // do something to let the user know the status  
  34.   
  35. NSLog(@ "Payment successful" );
  36.   
  37. } else {
  38. completion(PKPaymentAuthorizationStatusFailure);
  39.   
  40. // do something to let the user know the status  
  41. NSLog(@ "Payment failed" );
  42.   
  43. }
  44.   
  45. }
  46. #pragma mark ----Start payment
  47. - ( void )ApplePay{
  48. if ([PKPaymentAuthorizationViewController canMakePayments]) {
  49.   
  50. NSLog(@ "Support payment" );
  51.   
  52. PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
  53.   
  54. PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@ "Egg"  
  55. amount:[NSDecimalNumber decimalNumberWithString:@ "0.99" ]];
  56.   
  57. PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@ "Apple"  
  58. amount:[NSDecimalNumber decimalNumberWithString:@ "1.00" ]];
  59.   
  60. PKPaymentSummaryItem *widget3 = [PKPaymentSummaryItem summaryItemWithLabel:@ "2 apples"  
  61. amount:[NSDecimalNumber decimalNumberWithString:@ "2.00" ]];
  62.   
  63. PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@ "total amount" amount:[NSDecimalNumber decimalNumberWithString:@ "3.99" ] type:PKPaymentSummaryItemTypeFinal];
  64.   
  65. request.paymentSummaryItems = @[widget1, widget2, widget3, widget4];
  66.   
  67. request.countryCode = @ "CN" ;
  68. request.currencyCode = @ "CHW" ;
  69. //This property limits the payment card that can be paid. PKPaymentNetworkChinaUnionPay supports Chinese cards. 9.2 added  
  70. request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
  71. request.merchantIdentifier = @ "merchant.com.example.lbapplepaydemo" ;
  72. /*
  73. PKMerchantCapabilityCredit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 2, // Support credit cards
  74. PKMerchantCapabilityDebit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 3 // Debit card support
  75. */  
  76. request.merchantCapabilities = PKMerchantCapabilityCredit;
  77. //Add email and address information  
  78. request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;
  79. PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
  80. paymentPane.delegate = self;
  81.   
  82.   
  83. if (!paymentPane) {
  84.   
  85.   
  86.   
  87. NSLog(@ "something went wrong" );
  88.   
  89. }
  90.   
  91. [self presentViewController:paymentPane animated:YES completion:nil];
  92.   
  93.   
  94. } else {
  95. NSLog(@ "This device does not support payment" );
  96. }
  97.   
  98. }
  99.   
  100. #pragma mark ----Payment completed
  101. - ( void )paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
  102. {
  103.   
  104. [controller dismissViewControllerAnimated:TRUE completion:nil];
  105. }
  106.   
  107.   
  108. @end  

During this process you may encounter the following problems:

1. I don’t know how to check countryCode. I wrote this as China’s countryCode query website.

2. I don’t know how to check currencyCode. So far, I still don’t understand it. There are a lot of prompts in the error message, just pick one at random.

3. I am one of those who don’t have an iPhone 6…

For other properties that I think are more important, please see the Demo for details:

The above pictures are all simulators

<<:  【Special Topic】The 5th Global Mobile Game Conference GMGC2016

>>:  Media comments on the main function of Apple Pay:

Recommend

This amazing scientific phenomenon, even Newton made a mistake about it

Long ago, the ancient Egyptians and Mesopotamians...

In this astronomical "holy land", look for the interstellar glow

Author: Du Lian Audit│Ding Yi Editor: Zhao Jingyu...

Methods and techniques to build a brand without spending money

Everyone must like the idea of ​​building a brand...

Analysis of brand event marketing!

In recent years, the life cycle of traffic stars,...

2B operation: How to reach target users?

I haven’t posted an article for a long time. As a...

Deconstructing the universal formula for e-commerce operations

E-commerce operation is the most special operatio...