Récupérer les données de Google Pay

Les données de l'acheteur sont retournées à l'application mobile dans un objet PaymentData (appelé aussi "payload").

Pour traiter les données de la payload, surchargez la méthode onActivityResult() de votre activity comme suit:

Extrait de l'exemple de code:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    // Manage Google Pay result 
   if (requestCode == GooglePayManagement.GOOGLE_PAYMENT_CODE_RESULT) {
        when (resultCode) {
            Activity.RESULT_OK -> {
                if (data != null) {
                    val paymentData = PaymentData.getFromIntent(data)
                    val googlePayData = paymentData!!.toJson()
                    // Execute payment   
                 PayZenPayment.executeTransaction(googlePayData, this)
                } else {
                    PayZenPayment.returnsResult(false, PayZenPaymentErrorCode.UNKNOWN_ERROR, "Unknown error", this)
                }
            }
            Activity.RESULT_CANCELED -> {
                PayZenPayment.returnsResult(false, PayZenPaymentErrorCode.PAYMENT_CANCELLED_ERROR, "Payment cancelled by user", this)
            }
            AutoResolveHelper.RESULT_ERROR -> {
                PayZenPayment.returnsResult(false, PayZenPaymentErrorCode.UNKNOWN_ERROR, "Unknown error", this)
            }
        }
    }
}

Si vous avez opté pour un checkout rapide, vous devez transformer la payload en objet JSON puis l'analyser afin de sauvegarder les données nécessaires (coordonnées de l'acheteur, adresse de livraison etc..).

Exemple de récupération de l'adresse de facturation:

when (resultCode) {
	Activity.RESULT_OK -> {
		if (data != null) {
			val paymentData = PaymentData.getFromIntent(data)
			val googlePayData = paymentData!!.toJson()
			val billingAddress =
							JSONObject(
								JSONObject(
									JSONObject(
										googlePayload!!.toJson()
									).getString("paymentMethodData")
								).getString("info")
							).getString("billingAddress")			

Exemple d'objet PaymentData:

JSON result Example :{
	"apiVersionMinor":0,   
	"apiVersion":2,
	"paymentMethodData":{
		"description":"Visa •••• 3513",     
		"tokenizationData":{          
			"type":"PAYMENT_GATEWAY",        
			"token":"{\"signature\":\"MEUCIC1+XSqhtgCWQa\/3k7OFIesJX0SYBtCeOKjD+O2JBWO9AiEAg03FhXVVwD0NRhiPnjGWdfTqTXm39fnzr+XvHdbecY4\\u003d\",\"protocolVersion\":\"ECv1\",\"signedMessage\":\"{\\\"encryptedMessage\\\":\\\"qjXoUw7ptqWE\/G+PSOb8QoR+hqzM7h1S7iQ3jH\/mxmjJi\/\/MY5Kem6WmuQxMQbz0pXeX5mxjmUxvbIQzGhtLLELoTWxuAF\/mLSuoIznPbvJNxQ9xGOVMQYuo0jTKqUUv6QrjMBTSCcQx2ktkDS3EjHOWBPTw22mI\/kXz0wn5x6jlMLEUTFkNlPZYjeoFRvUXpw0XoECNdouiiJwub52zmyebY7pz1JlG5DQOMWRvrCnj5Kgs9sxzXp1VJJ+rIqewoBOgl\/BgKsf87fOxVJFYqOmq+LR\/wk8PDKE9VE1XakQBG8kxhSflMQaqC+ObObJfoUC9ruIDK7iFkT8EFV3FVWHu8ZVYmoek3HWabZ36NdRPUyGtDCjOR+CmT8tneRHyqRzy8S4Yk5LyCObTZ+rbI0toveNchZdakUhMLyFFLqkgnc\/g3WJnLpujRy3jgOe6\/+ymb1gaiOnQQA\\\\u003d\\\\u003d\\\",\\\"ephemeralPublicKey\\\":\\\"BKB3qFuCrmNI0bOxVg\/kVCcCtswHrwabcrm18JtPTB+w5L\/d39dnrgaGajdYvdLrFRQc5RyQ52Ug3e151yDNE6E\\\\u003d\\\",\\\"tag\\\":\\\"rJ5SapWOGc\/f4eqzif+P9D\/wSg\/yvfOJ63yVbRc1lGY\\\\u003d\\\"}\"}"
		},     
		"type":"CARD",
		"info":{          
			"cardNetwork":"VISA",        
			"cardDetails":"3513",        
			"billingAddress":{             
				"address3":"",           
				"sortingCode":"",
				"address2":"",
				"countryCode":"FR",
				"address1":"109 Rue de l'Innovation",
				"postalCode":"31670",
				"name":"My Network",
				"locality":"Labège",
				"administrativeArea":""         
			}
		}
	},
	"shippingAddress":{       
		"address3":"",     
		"sortingCode":"",     
		"address2":"",     
		"countryCode":"FR",
		"address1":"109 Rue de l'Innovation",     
		"postalCode":"31670",
		"name":"My Network",     
		"locality":"Labège",     
		"administrativeArea":""
	},  
	"email":"network.gpay@gmail.com"
}