Last active
June 8, 2017 22:01
-
-
Save bumble-bee-chuna/c744e90235252367605107318c6f6f04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.snapwebdevelopment.scanhappy.paymentApi; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.snapwebdevelopment.scanhappy.CommonConstants; | |
import com.snapwebdevelopment.scanhappy.managers.AuthenticatedUserManager; | |
import com.snapwebdevelopment.scanhappy.models.AuthenticatedUser; | |
import com.snapwebdevelopment.scanhappy.paymentApi.callbacks.ResultCallback; | |
import com.snapwebdevelopment.scanhappy.paymentApi.callbacks.StripeTokenCallback; | |
import com.snapwebdevelopment.scanhappy.paymentApi.callbacks.VolleyCallback; | |
import com.snapwebdevelopment.scanhappy.paymentApi.responseParsers.StripeJSONResponseParser; | |
import com.stripe.android.Stripe; | |
import com.stripe.android.TokenCallback; | |
import com.stripe.android.exception.AuthenticationException; | |
import com.stripe.android.model.Card; | |
import com.stripe.android.model.Token; | |
import org.json.JSONObject; | |
/** | |
* Created by beckah on 5/9/17. | |
* Comment: When payment info is stored in AutheticatedUserManager, | |
* it is the account id and card id, not the actual card numbers. | |
*/ | |
public class StripePaymentApi implements PaymentApiInterface { | |
private Context context; | |
private final VolleyCallback volleyCallback = new VolleyCallback(){ | |
@Override | |
public void onSuccess(JSONObject result){ | |
try { | |
//Update the AuthenticatedUserManager with new payment info | |
if(this.getRefreshUserInfo()) | |
refreshUserPaymentInfo(result); | |
//Return successful result because stripe server was able to be contacted | |
if (this.getResultCallback() != null) | |
this.getResultCallback().onSuccess(true); | |
} catch (Throwable throwable) { | |
Log.e(CommonConstants.STRIPE_ERROR, CommonConstants.STRIPE_PARSE_ERROR, throwable); | |
//Return error message because json response was unable to be parsed | |
if(this.getResultCallback() != null) | |
this.getResultCallback().onError(throwable); | |
} | |
} | |
@Override | |
public void onError(Throwable throwable) { | |
Log.d(CommonConstants.STRIPE_ERROR, CommonConstants.STRIPE_SERVER_ERROR, throwable); | |
} | |
}; | |
public StripePaymentApi(Context context){ | |
this.context = context; | |
} | |
/** | |
* Used to create an account on Stripe. If successfully created it will update | |
* AuthenticatedUserManager with payment info | |
* @param customerEmail customer email address to create a stripe account with | |
* @param card card to put on file with new stripe account | |
* @param callback used to let calling method know if account was created successfully | |
*/ | |
@Override | |
public void createUserPaymentAccount(final String customerEmail, Card card, final ResultCallback callback) { | |
final VolleyCallback mVolleyCallback = getVolleyCallback(callback); | |
createStripeToken(card, new StripeTokenCallback() { | |
@Override | |
public void onSuccess(Token token) { | |
String url = String.format(CommonConstants.CREATE_CUSTOMER_URL, customerEmail, token.getId()); | |
callStripe( | |
url, | |
Request.Method.POST, | |
mVolleyCallback); | |
} | |
@Override | |
public void onError(Throwable t) { | |
callback.onError(t); | |
} | |
}); | |
} | |
/** | |
* Used to delete an account on Stripe | |
* @param customerId account id to delete | |
*/ | |
@Override | |
public void deleteUserPaymentAccount(String customerId) { | |
String url = String.format(CommonConstants.DELETE_CUSTOMER_URL, customerId); | |
callStripe( | |
url, | |
Request.Method.POST, | |
volleyCallback | |
); | |
} | |
/** | |
* Get Stripe payment information for customer. Once successfully retrieved, | |
* it will update AuthenticatedUserManager with new info | |
* @param customerId id of customer account on Stripe | |
*/ | |
@Override | |
public void retrieveUserPaymentInfo(String customerId) { | |
String url = String.format(CommonConstants.RETRIEVE_CUSTOMER_INFO_URL, customerId); | |
callStripe( | |
url, | |
Request.Method.POST, | |
volleyCallback | |
); | |
} | |
/** | |
* Add payment method (credit card) to user Stripe account. | |
* Once successfully updated, it will update AuthenticatedUserManager | |
* with new info | |
* @param customerId id of customer account on Stripe | |
* @param card Stripe card to put on file | |
* @param callback used to let calling method know if card added successfully | |
*/ | |
@Override | |
public void addPaymentMethod(final String customerId, Card card, final ResultCallback callback) { | |
final VolleyCallback mVolleyCallback = getVolleyCallback(callback); | |
createStripeToken(card, new StripeTokenCallback() { | |
@Override | |
public void onSuccess(Token token) { | |
String url = String.format(CommonConstants.ADD_CUSTOMER_PAYMENT_METHOD_URL, token.getId(), customerId); | |
callStripe( | |
url, | |
Request.Method.POST, | |
mVolleyCallback | |
); | |
} | |
@Override | |
public void onError(Throwable t) { | |
callback.onError(t); | |
} | |
}); | |
} | |
/** | |
* Delete payment method (credit card) from user Stripe account. | |
* Once successfully delete, it will update AuthenticatedUserManager | |
* with new info | |
* @param customerId id of customer account on Stripe | |
* @param cardId Stripe card id to delete | |
* @param callback used to let calling method know if card deleted successfully | |
*/ | |
@Override | |
public void deletePaymentMethod(String customerId, String cardId, final ResultCallback callback) { | |
final VolleyCallback mVolleyCallback = getVolleyCallback(callback); | |
String url = String.format(CommonConstants.DELETE_CUSTOMER_PAYMENT_METHOD_URL, customerId, cardId); | |
callStripe( | |
url, | |
Request.Method.POST, | |
mVolleyCallback | |
); | |
} | |
/** | |
* Charge default payment method of user's Stripe account. | |
* @param customerId id of customer account on Stripe | |
* @param chargeAmount amount to money to charge user in the form of cents | |
* @param callback used to let calling method know user account was charged successfully | |
*/ | |
@Override | |
public void chargeUser(String customerId, Integer chargeAmount, final ResultCallback callback) { | |
final VolleyCallback mVolleyCallback = getVolleyCallback(callback); | |
mVolleyCallback.setRefreshUserInfo(false); | |
String url = String.format(CommonConstants.CHARGE_CUSTOMER_URL, customerId, chargeAmount); | |
callStripe( | |
url, | |
Request.Method.POST, | |
mVolleyCallback | |
); | |
} | |
public Context getContext() { | |
return context; | |
} | |
public void setContext(Context context) { | |
this.context = context; | |
} | |
/*** | |
* Helpers | |
*/ | |
/** | |
* Generic method to call server to get JSON response | |
* @param url url to try to contact via http | |
* @param httpMethod HTTP method to contact server with | |
* @param callback used to let calling method know if retrieved a response from the server | |
*/ | |
private void callStripe(String url, Integer httpMethod, final VolleyCallback callback) { | |
RequestQueue queue = Volley.newRequestQueue(context); | |
Log.d("URL", url); | |
// Request a string response from the provided URL. | |
StringRequest httpRequest = new StringRequest(httpMethod, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String responseJSON) { | |
try { | |
JSONObject jsonResponseObj = new JSONObject(responseJSON); | |
callback.onSuccess(jsonResponseObj); | |
} catch (Throwable throwable) { | |
Log.e(CommonConstants.STRIPE_ERROR, CommonConstants.STRIPE_PARSE_ERROR, throwable); | |
callback.onError(throwable); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Log.d(CommonConstants.STRIPE_ERROR, CommonConstants.STRIPE_SERVER_RESPONSE_ERROR + error.toString()); | |
callback.onError(error); | |
} | |
}); | |
// Add the request to the RequestQueue. | |
queue.add(httpRequest); | |
} | |
/** | |
* Use to create a special Stripe token | |
* @param card credit card to make the token out of | |
* @param callback used to return Stripe token to calling method | |
*/ | |
private void createStripeToken(Card card, final StripeTokenCallback callback){ | |
try { | |
Stripe stripe = new Stripe(CommonConstants.STRIPE_SECRET_KEY); | |
stripe.createToken( card, new TokenCallback() { | |
@Override | |
public void onSuccess(Token token) { | |
callback.onSuccess(token); | |
} | |
@Override | |
public void onError(Exception error) { | |
callback.onError(error); | |
} | |
}); | |
} catch (AuthenticationException e) { | |
e.printStackTrace(); | |
callback.onError(e); | |
} | |
} | |
@NonNull | |
private VolleyCallback getVolleyCallback(ResultCallback callback) { | |
final VolleyCallback mVolleyCallback = this.volleyCallback; | |
mVolleyCallback.setResultCallback(callback); | |
return mVolleyCallback; | |
} | |
/** | |
* Used to update user manager | |
* @param result with stripe account information (in the form of json) | |
* update the AuthenticatedUser payment information | |
*/ | |
private void refreshUserPaymentInfo(JSONObject result) { | |
AuthenticatedUser user = AuthenticatedUserManager.getInstance().getUser(); | |
//Update authenticated user's payment information | |
user.setPaymentInfo( | |
StripeJSONResponseParser.getPaymentInfoFromJSON(result) | |
); | |
//Commit change to AuthenticatedUserManager | |
AuthenticatedUserManager.getInstance().setUser(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment