【问题标题】:Stripe payment gateway in android安卓中的条纹支付网关
【发布时间】: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库?!!

标签: android stripe-payments


【解决方案1】:

Stripe 的 Android 绑定只允许您标记卡信息。创建令牌后,必须将其发送到外部服务器,您可以在其中在 API 请求中使用它。

您不能直接从应用程序中使用令牌,因为该应用程序绝不能访问您的密钥,因为攻击者可以轻松提取密钥,然后可以访问您的帐户。

回复。您的第二个问题,创建令牌时未通过银行验证卡(仍然有一些基本的完整性检查,例如检查位数,到期日在未来的事实等)。只有当您在服务器端 API 请求中使用令牌时,银行才会检查卡。

【讨论】:

  • 好的。我接到你了。但是昨天我试过了,付款成功了。虽然它是一个测试帐户。
  • 我也更新了我的问题。请在那里查看解决方案。如果我做错了什么,请告诉我。
  • 技术上可以直接从应用程序发出 API 请求。但几乎可以肯定的是,如果您这样做,有人会检索您的 API 密钥并访问您的帐户。您绝对不应该这样做,而是使用外部服务器。
猜你喜欢
  • 2016-08-02
  • 2021-10-08
  • 2020-10-14
  • 2022-08-19
  • 2014-03-07
  • 1970-01-01
  • 2020-04-12
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多