Skip to content

Instantly share code, notes, and snippets.

@matiasgualino
Last active April 20, 2016 17:58
Show Gist options
  • Save matiasgualino/f13e7bf8dffca3e7de668d935900c520 to your computer and use it in GitHub Desktop.
Save matiasgualino/f13e7bf8dffca3e7de668d935900c520 to your computer and use it in GitHub Desktop.
public void createPayment(final Activity activity, String token,
Integer installments, Long cardIssuerId, final PaymentMethod paymentMethod) {
if (paymentMethod != null) {
// Crear el item que se está cobrando
Item item = new Item(DUMMY_ITEM_ID, DUMMY_ITEM_QUANTITY,
DUMMY_ITEM_UNIT_PRICE);
// Obtener el ID del medio de pago
String paymentMethodId = paymentMethod.getId();
/* Crear el body del request que va a llegar a tu servidor.
* Debes indicar el item, la cantidad de cuotas, el banco,
* el token de la tarjeta, el medio de pago y cómo identificas
* a tu usuario para que puedas asignarle a éste el movimiento
*/
MerchantPayment payment = new MerchantPayment(item, installments,
cardIssuerId, token, paymentMethodId, DUMMY_MERCHANT_ACCESS_TOKEN);
// Enviar los datos a tu servidor
MerchantServer.createPayment(activity, TU_BASE_URL, TU_CREATE_PAYMENT_URI,
payment, new Callback<Payment>() {
@Override
public void success(Payment payment, Response response) {
// Ya se realizó el pago.
// Verifica si el pago es con tarjeta, sino es un medio offline.
if (MercadoPagoUtil.isCardPaymentType(paymentMethod.getPaymentTypeId())) {
// Si es un medio con tarjeta,
// debes mostrarle una pantalla de felicitaciones o de error
startCongratsActivity(payment, paymentMethod);
} else {
// Pero si es un medio offline, debes
// instruir al usuario a finalizar el pago.
startInstructionsActivity(payment, paymentMethod);
}
}
@Override
public void failure(RetrofitError error) {
// Ups, ha ocurrido un error.
Toast.makeText(activity, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(activity, "Invalid payment method", Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment