Java 将stripe与android应用程序集成
我正在尝试从我的android应用程序支持条带支付,但它不会运行。java不兼容吗?我的代码和错误如下:Java 将stripe与android应用程序集成,java,android,stripe-payments,Java,Android,Stripe Payments,我正在尝试从我的android应用程序支持条带支付,但它不会运行。java不兼容吗?我的代码和错误如下: package io.noq.kiosk.android; import java.util.HashMap; import java.util.Map; import com.stripe.Stripe; import com.stripe.exception.APIConnectionException; import com.stripe.exception.Authenticati
package io.noq.kiosk.android;
import java.util.HashMap;
import java.util.Map;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import io.noq.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class OnSwipeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Stripe.apiKey = "the secret (intentionally omitting)";
Map<String, Object> chargeMap = new HashMap<String, Object>();
chargeMap.put("amount", 100);
chargeMap.put("currency", "usd");
Map<String, Object> cardMap = new HashMap<String, Object>();
cardMap.put("number", "4242424242424242");
cardMap.put("exp_month", 12);
cardMap.put("exp_year", 2013);
chargeMap.put("card", cardMap);
try {
Charge charge = Charge.create(chargeMap);
System.out.println(charge);
Log.i("Status is: " + "exiting try onCreate-PayActivity", "let's see what goes out....");
System.out.println("Status is: "+ "exiting try onCreate-PayActivity");
} catch (CardException e) {
// Since it's a decline, CardException will be caught
System.out.println("CardException is: " + e.getCode());
System.out.println("CardException is: " + e.getParam());
Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCode());
} catch (InvalidRequestException e) {
// Invalid parameters were supplied to Stripe's API
System.out.println("InvalidRequestException: " + e.getMessage());
Log.i(OnSwipeActivity.class.getName(),"e.getCode() ...." + e.getCause());
} catch (AuthenticationException e) {
// Authentication with Stripe's API failed (maybe you changed API keys recently)
System.out.println("AuthenticationException: " + e.getMessage());
Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause());
} catch (APIConnectionException e) {
// Network communication with Stripe failed
System.out.println("APIConnectionException: " + e.getMessage());
Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause());
} catch (StripeException e) {
// Display a very generic error to the user, and maybe send yourself an email
System.out.println("StripeException: " + e.getMessage());
Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause());
} catch (Exception e) {
// Something else happened, completely unrelated to Stripe
System.out.println("Exception: " + e.getMessage());
Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause());
}
}
}
包io.noq.kiosk.android;
导入java.util.HashMap;
导入java.util.Map;
导入com.stripe.stripe;
导入com.stripe.exception.APIConnectionException;
导入com.stripe.exception.AuthenticationException;
导入com.stripe.exception.cardeexception;
导入com.stripe.exception.InvalidRequestException;
导入com.stripe.exception.StripeException;
导入com.stripe.model.Charge;
导入io.noq.android.R;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
公共类活动扩展了活动{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Stripe.apiKey=“秘密(故意省略)”;
Map chargeMap=newhashmap();
chargeMap.put(“金额”,100);
chargeMap.put(“货币”、“美元”);
Map cardMap=newhashmap();
卡片地图。放置(“编号”,“4242”);
cardMap.put(“exp_month”,12);
cardMap.put(“预期年份”,2013年);
chargeMap.put(“卡”,cardMap);
试一试{
Charge=Charge.create(chargeMap);
系统输出打印项次(费用);
Log.i(“状态为:“+”正在退出TryOnCreatePayActivity”,“让我们看看会发生什么…”);
System.out.println(“状态为:“+”正在退出try-onCreate PayActivity”);
}捕获(卡例外){
//由于价格下跌,CardException将被捕获
System.out.println(“CardException是:+e.getCode());
System.out.println(“CardException为:+e.getParam());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCode());
}捕获(无效请求异常e){
//为条带的API提供了无效参数
System.out.println(“InvalidRequestException:+e.getMessage());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCause());
}捕获(身份验证异常e){
//使用Stripe的API进行身份验证失败(可能您最近更改了API密钥)
System.out.println(“AuthenticationException:+e.getMessage());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCause());
}捕获(APIConnectionException e){
//与条带的网络通信失败
System.out.println(“APIConnectionException:+e.getMessage());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCause());
}捕获(条带异常e){
//向用户显示一个非常常见的错误,并可能向自己发送电子邮件
System.out.println(“StripeException:+e.getMessage());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCause());
}捕获(例外e){
//发生了另一件事,与Stripe完全无关
System.out.println(“异常:+e.getMessage());
Log.i(onsweepActivity.class.getName(),“e.getCode()..”+e.getCause());
}
}
}
以下是错误:
12-25 05:34:22.625:W/dalvikvm(801):threadid=1:线程退出时出现未捕获异常(组=0x40a70930)
12-25 05:34:22.647:E/AndroidRuntime(801):致命异常:主
12-25 05:34:22.647:E/AndroidRuntime(801):java.lang.NoClassDefFoundError com.stripe.stripe
12-25 05:34:22.647:E/AndroidRuntime(801):在io.noq.kiosk.android.onsweepActivity.onCreate(onsweepActivity.java:26)
12-25 05:34:22.647:E/AndroidRuntime(801):在android.app.Activity.performCreate(Activity.java:5104)上
12-25 05:34:22.647:E/AndroidRuntime(801):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-25 05:34:22.647:E/AndroidRuntime(801):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-25 05:34:22.647:E/AndroidRuntime(801):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-25 05:34:22.647:E/AndroidRuntime(801):在android.app.ActivityThread.access$600(ActivityThread.java:141)
12-25 05:34:22.647:E/AndroidRuntime(801):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-25 05:34:22.647:E/AndroidRuntime(801):在android.os.Handler.dispatchMessage(Handler.java:99)上
12-25 05:34:22.647:E/AndroidRuntime(801):在android.os.Looper.loop(Looper.java:137)
12-25 05:34:22.647:E/AndroidRuntime(801):位于android.app.ActivityThread.main(ActivityThread.java:5039)
12-25 05:34:22.647:E/AndroidRuntime(801):位于java.lang.reflect.Method.Invokenactive(本机方法)
12-25 05:34:22.647:E/AndroidRuntime(801):在java.lang.reflect.Method.invoke(Method.java:511)
12-25 05:34:22.647:E/AndroidRuntime(801):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-25 05:34:22.647:E/AndroidRuntime(801):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-25 05:34:22.647:E/AndroidRuntime(801):在dalvik.system.NativeStart.main(本机方法)
您可以在android项目上使用Strip java库。我已经使用来自的测试类实现了一个Poc 您必须将库作为图片导入: 您可以使用示例代码进行测试:
public class MainActivity extends Activity {
public static final String TAG = "TestStrip";
static Map<String, Object> defaultCardParams = new HashMap<String, Object>();
static Map<String, Object> defaultChargeParams = new HashMap<String, Object>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stripe.apiKey = "tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I";
init();
Map<String, Object> chargeMap = new HashMap<String, Object>();
chargeMap.put("amount", 100);
chargeMap.put("currency", "usd");
Map<String, Object> cardMap = new HashMap<String, Object>();
cardMap.put("number", "4242424242424242");
cardMap.put("exp_month", 12);
cardMap.put("exp_year", 2013);
chargeMap.put("card", cardMap);
try {
Charge createdCharge = Charge.create(defaultChargeParams);
Fee fee = createdCharge.getFeeDetails().get(0);
Preconditions.checkState("stripe_fee".equals(fee.getType()), "Stripe fee error.");
Preconditions.checkState(createdCharge.getFee() == fee.getAmount(), "Error ");
} catch (StripeException e) {
// Display a very generic error to the user, and maybe send yourself an email
Log.e(TAG, "e.getCode() ...." + e.getCause());
}
}
private void init() {
defaultCardParams.put("number", "4242424242424242");
defaultCardParams.put("exp_month", 12);
defaultCardParams.put("exp_year", 2015);
defaultCardParams.put("cvc", "123");
defaultCardParams.put("name", "Java Bindings Cardholder");
defaultCardParams.put("address_line1", "140 2nd Street");
defaultCardParams.put("address_line2", "4th Floor");
defaultCardParams.put("address_city", "San Francisco");
defaultCardParams.put("address_zip", "94105");
defaultCardParams.put("address_state", "CA");
defaultCardParams.put("address_country", "USA");
defaultChargeParams.put("amount", 100);
defaultChargeParams.put("currency", "usd");
defaultChargeParams.put("card", defaultCardParams);
}
}
公共类MainActivity扩展活动{
公共静态最终字符串TAG=“TestStrip”;
静态映射defaultCardParams=new HashMap();
静态映射defaultChargeParams=new HashMap();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stripe.apiKey=“tGN0bIwXnHdwOa85VAB
In Android Studio
add in gradle
compile 'com.stripe:stripe-android:+'
==============================================
Card card = new Card(cardnumber,exp.month,exp.year,cvv);
boolean validation = card.validateCard();
if (validation) {
startProgress();
new Stripe().createToken(
card,
PUBLISHABLE_STRIPE_KEY,
new TokenCallback() {
public void onSuccess(Token token) {
// you can get transcation token
}
public void onError(Exception error) {
handleError(error.getLocalizedMessage());
finishProgress();
}
});
} else if (!card.validateNumber()) {
handleError("The card number that you entered is invalid");
} else if (!card.validateExpiryDate()) {
handleError("The expiration date that you entered is invalid");
} else if (!card.validateCVC()) {
handleError("The CVC code that you entered is invalid");
} else {
handleError("The card details that you entered are invalid");
}