Skip to content

feat(android): Update to latest module and Ti SDK #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stripe_android/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation "com.stripe:stripe-java:22.21.0"
}
Binary file not shown.
Binary file not shown.
Binary file removed stripe_android/android/dist/stripe_android.jar
Binary file not shown.
2 changes: 0 additions & 2 deletions stripe_android/android/lib/README

This file was deleted.

Binary file removed stripe_android/android/lib/gson-2.8.2.jar
Binary file not shown.
Binary file removed stripe_android/android/lib/stripe-java-1.15.1.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions stripe_android/android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 3.0
version: 4.0.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Stripe payment library for Android
author: Ravindra Chherke
license: To Ravindra chherke
Expand All @@ -15,4 +15,4 @@ name: stripe_android
moduleid: com.ravindra.stripe
guid: 89b4eaf4-d57f-4956-b6a3-deb3a635ac2c
platform: android
minsdk: 7.0.0.GA
minsdk: 9.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,10 @@ public StripeAndroidModule() {

@Kroll.onAppCreate
public static void onAppCreate(TiApplication app) {
Log.d(LCAT, "inside onAppCreate");
// put module init code that needs to run when the application is
// created
}

// Methods
@Kroll.method
public String example() {
Log.d(LCAT, "example called");
return "hello world";
}

// Properties
@Kroll.getProperty
public String getExampleProp() {
Log.d(LCAT, "get example property");
return "hello world";
}

@Kroll.setProperty
public void setExampleProp(String value) {
Log.d(LCAT, "set example property: " + value);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Kroll.method
public boolean setCard(HashMap propsIn) {
KrollDict props = new KrollDict(propsIn);
Expand Down Expand Up @@ -107,22 +86,12 @@ public boolean setCard(HashMap propsIn) {
cvc = props.getString("cvc");
}

card = new Card(cardNumber, month, expiryYear, cvc);
card = new Card(cardNumber, (long) month, (long) expiryYear, cvc);

return true;

}


/*@SuppressWarnings({ "unchecked", "rawtypes" })
@Kroll.method
public String validateCard() {

}*/



@SuppressWarnings({ "unchecked", "rawtypes" })
@Kroll.method
public void requestForToken(HashMap args) {

Expand Down
69 changes: 35 additions & 34 deletions stripe_android/android/src/com/stripe/android/Stripe.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package com.stripe.android;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.Executor;

import com.stripe.android.compat.AsyncTask;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import com.stripe.android.util.TextUtils;
import com.stripe.exception.AuthenticationException;

public class Stripe {
private String defaultPublishableKey;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.Executor;

public class Stripe {
public TokenCreator tokenCreator = new TokenCreator() {
@Override
public void create(final Card card, final String publishableKey, final Executor executor,
final TokenCallback callback) {
final TokenCallback callback) {
AsyncTask<Void, Void, ResponseWrapper> task = new AsyncTask<Void, Void, ResponseWrapper>() {
protected ResponseWrapper doInBackground(Void... params) {
try {
com.stripe.model.Token stripeToken = com.stripe.model.Token.create(
hashMapFromCard(card), publishableKey);
hashMapFromCard(card));
com.stripe.model.Card stripeCard = stripeToken.getCard();
Card card = androidCardFromStripeCard(stripeCard);
Token token = androidTokenFromStripeToken(card, stripeToken);
Expand All @@ -35,22 +33,21 @@ protected ResponseWrapper doInBackground(Void... params) {

protected void onPostExecute(ResponseWrapper result) {
tokenTaskPostExecution(result, callback);
}
}
};

executeTokenTask(executor, task);
}
};

public TokenRequester tokenRequester = new TokenRequester() {
@Override
public void request(final String tokenId, final String publishableKey,
final Executor executor, final TokenCallback callback) {
@Override
public void request(final String tokenId, final String publishableKey,
final Executor executor, final TokenCallback callback) {
AsyncTask<Void, Void, ResponseWrapper> task = new AsyncTask<Void, Void, ResponseWrapper>() {
protected ResponseWrapper doInBackground(Void... params) {
try {
com.stripe.model.Token stripeToken = com.stripe.model.Token.retrieve(
tokenId, publishableKey);
tokenId);
com.stripe.model.Card stripeCard = stripeToken.getCard();
Card card = androidCardFromStripeCard(stripeCard);
Token token = androidTokenFromStripeToken(card, stripeToken);
Expand All @@ -62,12 +59,13 @@ protected ResponseWrapper doInBackground(Void... params) {

protected void onPostExecute(ResponseWrapper result) {
tokenTaskPostExecution(result, callback);
}
}
};

executeTokenTask(executor, task);
}
}
};
private String defaultPublishableKey;

public Stripe() {
}
Expand All @@ -83,10 +81,10 @@ public void setDefaultPublishableKey(String publishableKey) throws Authenticatio

private void validateKey(String publishableKey) throws AuthenticationException {
if (publishableKey == null || publishableKey.length() == 0) {
throw new AuthenticationException("Invalid Publishable Key: You must use a valid publishable key to create a token. For more info, see https://stripe.com/docs/stripe.js.");
throw new AuthenticationException("Invalid Publishable Key: You must use a valid publishable key to create a token. For more info, see https://stripe.com/docs/stripe.js.", "", "", 0);
}
if (publishableKey.startsWith("sk_")) {
throw new AuthenticationException("Invalid Publishable Key: You are using a secret key to create a token, instead of the publishable one. For more info, see https://stripe.com/docs/stripe.js");
throw new AuthenticationException("Invalid Publishable Key: You are using a secret key to create a token, instead of the publishable one. For more info, see https://stripe.com/docs/stripe.js", "", "", 0);
}
}

Expand All @@ -113,8 +111,7 @@ public void requestToken(final String tokenId, final String publishableKey, fina
validateKey(publishableKey);

tokenRequester.request(tokenId, publishableKey, executor, callback);
}
catch (AuthenticationException e) {
} catch (AuthenticationException e) {
callback.onError(e);
}
}
Expand Down Expand Up @@ -142,14 +139,18 @@ public void createToken(final Card card, final String publishableKey, final Exec
validateKey(publishableKey);

tokenCreator.create(card, publishableKey, executor, callback);
}
catch (AuthenticationException e) {
} catch (AuthenticationException e) {
callback.onError(e);
}
}

private Card androidCardFromStripeCard(com.stripe.model.Card stripeCard) {
return new Card(null, stripeCard.getExpMonth(), stripeCard.getExpYear(), null, stripeCard.getName(), stripeCard.getAddressLine1(), stripeCard.getAddressLine2(), stripeCard.getAddressCity(), stripeCard.getAddressState(), stripeCard.getAddressZip(), stripeCard.getAddressCountry(), stripeCard.getLast4(), stripeCard.getType(), stripeCard.getFingerprint(), stripeCard.getCountry());
return new Card(null, stripeCard.getExpMonth(), stripeCard.getExpYear(), null,
stripeCard.getName(), stripeCard.getAddressLine1(), stripeCard.getAddressLine2(),
stripeCard.getAddressCity(), stripeCard.getAddressState(), stripeCard.getAddressZip(),
stripeCard.getAddressCountry(), stripeCard.getLast4(), "",
stripeCard.getFingerprint(), stripeCard.getCountry()
);
}

private Token androidTokenFromStripeToken(Card androidCard, com.stripe.model.Token stripeToken) {
Expand Down Expand Up @@ -199,6 +200,14 @@ private Map<String, Object> hashMapFromCard(Card card) {
return tokenParams;
}

interface TokenCreator {
void create(Card card, String publishableKey, Executor executor, TokenCallback callback);
}

interface TokenRequester {
void request(String tokenId, String publishableKey, Executor executor, TokenCallback callback);
}

private class ResponseWrapper {
public final Token token;
public final Exception error;
Expand All @@ -208,12 +217,4 @@ private ResponseWrapper(Token token, Exception error) {
this.token = token;
}
}

interface TokenCreator {
public void create(Card card, String publishableKey, Executor executor, TokenCallback callback);
}

interface TokenRequester {
public void request(String tokenId, String publishableKey, Executor executor, TokenCallback callback);
}
}
}
28 changes: 14 additions & 14 deletions stripe_android/android/src/com/stripe/android/model/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class Card extends com.stripe.model.StripeObject {

private String number;
private String cvc;
private Integer expMonth;
private Integer expYear;
private Long expMonth;
private Long expYear;
private String name;
private String addressLine1;
private String addressLine2;
Expand All @@ -43,8 +43,8 @@ public class Card extends com.stripe.model.StripeObject {
public static class Builder {
private final String number;
private final String cvc;
private final Integer expMonth;
private final Integer expYear;
private final Long expMonth;
private final Long expYear;
private String name;
private String addressLine1;
private String addressLine2;
Expand All @@ -57,7 +57,7 @@ public static class Builder {
private String fingerprint;
private String country;

public Builder(String number, Integer expMonth, Integer expYear, String cvc) {
public Builder(String number, Long expMonth, Long expYear, String cvc) {
this.number = number;
this.expMonth = expMonth;
this.expYear = expYear;
Expand Down Expand Up @@ -144,7 +144,7 @@ private Card(Builder builder) {
this.last4 = getLast4();
}

public Card(String number, Integer expMonth, Integer expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry, String last4, String type, String fingerprint, String country) {
public Card(String number, Long expMonth, Long expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry, String last4, String type, String fingerprint, String country) {
this.number = TextUtils.nullIfBlank(normalizeCardNumber(number));
this.expMonth = expMonth;
this.expYear = expYear;
Expand All @@ -164,11 +164,11 @@ public Card(String number, Integer expMonth, Integer expYear, String cvc, String
this.last4 = getLast4();
}

public Card(String number, Integer expMonth, Integer expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry) {
public Card(String number, Long expMonth, Long expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry) {
this(number, expMonth, expYear, cvc, name, addressLine1, addressLine2, addressCity, addressState, addressZip, addressCountry, null, null, null, null);
}

public Card(String number, Integer expMonth, Integer expYear, String cvc) {
public Card(String number, Long expMonth, Long expYear, String cvc) {
this(number, expMonth, expYear, cvc, null, null, null, null, null, null, null, null, null, null, null);
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public boolean validateExpiryDate() {
if (!validateExpYear()) {
return false;
}
return !DateUtils.hasMonthPassed(expYear, expMonth);
return !DateUtils.hasMonthPassed(Math.toIntExact(expYear), Math.toIntExact(expMonth));
}

public boolean validateExpMonth() {
Expand All @@ -222,7 +222,7 @@ public boolean validateExpYear() {
if (expYear == null) {
return false;
}
return !DateUtils.hasYearPassed(expYear);
return !DateUtils.hasYearPassed(Math.toIntExact(expYear));
}

public boolean validateCVC() {
Expand Down Expand Up @@ -290,19 +290,19 @@ public void setCVC(String cvc) {
this.cvc = cvc;
}

public Integer getExpMonth() {
public Long getExpMonth() {
return expMonth;
}

public void setExpMonth(Integer expMonth) {
public void setExpMonth(Long expMonth) {
this.expMonth = expMonth;
}

public Integer getExpYear() {
public Long getExpYear() {
return expYear;
}

public void setExpYear(Integer expYear) {
public void setExpYear(Long expYear) {
this.expYear = expYear;
}

Expand Down