Java Android Studio 中的条纹集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20910451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:15:58  来源:igfitidea点击:

Stripe Integration in Android Studio

javaandroidstripe-payments

提问by localhost

I am working to integrate the Stripe payment gateway in an Android app that I am developing. I have followed the steps listed on https://stripe.com/docs/mobile/android.

我正在努力将 Stripe 支付网关集成到我正在开发的 Android 应用程序中。我已按照https://stripe.com/docs/mobile/android 上列出的步骤进行操作。

When I try to create a new card I get errors.

当我尝试创建新卡时出现错误。

I run this in my activity

我在我的活动中运行这个

saveButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
  Card card = new Card(
   // Get values from the form
   cardNumber.getText().toString(),
   getInteger(monthSpinner),
   getInteger(yearSpinner),
   cvc.getText().toString()
  );
 }
});

Which uses the Card model.

其中使用 Card 模型。

Card model

卡片型号

package com.stripe.android.model;

import com.stripe.android.util.DateUtils;
import com.stripe.android.util.TextUtils;

public class Card extends com.stripe.model.StripeObject {
 String number;
 String cvc;
 Integer expMonth;
 Integer expYear;
 String name;
 String addressLine1;
 String addressLine2;
 String addressCity;
 String addressState;
 String addressZip;
 String addressCountry;
 String last4;
 String type;
 String fingerprint;
 String country;

 public String getNumber() {
    return number;
 }

 public void setNumber(String number) {
    this.number = number;
 }

 public String getCVC() {
    return cvc;
 }

 public void setCVC(String cvc) {
    this.cvc = cvc;
 }

 public Integer getExpMonth() {
    return expMonth;
 }

 public void setExpMonth(Integer expMonth) {
    this.expMonth = expMonth;
 }

 public Integer getExpYear() {
    return expYear;
 }

 public void setExpYear(Integer expYear) {
    this.expYear = expYear;
 }

 public String getName() {
    return name;
 }

 public void setName(String name) {
    this.name = name;
 }

 public String getAddressLine1() {
    return addressLine1;
 }

 public void setAddressLine1(String addressLine1) {
    this.addressLine1 = addressLine1;
 }

 public String getAddressLine2() {
    return addressLine2;
 }

 public void setAddressLine2(String addressLine2) {
    this.addressLine2 = addressLine2;
 }

 public String getAddressCity() {
    return addressCity;
 }

 public void setAddressCity(String addressCity) {
    this.addressCity = addressCity;
 }

 public String getAddressZip() {
    return addressZip;
 }

 public void setAddressZip(String addressZip) {
    this.addressZip = addressZip;
 }

 public String getAddressState() {
    return addressState;
 }

 public void setAddressState(String addressState) {
    this.addressState = addressState;
 }

 public String getAddressCountry() {
    return addressCountry;
 }

 public void setAddressCountry(String addressCountry) {
    this.addressCountry = addressCountry;
 }

 public String getLast4() {
    if (!TextUtils.isBlank(last4)) {
        return last4;
    }
    if (number != null && number.length() > 4) {
        return number.substring(number.length() - 4, number.length());
    }
    return null;
 }

 public String getType() {
    if (TextUtils.isBlank(type) && !TextUtils.isBlank(number)) {
        if (TextUtils.hasAnyPrefix(number, "34", "37")) {
            return "American Express";
        } else if (TextUtils.hasAnyPrefix(number, "60", "62", "64", "65")) {
            return "Discover";
        } else if (TextUtils.hasAnyPrefix(number, "35")) {
            return "JCB";
        } else if (TextUtils.hasAnyPrefix(number, "30", "36", "38", "39")) {
            return "Diners Club";
        } else if (TextUtils.hasAnyPrefix(number, "4")) {
            return "Visa";
        } else if (TextUtils.hasAnyPrefix(number, "5")) {
            return "MasterCard";
        } else {
            return "Unknown";
        }
    }
    return type;
 }

 public String getFingerprint() {
    return fingerprint;
 }

 public String getCountry() {
    return country;
 }

 public Card(String number, Integer expMonth, Integer expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry, String last4, String type, String fingerprint, String country) {
    this.number = TextUtils.nullIfBlank(normalizeCardNumber(number));
    this.expMonth = expMonth;
    this.expYear = expYear;
    this.cvc = TextUtils.nullIfBlank(cvc);
    this.name = TextUtils.nullIfBlank(name);
    this.addressLine1 = TextUtils.nullIfBlank(addressLine1);
    this.addressLine2 = TextUtils.nullIfBlank(addressLine2);
    this.addressCity = TextUtils.nullIfBlank(addressCity);
    this.addressState = TextUtils.nullIfBlank(addressState);
    this.addressZip = TextUtils.nullIfBlank(addressZip);
    this.addressCountry = TextUtils.nullIfBlank(addressCountry);
    this.last4 = TextUtils.nullIfBlank(last4);
    this.type = TextUtils.nullIfBlank(type);
    this.fingerprint = TextUtils.nullIfBlank(fingerprint);
    this.country = TextUtils.nullIfBlank(country);
 }

 public Card(String number, Integer expMonth, Integer expYear, String cvc, String name, String addressLine1, String addressLine2, String addressCity, String addressState, String addressZip, String addressCountry) {
    this(number, expMonth, expYear, cvc, name, addressLine1, addressLine2, addressCity, addressState, addressZip, addressCountry, null, null, null, null);
 }

 public Card(String number, Integer expMonth, Integer expYear, String cvc) {
    this(number, expMonth, expYear, cvc, null, null, null, null, null, null, null, null, null, null, null);
    this.type = getType();
 }

 public boolean validateCard() {
    if (cvc == null) {
        return validateNumber() && validateExpiryDate();
    } else {
        return validateNumber() && validateExpiryDate() && validateCVC();
    }
 }

 public boolean validateNumber() {
    if (TextUtils.isBlank(number)) {
        return false;
    }

    String rawNumber = number.trim().replaceAll("\s+|-", "");
    if (TextUtils.isBlank(rawNumber)
            || !TextUtils.isWholePositiveNumber(rawNumber)
            || !isValidLuhnNumber(rawNumber)) {
        return false;
    }

    if (!"American Express".equals(type) && rawNumber.length() != 16) {
        return false;
    }

    if ("American Express".equals(type) && rawNumber.length() != 15) {
        return false;
    }

    return true;
 }

 public boolean validateExpiryDate() {
    if (!validateExpMonth()) {
        return false;
    }
    if (!validateExpYear()) {
        return false;
    }
    return !DateUtils.hasMonthPassed(expYear, expMonth);
 }

 public boolean validateExpMonth() {
    if (expMonth == null) {
        return false;
    }
    return (expMonth >= 1 && expMonth <= 12);
 }

 public boolean validateExpYear() {
    if (expYear == null) {
        return false;
    }
    return !DateUtils.hasYearPassed(expYear);
 }

 public boolean validateCVC() {
    if (TextUtils.isBlank(cvc)) {
        return false;
    }
    String cvcValue = cvc.trim();

    boolean validLength = ((type == null && cvcValue.length() >= 3 && cvcValue.length() <= 4) ||
            ("American Express".equals(type) && cvcValue.length() == 4) ||
            (!"American Express".equals(type) && cvcValue.length() == 3));


    if (!TextUtils.isWholePositiveNumber(cvcValue) || !validLength) {
        return false;
    }
    return true;
 }

 private boolean isValidLuhnNumber(String number) {
    boolean isOdd = true;
    int sum = 0;

    for (int index = number.length() - 1; index >= 0; index--) {
        char c = number.charAt(index);
        if (!Character.isDigit(c)) {
            return false;
        }
        int digitInteger = Integer.parseInt("" + c);
        isOdd = !isOdd;

        if (isOdd) {
            digitInteger *= 2;
        }

        if (digitInteger > 9) {
            digitInteger -= 9;
        }

        sum += digitInteger;
    }

    return sum % 10 == 0;
 }

 private String normalizeCardNumber(String number) {
  if (number == null) {
    return null;
  }
  return number.trim().replaceAll("\s+|-", "");
 }
}

This is the error I am getting.

这是我得到的错误。

enter image description here

在此处输入图片说明

This is new to me. What can I do to resolve this?

这对我来说是新的。我能做些什么来解决这个问题?

采纳答案by Kennedy Nyaga

Since this is a top hit on Google search for 'How to integrate Stripe to Android Studio' and since Android studio removed the import module this is how I solved the import.

由于这是 Google 搜索“如何将 Stripe 集成到 Android Studio”的热门搜索,并且由于 Android Studio 删除了导入模块,这就是我解决导入的方式。

  • Right click on the project and select > New > Module
  • In your directories copy contents under Stripe > Stripe folder to the module folder (You should see a newly created folder. Delete the contents of this new folder and paste the contents of Stripe > Stripe)
  • Back to Android Studio navigate to build.gradle under src add compile project(":stripe") under dependencies.
  • Refresh your gradle.
  • 右键单击项目并选择 > 新建 > 模块
  • 在您的目录中,将 Stripe > Stripe 文件夹下的内容复制到模块文件夹(您应该会看到一个新创建的文件夹。删除这个新文件夹的内容并粘贴 Stripe > Stripe 的内容)
  • 返回 Android Studio 导航到 src 下的 build.gradle 添加 compile project(":stripe") 下的依赖项。
  • 刷新你的gradle。

EDIT 1Since posting this answer some changes have happened. If you would wish to add stripe into your project do so via Maven. Just add this line to your app's build.gradle inside the dependencies section:

编辑 1自发布此答案以来,发生了一些变化。如果您希望将条带添加到您的项目中,请通过 Maven 进行。只需将此行添加到您的应用程序的 build.gradle 中的依赖项部分:

compile 'com.stripe:stripe-android:2.0.2'

EDIT 2It's now implementation and not compile.

编辑 2它现在是实现而不是编译。

implementation 'com.stripe:stripe-android:6.1.2'

You can get more details here : https://stripe.com/docs/mobile/android

您可以在此处获取更多详细信息:https: //stripe.com/docs/mobile/android

回答by user3749398

Okay, so I believe what's happening is that you don't have access to the proper Card() constructor because you haven't set things up properly.

好的,所以我相信正在发生的事情是您无法访问正确的 Card() 构造函数,因为您没有正确设置。

  1. Go to the github page and download the link [for the library]https://github.com/stripe/stripe-android. Unpack that folder and keep it handy.

  2. Now, go into android studio and hit 'import module'. Navigate into that stripe-android directory that you just unzipped, and hit okay. Make sure you only have 'stripe' checked when importing, and not 'example' (only 'example' will be checked by default: fix this.)

  3. Copy the jarfile stripe-java-1.12.0.jar to the directory :libs in your project (where you'd have other libraries). That jarfile should show up under the new 'stripe' directory in android studio.

  4. Go into your src directory and find your app's build.gradle. You're going to want to add, under dependencies:

    compile project(":stripe")

  1. 转到 github 页面并下载链接 [for the library] https://github.com/stripe/stripe-android。打开那个文件夹并放在手边。

  2. 现在,进入 android studio 并点击“导入模块”。导航到您刚刚解压缩的那个 stripe-android 目录,然后点击 OK。确保在导入时只选中了“stripe”,而不是“example”(默认情况下只会选中“example”:修复此问题。)

  3. 将 jarfile stripe-java-1.12.0.jar 复制到项目中的目录 :libs (您将在其中拥有其他库)。该 jarfile 应显示在 android studio 中新的“stripe”目录下。

  4. 进入您的 src 目录并找到您的应用程序的 build.gradle。您将要在依赖项下添加:

    编译项目(“:条纹”)

You may run into an error at some point saying that you need a newer version of build tools to build the project. If that's so, just start rummaging through the gradle files and changing numbers until it builds. That's what I do, at least.

您可能会在某个时候遇到错误,提示您需要更新版本的构建工具来构建项目。如果是这样,只需开始翻阅 gradle 文件并更改数字,直到它构建。至少我就是这样做的。

Hope this helps!

希望这可以帮助!

(p.s: remember to include com.stripe.android.* and not com.stripe.*!)

(ps:记得包含 com.stripe.android.* 而不是 com.stripe.*!)

Edit: Just ran into a new problem, and it turns out you should skip step 3. It'll cause dex to freak out that the same class is being defined in the same jarfile twice. So don't do it.

编辑:刚遇到一个新问题,结果你应该跳过第 3 步。它会导致 dex 惊慌失措,在同一个 jarfile 中定义了同一个类两次。所以不要这样做。

回答by Kyle Venn

If you're not using Gradle then below is how I got it to work:

如果你没有使用 Gradle,那么下面是我如何让它工作的:

  1. Download the zip from the stripe GitHub(stripe-android-master)
  2. Import JUST the stripe folder as a module into your project. You shouldn't have to do anything fancy here.
  3. It added to my project as "main". Go into Project Structure -> modules and add "main" as a module dependency to your working module
  4. Click on the "main" (stripe) module and click the "Export" checkbox on the lib so that your working module has access to it
  5. ????
  6. Profit
  1. 从 Stripe GitHub(stripe-android-master)下载 zip
  2. 仅将条带文件夹作为模块导入到您的项目中。你不应该在这里做任何花哨的事情。
  3. 它作为“主要”添加到我的项目中。进入 Project Structure -> modules 并将“main”作为模块依赖项添加到您的工作模块
  4. 单击“主”(条纹)模块,然后单击库上的“导出”复选框,以便您的工作模块可以访问它
  5. ???
  6. 利润

回答by Anne Gunn

I just had exactly the same problem as the OP. I was importing some variant of Stripe code but did not have the multi-argument constructor or any of the specific methods I was looking for, so I clearly was not importing what I wanted/needed.

我只是遇到了与 OP 完全相同的问题。我正在导入 Stripe 代码的一些变体,但没有多参数构造函数或我正在寻找的任何特定方法,所以我显然没有导入我想要/需要的东西。

I tried many of the Import Module or Add Library incantations found here or elsewhere. Finally, about to give up, I tried that most desperate of all measures: RTFM. Or, in this case the README.md that came with the project I downloaded.

我尝试了在这里或其他地方找到的许多导入模块或添加库的咒语。最后,即将放弃,我尝试了所有措施中最绝望的一种:RTFM。或者,在这种情况下,我下载的项目附带的 README.md。

There, for Android Studio users, was the trivial solution that actually worked for me:

对于 Android Studio 用户来说,这是一个对我有用的微不足道的解决方案:

No need to clone the repository or download any files -- just add this line to your app's `build.gradle` inside the `dependencies` section:

    compile 'com.stripe:stripe-android:+'

It worked like a charm.

它就像一个魅力。

Ironically, you don't have to download or clone to USE the library but so far the only way I know to get the README.md is to download the library files from here: https://stripe.com/docs/mobile/android

具有讽刺意味的是,您不必下载或克隆即可使用该库,但到目前为止,我知道获取 README.md 的唯一方法是从此处下载库文件:https://stripe.com/docs/mobile/ 安卓

Caveat: I wrote the above as soon as Android Studio started importing the right library and my IDE compilation errors went away. But as soon as I tried to actually build and run my code, I ran head on into the multiple dex horrorfor, I believe, pulling in multiple copies of the gson library that Stripe depends on and my code already uses. Sigh. I DID fix the problem by removing my local copy of the gson jar and, I presume, depending on the one located with the stripe package. Just deleting all the bin/intermediate/generated folders wasn't good enough. I'm not real happy with this solution but may live with it for now.

警告:当 Android Studio 开始导入正确的库并且我的 IDE 编译错误消失时,我就写了上面的内容。但是,一旦我尝试实际构建和运行我的代码,我就遇到了多个 dex 恐怖事件,我相信,因为引入了 Stripe 所依赖且我的代码已经使用的 gson 库的多个副本。叹。我确实通过删除我的 gson jar 的本地副本解决了这个问题,并且我认为这取决于位于 stripe 包中的那个。仅仅删除所有 bin/intermediate/generated 文件夹还不够好。我对这个解决方案并不满意,但现在可能会接受它。

回答by Kai Wang

well, to use Stripe, you don't need to download anything from github or Stripe.com. Here is how I do it. Since I can't post image (with only 1 reputation), its destribed as below:
1. Right-click on your project
2. click Open Module Settings
3. Click dependencies
4. click add
5. click choose library dependencies
6. input "stripe"
7. click search
8. click com.stripe:stripe.android 1.0.0
9. click OK.

好吧,要使用 Stripe,你不需要从 github 或 Stripe.com 下载任何东西。这是我如何做到的。由于我无法发布图像(只有 1 个声望),其描述如下:
1. 右键单击​​您的项目
2. 单击打开模块设置
3. 单击依赖项
4. 单击添加
5. 单击选择库依赖项
6. 输入“stripe”
7. 单击搜索
8. 单击 com.stripe:stripe.android 1.0.0
9. 单击确定。

回答by Liger

Card card = new Card(cardNumber.getText().toString(),getInteger(this.monthSpinner),getInteger(this.yearSpinner),cvc.getText().toString());

use this, the actual and formal parameter are not matching.

使用这个,实参和形参不匹配。

Thanks

谢谢

回答by Abhinav Pawar

For all those who could not find a fix with above answers.Here's what I did when I faced the same situation.

对于所有无法通过上述答案找到解决方案的人。这是我遇到相同情况时所做的。

  • Make sure you have gradle dependency as: compile 'com.stripe:stripe-android:+'
  • In the class where you are using Cardmodel-delete all the stripe import statements and then click on the Cardand make sure to import from -
  • 确保您具有 gradle 依赖项:compile 'com.stripe:stripe-android:+'
  • 在您使用Card模型的类中- 删除所有条带导入语句,然后单击Card并确保从 - 导入

"com.stripe.android.model"

“com.stripe.android.model”

Hope that fixes the issue.Thanks

希望能解决问题。谢谢

回答by arslan tahir

monthSpinner.getselecteditem() use this and pass it to the getinteger. Same for the yearSpinner..

monthSpinner.getselecteditem() 使用它并将其传递给 getinteger。同年Spinner..