Apple Pay works on the simulator, but not on the device
Can't find a solution to my problem. I can display the Apple Pay view controller on the simulator, but not on the device.
Added access rights and installed the certificate.
Please advise.
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
}
The above code just returns this string. Tell the user that they need to set up Apple Pay!
I am using an iPhone 6s since the device and Apple Pay and Wallet are included with a valid debit card and credit card. I am from Canada, so I am in the active region.
Any help would be appreciated.
Edited code
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
}
source to share
This is the same problem I am facing while incorporating Apple Pay into my application. Please make sure you include all valid shipping, handling and total charges on your PKPayment summary item.
Also, the total payment value must be greater than 0.0 to display the type of path to the payment gateway. If any value is 0.0, then do not add it as a SummaryItem.
Code for Valid 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;
}
This will try to fix your problem.
Edit: Just remove below code and check it again or set a check condition if the value for discount or shipping is greater than 0.0 then you can send shipping and discount as the final item:
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)
source to share