Created
February 14, 2019 14:35
-
-
Save amirul12/86ea11ef7ddb457f7adff7a165af63a8 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
| implementation 'com.stripe:stripe-android:7.0.0' | |
| public class MainActivity extends AppCompatActivity { | |
| public static final String PUBLISHABLE_KEY = "pk_test_RDj9RzgYggpcUuVoLoqBKWWl"; | |
| private Card card; | |
| private ProgressDialog progress; | |
| private Button purchase; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| card = new Card( | |
| "4242424242424242", //card number | |
| 12, //expMonth | |
| 2030,//expYear | |
| "123"//cvc | |
| ); | |
| progress = new ProgressDialog(this); | |
| purchase = (Button) findViewById(R.id.purchase); | |
| purchase.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Log.d("click", "clicked"); | |
| buy(); | |
| } | |
| }); | |
| } | |
| private void buy() { | |
| boolean validation = card.validateCard(); | |
| if (validation) { | |
| startProgress("Validating Credit Card"); | |
| new Stripe(this).createToken( | |
| card, | |
| PUBLISHABLE_KEY, | |
| new TokenCallback() { | |
| @Override | |
| public void onError(Exception error) { | |
| Toast.makeText(MainActivity.this, | |
| "Stripe -" + error.toString(), | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| @Override | |
| public void onSuccess(Token token) { | |
| finishProgress(); | |
| charge(token); | |
| } | |
| }); | |
| } else if (!card.validateNumber()) { | |
| Toast.makeText(MainActivity.this, | |
| "Stripe - The card number that you entered is invalid", | |
| Toast.LENGTH_LONG).show(); | |
| } else if (!card.validateExpiryDate()) { | |
| Toast.makeText(MainActivity.this, | |
| "Stripe - The expiration date that you entered is invalid", | |
| Toast.LENGTH_LONG).show(); | |
| } else if (!card.validateCVC()) { | |
| Toast.makeText(MainActivity.this, | |
| "Stripe - The CVC code that you entered is invalid", | |
| Toast.LENGTH_LONG).show(); | |
| } else { | |
| Toast.makeText(MainActivity.this, | |
| "Stripe - The card details that you entered are invalid", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| private void charge(Token cardToken){ | |
| HashMap<String, Object> params = new HashMap<String, Object>(); | |
| params.put("ItemName", "test"); | |
| params.put("cardToken", cardToken.getId()); | |
| params.put("name","Dominic Wong"); | |
| params.put("email","dominwong4@gmail.com"); | |
| params.put("address","HIHI"); | |
| params.put("zip","99999"); | |
| params.put("city_state","CA"); | |
| startProgress("Purchasing Item"); | |
| Log.d("token ", cardToken.getId()); | |
| /* 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(); | |
| } | |
| } | |
| });*/ | |
| } | |
| private void finishProgress(){ | |
| progress.dismiss(); | |
| } | |
| private void startProgress(String title) { | |
| progress.setTitle(title); | |
| progress.setMessage("Please Wait"); | |
| progress.show(); | |
| } | |
| } | |
| xml layout added: | |
| ---------------- | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:app="http://schemas.android.com/apk/res-auto" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| tools:context=".MainActivity"> | |
| <LinearLayout | |
| android:id="@+id/activity_main" | |
| android:layout_width="0dp" | |
| android:layout_height="0dp" | |
| android:orientation="vertical" | |
| android:paddingBottom="@dimen/activity_vertical_margin" | |
| android:paddingLeft="@dimen/activity_horizontal_margin" | |
| android:paddingRight="@dimen/activity_horizontal_margin" | |
| android:paddingTop="@dimen/activity_vertical_margin" | |
| android:weightSum="1" | |
| tools:layout_constraintTop_creator="1" | |
| tools:layout_constraintRight_creator="1" | |
| tools:layout_constraintBottom_creator="1" | |
| android:layout_marginStart="9dp" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| android:layout_marginEnd="9dp" | |
| app:layout_constraintRight_toRightOf="parent" | |
| android:layout_marginTop="8dp" | |
| tools:layout_constraintLeft_creator="1" | |
| android:layout_marginBottom="8dp" | |
| app:layout_constraintLeft_toLeftOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" | |
| android:layout_marginLeft="9dp" | |
| android:layout_marginRight="9dp"> | |
| <Button | |
| android:id="@+id/purchase" | |
| android:layout_width="365dp" | |
| android:layout_height="60dp" | |
| android:text="Purchase Button" | |
| android:visibility="visible" | |
| android:layout_marginTop="5dp" | |
| android:layout_marginLeft="5dp" | |
| tools:layout_editor_absoluteX="11dp" | |
| tools:layout_editor_absoluteY="5dp" /> | |
| </LinearLayout> | |
| </android.support.constraint.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment