英文:
How do I fix error error: cannot find symbol import com.stripe.android.TokenCallback;
问题
package com.example.androidstripepayments;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.stripe.android.Stripe;
import com.stripe.android.TokenCallback;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import java.text.ParseException;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
public static final String PUBLISHABLE_KEY = "pk_test_qeSW2ZpZCFBZe0";
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) {
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");
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 startProgress(String title){
progress.setTitle(title);
progress.setMessage("Please Wait");
progress.show();
}
private void finishProgress(){
progress.dismiss();
}
}
// build.gradle(Project:)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// build.gradle(Module:)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.androidstripepayments"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
ext {
parseVersion = "1.23.1"
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.stripe:stripe-android:+'
implementation "com.github.parse-community.Parse-SDK-Android:coroutines:1.23.1"
implementation 'com.parse.bolts:bolts-android:1.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
implementation "com.github.parse-community.Parse-SDK-Android:parse:$parseVersion"
}
英文:
package com.example.androidstripepayments;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.stripe.android.Stripe;
import com.stripe.android.TokenCallback;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
import java.text.ParseException;
import java.util.HashMap;
//import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static final String PUBLISHABLE_KEY = "pk_test_qeSW2ZpZCFBZe0";
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) {
buy();
}
});
}
private void buy(){
boolean validation = card.validateCard();
if(validation){
startProgress("Validating Credit Card");
new Stripe(this).createToken(
card,
PUBLISHABLE_KEY,
I am having trouble with this line right here with TokenCallback()
And I dont know how to replace this method with another method to make it work
Every time i compile the code I always get the same error TokenCallback is always
highlighted in red. It also not allowing me to use the library This library does not seem to work anymore for what ever reason.
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");
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 startProgress(String title){
progress.setTitle(title);
progress.setMessage("Please Wait");
progress.show();
}
private void finishProgress(){
progress.dismiss();
}
}
this is the build.gradle(Project:)
Im pretty sure I added all the right dependencies
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is my build.gradle(Module)
I made sure to add all the right stuff in here.
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.androidstripepayments"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
ext {
parseVersion = "1.23.1"
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Stripe Package
implementation 'com.stripe:stripe-android:+'
implementation "com.github.parse-community.Parse-SDK-Android:coroutines:1.23.1"
implementation 'com.parse.bolts:bolts-android:1.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
implementation "com.github.parse-community.Parse-SDK-Android:parse:$parseVersion"
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论