applepay无法在真实设备中运行

时间:2018-03-23 08:40:57

标签: ios stripe-payments applepay

我使用swift在ios中使用条带实现了苹果支付。因此,当测试程序时,它可以通过模拟器运行,但不能在真实设备中运行。要使用真实设备测试苹果支付,它不会显示处理交易的支付表。我的疑问是用真实设备测试苹果支付我们可以将测试卡添加到钱包或拥有沙盒帐户吗?是否需要在使用条纹门方式时将测试卡添加到钱包?你能告诉我一个清晰度吗?

这是我的代码:

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)
    }


}

1 个答案:

答案 0 :(得分:1)

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

不确定为什么付款单在没有看到某些代码的情况下没有显示在真实设备上,但你想确保该设备已经设置为苹果支付,主要是它已经添加了卡到钱包(你支持)

并非所有国家/地区support Apple Pay都没有向卡片添加卡片的选项,这意味着由于if Stripe.canSubmitPaymentRequest(request)而无法显示Apple Pay表格返回假。