【发布时间】:2016-05-24 04:45:24
【问题描述】:
我正在将 Stripe 集成到我的 android 应用程序中。 我发现了两个问题,我被卡住了。
第一个(也是主要的)问题是 - 我从卡信息中获得了令牌密钥。但是当我尝试进行第一次充电时,它在以下行中显示错误。
Stripe.apiKey = "sk_test_0000000000000";
我也用谷歌搜索过,但找不到任何解决方案。
Creating Stripe Customer - cannot resolve symbol apiKey
我什至也见过这种问题。但我无法理解它的解决方案。
以下是我的收费代码:
final String publishableApiKey = BuildConfig.DEBUG ?
"pk_test_000000000000000000" :
//"sk_test_00000000000000000000000" :
getString(R.string.com_stripe_publishable_key);
final TextView cardNumberField = (TextView) findViewById(R.id.cardNumber);
final TextView monthField = (TextView) findViewById(R.id.month);
final TextView yearField = (TextView) findViewById(R.id.year);
TextView cvcField = (TextView) findViewById(R.id.cvc);
Card card = new Card(cardNumberField.getText().toString(),
Integer.valueOf(monthField.getText().toString()),
Integer.valueOf(yearField.getText().toString()),
cvcField.getText().toString());
Stripe stripe = new Stripe();
stripe.createToken(card, publishableApiKey, new TokenCallback() {
public void onSuccess(Token token) {
// TODO: Send Token information to your backend to initiate a charge
Toast.makeText(
getApplicationContext(),
"Charge Token created: " + token.getId(),
Toast.LENGTH_LONG).show();
/*make a charge starts*/
// Set your secret key: remember to change this to your live secret key in production
// Create the charge on Stripe's servers - this will charge the user's card
Stripe.apiKey = "sk_test_0000000000000000000000";
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 100); // amount in cents, again
chargeParams.put("currency", "usd");
chargeParams.put("source", token.getId());
chargeParams.put("description", "Example charge");
Charge charge = Charge.create(chargeParams);
System.out.println("Charge Log :" + charge);
} catch (CardException e) {
// The card has been declined
} catch (APIException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
}
/*charge ends*/
}
我已经从不同的示例中尝试了这些代码。我也关注了 Stripe 文档。
但是我收到了这个错误:
com.stripe.exception.AuthenticationException:未提供 API 密钥。 (提示:使用 'Stripe.apiKey = ' 设置您的 API 密钥。
第二个问题是 - 关于验证。 如果我输入了我自己的卡详细信息,并且我写了错误的 cvv 号码。它仍然生成令牌密钥。
我已经使用 Stripe 的官方文档实现了字段验证。我不知道如何用实时数据来验证它。
第一个问题的解决方案:
com.stripe.Stripe.apiKey = "sk_test_xxxxxxxxxxxxxxxxxxx";
try {
final Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 500); // amount in cents, again
chargeParams.put("currency", "usd");
chargeParams.put("source", token.getId());
chargeParams.put("description", "Example charge");
new Thread(new Runnable() {
@Override
public void run() {
Charge charge = null;
try {
charge = Charge.create(chargeParams);
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (CardException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
System.out.println("Charge Log :" + charge);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
【问题讨论】:
-
@halfer - 感谢您的编辑。
-
嘿嘿 .Charge Charge = null;如何导入 Charge 。没有用于 Charge 的库
-
导入Charge模型时发现错误,找不到,如何导入Charge。没有charge库?!!