【问题标题】:Apple Pay works on simulator but not on deviceApple Pay 适用于模拟器,但不适用于设备
【发布时间】:2017-03-30 14:20:03
【问题描述】:

我可以在模拟器上显示 Apple Pay 视图控制器,但不能在设备上显示。

已添加权利并已安装证书。

class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate {

var paymentRequest: PKPaymentRequest! 

override func viewDidLoad() {
    super.viewDidLoad()

}


func rydes(shipping: Double) -> [PKPaymentSummaryItem] {

    let ryde = PKPaymentSummaryItem(label: "Your Fare", amount: 1.00)
    let discount = PKPaymentSummaryItem(label: "Discount", amount: -0.00)
    let shipping = PKPaymentSummaryItem(label: "Shipping", amount: NSDecimalNumber(string: "\(shipping)"))
    let subTotal = ryde.amount.adding(discount.amount)
    let total = PKPaymentSummaryItem(label: "rydes", amount: subTotal)

    return [ryde, discount, shipping, total]

} // rydes() func


    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didSelect shippingMethod: PKShippingMethod, completion: @escaping (PKPaymentAuthorizationStatus, [PKPaymentSummaryItem]) -> Void) {

        completion(PKPaymentAuthorizationStatus.success, rydes(shipping: Double(shippingMethod.amount)))

    } // paymentAuthorizationViewController( didSelectShippingMethod )



    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {

        completion(PKPaymentAuthorizationStatus.success)

    } // paymentAuthorizationViewController( didAuthorizePayment )



    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {

        controller.dismiss(animated: true, completion: nil)

    } // paymentAuthorizationViewControllerDidFinish()


@IBAction func applePayPressed(_ sender: Any) {


    print("enable apple pay")

    // send user to Apple Pay to make payment

    let paymentNetworks = [PKPaymentNetwork.visa, .masterCard, .interac, .discover, .amex]
    let merchantId = "merchant.com.xxx"


    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {

        paymentRequest = PKPaymentRequest()
        paymentRequest.currencyCode = "CAD"
        paymentRequest.countryCode = "CA"
        paymentRequest.merchantIdentifier = merchantId
        paymentRequest.supportedNetworks = paymentNetworks
        paymentRequest.merchantCapabilities = .capability3DS
        paymentRequest.requiredShippingAddressFields = [.all]
        paymentRequest.paymentSummaryItems = self.rydes(shipping: 0.00)

        // . . .  shipping - not really needed

        let samedayShipping = PKShippingMethod(label: "Same Day", amount: 12.99)
        samedayShipping.detail = "Guaranteed same day delivery."
        samedayShipping.identifier = "sameday"

        let twodayShipping = PKShippingMethod(label: "Two Day", amount: 4.99)
        twodayShipping.detail = "Guaranteed within two days."
        twodayShipping.identifier = "twoday"

        let freeShipping = PKShippingMethod(label: "Same Day", amount: 0.00)
        freeShipping.detail = "Guaranteed same day."
        freeShipping.identifier = "freeShipping"

        paymentRequest.shippingMethods = [samedayShipping, twodayShipping, freeShipping]

        // . . .  shipping - not really needed

        let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        applePayVC.delegate = self
        self.present(applePayVC, animated: true, completion: nil)

    }  else {

        print("Tell the user they need to set up Apple Pay!")
    }

} // applePayPressed func ACTION

}

上面的代码只返回这一行 - 告诉用户他们需要设置 Apple Pay!

我使用 iPhone 6s 作为设备,并且 Apple Pay 和 Wallet 已通过有效的借记卡和信用卡启用。我来自加拿大,所以我在一个有效的地区。

编辑代码

class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate {

var paymentRequest: PKPaymentRequest! // apple pay

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


func rydes() -> [PKPaymentSummaryItem] {

    let ryde = PKPaymentSummaryItem(label: "Your Fare", amount: 1.00)
    let subTotal = ryde.amount
    let total = PKPaymentSummaryItem(label: "rydes", amount: subTotal)

    return [ryde, total]

} // rydes() func



    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {

        completion(PKPaymentAuthorizationStatus.success)

    } // paymentAuthorizationViewController( didAuthorizePayment )



    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {

        controller.dismiss(animated: true, completion: nil)

    } // paymentAuthorizationViewControllerDidFinish()


@IBAction func applePayPressed(_ sender: Any) {

    print("enable apple pay")

    // send user to Apple Pay to make payment

    let paymentNetworks = [PKPaymentNetwork.visa, .masterCard, .interac, .discover, .amex]
    let merchantID = "merchant.com.xxx"

    if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
        paymentRequest = PKPaymentRequest()
        paymentRequest.currencyCode = "CAD"
        paymentRequest.countryCode = "CA"
        paymentRequest.merchantIdentifier = merchantID
        paymentRequest.supportedNetworks = paymentNetworks
        paymentRequest.merchantCapabilities = .capability3DS
        paymentRequest.requiredShippingAddressFields = [.all]
        paymentRequest.paymentSummaryItems = self.rydes()

        let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest)
        applePayVC.delegate = self
        self.present(applePayVC, animated: true, completion: nil)

    }  else {

        print("Tell the user they need to set up Apple Pay!")
    }

} // applePayPressed func ACTION

}

【问题讨论】:

  • 怎么可能被家长控制限制了?
  • 我检查过了,所有限制都关闭了!
  • 您在以下位置缺少报价:let merchantId = merchant.com.xxx" <---- is not matched by a beginning one. 应该是:let merchantId = "merchant.com.xxx"。编辑:我修好了。
  • @Brandon 我只是把它放在那里,以免显示我的商家 ID。 - 感谢更新:-)

标签: ios swift applepay


【解决方案1】:

我在为我的应用程序启用 Apple Pay 时遇到了同样的问题。确保您已在 PKPayment​Summary​Item 中输入所有有效的运费、服务和总付款值。

总支付值也应大于 0.0 以显示支付网关视图。如果任何值为 0.0,则不要添加为 SummaryItem。

有效 PKPaymentSummaryItem 的代码:

{
// 12.75 subtotal
NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275
 exponent:-2 isNegative:NO];
self.subtotal = [PKPaymentSummaryItem summaryItemWithLabel:@"Subtotal" amount:subtotalAmount];

// 2.00 discount
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithMantissa:200 exponent:-2 isNegative:YES];
self.discount = [PKPaymentSummaryItem summaryItemWithLabel:@"Discount" amount:discountAmount];

// 12.75 - 2.00 = 10.75 grand total
NSDecimalNumber *totalAmount = [NSDecimalNumber zero];
totalAmount = [totalAmount decimalNumberByAdding:subtotalAmount];
totalAmount = [totalAmount decimalNumberByAdding:discountAmount];
self.total = [PKPaymentSummaryItem summaryItemWithLabel:@"My Company Name" amount:totalAmount];
self.summaryItems = @[self.subtotal, self.discount, self.total];
request.paymentSummaryItems = self.summaryItems;
}

这将尝试解决您的上述问题。

编辑: 只需删除下面的代码并再次检查,如果折扣或运费的值大于 0.0,则输入检查条件,然后您可以将运费和折扣作为汇总项目发送:

    let ryde = PKPaymentSummaryItem(label: "Your Fare", amount: 1.00)
//change some value here:
    let discount = PKPaymentSummaryItem(label: "Discount", amount: -0.00)
    let shipping = PKPaymentSummaryItem(label: "Shipping", amount: NSDecimalNumber(string: "\(shipping)"))
Make sure your subtotal or total is greater that 0.0.
    let subTotal = ryde.amount.adding(discount.amount)
    let total = PKPaymentSummaryItem(label: "rydes", amount: subTotal)

【讨论】:

  • 我对Objective-C不是很了解,你能把这个放到Swift 3中吗?
  • 不走运:/。我删除了所有折扣和运费,因为我不使用它们并且仍然没有运气。在编辑中修改了代码。
  • 我发现了问题,除了运费和折扣外,我决定将我的签证连同 Interac Debit 一起添加到我的钱包中,并且成功了。当我查看卡时,它说借记卡不适用于应用内支付。知道为什么吗?
  • 嗨@LizG你解决问题了吗?我有同样的错误?我使用 swift 实现了带条纹的苹果支付。我运行该程序,它在模拟器中运行良好,但在真正的 iPhone 设备中运行良好?在真实设备中它不显示付款单。为什么 ?你知道原因吗,请帮帮我。
  • 要在真机上测试apple pay,可以把测试卡加到我们的钱包里吗?只有在真机上才能用
【解决方案2】:

如果您可以在模拟器上看到 Apple Pay 选项,并且对为什么它没有在真实设备上显示感到困惑,那么这个答案适合您。

最初,您可能在真实设备上看不到 Apple Pay 选项,因为您可能没有将任何卡添加到 Wallet 应用中。

在这种情况下,您必须关注Apple Pay - Sandbox Testing - Apple Developer并设置一个沙盒帐户来测试apple pay。

步骤:

  1. 转到https://appstoreconnect.apple.com/access/testers 并添加您的测试人员帐户。使用未用于任何 Apple 服务的新电子邮件地址。
  2. 在您的 iPhone 上,转到设置,从您登录的 Apple ID 中退出,然后登录到您的新测试人员帐户。
  3. 选择您可以使用 Apple Pay 的地区。 E.J.美国(如果还没有)
  4. 打开电子钱包应用程序并添加一张新卡。

姓名:任意
卡号:3499 569590 41362
安全码:1111
有效期:12/2022

  1. 成功添加卡后,打开您的 iOS 应用,您现在可以看到 apple pay。

【讨论】:

    【解决方案3】:

    确保您在测试设备上的 Apple Pay 钱包中添加了一张卡:) 很简单,但对我来说是解决办法。

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2011-03-08
      • 2015-02-13
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多