Conekta Android

Conekta-Android te permite tokenizar tarjetas utilizando la llave pública de Conekta API para después procesar pagos en tu servidor. En esta sección, aprenderás a integrar Conekta-Android en aplicaciones de Android.


Hay 2 maneras de integrar conekta-android. Por medio de artifacts o incluyendo el módulo.

Instalar Conekta Android Artifacts (AAR) 2.0

Instrucciones

Este ejemplo usa mavenCentral para instalar el artefacto (conektasdk) .aar

repositories {
    mavenCentral()
}
...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'io.conekta:conektasdk:2.0'
}

Ahora solo sincroniza Gradle.


Importar el módulo 2.0 de Conekta Android

Este es un ejemplo de cómo importar el módulo de Conekta en una app de Android.

  • Vía git:
    git clone git@github.com:conekta/conekta-android.git
  • En Android Studio: File -> New -> Import Module y escoge el folder conektasdk.
  • Dirígete a: File -> Project Structure..., esto abrirá una nueva ventana, escoge la sección Modules, y haz click en la pestaña Dependencies, posteriormente haz click en el botón +, en la ventana de Module dependency, escoge conektasdk.

Uso del SDK Conekta

Importa todas las clases del namespace com.conekta

// Your code....
							import io.conekta.conektasdk.Conekta;
							import io.conekta.conektasdk.Card;
							import io.conekta.conektasdk.Token;
							import org.json.JSONObject;
							// Your code...

							private Activity activity = this;

							Conekta.setPublicKey("{global_api_key}"); //Set public key
							Conekta.setApiVersion("{global_api_version}"); //Set api version (optional)
							Conekta.collectDevice(activity); //Collect device

							Card card = new Card("Josue Camara", "4242424242424242", "332", "11", "2017");
							Token token = new Token(activity);

							token.onCreateTokenListener( new Token.CreateToken(){
							  @Override
							  public void onCreateTokenReady(JSONObject data) {
							    try {
							      //TODO: Create charge
							      Log.d("Token::::", data.getString("id"));
							    } catch (Exception err) {
							    //TODO: Handle error
							      Log.d("Error: " + err.toString());
							    }
							  }
							});

							token.create(card);//Create token
							// Your code below....

Realiza un cargo en tu servidor

Tras obtener el token, deberás enviarlo a tu servidor y generar el cargo.

#N/A: curl -u {global_api_key}: \
require "conekta"
Conekta.api_key = "{global_api_key}"
require_once("/path/to/lib/Conekta.php");
\Conekta\Conekta::setApiKey("{global_api_key}");
import conekta
conekta.api_key = "{global_api_key}"
var conekta = require('conekta');
conekta.api_key = "{global_api_key}";
import com.conekta;
Conekta.setApiKey("{global_api_key}");
using conekta;
conekta.Api.apiKey = "{global_api_key}"; 
curl -H "Accept: application/vnd.conekta-v{global_api_version}+json" \
     -H "Content-type: application/json" \
     -u {global_api_key}: \
     -X POST -d '{
      "description":"Stogies",
      "amount": 20000,
      "currency":"MXN",
      "reference_id":"9839-wolf_pack",
      "card": "tok_test_visa_4242",
      "details": {
        "name": "Arnulfo Quimare",
        "phone": "403-342-0642",
        "email": "logan@x-men.org",
        "customer": {
          "logged_in": true,
          "successful_purchases": 14,
          "created_at": 1379784950,
          "updated_at": 1379784950,
          "offline_payments": 4,
          "score": 9
        },
        "line_items": [{
          "name": "Box of Cohiba S1s",
          "description": "Imported From Mex.",
          "unit_price": 20000,
          "quantity": 1,
          "sku": "cohb_s1",
          "category": "food"
        }],
        "billing_address": {
          "street1":"77 Mystery Lane",
          "street2": "Suite 124",
          "city": "Darlington",
          "state":"NJ",
          "zip": "10192",
          "country": "Mexico",
          "tax_id": "xmn671212drx",
          "company_name":"X-Men Inc.",
          "phone": "77-777-7777",
          "email": "purshasing@x-men.org"
        }
      }
    }' https://api.conekta.io/charges
try:
charge = conekta.Charge.create({
  "description":"Stogies",
  "amount": 20000,
  "currency":"MXN",
  "reference_id":"9839-wolf_pack",
  "card": "tok_test_visa_4242",
  "details": {
    "name": "Arnulfo Quimare",
    "phone": "403-342-0642",
    "email": "logan@x-men.org",
    "customer": {
      "logged_in": true,
      "successful_purchases": 14,
      "created_at": 1379784950,
      "updated_at": 1379784950,
      "offline_payments": 4,
      "score": 9
    },
    "line_items": [{
      "name": "Box of Cohiba S1s",
      "description": "Imported From Mex.",
      "unit_price": 20000,
      "quantity": 1,
      "sku": "cohb_s1",
      "category": "food"
    }],
    "billing_address": {
      "street1":"77 Mystery Lane",
      "street2": "Suite 124",
      "street3": null,
      "city": "Darlington",
      "state":"NJ",
      "zip": "10192",
      "country": "Mexico",
      "tax_id": "xmn671212drx",
      "company_name":"X-Men Inc.",
      "phone": "77-777-7777",
      "email": "purshasing@x-men.org"
    }
  }
})
except conekta.ConektaError as e:
  print e.message_to_purchaser
conekta.Charge.create({
  "description":"Stogies",
  "amount": 20000,
  "currency":"MXN",
  "reference_id":"9839-wolf_pack",
  "card": "tok_test_visa_4242",
  "details": {
    "name": "Arnulfo Quimare",
    "phone": "403-342-0642",
    "email": "logan@x-men.org",
    "customer": {
      "logged_in": true,
      "successful_purchases": 14,
      "created_at": 1379784950,
      "updated_at": 1379784950,
      "offline_payments": 4,
      "score": 9
    },
    "line_items": [{
      "name": "Box of Cohiba S1s",
      "description": "Imported From Mex.",
      "unit_price": 20000,
      "quantity": 1,
      "sku": "cohb_s1",
      "category": "food"
    }],
    "billing_address": {
      "street1":"77 Mystery Lane",
      "street2": "Suite 124",
      "street3": null,
      "city": "Darlington",
      "state":"NJ",
      "zip": "10192",
      "country": "Mexico",
      "tax_id": "xmn671212drx",
      "company_name":"X-Men Inc.",
      "phone": "77-777-7777",
      "email": "purshasing@x-men.org"
    }
  }
}, function(err, res) {
    console.log(res.toObject());
});
JSONObject payment_params;

payment_params = new JSONObject("{"
  + "'description':'Stogies',"
  + "'amount': 20000,"
  + "'currency':'MXN',"
  + "'reference_id':'9839-wolf_pack',"
  + "'card': 'tok_test_visa_4242',"
  + "'details': {"
    + "'name': 'Arnulfo Quimare',"
    + "'phone': '403-342-0642',"
    + "'email': 'logan@x-men.org',"
    + "'customer': {"
      + "'logged_in': true,"
      + "'successful_purchases': 14,"
      + "'created_at': 1379784950,"
      + "'updated_at': 1379784950,"
      + "'offline_payments': 4,"
      + "'score': 9"
    + "},"
    + "'line_items': [{"
      + "'name': 'Box of Cohiba S1s',"
      + "'description': 'Imported From Mex.',"
      + "'unit_price': 20000,"
      + "'quantity': 1,"
      + "'sku': 'cohb_s1',"
      + "'category': 'food'"
    + "}],"
    + "'billing_address': {"
      + "'street1':'77 Mystery Lane',"
      + "'street2': 'Suite 124',"
      + "'street3': null,"
      + "'city': 'Darlington',"
      + "'state':'NJ',"
      + "'zip': '10192',"
      + "'country': 'Mexico',"
      + "'tax_id': 'xmn671212drx',"
      + "'company_name':'X-Men Inc.',"
      + "'phone': '77-777-7777',"
      + "'email': 'purshasing@x-men.org'"
    + "}"
  + "}"
+ "}");

try {
  Charge conektaCharge = Charge.create(payment_params);
}
catch (Error e){
  System.out.println(e.toString());
}
	try {
		$charge = Conekta_Charge::create(array(
		  'description'=> 'Stogies',
		  'reference_id'=> '9839-wolf_pack',
		  'amount'=> 20000,
		  'currency'=>'MXN',
		  'card'=> 'tok_test_visa_4242',
		  'details'=> array(
		    'name'=> 'Arnulfo Quimare',
		    'phone'=> '403-342-0642',
		    'email'=> 'logan@x-men.org',
		    'customer'=> array(
		      'corporation_name'=> 'Conekta Inc.',
		      'logged_in'=> true,
		      'successful_purchases'=> 14,
		      'created_at'=> 1379784950,
		      'updated_at'=> 1379784950,
		      'offline_payments'=> 4,
		      'score'=> 9
		    ),
		    'line_items'=> array(
		      array(
		        'name'=> 'Box of Cohiba S1s',
		        'description'=> 'Imported From Mex.',
		        'unit_price'=> 20000,
		        'quantity'=> 1,
		        'sku'=> 'cohb_s1',
		        'type'=> 'food'
		      )
		    ),
		    'billing_address'=> array(
		      'street1'=>'77 Mystery Lane',
		      'street2'=> 'Suite 124',
		      'street3'=> null,
		      'city'=> 'Darlington',
		      'state'=>'NJ',
		      'zip'=> '10192',
		      'country'=> 'Mexico',
		      'phone'=> '77-777-7777',
		      'email'=> 'purshasing@x-men.org'
		    )
		  )
		));
	} catch (Conekta_Error $e) {
		echo $e->message_to_purchaser;
	}
begin
  charge = Conekta::Charge.create({
    "amount"=> 51000,
    "currency"=> "MXN",
    "description"=> "Pizza Delivery",
    "reference_id"=> "orden_de_id_interno",
    "card"=> params[:conektaTokenId],  # Ej. "tok_a4Ff0dD2xYZZq82d9"
    "details"=> {
      "name"=> "Arnulfo Quimare",
      "phone"=> "403-342-0642",
      "email"=> "logan@x-men.org",
      "customer"=> {
        "logged_in"=> true,
        "successful_purchases"=> 14,
        "created_at"=> 1379784950,
        "updated_at"=> 1379784950,
        "offline_payments"=> 4,
        "score"=> 9
      },
      "line_items"=> [{
        "name"=> "Box of Cohiba S1s",
        "description"=> "Imported From Mex.",
        "unit_price"=> 20000,
        "quantity"=> 1,
        "sku"=> "cohb_s1",
        "category"=> "food"
      }],
      "billing_address"=> {
        "street1"=>"77 Mystery Lane",
        "street2"=> "Suite 124",
        "city"=> "Darlington",
        "state"=>"NJ",
        "zip"=> "10192",
        "country"=> "Mexico",
        "tax_id"=> "xmn671212drx",
        "company_name"=>"X-Men Inc.",
        "phone"=> "77-777-7777",
        "email"=> "purshasing@x-men.org"
      }
    }
  })
rescue Conekta::ParameterValidationError => e
  puts e.message_to_purchaser 
  #alguno de los parámetros fueron inválidos

rescue Conekta::ProcessingError => e
  puts e.message_to_purchaser 
  #la tarjeta no pudo ser procesada

rescue Conekta::Error => e
  puts e.message_to_purchaser 
  #un error ocurrió que no sucede en el flujo normal de cobros como por ejemplo un auth_key incorrecto

end
conekta.Charge charge = new conekta.Charge ().create(@"{
  ""description"":""Stogies"",
  ""amount"": 20000,
  ""currency"":""MXN"",
  ""reference_id"":""9839-wolf_pack"",
  ""card"": ""tok_test_visa_4242"",
  ""details"": {
    ""name"": ""Arnulfo Quimare"",
    ""phone"": ""403-342-0642"",
    ""email"": ""logan@x-men.org"",
    ""customer"": {
      ""logged_in"": true,
      ""successful_purchases"": 14,
      ""created_at"": 1379784950,
      ""updated_at"": 1379784950,
      ""offline_payments"": 4,
      ""score"": 9
    },
    ""line_items"": [{
      ""name"": ""Box of Cohiba S1s"",
      ""description"": ""Imported From Mex."",
      ""unit_price"": 20000,
      ""quantity"": 1,
      ""sku"": ""cohb_s1"",
      ""category"": ""food""
    }],
    ""billing_address"": {
      ""street1"":""77 Mystery Lane"",
      ""street2"": ""Suite 124"",
      ""street3"": null,
      ""city"": ""Darlington"",
      ""state"":""NJ"",
      ""zip"": ""10192"",
      ""country"": ""Mexico"",
      ""tax_id"": ""xmn671212drx"",
      ""company_name"":""X-Men Inc."",
      ""phone"": ""77-777-7777"",
      ""email"": ""purshasing@x-men.org""
    }
  }
}");
	try {
		$charge = Conekta_Charge::create(array(
		  'description'=> 'Stogies',
		  'reference_id'=> '9839-wolf_pack',
		  'amount'=> 20000,
		  'currency'=>'MXN',
		  'card'=> 'tok_test_visa_4242',
		  'monthly_installments'=> 3,
		  'details'=> array(
		    'name'=> 'Arnulfo Quimare',
		    'phone'=> '403-342-0642',
		    'email'=> 'logan@x-men.org',
		    'customer'=> array(
		      'corporation_name'=> 'Conekta Inc.',
		      'logged_in'=> true,
		      'successful_purchases'=> 14,
		      'created_at'=> 1379784950,
		      'updated_at'=> 1379784950,
		      'offline_payments'=> 4,
		      'score'=> 9
		    ),
		    'line_items'=> array(
		      array(
		        'name'=> 'Box of Cohiba S1s',
		        'description'=> 'Imported From Mex.',
		        'unit_price'=> 20000,
		        'quantity'=> 1,
		        'sku'=> 'cohb_s1',
		        'type'=> 'food'
		      )
		    ),
		    'billing_address'=> array(
		      'street1'=>'77 Mystery Lane',
		      'street2'=> 'Suite 124',
		      'street3'=> null,
		      'city'=> 'Darlington',
		      'state'=>'NJ',
		      'zip'=> '10192',
		      'country'=> 'Mexico',
		      'phone'=> '77-777-7777',
		      'email'=> 'purshasing@x-men.org'
		    )
		  )
		));
	} catch (Exception $e) {
		echo $e->message_to_purchaser;
	}
try:
charge = conekta.Charge.create({
  "description":"Stogies",
  "amount": 20000,
  "currency":"MXN",
  "reference_id":"9839-wolf_pack",
  "card": "tok_test_visa_4242",
  "monthly_installments": 3,
  "details": {
    "name": "Arnulfo Quimare",
    "phone": "403-342-0642",
    "email": "logan@x-men.org",
    "customer": {
      "logged_in": true,
      "successful_purchases": 14,
      "created_at": 1379784950,
      "updated_at": 1379784950,
      "offline_payments": 4,
      "score": 9
    },
    "line_items": [{
      "name": "Box of Cohiba S1s",
      "description": "Imported From Mex.",
      "unit_price": 20000,
      "quantity": 1,
      "sku": "cohb_s1",
      "category": "food"
    }],
    "billing_address": {
      "street1":"77 Mystery Lane",
      "street2": "Suite 124",
      "street3": null,
      "city": "Darlington",
      "state":"NJ",
      "zip": "10192",
      "country": "Mexico",
      "tax_id": "xmn671212drx",
      "company_name":"X-Men Inc.",
      "phone": "77-777-7777",
      "email": "purshasing@x-men.org"
    }
  }
})
except conekta.ConektaError as e:
  print e.message_to_purchaser
conekta.Charge.create({
  "description":"Stogies",
  "amount": 20000,
  "currency":"MXN",
  "reference_id":"9839-wolf_pack",
  "card": "tok_test_visa_4242",
  "monthly_installments": 3,
  "details": {
    "name": "Arnulfo Quimare",
    "phone": "403-342-0642",
    "email": "logan@x-men.org",
    "customer": {
      "logged_in": true,
      "successful_purchases": 14,
      "created_at": 1379784950,
      "updated_at": 1379784950,
      "offline_payments": 4,
      "score": 9
    },
    "line_items": [{
      "name": "Box of Cohiba S1s",
      "description": "Imported From Mex.",
      "unit_price": 20000,
      "quantity": 1,
      "sku": "cohb_s1",
      "category": "food"
    }],
    "billing_address": {
      "street1":"77 Mystery Lane",
      "street2": "Suite 124",
      "street3": null,
      "city": "Darlington",
      "state":"NJ",
      "zip": "10192",
      "country": "Mexico",
      "tax_id": "xmn671212drx",
      "company_name":"X-Men Inc.",
      "phone": "77-777-7777",
      "email": "purshasing@x-men.org"
    }
  }
}, function(err, res) {
    console.log(res.toObject());
});
curl -H "Accept: application/vnd.conekta-v{global_api_version}+json" \
     -H "Content-type: application/json" \
     -u {global_api_key}: \
     -X POST -d '{
      "description":"Stogies",
      "amount": 20000,
      "currency":"MXN",
      "reference_id":"9839-wolf_pack",
      "card": "tok_test_visa_4242",
      "monthly_installments": 3,
      "details": {
        "name": "Arnulfo Quimare",
        "phone": "403-342-0642",
        "email": "logan@x-men.org",
        "customer": {
          "logged_in": true,
          "successful_purchases": 14,
          "created_at": 1379784950,
          "updated_at": 1379784950,
          "offline_payments": 4,
          "score": 9
        },
        "line_items": [{
          "name": "Box of Cohiba S1s",
          "description": "Imported From Mex.",
          "unit_price": 20000,
          "quantity": 1,
          "sku": "cohb_s1",
          "category": "food"
        }],
        "billing_address": {
          "street1":"77 Mystery Lane",
          "street2": "Suite 124",
          "street3": null,
          "city": "Darlington",
          "state":"NJ",
          "zip": "10192",
          "country": "Mexico",
          "tax_id": "xmn671212drx",
          "company_name":"X-Men Inc.",
          "phone": "77-777-7777",
          "email": "purshasing@x-men.org"
        }
      }
    }' https://api.conekta.io/charges
JSONObject payment_params;

payment_params = new JSONObject("{"
  + "'description':'Stogies',"
  + "'amount': 20000,"
  + "'currency':'MXN',"
  + "'reference_id':'9839-wolf_pack',"
  + "'card': 'tok_test_visa_4242',"
  + "'monthly_installments': 3,"
  + "'details': {"
    + "'name': 'Arnulfo Quimare',"
    + "'phone': '403-342-0642',"
    + "'email': 'logan@x-men.org',"
    + "'customer': {"
      + "'logged_in': true,"
      + "'successful_purchases': 14,"
      + "'created_at': 1379784950,"
      + "'updated_at': 1379784950,"
      + "'offline_payments': 4,"
      + "'score': 9"
    + "},"
    + "'line_items': [{"
      + "'name': 'Box of Cohiba S1s',"
      + "'description': 'Imported From Mex.',"
      + "'unit_price': 20000,"
      + "'quantity': 1,"
      + "'sku': 'cohb_s1',"
      + "'category': 'food'"
    + "}],"
    + "'billing_address': {"
      + "'street1':'77 Mystery Lane',"
      + "'street2': 'Suite 124',"
      + "'street3': null,"
      + "'city': 'Darlington',"
      + "'state':'NJ',"
      + "'zip': '10192',"
      + "'country': 'Mexico',"
      + "'tax_id': 'xmn671212drx',"
      + "'company_name':'X-Men Inc.',"
      + "'phone': '77-777-7777',"
      + "'email': 'purshasing@x-men.org'"
    + "}"
  + "}"
+ "}");

try {
  Charge conektaCharge = Charge.create(payment_params);
}
catch (Error e){
  System.out.println(e.toString());
}
begin
  charge = Conekta::Charge.create({
    "amount"=> 51000,
    "currency"=> "MXN",
    "description"=> "Pizza Delivery",
    "reference_id"=> "orden_de_id_interno",
    "monthly_installments" => 3,
    "card"=> params[:conektaTokenId],  # Ej. "tok_a4Ff0dD2xYZZq82d9"
    "details"=> {
      "name"=> "Arnulfo Quimare",
      "phone"=> "403-342-0642",
      "email"=> "logan@x-men.org",
      "customer"=> {
        "logged_in"=> true,
        "successful_purchases"=> 14,
        "created_at"=> 1379784950,
        "updated_at"=> 1379784950,
        "offline_payments"=> 4,
        "score"=> 9
      },
      "line_items"=> [{
        "name"=> "Box of Cohiba S1s",
        "description"=> "Imported From Mex.",
        "unit_price"=> 20000,
        "quantity"=> 1,
        "sku"=> "cohb_s1",
        "category"=> "food"
      }],
      "billing_address"=> {
        "street1"=>"77 Mystery Lane",
        "street2"=> "Suite 124",
        "city"=> "Darlington",
        "state"=>"NJ",
        "zip"=> "10192",
        "country"=> "Mexico",
        "tax_id"=> "xmn671212drx",
        "company_name"=>"X-Men Inc.",
        "phone"=> "77-777-7777",
        "email"=> "purshasing@x-men.org"
      }
    }
  })
rescue Conekta::ParameterValidationError => e
  puts e.message_to_purchaser 
#alguno de los parámetros fueron inválidos
rescue Conekta::ProcessingError => e
  puts e.message_to_purchaser 
#la tarjeta no pudo ser procesada
rescue Conekta::Error => e
  puts e.message_to_purchaser 
#un error ocurrió que no sucede en el flujo normal de cobros como por ejemplo un auth_key incorrecto
end
conekta.Charge charge = new conekta.Charge ().create(@"{
  ""description"":""Stogies"",
  ""amount"": 30000,
  ""currency"":""MXN"",
  ""reference_id"":""9839-wolf_pack"",
  ""card"": ""tok_test_visa_4242"",
  ""monthly_installments"": 3,
  ""details"": {
    ""name"": ""Arnulfo Quimare"",
    ""phone"": ""403-342-0642"",
    ""email"": ""logan@x-men.org"",
    ""customer"": {
      ""logged_in"": true,
      ""successful_purchases"": 14,
      ""created_at"": 1379784950,
      ""updated_at"": 1379784950,
      ""offline_payments"": 4,
      ""score"": 9
    },
    ""line_items"": [{
      ""name"": ""Box of Cohiba S1s"",
      ""description"": ""Imported From Mex."",
      ""unit_price"": 20000,
      ""quantity"": 1,
      ""sku"": ""cohb_s1"",
      ""category"": ""food""
    }],
    ""billing_address"": {
      ""street1"":""77 Mystery Lane"",
      ""street2"": ""Suite 124"",
      ""street3"": null,
      ""city"": ""Darlington"",
      ""state"":""NJ"",
      ""zip"": ""10192"",
      ""country"": ""Mexico"",
      ""tax_id"": ""xmn671212drx"",
      ""company_name"":""X-Men Inc."",
      ""phone"": ""77-777-7777"",
      ""email"": ""purshasing@x-men.org""
    }
  }
}");

Puedes utilizar el token tok_test_visa_4242 en modo sandbox para procesar cargos de prueba rápidamente del lado del servidor.

Lee más sobre otras técnicas y tokens de prueba.