Apple Pay and Google Pay
You can use Apple Pay and Google Pay to collect payment details as a token representing a payment card that the payer has added to their Apple or Google Pay wallet.
Apple Pay
To obtain the Apple Pay token from the payer’s wallet:
- To accept payments with Apple Pay, configure your app for Apple Pay. For instructions, see Configuring your Environment in Apple's developer documentation.
- When the payer's order process reaches your checkout page, determine whether the device supports Apple Pay by using the
canMakePayments()
function onPKPaymentAuthorizationController
. If Apple Pay is supported, display the Apple Pay button as a payment option, using thePKPaymentButton
provided by Apple. - Construct and use a
PKPaymentRequest
. For instructions, see Creating Payment Requests in Apple's developer documentation. - After a user has authorized Apple Pay, update the gateway session with the payment token,
PKPAYMENTTOKEN
:- Get the payment token as a string to send to the gateway.
Example request
let tokenString = String(data: payment.token.paymentData, encoding.utf8)
- Populate the token string into the
GatewayMap
as thedevicePayment
and specify that the token came from Apple Pay by setting thewalletProvider
.Example requestvar request = GatewayMap() request.sourceOfFunds.provided.card.devicePayment.paymentToken = tokenString request.order.walletProvider = "APPLE_PAY"
- Send the
GatewayMap
for your request to the gateway using theupdateSession()
function on theGatewayAPI
object.Example requestGatewayAPI.shared.updateSession("< session id#>", apiVersion: "< Gateway API Version#<", payload: request) { (result) in switch result { case .success(let response): print(response.description) case .error(let error): print(error) } }
- Get the payment token as a string to send to the gateway.
- Complete an Apple Pay transaction from your server just as any other payment, using the PAY or AUTHORIZE transaction.
When completing a payment on a session that is using Apple Pay, set the
order.walletProvider
field toAPPLE_PAY
and include the session ID in the request.
When creating an Apple Pay Payment Processing Certificate, use the certificate signing request (CSR) provided to you by the gateway. For instructions on how to generate the CSR, see Configuring your Environment.
Google Pay
The Mobile SDK includes a helper utility, called GooglePayHandler, for collecting a token from the payer’s Google Pay wallet:
- Enable Google Pay support within your app. For instructions, see the Google Pay documentation. The instructions guide you to:
- Set up the Google Pay API.
- Request payment information from the Google wallet.
- Incorporate the Google Pay button in your checkout layout.
- Manage the response from the Google Pay API.
- Since Google Pay integration is optional with the Mobile SDK, provide the appropriate play services dependency:
Example request
implementation 'com.google.android.gms:play-services-wallet:X.X.X'
The gateway is integrated with Google Pay as a
PAYMENT_GATEWAY
type. - To request encrypted card information from Google Pay:
- Use the valid name
mpgs
in your request. - Replace
MERCHANT_ID
with the merchant ID you use on the gateway.Example requestval tokenizationSpecification = JSONObject() .put("type", "PAYMENT_GATEWAY") .put("parameters", JSONObject() .put("gateway", "mpgs") .put("gatewayMerchantId", "MERCHANT_ID"))
- Use the valid name
- Use the
GooglePayHandler
to launch the Google Pay wallet with your constructed payments client and request data.Example request// YourActivity.kt fun googlePayButtonClicked() { try { val request = PaymentDataRequest.fromJson(paymentDataRequest.toString()) // use the Gateway convenience handler for launching the Google Pay flow GooglePayHandler.requestData(this, paymentsClient, request) } catch (e: JSONException) { Toast.makeText(this, "Could not request payment data", Toast.LENGTH_SHORT).show() } }
- To manage wallet responses, the Mobile SDK offers a Google Pay lifecycle handler. You can implement the provided Google Pay callback and use the result handler within your activity. This eliminates the need to manually manage the Google Pay activity results and delegates the important transaction steps to the callback methods.
Example request
// YourActivity.kt override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { // handle the Google Pay response if (GooglePayHandler.handleActivityResult(requestCode, resultCode, data, callback)) { return } super.onActivityResult(requestCode, resultCode, data) } val googlePayCallback = object : GooglePayCallback { override fun onReceivedPaymentData(paymentData: JSONObject) { try { val description = paymentData.getJSONObject("paymentMethodData") .getString("description") Log.d(MyGooglePayCallback::class.java.simpleName,"ReceivedPaymentData: $description") } catch (e: Exception) { // handle exception } handleGooglePayData(paymentData) } override fun onGooglePayCancelled() { // handle cancelled } override fun onGooglePayError(status: Status) { // handle error } }
- When you receive payment data from Google Pay, update the gateway session with the payment token.
Example request
fun handleGooglePayData(paymentData: JSONObject) { val token = paymentData.getJSONObject("paymentMethodData") .getJSONObject("tokenizationData") .getString("token") val request = GatewayMap() .set("sourceOfFunds.provided.card.devicePayment.paymentToken", token) GatewayAPI.updateSession(session, request, callback) }
- Complete a Google Pay transaction from your server just as any other payment, using the PAY or AUTHORIZE transaction.
When completing a payment on a session that is using Google Pay, set the
order.walletProvider
field toGOOGLE_PAY
and include the session ID in the request.