如何修复错误:错误:找不到符号import com.stripe.android.TokenCallback;

huangapple 未分类评论47阅读模式
英文:

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 = &quot;pk_test_qeSW2ZpZCFBZe0&quot;;
    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(
                &quot;4242424242424242&quot;, //card number
                12, //expMonth
                2030,//expYear
                &quot;123&quot;//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(&quot;Validating Credit Card&quot;);
            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,
                                    &quot;Stripe -&quot; + error.toString(),
                                    Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onSuccess(Token token) {
                            finishProgress();
                            charge(token);
                        }
                    });
        } else if (!card.validateNumber()) {
            Toast.makeText(MainActivity.this,
                    &quot;Stripe - The card number that you entered is invalid&quot;,
                    Toast.LENGTH_LONG).show();
        } else if (!card.validateExpiryDate()) {
            Toast.makeText(MainActivity.this,
                    &quot;Stripe - The expiration date that you entered is invalid&quot;,
                    Toast.LENGTH_LONG).show();
        } else if (!card.validateCVC()) {
            Toast.makeText(MainActivity.this,
                    &quot;Stripe - The CVC code that you entered is invalid&quot;,
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(MainActivity.this,
                    &quot;Stripe - The card details that you entered are invalid&quot;,
                    Toast.LENGTH_LONG).show();
        }
    }

    private void charge(Token cardToken){
        HashMap&lt;String, Object&gt; params = new HashMap&lt;String, Object&gt;();
        params.put(&quot;ItemName&quot;, &quot;test&quot;);
        params.put(&quot;cardToken&quot;, cardToken.getId());
        params.put(&quot;name&quot;,&quot;Dominic Wong&quot;);
        params.put(&quot;email&quot;,&quot;dominwong4@gmail.com&quot;);
        params.put(&quot;address&quot;,&quot;HIHI&quot;);
        params.put(&quot;zip&quot;,&quot;99999&quot;);
        params.put(&quot;city_state&quot;,&quot;CA&quot;);
        startProgress(&quot;Purchasing Item&quot;);
        ParseCloud.callFunctionInBackground(&quot;purchaseItem&quot;, params, new FunctionCallback&lt;Object&gt;() {
            public void done(Object response, ParseException e) {
                finishProgress();
                if (e == null) {
                    Log.d(&quot;Cloud Response&quot;, &quot;There were no exceptions! &quot; + response.toString());
                    Toast.makeText(getApplicationContext(),
                            &quot;Item Purchased Successfully &quot;,
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d(&quot;Cloud Response&quot;, &quot;Exception: &quot; + e);
                    Toast.makeText(getApplicationContext(),
                            e.getMessage().toString(),
                            Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void startProgress(String title){
        progress.setTitle(title);
        progress.setMessage(&quot;Please Wait&quot;);
        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 &#39;com.android.tools.build:gradle:3.6.0&#39;
    
    
            // 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 &quot;https://jitpack.io&quot; }
        }
    }
    
    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: &#39;com.android.application&#39;
    
    android {
        compileSdkVersion 29
        buildToolsVersion &quot;29.0.3&quot;
    
        defaultConfig {
            applicationId &quot;com.example.androidstripepayments&quot;
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName &quot;1.0&quot;
    
            testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
            }
        }
    
    }
    
    ext {
        parseVersion = &quot;1.23.1&quot;
    }
    
    dependencies {
        implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])
    
        implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
        implementation &#39;com.google.android.material:material:1.1.0&#39;
        implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
        implementation &#39;androidx.navigation:navigation-fragment:2.2.1&#39;
        implementation &#39;androidx.navigation:navigation-ui:2.2.1&#39;
        testImplementation &#39;junit:junit:4.12&#39;
        androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
        androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;
        // Stripe Package
        implementation &#39;com.stripe:stripe-android:+&#39;
        implementation &quot;com.github.parse-community.Parse-SDK-Android:coroutines:1.23.1&quot;
        implementation &#39;com.parse.bolts:bolts-android:1.4.0&#39;
        implementation &#39;com.squareup.okhttp3:logging-interceptor:3.8.1&#39;
    
        implementation &quot;com.github.parse-community.Parse-SDK-Android:parse:$parseVersion&quot;
    }
    
    allprojects {
        repositories {
            maven { url &quot;https://jitpack.io&quot; }
        }
    }

huangapple
  • 本文由 发表于 2020年4月7日 13:02:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61073131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定