Apple Pay

Apple Pay

Apple Pay 已經在台灣流通一段時間,第三方支付的相關業者也相接著推出串接的服務; 而撇除串接第三方支付的 SDK, iOS 開發者本身的流程呢?

Apple developer 相關設定

首先,你需要先到 Apple developer > Identifiers > Merchant IDs 註冊一個 Identifier

新建完之後,點擊裡頭的 edit,需要補上 CSR 檔; 而如果你只是要測試這個流程,沒有要走到金流相關的測試的話,可以先跳過這段。

Xcode 相關設定

接著到你的專案裡頭,打開 Apple Pay 的開關並填入剛剛註冊的 Merchant ID。

以及 import PassKit,便可以在專案中使用 Apple Pay!

流程解釋

使用者按下 Apple Pay 的按鈕後,我們該做的流程為

  1. 建立訂單(PKPaymentRequest)
  2. 填寫訂單相關內容
  3. 建立 Apple Pay 的畫面(PKPaymentAuthorizationViewController)
  4. 設定 PKPaymentAuthorizationViewController 的 delegate
  5. present Apple Pay 的畫面 而訂單相關內容可以填寫寄送相關(ship)以及聯絡人相關(contact)和金額那些基本的。
func applePay() {
    let request = PKPaymentRequest()
    request.merchantIdentifier = "merchant.tw.archie"
    request.merchantCapabilities = .capability3DS
    request.countryCode = "US"
    request.currencyCode = "USD"
    request.supportedNetworks = [.visa]
    request.paymentSummaryItems = [PKPaymentSummaryItem.init(label: "Archie's shake hand", amount: 2000, type: .final)]
    request.shippingType = .storePickup
    let contact = PKContact()
    contact.emailAddress = "xpopchi@gmail.com"
    var name = PersonNameComponents()
    name.nickname = "Archie"
    contact.name = name
    let phone = CNPhoneNumber(stringValue: "0912345678")
    contact.phoneNumber = phone
    request.billingContact = contact
    let applePayController = PKPaymentAuthorizationViewController(paymentRequest: request)
    applePayController.delegate = self
    present(applePayController, animated: true, completion: nil)
}

畫面上就會出現 Apple Pay 的 View

出現之後⋯⋯

我們可以透過 PKPaymentAuthorizationViewControllerDelegate 來做後續的事情:

 func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        
    }
    
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Swift.Void) {
        
    }
    
    func paymentAuthorizationViewControllerWillAuthorizePayment(_ controller: PKPaymentAuthorizationViewController) {
        
    }
    
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect paymentMethod: PKPaymentMethod, completion: @escaping ([PKPaymentSummaryItem]) -> Void) {
        
    }
    
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect shippingMethod: PKShippingMethod, completion: @escaping (PKPaymentAuthorizationStatus, [PKPaymentSummaryItem]) -> Void) {
        
    }
    
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelectShippingContact contact: PKContact, completion: @escaping (PKPaymentAuthorizationStatus, [PKShippingMethod], [PKPaymentSummaryItem]) -> Void) {
        
    }

像是最重要的是,付款完之後的 payment.token 以及 payment 裡頭的相關資訊等, 再來就看專案要做哪些事情了XD

最後,給設計師

Apple Pay 有規範一些它的 style 相關的設計要求,可以看這邊

comments powered by Disqus