-
Notifications
You must be signed in to change notification settings - Fork 1.3k
ท #322
Description
/*
- Copyright 2017 Google Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
package com.google.android.gms.samples.wallet
import android.app.Activity
import com.google.android.gms.wallet.PaymentsClient
import com.google.android.gms.wallet.Wallet
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.math.BigDecimal
import java.math.RoundingMode
/**
-
Contains helper static methods for dealing with the Payments API.
-
Many of the parameters used in the code are optional and are set here merely to call out their
-
existence. Please consult the documentation to learn more and feel free to remove ones not
-
relevant to your implementation.
*/
object PaymentsUtil {
val MICROS = BigDecimal(1000000.0)/**
- Create a Google Pay API base request object with properties used in all requests.
- @return Google Pay API base request object.
- @throws JSONException
*/
private val baseRequest = JSONObject().apply {
put("apiVersion", 2)
put("apiVersionMinor", 0)
}
/**
- Gateway Integration: Identify your gateway and your app's gateway merchant identifier.
- The Google Pay API response will return an encrypted payment method capable of being charged
- by a supported gateway after payer authorization.
- TODO: Check with your gateway on the parameters to pass and modify them in Constants.java.
- @return Payment data tokenization for the CARD payment method.
- @throws JSONException
- @see PaymentMethodTokenizationSpecification
*/
private fun gatewayTokenizationSpecification(): JSONObject {
return JSONObject().apply {
put("type", "PAYMENT_GATEWAY")
put("parameters", JSONObject(Constants.PAYMENT_GATEWAY_TOKENIZATION_PARAMETERS))
}
}
/**
-
DIRECTIntegration: Decrypt a response directly on your servers. This configuration has -
additional data security requirements from Google and additional PCI DSS compliance complexity.
-
Please refer to the documentation for more information about
DIRECTintegration. The -
type of integration you use depends on your payment processor.
-
@return Payment data tokenization for the CARD payment method.
-
@throws JSONException
-
@see PaymentMethodTokenizationSpecification
*/
private fun directTokenizationSpecification(): JSONObject {
if (Constants.DIRECT_TOKENIZATION_PUBLIC_KEY == "REPLACE_ME" ||
(Constants.DIRECT_TOKENIZATION_PARAMETERS.isEmpty() ||
Constants.DIRECT_TOKENIZATION_PUBLIC_KEY.isEmpty())) {throw RuntimeException( "Please edit the Constants.java file to add protocol version & public key.")}
return JSONObject().apply {
put("type", "DIRECT")
put("parameters", JSONObject(Constants.DIRECT_TOKENIZATION_PARAMETERS))
}
}
/**
- Card networks supported by your app and your gateway.
- TODO: Confirm card networks supported by your app and gateway & update in Constants.java.
- @return Allowed card networks
- @see CardParameters
*/
private val allowedCardNetworks = JSONArray(Constants.SUPPORTED_NETWORKS)
/**
- Card authentication methods supported by your app and your gateway.
- TODO: Confirm your processor supports Android device tokens on your supported card networks
- and make updates in Constants.java.
- @return Allowed card authentication methods.
- @see CardParameters
*/
private val allowedCardAuthMethods = JSONArray(Constants.SUPPORTED_METHODS)
/**
-
Describe your app's support for the CARD payment method.
-
The provided properties are applicable to both an IsReadyToPayRequest and a
-
PaymentDataRequest.
-
@return A CARD PaymentMethod object describing accepted cards.
-
@throws JSONException
-
@see PaymentMethod
*/
// Optionally, you can add billing address/phone number associated with a CARD payment method.
private fun baseCardPaymentMethod(): JSONObject {
return JSONObject().apply {val parameters = JSONObject().apply { put("allowedAuthMethods", allowedCardAuthMethods) put("allowedCardNetworks", allowedCardNetworks) put("billingAddressRequired", true) put("billingAddressParameters", JSONObject().apply { put("format", "FULL") }) } put("type", "CARD") put("parameters", parameters)}
}
/**
-
Describe the expected returned payment data for the CARD payment method
-
@return A CARD PaymentMethod describing accepted cards and optional fields.
-
@throws JSONException
-
@see PaymentMethod
*/
private fun cardPaymentMethod(): JSONObject {
val cardPaymentMethod = baseCardPaymentMethod()
cardPaymentMethod.put("tokenizationSpecification", gatewayTokenizationSpecification())return cardPaymentMethod
}
/**
-
An object describing accepted forms of payment by your app, used to determine a viewer's
-
readiness to pay.
-
@return API version and payment methods supported by the app.
-
@see IsReadyToPayRequest
*/
fun isReadyToPayRequest(): JSONObject? {
return try {
val isReadyToPayRequest = JSONObject(baseRequest.toString())
isReadyToPayRequest.put(
"allowedPaymentMethods", JSONArray().put(baseCardPaymentMethod()))isReadyToPayRequest} catch (e: JSONException) {
null
}
}
/**
- Information about the merchant requesting payment information
- @return Information about the merchant.
- @throws JSONException
- @see MerchantInfo
*/
private val merchantInfo: JSONObject
@throws(JSONException::class)
get() = JSONObject().put("merchantName", "Example Merchant")
/**
-
Creates an instance of [PaymentsClient] for use in an [Activity] using the
-
environment and theme set in [Constants].
-
@param activity is the caller's activity.
*/
fun createPaymentsClient(activity: Activity): PaymentsClient {
val walletOptions = Wallet.WalletOptions.Builder()
.setEnvironment(Constants.PAYMENTS_ENVIRONMENT)
.build()return Wallet.getPaymentsClient(activity, walletOptions)
}
/**
- Provide Google Pay API with a payment amount, currency, and amount status.
- @return information about the requested payment.
- @throws JSONException
- @see TransactionInfo
*/
@throws(JSONException::class)
private fun getTransactionInfo(price: String): JSONObject {
return JSONObject().apply {
put("totalPrice", price)
put("totalPriceStatus", "FINAL")
put("countryCode", Constants.COUNTRY_CODE)
put("currencyCode", Constants.CURRENCY_CODE)
}
}
/**
-
An object describing information requested in a Google Pay payment sheet
-
@return Payment data expected by your app.
-
@see PaymentDataRequest
*/
fun getPaymentDataRequest(price: String): JSONObject? {
try {
return JSONObject(baseRequest.toString()).apply {
put("allowedPaymentMethods", JSONArray().put(cardPaymentMethod()))
put("transactionInfo", getTransactionInfo(price))
put("merchantInfo", merchantInfo)// An optional shipping address requirement is a top-level property of the // PaymentDataRequest JSON object. val shippingAddressParameters = JSONObject().apply { put("phoneNumberRequired", false) put("allowedCountryCodes", JSONArray(Constants.SHIPPING_SUPPORTED_COUNTRIES)) } put("shippingAddressRequired", true) put("shippingAddressParameters", shippingAddressParameters) }} catch (e: JSONException) {
return null
}
}
}
/**
- Converts micros to a string format accepted by [PaymentsUtil.getPaymentDataRequest].
- @param micros value of the price.
*/
fun Long.microsToString() = BigDecimal(this)
.divide(PaymentsUtil.MICROS)
.setScale(2, RoundingMode.HALF_EVEN)
.toString()