Skip to content

Commit 23457fa

Browse files
committed
fix(ios): incorrect usage
fixes: #3 feat: expose number, expMonth, expYear & cvc on cardParams
1 parent dafe2d3 commit 23457fa

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

packages/nativescript-stripe/index.ios.ts

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class BankAccount implements IBankAccount {
155155
}
156156

157157
public static fromNative(bankAccount: STPBankAccount) {
158-
if(bankAccount){
158+
if (bankAccount) {
159159
return new BankAccount(bankAccount);
160160
}
161161
return null;
@@ -309,19 +309,33 @@ export class Stripe {
309309
);
310310
}
311311

312+
313+
private get _rootViewController(): UIViewController | undefined {
314+
const keyWindow = UIApplication.sharedApplication.keyWindow;
315+
return keyWindow != null ? keyWindow.rootViewController : undefined;
316+
}
317+
312318
/*
313319
*. Private
314320
*/
315321

316-
private _getAuthentificationContext(): STPPaymentContext {
317-
const authContext = STPPaymentContext.alloc();
318-
const rootVC = Frame.topmost().currentPage.ios;
322+
private _getAuthentificationContext(): STPAuthenticationContext {
323+
const rootVC = Frame.topmost().currentPage.ios || this._rootViewController;
324+
return STPAuthenticationContextImp.initWithViewController(Utils.ios.getVisibleViewController(rootVC));
325+
}
326+
}
319327

320-
authContext.hostViewController = Utils.ios.getVisibleViewController(rootVC);
321-
authContext.authenticationPresentingViewController = () => {
322-
return authContext.hostViewController;
323-
};
324-
return authContext;
328+
@NativeClass
329+
class STPAuthenticationContextImp extends NSObject implements STPAuthenticationContext {
330+
static ObjCProtocols = [STPAuthenticationContext];
331+
private _vc: UIViewController;
332+
public static initWithViewController(vc: UIViewController) {
333+
const delegate = <STPAuthenticationContextImp>STPAuthenticationContextImp.new();
334+
delegate._vc = vc;
335+
return delegate;
336+
}
337+
authenticationPresentingViewController(): UIViewController {
338+
return this._vc;
325339
}
326340
}
327341

@@ -412,16 +426,59 @@ export class Card implements ICard {
412426
export class CardParams implements ICardParams {
413427
private _cardParams: STPCardParams;
414428

415-
private constructor(
416-
params: STPCardParams
417-
) {
418-
this._cardParams = params;
429+
constructor()
430+
constructor(params: STPCardParams)
431+
constructor(number: string, expMonth: number, expYear: number, cvc: string)
432+
constructor(...args) {
433+
if (args[0] instanceof STPCardParams) {
434+
this._cardParams = args[0];
435+
} else if (args.length === 4) {
436+
this._cardParams = STPCardParams.alloc().init();
437+
this._cardParams.number = args[0];
438+
this._cardParams.expMonth = args[1];
439+
this._cardParams.expYear = args[2];
440+
this._cardParams.cvc = args[3];
441+
} else {
442+
this._cardParams = STPCardParams.alloc().init();
443+
}
419444
}
420445

421446
public static fromNative(card: STPCardParams): CardParams {
422447
return new CardParams(card);
423448
}
424449

450+
get number() {
451+
return this._cardParams.number;
452+
}
453+
454+
set number(number: string) {
455+
this._cardParams.number = number;
456+
}
457+
458+
get expMonth() {
459+
return this._cardParams.expMonth;
460+
}
461+
462+
set expMonth(month: number) {
463+
this._cardParams.expMonth = month;
464+
}
465+
466+
get expYear() {
467+
return this._cardParams.expYear;
468+
}
469+
470+
set expYear(year: number) {
471+
this._cardParams.expYear = year;
472+
}
473+
474+
get cvc() {
475+
return this._cardParams.cvc
476+
}
477+
478+
set cvc(cvc: string) {
479+
this._cardParams.cvc = cvc;
480+
}
481+
425482
get address(): Address {
426483
return Address.fromNative(this._cardParams.address);
427484
}
@@ -703,9 +760,8 @@ export class StripeSetupIntentParams {
703760
native: STPSetupIntentConfirmParams;
704761

705762
constructor(paymentMethodId: string, clientSecret: string) {
706-
this.native = STPSetupIntentConfirmParams.alloc();
763+
this.native = STPSetupIntentConfirmParams.alloc().initWithClientSecret(clientSecret);
707764
this.native.paymentMethodID = paymentMethodId;
708-
this.native.clientSecret = clientSecret;
709765
}
710766
}
711767

0 commit comments

Comments
 (0)