Apple Pay token contains wrong transactionAmount info. E.g if the transaction amount is $1.10, on iOS device, Apple Pay screen stills show correct amount but paymentToken returns 110 when decrypted
I used this library to decrypt Apple Pay token using my own public-private key pair
https://github.com/sidimansourjs/applepay-token
My sample code
class ViewController: UIViewController {
...
@IBAction func payBtn(_ sender: UIButton) {
do {
let paymentItem = PKPaymentSummaryItem.init(label: "Test item", amount: NSDecimalNumber(value: 1.10))
let paymentNetworks = [PKPaymentNetwork.amex, .discover, .masterCard, .visa]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks) {
let request = PKPaymentRequest()
request.currencyCode = "SGD" // 1
request.countryCode = "SG" // 2
request.merchantIdentifier = "merchant.com.xxxxx" // 3
request.merchantCapabilities = PKMerchantCapability.capability3DS // 4
request.supportedNetworks = paymentNetworks // 5
request.paymentSummaryItems = [paymentItem] // 6
guard let paymentVC = PKPaymentAuthorizationViewController(paymentRequest: request) else {
displayDefaultAlert(title: "Error", message: "Unable to present Apple Pay authorization.")
return
}
paymentVC.delegate = self
self.present(paymentVC, animated: true, completion: nil)
} else {
displayDefaultAlert(title: "Error", message: "Unable to make Apple Pay transaction.")
}
} catch {
print(error.localizedDescription)
}
}
}
extension ViewController: PKPaymentAuthorizationViewControllerDelegate {
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
dismiss(animated: true, completion: nil)
}
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
let token = String(data: payment.token.paymentData, encoding: .utf8)
let utf8str = token!.data(using: .utf8)
if let base64Encoded = utf8str?.base64EncodedString()
{
print("Encoded: \(base64Encoded)")
//Send token to backend server to decrypt
}
}
}
I discovered this when try to make payment request to Adyen (a payment gateway). Request must contain both amount
field and paymentToken
field but was never successful because amount in amount
and paymentToken
is mis-matched. Then I tried amount
110 instead of 1.10 then request was success, but ironically in Adyen dashboard somehow it still understood transaction amount was $1.10
I expected transactionAmount
in Apple Pay token to be 1.10
Please help to explain why there's this difference in Apple Pay amount shown on device and in token