package com.stripe.example.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.stripe.example.R;
import com.stripe.android.Stripe;
import com.stripe.android.TokenCallback;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import com.stripe.exception.*;
import com.stripe.example.dialog.ValidationErrorDialog;
import com.stripe.example.dialog.ValidationProgressDialog;
import com.stripe.example.PaymentForm;
import com.stripe.example.TokenList;


public class PaymentActivity extends FragmentActivity {

    /*
     * You need to set this to your stripe test publishable key.
     *
     * For more info, see https://stripe.com/docs/stripe.js
     *
     * E.g.
     *
     *   private static final String publishableKey = "pk_something123456789";
     *
     */
    public static final String PUBLISHABLE_KEY = "pk_qY2cZzSHrsrThxl07cHWpqh6Zr5LO";

    ValidationProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.payment_activity);
    }

    public void saveCreditCard(PaymentForm form) {

        Card card = new Card(
                form.getCardNumber(),
                form.getExpMonth(),
                form.getExpYear(),
                form.getCvc());

        boolean validation = card.validateCard();
        if (validation) {
            startProgress();
			new Stripe().createToken(
					card,
					PUBLISHABLE_KEY,
					new TokenCallback() {
					public void onSuccess(Token token) {
						getTokenList().addToList(token);
						finishProgress();
						}
					public void onError(Exception error) {
							handleError(error.getLocalizedMessage());
							finishProgress();						
						}
					});
        } else {
            handleError("You did not enter a valid card");
        }
    }

    private void startProgress() {
        String progressMessage = getResources().getString(R.string.progressMessage);
        this.progressDialog = new ValidationProgressDialog(this, progressMessage);
        this.progressDialog.show();
    }

    private void finishProgress() {
        this.progressDialog.dismiss();
    }

    private void handleError(String error) {
        new ValidationErrorDialog(this, error).show();
    }

    private TokenList getTokenList() {
        return (TokenList)(getSupportFragmentManager().findFragmentById(R.id.token_list));
    }
}

@@ -880,42 +880,28 @@
 Y = 
-%22pk_qY2cZzSHrsrThxl07cHWpqh6Zr5LO%22
+YOUR_PUBLISHABLE_KEY
 ;%0A%0A 
@@ -1444,19 +1444,28 @@
 ress();%0A
-%09%09%09
+            
 new Stri
@@ -1486,46 +1486,91 @@
 en(%0A
-%09%09%09%09%09card,%0A%09%09%09%09%09PUBLISHABLE_KEY,%0A%09%09%09%09%09
+                    card,%0A                    PUBLISHABLE_KEY,%0A                    
 new 
@@ -1587,21 +1587,36 @@
 ack() %7B%0A
-%09%09%09%09%09
+                    
 public v
@@ -1644,22 +1644,40 @@
 oken) %7B%0A
-%09%09%09%09%09%09
+                        
 getToken
@@ -1705,45 +1705,96 @@
 n);%0A
-%09%09%09%09%09%09finishProgress();%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09
+                        finishProgress();%0A                        %7D%0A                    
 publ
@@ -1828,23 +1828,44 @@
 rror) %7B%0A
-%09%09%09%09%09%09%09
+                            
 handleEr
@@ -1902,52 +1902,100 @@
 ));%0A
-%09%09%09%09%09%09%09finishProgress();%09%09%09%09%09%09%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09
+                            finishProgress();%0A                        %7D%0A                    
 %7D);%0A

Put back YOUR_PUBLISHABLE_KEY as placeholder