Skip to content
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

Fix for issue #8 and manifest tweak #12

Open
wants to merge 5 commits 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
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Ben Mills
Ben Mills
Ezekiel Buchheit
10 changes: 1 addition & 9 deletions balanced-android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,5 @@
android:versionName="1.1-SNAPSHOT">
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="19"/>
<application android:label="balanced-android">
<activity android:name="main"
android:label="balanced-android">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<application />
</manifest>
324 changes: 186 additions & 138 deletions balanced-android/src/main/java/com/balancedpayments/android/Balanced.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.balancedpayments.android;

import android.content.Context;
import android.os.AsyncTask;

import com.balancedpayments.android.exception.*;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
Expand All @@ -23,141 +25,187 @@
*/
public class Balanced
{
public static final String VERSION = "1.1-SNAPSHOT";
private static String API_URL = "https://api.balancedpayments.com";
private static String API_VERSION = "1.1";
private static int connectionTimeout = 6000;
private static int socketTimeout = 6000;
private Context appContext;

public enum BPFundingInstrumentType {
Card,
BankAccount
}

public Balanced(Context context) {
appContext = context;
}

public Map<String, Object> createCard(String number, Integer expMonth, Integer expYear) throws CreationFailureException, FundingInstrumentNotValidException {
return createCard(number, expMonth, expYear, null);
}

public Map<String, Object> createCard(String number,
Integer expMonth,
Integer expYear,
Map<String, Object> optionalFields)
throws FundingInstrumentNotValidException, CreationFailureException {
Card card = new Card(number, expMonth, expYear);

if (card.isValid()) {
HashMap<String, Object> payload = new HashMap<String, Object>();
payload.put("number", card.getNumber());
payload.put("expiration_month", Integer.toString(card.getExpirationMonth()));
payload.put("expiration_year", Integer.toString(card.getExpirationYear()));

if (optionalFields != null && optionalFields.size() > 0) {
payload.putAll(optionalFields);
}

return createFundingInstrument(payload, Balanced.BPFundingInstrumentType.Card);
}
else {
throw new FundingInstrumentNotValidException("Card is not valid");
}
}

public Map<String, Object> createBankAccount(String routingNum,
String accountNum,
BankAccount.AccountType accountType,
String accountName)
throws CreationFailureException, FundingInstrumentNotValidException {
return createBankAccount(routingNum, accountNum, accountType, accountName, null);
}

public Map<String, Object> createBankAccount(String routingNum,
String accountNum,
BankAccount.AccountType accountType,
String accountName,
HashMap<String, Object> optionalFields)
throws FundingInstrumentNotValidException, CreationFailureException {
BankAccount bankAccount = new BankAccount(routingNum, accountNum, accountType, accountName, optionalFields);

if (bankAccount.isValid()) {
HashMap<String, Object> payload = new HashMap<String, Object>();
payload.put("routing_number", routingNum);
payload.put("account_number", accountNum);
payload.put("name", accountName);
payload.put("account_type", accountType);

if (optionalFields != null && optionalFields.size() > 0) {
payload.putAll(optionalFields);
}

return createFundingInstrument(payload, BPFundingInstrumentType.BankAccount);
}
else {
throw new FundingInstrumentNotValidException("Card is not valid");
}
}

public Map<String, Object> createFundingInstrument(Map<String, Object> payload, BPFundingInstrumentType type) throws CreationFailureException {
payload.put("meta", Utilities.capabilities(appContext));
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeout);
HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost request = new HttpPost(
API_URL +
(type.equals(BPFundingInstrumentType.Card) ? "/cards" : "/bank_accounts")
);

request.setHeader("accept", "application/json");
request.setHeader("Content-Type", "application/json");
request.setHeader("User-Agent", Utilities.userAgentString());

request.setEntity(new StringEntity(serialize(payload), "UTF8"));
HttpResponse response = httpClient.execute(request);

BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));

String responseData;
String jsonData = "";
while ((responseData = br.readLine()) != null) {
jsonData += responseData;
}

int statusCode = response.getStatusLine().getStatusCode();
Map<String, Object> jsonResponse = deserialize(jsonData);

if (statusCode != 201) {
throw new CreationFailureException((String)jsonResponse.get("description"));
}

httpClient.getConnectionManager().shutdown();

jsonResponse.put("status_code", statusCode);

return jsonResponse;
}
catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage());
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}

private String serialize(Object payload) {
Gson gson = new Gson();
String json = gson.toJson(payload);
return json;
}

private Map<String, Object> deserialize(String body) {
Gson gson = new Gson();
return gson.fromJson(body, HashMap.class);
}
}
public static final String VERSION = "1.1-SNAPSHOT";
private static String API_URL = "https://api.balancedpayments.com";
private static String API_VERSION = "1.1";
private static int connectionTimeout = 6000;
private static int socketTimeout = 6000;
private Context appContext;

private AsynchCreateFundingInstrument asynchCreateFundingInstrument;


public enum BPFundingInstrumentType {
Card,
BankAccount
}

public Balanced(Context context) {
appContext = context;
}

public void asynchCreateCard(OnTaskCompleted taskCompleted, String number, Integer expMonth, Integer expYear) {
asynchCreateFundingInstrument = new AsynchCreateFundingInstrument(taskCompleted, number, expMonth, expYear);
asynchCreateFundingInstrument.execute();
}

public Map<String, Object> createCard(String number, Integer expMonth, Integer expYear) throws CreationFailureException, FundingInstrumentNotValidException {
return createCard(number, expMonth, expYear, null);
}

public Map<String, Object> createCard(String number,
Integer expMonth,
Integer expYear,
Map<String, Object> optionalFields)
throws FundingInstrumentNotValidException, CreationFailureException {
Card card = new Card(number, expMonth, expYear);

if (card.isValid()) {
HashMap<String, Object> payload = new HashMap<String, Object>();
payload.put("number", card.getNumber());
payload.put("expiration_month", Integer.toString(card.getExpirationMonth()));
payload.put("expiration_year", Integer.toString(card.getExpirationYear()));

if (optionalFields != null && optionalFields.size() > 0) {
payload.putAll(optionalFields);
}

return createFundingInstrument(payload, Balanced.BPFundingInstrumentType.Card);
}
else {
throw new FundingInstrumentNotValidException("Card is not valid");
}
}

public Map<String, Object> createBankAccount(String routingNum,
String accountNum,
BankAccount.AccountType accountType,
String accountName)
throws CreationFailureException, FundingInstrumentNotValidException {
return createBankAccount(routingNum, accountNum, accountType, accountName, null);
}

public Map<String, Object> createBankAccount(String routingNum,
String accountNum,
BankAccount.AccountType accountType,
String accountName,
HashMap<String, Object> optionalFields)
throws FundingInstrumentNotValidException, CreationFailureException {
BankAccount bankAccount = new BankAccount(routingNum, accountNum, accountType, accountName, optionalFields);

if (bankAccount.isValid()) {
HashMap<String, Object> payload = new HashMap<String, Object>();
payload.put("routing_number", routingNum);
payload.put("account_number", accountNum);
payload.put("name", accountName);
payload.put("account_type", accountType);

if (optionalFields != null && optionalFields.size() > 0) {
payload.putAll(optionalFields);
}

return createFundingInstrument(payload, BPFundingInstrumentType.BankAccount);
}
else {
throw new FundingInstrumentNotValidException("Card is not valid");
}
}

public interface OnTaskCompleted{
void onTaskCompleted(Map<String, Object> map);
}

private class AsynchCreateFundingInstrument extends AsyncTask<HashMap<String, Object>, Object, Map<String, Object>> {

private OnTaskCompleted taskCompleted;
private String number;
private Integer expMonth;
private Integer expYear;

public AsynchCreateFundingInstrument(OnTaskCompleted taskCompleted, String number, Integer expMonth, Integer expYear) {
this.taskCompleted = taskCompleted;
this.number = number;
this.expMonth = expMonth;
this.expYear = expYear;
}

@Override
protected Map<String, Object> doInBackground(HashMap<String, Object>... payload) {
try {
try {
return createCard(number, expMonth, expYear, null);
} catch (FundingInstrumentNotValidException e) {
e.printStackTrace();
}
} catch (CreationFailureException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Map<String, Object> stringObjectMap) {
taskCompleted.onTaskCompleted(stringObjectMap);
}
}

public Map<String, Object> createFundingInstrument(Map<String, Object> payload, BPFundingInstrumentType type) throws CreationFailureException {
payload.put("meta", Utilities.capabilities(appContext));
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeout);
HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost request = new HttpPost(
API_URL +
(type.equals(BPFundingInstrumentType.Card) ? "/cards" : "/bank_accounts")
);

request.setHeader("accept", "application/json");
request.setHeader("Content-Type", "application/json");
request.setHeader("User-Agent", Utilities.userAgentString());

request.setEntity(new StringEntity(serialize(payload), "UTF8"));
HttpResponse response = httpClient.execute(request);

BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));

String responseData;
String jsonData = "";
while ((responseData = br.readLine()) != null) {
jsonData += responseData;
}

int statusCode = response.getStatusLine().getStatusCode();
Map<String, Object> jsonResponse = deserialize(jsonData);

if (statusCode != 201) {
throw new CreationFailureException((String)jsonResponse.get("description"));
}

httpClient.getConnectionManager().shutdown();

jsonResponse.put("status_code", statusCode);

return jsonResponse;
}
catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage());
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}

private String serialize(Object payload) {
Gson gson = new Gson();
String json = gson.toJson(payload);
return json;
}

private Map<String, Object> deserialize(String body) {
Gson gson = new Gson();
return gson.fromJson(body, HashMap.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ private boolean isNumberValid() {
}

private boolean isExpired() {
if (expirationMonth > 12 || expirationYear < 1) { return false; }
if (expirationMonth > 12 || expirationYear < 1) { return true; }

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());

return cal.get(Calendar.YEAR) > expirationYear || (cal.get(Calendar.YEAR) == expirationYear
&& cal.get(Calendar.MONTH) >= expirationMonth);
Expand Down