3

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

2
  • Payment processors require the currency’s smallest unit, so 110 = 1.10
    – Don
    Commented Jul 11, 2019 at 4:41
  • Do you have any reference for this ?
    – Brian Pham
    Commented Jul 11, 2019 at 5:27

1 Answer 1

4

Found answer as suggested by @Don Singapore Dollar SGD's smallest unit is cent, which is 1/100 of a dollar. Therefore I need to convert dollar amount into cent integer by multiply with 100. Then, use this value when send over to Adyen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.