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)
/*
*/
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)
/**
*/
private val baseRequest = JSONObject().apply {
put("apiVersion", 2)
put("apiVersionMinor", 0)
}
/**
*/
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 hasadditional data security requirements from Google and additional PCI DSS compliance complexity.
Please refer to the documentation for more information about
DIRECTintegration. Thetype 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())) {
}
return JSONObject().apply {
put("type", "DIRECT")
put("parameters", JSONObject(Constants.DIRECT_TOKENIZATION_PARAMETERS))
}
}
/**
*/
private val allowedCardNetworks = JSONArray(Constants.SUPPORTED_NETWORKS)
/**
*/
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 {
}
}
/**
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()))
} catch (e: JSONException) {
null
}
}
/**
*/
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)
}
/**
*/
@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)
} catch (e: JSONException) {
return null
}
}
}
/**
*/
fun Long.microsToString() = BigDecimal(this)
.divide(PaymentsUtil.MICROS)
.setScale(2, RoundingMode.HALF_EVEN)
.toString()