![](/img/trans.png)
[英]iOS Simulator gets stuck at “Pay with Passcode” when trying Apple Pay
[英]Apple Pay works on simulator but not on device
我可以在模擬器上顯示 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
}
在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)
如果您可以在模擬器上看到 apple pay 選項,並且對為什么它沒有顯示在真實設備上感到困惑,那么這個答案就是為您准備的。
最初,您可能在真實設備上看不到 Apple Pay 選項,因為您可能沒有將任何卡添加到 Wallet 應用程序中。
在這種情況下,您必須遵循Apple Pay - Sandbox Testing - Apple Developer並設置一個沙箱帳戶來測試Apple Pay。
腳步:
名稱:任何
卡號:3499 569590 41362
保安編碼:1111
到期日:12/2022
確保你在你正在測試的設備上的 Apple Pay 錢包中添加了一張卡:) 簡單,但對我來說是解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.