Skip to content

Commit a4078e5

Browse files
committed
feat(android): expose number, expMonth, expYear & cvc on cardParams
1 parent 23457fa commit a4078e5

File tree

1 file changed

+82
-37
lines changed

1 file changed

+82
-37
lines changed

packages/nativescript-stripe/index.android.ts

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -787,60 +787,105 @@ export class CardParams implements ICardParams {
787787
_cardParams: com.stripe.android.model.CardParams;
788788
_address = new com.stripe.android.model.Address.Builder;
789789

790-
private constructor(
790+
constructor()
791+
constructor(
791792
params: com.stripe.android.model.CardParams
792-
) {
793-
this._cardParams = params;
793+
)
794+
constructor(number: string, expMonth: number, expYear: number, cvc: string)
795+
constructor(...args) {
796+
if (args[0] instanceof com.stripe.android.model.CardParams) {
797+
this._cardParams = args[0];
798+
this._address = new com.stripe.android.model.Address.Builder();
799+
const address = this._cardParams.getAddress();
800+
if (address) {
801+
this._address.setCity(address.getCity());
802+
this._address.setState(address.getState());
803+
this._address.setLine1(address.getLine1());
804+
this._address.setLine2(address.getLine2());
805+
this._address.setPostalCode(address.getPostalCode());
806+
this._address.setCountry(address.getCountry());
807+
}
808+
} else if (args.length === 4) {
809+
this._cardParams = new com.stripe.android.model.CardParams(args[0], args[1], args[2], args[3]);
810+
this._address = new com.stripe.android.model.Address.Builder();
811+
} else {
812+
this._number = "";
813+
this._expMonth = 0;
814+
this._expYear = 0;
815+
this._cvc = "";
816+
this._cardParams = new com.stripe.android.model.CardParams("", 0, 0);
817+
this._address = new com.stripe.android.model.Address.Builder();
818+
}
819+
}
820+
821+
private _copyAddress(oldParams: com.stripe.android.model.CardParams, newParams: com.stripe.android.model.CardParams) {
794822
this._address = new com.stripe.android.model.Address.Builder();
795-
const address = this._cardParams.getAddress();
823+
const address = oldParams.getAddress();
796824
if (address) {
797825
this._address.setCity(address.getCity());
798826
this._address.setState(address.getState());
799827
this._address.setLine1(address.getLine1());
800828
this._address.setLine2(address.getLine2());
801829
this._address.setPostalCode(address.getPostalCode());
802830
this._address.setCountry(address.getCountry());
831+
this._cardParams.setAddress(newParams.getAddress())
803832
}
804833
}
805834

806835
public static fromNative(card: com.stripe.android.model.CardParams): CardParams {
807-
const address = card.getAddress();
808-
const newCard = new CardParams(card);
809-
if (address) {
810-
newCard._address.setCountry(address.getCountry());
811-
newCard._address.setCity(address.getCity());
812-
newCard._address.setLine1(address.getLine1());
813-
newCard._address.setLine2(address.getLine2());
814-
newCard._address.setPostalCode(address.getPostalCode());
815-
newCard._address.setState(address.getState());
836+
if (!card) {
837+
return undefined;
816838
}
817-
return newCard;
839+
return new CardParams(card);
818840
}
819841

820-
/*
821-
public static fromNativePaymentMethod(pm: com.stripe.android.model.PaymentMethod): Card {
822-
const pmCard = pm.component8(); // card
842+
private _number: string;
843+
get number() {
844+
return this._number;
845+
}
823846

824-
const newCard = new Card();
825-
newCard._last4 = pmCard.component7(); // last4
826-
newCard._brand = GetBrand(pmCard.component1()); // brand
827-
newCard._cardParams = new com.stripe.android.model.CardParams(
828-
null,
829-
pmCard.expiryMonth as any,
830-
pmCard.expiryYear as any
831-
);
832-
// pmCard.component3()
833-
newCard._address.setCountry(pmCard.country);
834-
newCard._cardParams.setAddress(newCard._address.build());
835-
// newCard._cardParams = new com.stripe.android.model.CardParams(
836-
// null,
837-
// pmCard.component4(), // expiryMonth
838-
// pmCard.component5(), // expiryYear
839-
// null).country(pmCard.component3()); // country
847+
set number(number: string) {
848+
this._number = number;
849+
const newParams = new com.stripe.android.model.CardParams(number, this.expMonth, this.expYear, this.cvc);
850+
this._copyAddress(this._cardParams, newParams);
851+
this._cardParams = newParams;
852+
}
840853

841-
return newCard;
854+
private _expMonth: number;
855+
get expMonth() {
856+
return this._expMonth;
857+
}
858+
859+
set expMonth(month: number) {
860+
this._expMonth = month;
861+
const newParams = new com.stripe.android.model.CardParams(this.number, month, this.expYear, this.cvc);
862+
this._copyAddress(this._cardParams, newParams);
863+
this._cardParams = newParams;
864+
}
865+
866+
private _expYear;
867+
get expYear() {
868+
return this._expYear;
869+
}
870+
871+
set expYear(year: number) {
872+
this._expMonth = year;
873+
const newParams = new com.stripe.android.model.CardParams(this.number, this.expMonth, year, this.cvc);
874+
this._copyAddress(this._cardParams, newParams);
875+
this._cardParams = newParams;
876+
}
877+
878+
private _cvc: string;
879+
get cvc() {
880+
return this._cvc
881+
}
882+
883+
set cvc(cvc: string) {
884+
this._cvc = cvc;
885+
const newParams = new com.stripe.android.model.CardParams(this.number, this.expMonth, this.expYear, cvc);
886+
this._copyAddress(this._cardParams, newParams);
887+
this._cardParams = newParams;
842888
}
843-
*/
844889

845890
get address(): Address {
846891
return Address.fromNativeBuilder(this._address);
@@ -896,9 +941,9 @@ export class CreditCardView extends CreditCardViewBase {
896941
}
897942

898943
public createNativeView(): com.stripe.android.view.CardInputWidget | com.stripe.android.view.CardMultilineWidget {
899-
if(android.os.Build.VERSION.SDK_INT < 21){
944+
if (android.os.Build.VERSION.SDK_INT < 21) {
900945
this._widget = new com.stripe.android.view.CardInputWidget(this._context);
901-
}else {
946+
} else {
902947
this._widget = new com.stripe.android.view.CardMultilineWidget(this._context);
903948
}
904949
return this._widget;

0 commit comments

Comments
 (0)