@@ -31,7 +31,6 @@ import kotlinx.coroutines.launch
3131import org.json.JSONArray
3232import org.json.JSONException
3333import org.json.JSONObject
34- import kotlin.toString
3534
3635/* *
3736 * Used to create and tokenize Google Pay payment methods. For more information see the [documentation](https://developer.paypal.com/braintree/docs/guides/google-pay/overview)
@@ -76,8 +75,10 @@ class GooglePayClient internal constructor(
7675
7776 /* *
7877 * Before starting the Google Pay flow, use this method to check whether the Google Pay API is
79- * supported and set up on the device. When the callback is called with `true`, show the
80- * Google Pay button. When it is called with `false`, display other checkout options.
78+ * supported and set up on the device.
79+ *
80+ * When [callback] is called with [GooglePayReadinessResult.ReadyToPay], show the Google Pay Button.
81+ * When [callback] is called with [GooglePayReadinessResult.NotReadyToPay], display other checkout options.
8182 *
8283 * @param context Android Context
8384 * @param request [ReadyForGooglePayRequest]
@@ -95,9 +96,20 @@ class GooglePayClient internal constructor(
9596 }
9697 }
9798
98- private suspend fun isReadyToPay (context : Context , request : ReadyForGooglePayRequest ? ): GooglePayReadinessResult {
99+ /* *
100+ * Before starting the Google Pay flow, use this method to check whether the Google Pay API is
101+ * supported and set up on the device.
102+ *
103+ * When the return value is [GooglePayReadinessResult.ReadyToPay], show the Google Pay Button.
104+ * When the Return value is [GooglePayReadinessResult.NotReadyToPay], display other checkout options.
105+ *
106+ * @param context Android Context
107+ * @param request [ReadyForGooglePayRequest]
108+ * @return [GooglePayReadinessResult]
109+ */
110+ suspend fun isReadyToPay (context : Context , request : ReadyForGooglePayRequest ? ): GooglePayReadinessResult {
99111 return try {
100- Class .forName( PaymentsClient :: class .java.name )
112+ verifyGoogleWalletOnClasspath( )
101113 val configuration = braintreeClient.getConfiguration()
102114 if (! configuration.isGooglePayEnabled) {
103115 NotReadyToPay (null )
@@ -125,7 +137,7 @@ class GooglePayClient internal constructor(
125137 request.isExistingPaymentMethodRequired
126138 )
127139 }
128- } catch (ignored : JSONException ) {
140+ } catch (_ : JSONException ) {
129141 }
130142 val readyToPayRequest = IsReadyToPayRequest .fromJson(json.toString())
131143 internalGooglePayClient.isReadyToPay(
@@ -140,6 +152,15 @@ class GooglePayClient internal constructor(
140152 }
141153 }
142154
155+ @Throws(
156+ LinkageError ::class ,
157+ ExceptionInInitializerError ::class ,
158+ ClassNotFoundException ::class
159+ )
160+ private fun verifyGoogleWalletOnClasspath () {
161+ Class .forName(PaymentsClient ::class .java.name)
162+ }
163+
143164 /* *
144165 * Get Braintree specific tokenization parameters for a Google Pay. Useful for when full control
145166 * over the [PaymentDataRequest] is required.
@@ -150,7 +171,7 @@ class GooglePayClient internal constructor(
150171 * [PaymentDataRequest.Builder.setPaymentMethodTokenizationParameters]
151172 * and [allowedCardNetworks] should be supplied to the
152173 * [CardRequirements] via
153- * [CardRequirements.Builder.addAllowedCardNetworks]} .
174+ * [CardRequirements.Builder.addAllowedCardNetworks].
154175 *
155176 * @param callback [GooglePayGetTokenizationParametersCallback]
156177 */
@@ -163,7 +184,21 @@ class GooglePayClient internal constructor(
163184 }
164185 }
165186
166- private suspend fun getTokenizationParameters (): GooglePayTokenizationParameters {
187+ /* *
188+ * Get Braintree specific tokenization parameters for a Google Pay. Useful for when full control
189+ * over the [PaymentDataRequest] is required.
190+ *
191+ *
192+ * [PaymentMethodTokenizationParameters] should be supplied to the
193+ * [PaymentDataRequest] via
194+ * [PaymentDataRequest.Builder.setPaymentMethodTokenizationParameters]
195+ * and [allowedCardNetworks] should be supplied to the
196+ * [CardRequirements] via
197+ * [CardRequirements.Builder.addAllowedCardNetworks].
198+ *
199+ * @return [GooglePayTokenizationParameters]
200+ */
201+ suspend fun getTokenizationParameters (): GooglePayTokenizationParameters {
167202 return try {
168203 val configuration = braintreeClient.getConfiguration()
169204 GooglePayTokenizationParameters .Success (
@@ -181,7 +216,7 @@ class GooglePayClient internal constructor(
181216 * used to present Google Pay payment sheet in
182217 * [GooglePayLauncher.launch]
183218 *
184- * @param request The [GooglePayRequest] containing options for the transaction.
219+ * @param request The [GooglePayRequest] containing options for the transaction.
185220 * @param callback [GooglePayPaymentAuthRequestCallback]
186221 */
187222 fun createPaymentAuthRequest (
@@ -203,7 +238,15 @@ class GooglePayClient internal constructor(
203238 }
204239 }
205240
206- private suspend fun createPaymentAuthRequest (request : GooglePayRequest ): GooglePayPaymentAuthRequest {
241+ /* *
242+ * Start the Google Pay payment flow. This will return [GooglePayPaymentAuthRequestParams] that are
243+ * used to present Google Pay payment sheet in
244+ * [GooglePayLauncher.launch]
245+ *
246+ * @param request The [GooglePayRequest] containing options for the transaction.
247+ * @return [GooglePayPaymentAuthRequest]
248+ */
249+ suspend fun createPaymentAuthRequest (request : GooglePayRequest ): GooglePayPaymentAuthRequest {
207250 analyticsParamRepository.reset()
208251 braintreeClient.sendAnalyticsEvent(GooglePayAnalytics .PAYMENT_REQUEST_STARTED )
209252
@@ -238,13 +281,16 @@ class GooglePayClient internal constructor(
238281 /* *
239282 * Call this method when you've received a successful [PaymentData] response from a
240283 * direct Google Play Services integration to get a [GooglePayCardNonce] or
241- * [PayPalAccountNonce].
284+ * [com.braintreepayments.api.paypal. PayPalAccountNonce].
242285 *
243286 * @param paymentData [PaymentData] retrieved from directly integrating with Google Play
244287 * Services through [PaymentsClient.loadPaymentData]
245288 * @param callback [GooglePayTokenizeCallback]
246289 */
247- fun tokenize (paymentData : PaymentData , callback : GooglePayTokenizeCallback ) {
290+ fun tokenize (
291+ paymentData : PaymentData ,
292+ callback : GooglePayTokenizeCallback
293+ ) {
248294 val result = tokenize(paymentData)
249295 when (result) {
250296 is GooglePayResult .Success -> callbackTokenizeSuccess(result, callback)
@@ -258,7 +304,7 @@ class GooglePayClient internal constructor(
258304 return try {
259305 val result = JSONObject (paymentData.toJson())
260306 GooglePayResult .Success (fromJSON(result))
261- } catch (e : JSONException ) {
307+ } catch (_ : JSONException ) {
262308 try {
263309 val token =
264310 JSONObject (paymentData.toJson()).getJSONObject(" paymentMethodData" )
@@ -267,7 +313,7 @@ class GooglePayClient internal constructor(
267313 } catch (e: Exception ) {
268314 GooglePayResult .Failure (e)
269315 }
270- } catch (e : NullPointerException ) {
316+ } catch (_ : NullPointerException ) {
271317 try {
272318 val token =
273319 JSONObject (paymentData.toJson()).getJSONObject(" paymentMethodData" )
@@ -283,7 +329,7 @@ class GooglePayClient internal constructor(
283329 * After a user successfully authorizes Google Pay payment via
284330 * [GooglePayClient.createPaymentAuthRequest], this
285331 * method should be invoked to tokenize the payment method to retrieve a
286- * [PaymentMethodNonce]
332+ * [com.braintreepayments.api.core. PaymentMethodNonce]
287333 *
288334 * @param paymentAuthResult the result of [GooglePayLauncher.launch]
289335 * @param callback [GooglePayTokenizeCallback]
@@ -335,7 +381,7 @@ class GooglePayClient internal constructor(
335381
336382 val version = try {
337383 metadata.getString(" version" )
338- } catch (e : JSONException ) {
384+ } catch (_ : JSONException ) {
339385 com.braintreepayments.api.core.BuildConfig .VERSION_NAME
340386 }
341387
@@ -440,7 +486,7 @@ class GooglePayClient internal constructor(
440486 .put(" phoneNumberRequired" , request.isPhoneNumberRequired)
441487 )
442488 }
443- } catch (ignored : JSONException ) {
489+ } catch (_ : JSONException ) {
444490 }
445491 return defaultParameters
446492 }
@@ -462,7 +508,7 @@ class GooglePayClient internal constructor(
462508 )
463509
464510 defaultParameters.put(" purchase_context" , purchaseContext)
465- } catch (ignored : JSONException ) {
511+ } catch (_ : JSONException ) {
466512 }
467513
468514 return defaultParameters
@@ -495,12 +541,12 @@ class GooglePayClient internal constructor(
495541 configuration.googlePayAuthorizationFingerprint
496542 parameters.put(" braintree:authorizationFingerprint" , googlePayAuthFingerprint)
497543 }
498- } catch (ignored : JSONException ) {
544+ } catch (_ : JSONException ) {
499545 }
500546
501547 try {
502548 cardJson.put(" type" , " PAYMENT_GATEWAY" ).put(" parameters" , parameters)
503- } catch (ignored : JSONException ) {
549+ } catch (_ : JSONException ) {
504550 }
505551
506552 return cardJson
@@ -528,7 +574,7 @@ class GooglePayClient internal constructor(
528574 .put(" platform" , " android" ).toString()
529575 )
530576 )
531- } catch (ignored : JSONException ) {
577+ } catch (_ : JSONException ) {
532578 }
533579
534580 return json
0 commit comments