Using Android SDK
PsGooglePayContext, part of the Paysafe Android SDK, is the fastest and easiest way to start accepting Google Pay in your Android apps. To accept payments using Google Pay, you need to create a Google Pay button and then tokenize the payment when the button is tapped.
NOTE: For information about API keys, and how to integrate and set up the Android SDK, please refer to the Android SDK Overview.
1. Enable the Google Pay API
To use Google Pay, first enable the Google Pay API by adding the following metadata tag to your app AndroidManifest.xml:
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
2. Initialize
To use the Google Pay module, you need to initialize PSGooglePayContext.
The signature of the initialize() method is as follows:
/**
Initializes the Google Pay integration.
@param activity The activity instance from which the Google Pay integration is initialized.
@param psGooglePayConfig The configuration for Google Pay integration.
@param callback Callback to receive the initialized Google Pay context. The callback provides a PSGooglePayContext object containing the necessary context for Google Pay operations.
*/
fun initialize(
activity: ComponentActivity,
psGooglePayConfig: PSGooglePayConfig,
callback: PSCallback<PSGooglePayContext>
)
/**
Initializes the Google Pay integration.
@param fragment The fragment instance from which the Google Pay integration is initialized.
@param psGooglePayConfig The configuration for Google Pay integration.
@param callback Callback to receive the initialized Google Pay context. The callback provides a PSGooglePayContext object containing the necessary context for Google Pay operations.
*/
fun initialize(
fragment: Fragment,
psGooglePayConfig: PSGooglePayConfig,
callback: PSCallback<PSGooglePayContext>
)
Parameter | Required | Type | Description |
---|---|---|---|
activity | true | ComponentActivity | The activity instance from which the Google Pay integration is initialized. |
fragment | true | Fragment | The fragment instance from which the Google Pay integration is initialized. |
psGooglePayConfig | true | PSGooglePayConfig | The configuration for Google Pay integration. |
callback | true | PSCallback<PSGooglePayContext> | Callback to receive the initialized Google Pay context. The callback provides a PSGooglePayContext object containing the necessary context for Google Pay operations. |
PSGooglePayConfig | |||
currencyCode | true | String | The currencyCode accepts 3 letter abbreviations of the ISO standard currencies. |
accountId | true | String | The id of the selected merchant account to use to process the payment. If you are a merchant, this field is required only if you have more than one account configured for the same payment method and currency. If you are a partner using a shared API key, this field is mandatory. |
countryCode | true | String | Set this property to the two-letter ISO 3166 code for the country or region of your principal place of business. This is often the location for settling the payment. |
requestBillingAddress | true | Boolean | True if the billing address is required for the Google Pay payment. |
Usage example
PSGooglePayContext.initialize(
this,
PSGooglePayConfig(
countryCode = "US",
currencyCode = "USD",
accountId = "<account-id>",
requestBillingAddress = true
),
object : PSCallback<PSGooglePayContext> {
override fun onSuccess(value: PSGooglePayContext) {
googlePayContext = value
}
override fun onFailure(exception: Exception) {
// handle exception
}
}
)
3. Create Google Pay button
Paysafe Android SDK provides a straightforward way to add a Google Pay button to your checkout screens.
You can add the Google Pay button to your XML layout:
<com.paysafe.android.google_pay.button.PSGooglePayButton
android:id="@+id/selectPayMethodGooglePay"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Initialize PSGooglePayButton in your Activity or Fragment:
if (googlePayContext != null) {
binding.selectPayMethodGooglePay.initialize(
PSGooglePayButtonOptions.Builder(googlePayContext.providePaymentMethodConfig()) // Pass payment method config from google pay context
.buttonTheme(PSGooglePayButtonTheme.DARK) // or .LIGHT
.buttonType(PSGooglePayButtonType.BUY) // or .DONATE
.cornerRadius(3)
.build()
)
}
PSGooglePayButtonOptions.Builder parameter description:
Parameter | Required | Type | Description |
---|---|---|---|
paymentMethodConfig | true | PSGooglePayPaymentMethodConfig | It is automatically available after the context initialization and contains the configuration details provided in the Paysafe Business Portal. |
The modifiers for PSGooglePayButtonOptions are described below:
Parameter | Required | Type | Description |
---|---|---|---|
type | false | PSGooglePayButtonType | An enum type with the following cases:
|
theme | false | PSGooglePayButtonTheme | An enum type with the following cases:
|
cornerRadius | false | Int | Corner radius of the GPay button. |
4. Tokenize
The tokenize method returns a PaymentHandle result, which resolves to a single-use payment handle representing the customer's sensitive card data. This handle can be used by the Payments API to take payments. Single-use handles are valid for only 15 minutes and are not consumed by verification. For more information, see Payments with a Handle.
Tokenize callback signature
The tokenize function has the following signature:
/**
Initiates tokenization using GooglePay.
@param googlePayTokenizeOptions Options for tokenizing Google Pay data.
@param callback Callback to receive the tokenized Google Pay data or any error encountered during the tokenization process.
The callback provides a PSGooglePayTokenizeResult object containing the tokenized data or an error.
*/
fun tokenize(
googlePayTokenizeOptions: PSGooglePayTokenizeOptions,
callback: PSGooglePayTokenizeCallback
)
The tokenize() function provides a convenient method for tokenizing, allowing secure payment processing. Once invoked, the function returns a callback that resolves to a single-use payment handle, which can be used by the Payments API for payment transactions. It's important to note that single-use handles are valid for only 15 minutes and are not consumed during verification.
Parameters
- options: An instance of PSGooglePayTokenizeOptions containing various parameters for tokenization, such as the payment amount, transaction type, account ID and GooglePay configuration.
The PSGooglePayTokenizeCallback is an interface described below:
/**
Callback interface for receiving the result of Google Pay tokenization.
*/
interface PSGooglePayTokenizeCallback {
/**
Called when Google Pay tokenization is successful.
@param paymentHandle The payment handle representing the tokenized Google Pay data.
*/
fun onSuccess(paymentHandleToken: String)
/**
Called when an error occurs during Google Pay tokenization.
@param paysafeException The Paysafe exception indicating the error encountered during tokenization.
*/
fun onFailure(paysafeException: PaysafeException)
/**
Called when Google Pay tokenization is cancelled.
@param paysafeException The Paysafe exception indicating the cancellation reason.
*/
fun onCancelled(paysafeException: PaysafeException)
}
PSGooglePayTokenizeOptions
The PSGooglePayTokenizeOptions structure provides a comprehensive set of options for configuring the tokenization process. For a detailed explanation of these options, see Android SDK Card Payments Tokenize Options.
Usage example
val amount = price * 100
googlePayContext?.tokenize(
PSGooglePayTokenizeOptions(
amount = amount,
currencyCode = "USD",
transactionType = TransactionType.PAYMENT,
merchantRefNum = PaysafeSDK.getMerchantReferenceNumber(),
billingDetails = BillingDetails(
nickName = "nickName",
street = "street",
city = "city",
state = "AL",
country = "US",
zip = "12345"
),
profile = Profile(
firstName = "firstName",
lastName = "lastName",
locale = ProfileLocale.EN_GB,
dateOfBirth = DateOfBirth(
day = 1,
month = 1,
year = 1990
),
email = "email@mail.com",
phone = "0123456789"
),
accountId = Consts.CARDS_ACCOUNT_ID,
merchantDescriptor = MerchantDescriptor(
dynamicDescriptor = "dynamicDescriptor",
phone = "0123456789"
),
shippingDetails = ShippingDetails(
shipMethod = ShippingMethod.NEXT_DAY_OR_OVERNIGHT,
street = "street",
street2 = "street2",
city = "city",
state = "AL",
countryCode = "US",
zip = "12345",
),
threeDS = ThreeDS(
merchantUrl: "https://api.qa.paysafe.com/checkout/v2/index.html#/desktop",
deviceChannel: "BROWSER",
messageCategory: "PAYMENT",
authenticationPurpose: "PAYMENT_TRANSACTION"
)
),
object : PSGooglePayTokenizeCallback {
override fun onSuccess(paymentHandle: String) {
// handle paymentHandle
}
override fun onFailure(paysafeException: PaysafeException) {
// handle exception
}
override fun onCancelled(paysafeException: PaysafeException) {
context?.longToast("User Cancelled Google Pay Flow")
}
}
)
Google Pay exceptions
Error code | Display message | Detailed message | Comments |
---|---|---|---|
9061 | There was an error (9061) please contact our support. | Invalid account id for ${paymentMethod} | AccountId is present, but it is not configured for the desired payment context. |
9042 | There was an error (9042) please contact our support. | User aborted authentication | When a prompt to authenticate the use of PayPal/ Google Pay is canceled by the customer with the payment method provider. |
9054 | There was an error (9054) please contact our support. | Amount should be a number greater than 0 no longer than 11 characters | The value for the amount should be a positive number. |
9136 | There was an error (9136) please contact our support. | Tokenization is already in progress. | When you call the tokenize function before the previous one has completed. |