首页 > 解决方案 > 为什么最新版本的 Stripe gradle 不允许 .setName?

问题描述

我正在尝试将 Stripe 付款和最新版本的 gradle 集成为com.stripe:stripe-android:9.2.0. 但是我无法使用该.setName功能添加到卡数据中。为什么会这样?我必须恢复到旧9.1.1版本才能做到这一点。

我的代码目前如下所示:

Card cardToSave = mCardInputWidget.getCard();
                Card.Builder builder = new Card.Builder(cardToSave.getName()); // pass the arguments here
                builder.setName(cardHolderName);
                Card newCard = builder.build();

                Stripe stripe = new Stripe(paymentContext, publishableApiKey );
                stripe.createToken(newCard, publishableApiKey, new TokenCallback() {
                    @Override
                    public void onSuccess(@NonNull Token result) {
                        // Create plan
                    }

                    @Override
                    public void onError(@NonNull Exception e) {
                        // Handle error
                    }
                });

标签: androidstripe-payments

解决方案


当您使用构造函数时,我发现它接收名称作为参数。在最新的库com.stripe:stripe-android:9.2.0中,它改变了以前的方式

private Card(@NonNull Builder builder) {
    this.number = StripeTextUtils.nullIfBlank(normalizeCardNumber(builder.number));
    this.expMonth = builder.expMonth;
    this.expYear = builder.expYear;
    this.cvc = StripeTextUtils.nullIfBlank(builder.cvc);
    this.name = StripeTextUtils.nullIfBlank(builder.name);
    this.addressLine1 = StripeTextUtils.nullIfBlank(builder.addressLine1);
    this.addressLine1Check = StripeTextUtils.nullIfBlank(builder.addressLine1Check);
    this.addressLine2 = StripeTextUtils.nullIfBlank(builder.addressLine2);
    this.addressCity = StripeTextUtils.nullIfBlank(builder.addressCity);
    this.addressState = StripeTextUtils.nullIfBlank(builder.addressState);
    this.addressZip = StripeTextUtils.nullIfBlank(builder.addressZip);
    this.addressZipCheck = StripeTextUtils.nullIfBlank(builder.addressZipCheck);
    this.addressCountry = StripeTextUtils.nullIfBlank(builder.addressCountry);
    this.last4 = StripeTextUtils.nullIfBlank(builder.last4) == null
            ? calculateLast4(number, builder.last4)
            : builder.last4;
    this.brand = asCardBrand(builder.brand) == null
            ? calculateBrand(builder.brand)
            : builder.brand;
    this.fingerprint = StripeTextUtils.nullIfBlank(builder.fingerprint);
    this.funding = asFundingType(builder.funding);
    this.country = StripeTextUtils.nullIfBlank(builder.country);
    this.currency = StripeTextUtils.nullIfBlank(builder.currency);
    this.customerId = StripeTextUtils.nullIfBlank(builder.customer);
    this.cvcCheck = StripeTextUtils.nullIfBlank(builder.cvcCheck);
    this.id = StripeTextUtils.nullIfBlank(builder.id);
    this.tokenizationMethod = StripeTextUtils.nullIfBlank(builder.tokenizationMethod);
    this.metadata = builder.metadata;
}

或者你可以使用 Builder 方法,它也有.name(@Nullable String name)你可以使用的方法。

推荐的方法 是在构建器本身中使用Card.Builder()和传递参数。如果您需要任何帮助,请告诉我

编辑

目前您可以像这样解决以下情况:

Card cardToSave = mCardInputWidget.getCard();
Card.Builder builder = new Card.Builder( String number, Integer expMonth, Integer expYear, String cvc); // pass the arguments here
builder.setName("");
Card newCard = builder.build();
newCard.setName(cardHolderName);
// use this **newCard** for payment

推荐阅读