Gets read-only credit card information associated with the customer who is currently logged in.

Gets read-only credit card information associated with the customer who is currently logged in.

Description

Gets read-only credit card information associated with the customer who is currently logged in. You can only use this method on a secure domain; if you use it on a non-secure domain, it returns an error.

In the following example, getCreditCards() is used to obtain all credit cards. We then check if any of the credit cards have expired based on the expirationDate property of each card in the returned array.

// Basic validation of credit card expiry date. Returns True if credit card has expired.
function creditCardExpired(ccDate) {
 var now = new Date();
 var currentMonth = now.getMonth() + 1;
 var currentYear = now.getFullYear();
 var regExpCard = /^(\d{1,2})\/(\d{1,2})\/(\d{4})/;
 var regExpCardMatched = ccDate.match(regExpCard);
 var ccCardMonth = regExpCardMatched[1];
 var ccCardYear = regExpCardMatched[3];

 if (ccCardYear < currentYear) {
     return true;
 }
 else if (ccCardMonth <= currentMonth) {
     return true;
 }
 else {
     return false;
 }
}

var userProfile = container.getComponent('UserProfile');

userprofile.getCreditCards().then(function(creditCards) {

 for (i = 0; i <= creditCards.length; i++) {

     if (creditCardExpired(creditCards[i].expirationDate)) {
         alert("Credit card " + creditCards[i].number + " has expired.");
         break;
     }
 }
}).fail(function(error) {
 console.log(error);
});
Returns

Array.<CreditCardInfo>

Returns a Deferred object. If the promise is resolved, it returns an array of CreditCardInfo objects. If the user is not logged in, it returns an empty array. If the promise is rejected, it returns an error.

Leave a comment

Your email address will not be published. Required fields are marked *