From 5b9a6a7162563ee58583484283983808baf11ff0 Mon Sep 17 00:00:00 2001
From: Octavian Cimpu <octavian-george.cimpu@db.com>
Date: Tue, 19 Jul 2016 00:36:13 +0200
Subject: [PATCH] Fixed the error for registering a card twice

---
 .../android/example/MainActivity.java         |  6 ++--
 .../com/mangopay/android/sdk/MangoPay.java    | 17 +++++++---
 .../mangopay/android/sdk/MangoPayBuilder.java | 14 +++++++--
 .../mangopay/android/sdk/model/MangoCard.java | 31 +++++++++++++++++++
 .../android/sdk/model/MangoSettings.java      | 12 +++++++
 5 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/example/src/main/java/com/mangopay/android/example/MainActivity.java b/example/src/main/java/com/mangopay/android/example/MainActivity.java
index 4343337..7410701 100644
--- a/example/src/main/java/com/mangopay/android/example/MainActivity.java
+++ b/example/src/main/java/com/mangopay/android/example/MainActivity.java
@@ -111,7 +111,9 @@ private void getCardRegistration() {
                     .preregistrationData(preregistrationData)
                     .cardPreregistrationId(cardPreregistrationId)
                     .cardNumber("3569990000000157")
-                    .cardExpirationDate("0920")
+                     //.cardExpirationDate("0920")
+                    .cardExpirationYear(2017)
+                    .cardExpirationMonth(2)
                     .cardCvx("123")
                     .logLevel(LogLevel.FULL)
                     .callback(new Callback() {
@@ -119,7 +121,7 @@ private void getCardRegistration() {
                         Log.d(MainActivity.class.getSimpleName(), cardRegistration.toString());
                       }
 
-                      @Override public void failure(MangoError error) {
+                      @Override public void failure(MangoException error) {
                         Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                       }
                     }).start();
diff --git a/mangopay/src/main/java/com/mangopay/android/sdk/MangoPay.java b/mangopay/src/main/java/com/mangopay/android/sdk/MangoPay.java
index c505088..7976281 100644
--- a/mangopay/src/main/java/com/mangopay/android/sdk/MangoPay.java
+++ b/mangopay/src/main/java/com/mangopay/android/sdk/MangoPay.java
@@ -50,12 +50,18 @@ public void registerCard() {
     registerCard(null, null);
   }
 
+  public void registerCard(Callback callback) {
+    registerCard(null, callback);
+  }
+
   public void registerCard(MangoCard card, Callback callback) {
-    PrintLog.debug("MangoPay SDK register card started");
-    if (mCallback == null)
+    PrintLog.debug("MangoPay SDK register card started ");
+    if (callback != null) {
       this.mCallback = callback;
-    if (mCard == null)
-      mCard = card;
+    }
+    if (card != null && !card.equals(mCard)) {
+      this.mCard = card;
+    }
 
     GetTokenInteractor.Callback serviceCallback = new GetTokenInteractor.Callback() {
       @Override public void onGetTokenSuccess(String response) {
@@ -72,6 +78,9 @@ public void registerCard(MangoCard card, Callback callback) {
       mSettings.validate();
       mCard.validate();
 
+      PrintLog.debug(mSettings.toString());
+      PrintLog.debug(mCard.toString());
+
       getTokenInteractor.execute(serviceCallback, mSettings.getCardRegistrationURL(),
               mSettings.getPreregistrationData(), mSettings.getAccessKey(),
               mCard.getCardNumber(), mCard.getExpirationDate(), mCard.getCvx());
diff --git a/mangopay/src/main/java/com/mangopay/android/sdk/MangoPayBuilder.java b/mangopay/src/main/java/com/mangopay/android/sdk/MangoPayBuilder.java
index c80a233..c3a8b10 100644
--- a/mangopay/src/main/java/com/mangopay/android/sdk/MangoPayBuilder.java
+++ b/mangopay/src/main/java/com/mangopay/android/sdk/MangoPayBuilder.java
@@ -3,7 +3,6 @@
 import android.content.Context;
 
 import com.mangopay.android.sdk.model.LogLevel;
-import com.mangopay.android.sdk.model.MangoCard;
 import com.mangopay.android.sdk.util.PrintLog;
 
 import java.util.Calendar;
@@ -207,9 +206,18 @@ public MangoPayBuilder logLevel(LogLevel logLevel) {
     return this;
   }
 
+  /**
+   * Starts the card registration process
+   */
   public void start() {
-    MangoCard mCard = new MangoCard(cardNumber, expirationDate, cvx);
-    build().registerCard(mCard, callback);
+    build().registerCard(callback);
+    resetBuilderCardInfo();
+  }
+
+  private void resetBuilderCardInfo() {
+    this.cardNumber= null;
+    this.expirationDate= null;
+    this.cvx= null;
   }
 
   public MangoPay build() {
diff --git a/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoCard.java b/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoCard.java
index b6f1271..6a9c4aa 100644
--- a/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoCard.java
+++ b/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoCard.java
@@ -71,4 +71,35 @@ private void validateCardCvx() {
   private boolean isFieldValid(String field, String regex) {
     return field != null && field.matches(regex);
   }
+
+  @Override
+  public String toString() {
+    return "MangoCard{" +
+            "cardNumber='" + cardNumber + '\'' +
+            ", expirationDate='" + expirationDate + '\'' +
+            ", cvx='" + cvx + '\'' +
+            '}';
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    MangoCard mangoCard = (MangoCard) o;
+
+    if (cardNumber != null ? !cardNumber.equals(mangoCard.cardNumber) : mangoCard.cardNumber != null)
+      return false;
+    if (expirationDate != null ? !expirationDate.equals(mangoCard.expirationDate) : mangoCard.expirationDate != null)
+      return false;
+    return cvx != null ? cvx.equals(mangoCard.cvx) : mangoCard.cvx == null;
+  }
+
+  @Override
+  public int hashCode() {
+    int result = cardNumber != null ? cardNumber.hashCode() : 0;
+    result = 31 * result + (expirationDate != null ? expirationDate.hashCode() : 0);
+    result = 31 * result + (cvx != null ? cvx.hashCode() : 0);
+    return result;
+  }
 }
diff --git a/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoSettings.java b/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoSettings.java
index bc73d5f..8d6605e 100644
--- a/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoSettings.java
+++ b/mangopay/src/main/java/com/mangopay/android/sdk/model/MangoSettings.java
@@ -69,4 +69,16 @@ public void validate() throws MangoException {
       throw new MangoException(e);
     }
   }
+
+  @Override
+  public String toString() {
+    return "MangoSettings{" +
+            "baseURL='" + baseURL + '\'' +
+            ", clientId='" + clientId + '\'' +
+            ", cardPreregistrationId='" + cardPreregistrationId + '\'' +
+            ", cardRegistrationURL='" + cardRegistrationURL + '\'' +
+            ", preregistrationData='" + preregistrationData + '\'' +
+            ", accessKey='" + accessKey + '\'' +
+            '}';
+  }
 }