我使用Swift在iOS中使用条纹实现了Apple Pay。因此,在测试程序时,可以在模拟器上正常运行,而不能在真实设备中运行。要使用真实设备测试Apple Pay,它不会显示付款单来处理交易。我的疑问是使用真实设备使用Apple Pay进行测试,我们可以将测试卡添加到钱包中还是拥有沙盒帐户?使用条纹门方式时,需要向钱包中添加测试卡吗?您能给我一个清晰的声音吗?

这是我的代码:

import UIKit
import PassKit
import Stripe

class ViewController: UIViewController {

    let request = PKPaymentRequest()
    var paymentStatus = PKPaymentAuthorizationStatus.failure
    var paymentController: PKPaymentAuthorizationController?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func applepay(_ sender: Any) {
        let merchantIdentifier = "merchant.Appplepay"
        let paymentNetworks = [PKPaymentNetwork.masterCard, PKPaymentNetwork.visa,PKPaymentNetwork.amex,PKPaymentNetwork.discover,PKPaymentNetwork.interac]
        if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks)
        {

            request.merchantIdentifier = "merchant.Appplepay"
            request.countryCode = "US"
            request.currencyCode = "USD"
            request.supportedNetworks = paymentNetworks
            request.merchantCapabilities = .capability3DS
            request.requiredShippingAddressFields = [.phone, .email,.postalAddress,.all]
            request.paymentSummaryItems  = [PKPaymentSummaryItem(label: "Fancy Hat", amount: 50.00),
                                            // The final line should represent your company;
                // it'll be prepended with the word "Pay" (i.e. "Pay iHats, Inc $50")
                                            PKPaymentSummaryItem(label: "iHats, Inc", amount: 50.00),]
        } else {
            // Traditional checkout flow
        }
        func applePaySupported() -> Bool {
            return PKPaymentAuthorizationViewController.canMakePayments() && PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [.amex, .visa, .masterCard])
        }
           // Setup payment authorization view controller
        if Stripe.canSubmitPaymentRequest(request) {
            // Setup payment authorization view controller
            let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
            paymentAuthorizationViewController?.delegate = self

            // Present payment authorization view controller
            self.present((paymentAuthorizationViewController)!, animated: true,completion: nil)
        }
        else {
            // There is a problem with your Apple Pay configuration
            print("Failed to present PaymentSheet");
        }

    }

}
extension ViewController : PKPaymentAuthorizationViewControllerDelegate {

    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment  payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
        if payment.shippingContact?.emailAddress == nil || payment.shippingContact?.phoneNumber == nil {
            paymentStatus = .invalidShippingContact
        } else {
            STPAPIClient.shared().createToken(with: payment) { (token: STPToken?, error)-> Void  in
            print("Stripe token is \(String(describing: token!))")
                //self.paymentStatus = .success
                 completion(.success)

        }
        }

    }

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


}

最佳答案

要回答有关在真实设备上进行测试的问题:您只能将真实卡添加到钱包中才能与Apple Pay一起使用,但是,如果您使用测试条api密钥that is detected by stripe and your real card will not be charged

不知道为什么付款单在没有看到一些代码的情况下就不会显示在真实设备上,但是您要确保该设备设置为使用Apple Pay,主要是它在钱包中添加了卡(您支持)

并非所有国家/地区都使用support Apple Pay,因此并非总是可以选择将卡添加到钱包中,这意味着由于if Stripe.canSubmitPaymentRequest(request)返回false,因此无法显示Apple Pay表。

关于ios - applepay在实际设备中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49445470/

10-13 00:42