Initialise SDK in your iOS app for Apple Pay

  Ensure you have met all requirements before proceeding.
Click here for further information.

 

Configure the instance

Before you can perform any payment request, you will need to set the username, gateway and environment, as shown in the example below:

TrustPayments.instance.configure(
username: username_from_trustpayments,
gateway: .eu,
environment: .staging,
translationsForOverride: nil
)

 

 

1. Add Apple Pay into existing drop-in view controller

This section demonstrates how to include the Apple Pay button in the drop-in view controller.

let dropInVC = try ViewControllerFactory.shared.dropInViewController(

jwt: jwt,
customDropInView: dropInCustomView,
visibleFields: [.securityCode3],
applePayConfiguration: applePayConfiguration,
dropInViewStyleManager: dropInViewStyleManager,
dropInViewDarkModeStyleManager: dropInViewDarkModeStyleManager,
cardinalStyleManager: cardinalStyleManager,
cardinalDarkModeStyleManager: cardinalDarkModeStyleManager,

payButtonTappedClosureBeforeTransaction: {
(
controller: DropInController
)
in
},

transactionResponseClosure: {
(
jwt: [String],
threeDResponse: ThreeDResponse?,
error: APIClientError?
)
in
}

)
  Field Format Description
table-required.png
supportedNetworks
List The payment networks supported by the merchant. The value must be one or more of the following: “amex“, “masterCard” or “visa“.
table-required.png
merchantCapabilities
List The payment capabilities supported by the merchant.The field must contain “supports3DS” (to enable Touch ID / Face ID verification), and at least one of the following additional values:
  • “supportsCredit” – To enable credit card payments.
  • “supportsDebit” – To enable debit card payments.
For a full specification, please refer to Apple’s own documentation.
table-required.png countryCode Alpha (2) The merchant’s country code in ISO2a format.
table-required.png currencyCode Alpha (3) The transaction currency code in ISO3a format.
table-optional.png
buttonDarkModeStyle
Alphanumeric

Used to change the appearance of the Apple Pay button when device is in dark mode.

Click here for further information.

table-optional.png buttonStyle Alphanumeric

Used to change the appearance of the Apple Pay button when device is in light mode.

Click here for further information.

table-optional.png buttonType Alphanumeric

Used to change the text displayed on the Apple Pay button for different payment scenarios.

Click here for further information.

  Useful resources regarding the Apple Pay button


You can change the colour of the button and the text displayed to better suit the needs of your checkout.
Click here to learn more.


Apple has published guidelines on how to best position the Apple Pay button on your checkout.
Click here to learn more.

 

2. Configuring drop-in view controller to only show Apple Pay

This section demonstrates how to configure the drop-in view controller to only show Apple Pay using the minimum number of parameters. If you require further examples, you can review the example app found within the GitLab repository.

let request = PKPaymentRequest()

request.supportedNetworks = [.visa, .masterCard, .amex]
request.merchantCapabilities = [.capability3DS, .capabilityCredit, .capabilityDebit]
request.merchantIdentifier = "merchant.ios.trustpayments.test"
request.countryCode = "GB"
request.currencyCode = "GBP"

let applePayConfiguration = TPApplePayConfiguration(
handler: self,
request: request,
buttonStyle: .black,
buttonDarkModeStyle: .white,
buttonType: .plain
)

let dropInVC = try ViewControllerFactory.shared.dropInViewController(
jwt: jwt,
customDropInView: nil,
visibleFields: [],
applePayConfiguration: applePayConfiguration,
dropInViewStyleManager: dropInViewStyleManager,
dropInViewDarkModeStyleManager: dropInViewDarkModeStyleManager,
payButtonTappedClosureBeforeTransaction: nil,
transactionResponseClosure: { (jwt, threeDResponse, error) in }
)

 

3. Add Apple Pay button to custom view

Add additional field wallettoken to the performWalletTransaction() method, containing the string representation of the PKPayment token that you get after the Apple authorisation:

let claim = TPClaims(
iss: keys.merchantUsername,
iat: Date(timeIntervalSinceNow: 0),
payload: Payload(
accounttypedescription: "ECOM",
sitereference: keys.merchantSiteReference,
currencyiso3a: "GBP",
baseamount: 299,
requesttypedescriptions: ["THREEDQUERY", "AUTH"],
termurl: "https://payments.securetrading.net/process/payments/mobilesdklistener"
)
)

guard let jwt = JWTHelper.createJWT(
basedOn: claim, signWith: keys.jwtSecretKey
), let token = payment.stringRepresentation else { return }
paymentTransactionManager.performWalletTransaction(
walletSource: .applePay, walletToken: token, jwt: jwt, transactionResponseClosure: {
(jwt, threeDResponse, error) in
}
)

The wallettoken field submitted to the gateway must require the following fields provided by Apple in the PKPayment. The following is an example of how the expected JSON can be generated, which will need to submitted in the wallettoken field:

let expectedJson: [String: Any] = [
"token":[
"transactionIdentifier": self.token.transactionIdentifier,
"paymentData": paymentDataJson,
"paymentMethod":[
"network": self.token.paymentMethod.network?.rawValue,
"type": self.token.paymentMethod.type.description,
"displayName": self.token.paymentMethod.displayName
]
]
]

If you prefer, we also have a utility that is demonstrated in our example app in the GitLab repository, which can take the PKPayment provided by Apple and return the expected JSON needed to perform a wallet transaction:

extension PKPayment {
var stringRepresentation: String? {
let paymentData = self.token.paymentData
guard let paymentDataJson = try? JSONSerialization.jsonObject(
with: paymentData,
options: JSONSerialization.ReadingOptions(rawValue: 0)
) as? [String: Any] else { return nil }
let expectedJson: [String: Any] = [
"token":[
"transactionIdentifier": self.token.transactionIdentifier,
"paymentData": paymentDataJson,
"paymentMethod":[
"network": self.token.paymentMethod.network?.rawValue,
"type": self.token.paymentMethod.type.description,
"displayName": self.token.paymentMethod.displayName
]
]
]
guard let expectedJsonData = try? JSONSerialization.data(
withJSONObject: expectedJson,
options: JSONSerialization.WritingOptions(rawValue: 0)
) else { return nil }
return String(data: expectedJsonData, encoding: .utf8)
}
}

 

Handling data update

If you allow users to update their shipping address and payment method on the Apple Pay form, you must conform to the TPApplePayConfigurationHandler protocol and handle the appropriate methods:

shippingMethodChanged(to method: PKShippingMethod, updatedWith: @escaping ([PKPaymentSummaryItem]) -> Void)

shippingAddressChanged(to address: CNPostalAddress, updatedWith: @escaping ([Error]?, [PKPaymentSummaryItem]) -> Void)

When conforming to those methods, you need to call updateWith closure with new PKPaymentSummaryItem, otherwise the authorisation will time out.

Also you will need to listen to the didAuthorizedPayment(payment: PKPayment, updatedRequestParameters: @escaping ((_ jwt: String?, _ walletToken: String?, [Error]?) -> Void)) method and provide updated JWT based on received PKPayment object.

This can be useful if you need to be able to verify the address details before the payment request is submitted to the gateway for orders placed by your customers.

 

Where next?

  Performing additional requests

For more advanced configurations, additional requests can be referenced in the drop-in view controller to perform additional actions.

Click here to learn how.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request