Android App With Stripe Payment

In this Tutorial, we will create a ONE CLICK PURCHASE BUTTON with Stripe Payment.

To View Whole Project Code:

https://github.com/dominwong4/Back4app-Stripe-Android-Tutorial

gm_20160914_151704gm_20160914_151523

Step 1 : Setup Dashboard

go to Your Parse Dashboard and

create a Class : Item

insert the following columns:
ItemName
price
quantityAvailable

insert a sample item record
ItemName: test
price: 10
quantityAvailable: 10

screen-shot-2016-09-13-at-6-14-52-pm

create a Class : Order

insert the following colums:
name
email
address
zip
city_state
item
size
fulfilled : Boolean
charged : Boolean
stripePaymentId

 

Step 2 : Get Stripe API Key

 

Create an account and login

Go to this link: https://manage.stripe.com/account/apikeys

if it does not work follow below instruction.

screen-shot-2016-09-13-at-5-12-53-pm
Go to Account Settings -> API Key

copy both
test publishable key (for Android Client)
pk_test_1IqGGvSacpQ8lpIBxuVvI5rP
test secret key (for CloudCode Server)
sk_test_U4ztvrYPxe7Ph3w0ii155rnw

Step 3 : Create Cloud Code for Stripe Payment

 

https://github.com/dominwong4/Back4app-Stripe-Android-Tutorial/blob/master/CloudCode/main.js

main.js (replace Stripe.initialize to your secret key)

Click here to view how to upload your CloudCode

Step 4 : Create your Android Client Application

1. add parse and stripe package to your project

go to build.gradle and add following code:

compile ‘com.stripe:stripe-android:+’
compile ‘com.parse:parse-android:1.13.0’

and then click Sync now

then go to AndroidManifest.xml

add <uses-permission android:name=”android.permission.INTERNET”/>
if not working , go to https://github.com/stripe/stripe-android to download their package.

2. To implement Stripe Payment, the follow is the mandatory functions are needed.

View Whole Code: https://github.com/dominwong4/Back4app-Stripe-Android-Tutorial/blob/master/app/src/main/java/dominwong4/scm/back4app_stripe_android_tutorial/MainActivity.java

Above the onCreate()

public static final String PUBLISHABLE_KEY = "pk_test_1IqGGvSacpQ8lpIBxuVvI5rP";
public static final String APPLICATION_ID = "RKNck9SdN6sqcznBvy5lqnN2ln1FrrSabNcq8YEK";
public static final String CLIENT_KEY = "zWtkaYFS0Ia91jKkgmIHJql30cARcrDmKUGAXLTY";
public static final String BACK4PAPP_API = "https://parseapi.back4app.com/";

For your application initialize

Inside OnCreate()

Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(APPLICATION_ID)
.clientKey(CLIENT_KEY)
.server(BACK4PAPP_API).build());

And A Dummy Card
//Note that you need to create a Credit card form for user so that they can use their card, the Card construction is following
card = new Card(
        "4242424242424242", //card number
        12, //expMonth
        2016,//expYear
        "123"//cvc
);

For the Purchasing main function

private void buy(){
    boolean validation = card.validateCard();
    if(validation){
        startProgress("Validating Credit Card");
        new Stripe().createToken(
                card,
                PUBLISHABLE_KEY,
                new TokenCallback() {
                    @Override
                    public void onError(Exception error) {
                        Log.d("Stripe",error.toString());
                    }

                    @Override
                    public void onSuccess(Token token) {
                        finishProgress();
                        charge(token);
                    }
                });
    } else if (!card.validateNumber()) {
        Log.d("Stripe","The card number that you entered is invalid");
    } else if (!card.validateExpiryDate()) {
        Log.d("Stripe","The expiration date that you entered is invalid");
    } else if (!card.validateCVC()) {
        Log.d("Stripe","The CVC code that you entered is invalid");
    } else {
        Log.d("Stripe","The card details that you entered are invalid");
    }
}

private void charge(Token cardToken){
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("itemName", "test"); //This part you need to pass your item detail, implement yourself
    params.put("cardToken", cardToken.getId());
    params.put("name","Dominic Wong");
    params.put("email","[email protected]");
    params.put("address","HIHI");
    params.put("zip","99999");
    params.put("city_state","CA");
    startProgress("Purchasing Item");
    ParseCloud.callFunctionInBackground("purchaseItem", params, new FunctionCallback<Object>() {
        public void done(Object response, ParseException e) {
            finishProgress();
            if (e == null) {
                Log.d("Cloud Response", "There were no exceptions! " + response.toString());
                Toast.makeText(getApplicationContext(),
                        "Item Purchased Successfully ",
                        Toast.LENGTH_LONG).show();
            } else {
                Log.d("Cloud Response", "Exception: " + e);
                Toast.makeText(getApplicationContext(),
                        e.getMessage().toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

 

Step 5 : Test

 

If the things going well, there are few things can be judged.

  1. the Stock is decreasedscreen-shot-2016-09-14-at-3-34-37-pm
  2. the order can be found at Class:Order
  3. the charged -> true and stripePaymentId != undefined at Class:Orderscreen-shot-2016-09-14-at-2-57-20-pm
  4. Stripe Dashboard has transaction records.screen-shot-2016-09-13-at-10-03-42-pmscreen-shot-2016-09-14-at-3-33-17-pm