Apple Pay集成 – Swift

huangapple go评论89阅读模式
英文:

Apple Pay integration - Swift

问题

我正在集成Apple Pay。我正在遵循Apple Pay的文档。以下是我的代码。

import UIKit
import PassKit

class ApplePayViewController: UIViewController {
    
    @IBOutlet var lblLoading: UILabel!
    @IBOutlet var loader: UIActivityIndicatorView!
    
    var totalAmount = "100.0"
    let paymentRequest = PKPaymentRequest()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置付款请求
        paymentRequest.merchantIdentifier = "merchant.com.apple.example"
        paymentRequest.supportedNetworks = [.visa, .masterCard, .amex, .discover]
        paymentRequest.merchantCapabilities = .capability3DS
        paymentRequest.countryCode = "US"
        paymentRequest.currencyCode = "USD"
        
        // 添加支付项目
        let item = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: totalAmount))
        paymentRequest.paymentSummaryItems = [item]
        
        // 检查设备是否支持付款
        if PKPaymentAuthorizationViewController.canMakePayments() {
            let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
            authorizationViewController?.delegate = self
            present(authorizationViewController!, animated: true, completion: nil)
        } else {
            SharedManager.showAlertWithMessage(title: NSLocalizedString("Sorry", comment: ""), alertMessage: "This device is not capable of making payments.", viewController: self)
        }
    }
}

extension ApplePayViewController: PKPaymentAuthorizationViewControllerDelegate {
    
    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
        // 通过服务器验证支付
        // ...
        
        completion(PKPaymentAuthorizationResult(status: .success, errors: nil))
    }
}

didAuthorizePayment代理方法中,我不知道如何与服务器验证支付。在文档中,我找不到任何与此相关的函数或发送数据的信息。你能帮我吗?

谢谢。

英文:

I am integrating apple pay. I am following applepay documentation. Here is my code.

import UIKit
import PassKit

class ApplePayViewController: UIViewController {

@IBOutlet var lblLoading : UILabel!
@IBOutlet var loader : UIActivityIndicatorView!

var totalAmount = "100.0"
let paymentRequest = PKPaymentRequest()
override func viewDidLoad() {
    super.viewDidLoad()
    // Set up the payment request
    paymentRequest.merchantIdentifier = "merchant.com.apple.example"
    paymentRequest.supportedNetworks = [.visa, .masterCard, .amex, .discover]
    paymentRequest.merchantCapabilities = .capability3DS
    paymentRequest.countryCode = "US"
    paymentRequest.currencyCode = "USD"
    
    // Add a payment item
    let item = PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: totalAmount))
    paymentRequest.paymentSummaryItems = [item]
    
    // Check if the device is capable of making payments
    if PKPaymentAuthorizationViewController.canMakePayments() {
        let authorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        authorizationViewController?.delegate = self
        present(authorizationViewController!, animated: true, completion: nil)
    } else {
        SharedManager.showAlertWithMessage(title: NSLocalizedString("Sorry", comment: ""), alertMessage: "This device is not capable of making payments.", viewController: self)
    }
}}

extension ApplePayViewController: PKPaymentAuthorizationViewControllerDelegate {

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
    controller.dismiss(animated: true, completion: nil)
}

func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
    // Verify the payment with your server
    // ...
    
    completion(PKPaymentAuthorizationResult(status: .success, errors: nil))
}}

In didAuthorizePayment delegate i dont know how to verify the payment with server. I cannot find any function or post data related to this in document. Can you help me with this.

Thank you

答案1

得分: 1

需要有一个支付处理器(比如Stripe)来实际执行交易。

查看这个教程:https://www.kodeco.com/2113-apple-pay-tutorial-getting-started

它解释了如何使用Stripe来实现这一点。

在这个函数中,如果Stripe成功,您还需要告诉您自己的后端所有必要的信息,以便实际发送产品或用户订购的任何内容。

英文:

You need to have a payment processor (like Stripe) to actually do the transaction.

See this tutorial: https://www.kodeco.com/2113-apple-pay-tutorial-getting-started

It explains how to do this with Stripe.

In this function, if Stripe is successful, you will also have to tell your own backend all of the information it needs to actually send the product or whatever the user ordered.

huangapple
  • 本文由 发表于 2023年2月8日 21:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定