UNPKG

@nativescript/square-in-app-payments

Version:

Square In-App Payments SDK for NativeScript

279 lines 9.94 kB
// custom PassKit typings for Square In-App Payments SDK /// <reference path="./typings/objc!PassKit.d.ts" /> import { SquareInAppPaymentsCommon, CardBrand, CardType, CardPrepaidType } from './common'; export { CardBrand, CardType, CardPrepaidType }; function getCardBrand(brand) { switch (brand) { case CardBrand.visa: return 1 /* SQIPCardBrand.Visa */; case CardBrand.mastercard: return 2 /* SQIPCardBrand.Mastercard */; case CardBrand.americanExpress: return 3 /* SQIPCardBrand.AmericanExpress */; case CardBrand.discover: return 4 /* SQIPCardBrand.Discover */; case CardBrand.discoverDiners: return 5 /* SQIPCardBrand.DiscoverDiners */; case CardBrand.jcb: return 6 /* SQIPCardBrand.JCB */; case CardBrand.chinaUnionPay: return 7 /* SQIPCardBrand.ChinaUnionPay */; case CardBrand.squareGiftCard: return 8 /* SQIPCardBrand.SquareGiftCard */; case CardBrand.other: return 0 /* SQIPCardBrand.OtherBrand */; default: throw new Error('Invalid CardBrand'); } } function toCardBrand(brand) { switch (brand) { case 1 /* SQIPCardBrand.Visa */: return CardBrand.visa; case 2 /* SQIPCardBrand.Mastercard */: return CardBrand.mastercard; case 3 /* SQIPCardBrand.AmericanExpress */: return CardBrand.americanExpress; case 4 /* SQIPCardBrand.Discover */: return CardBrand.discover; case 5 /* SQIPCardBrand.DiscoverDiners */: return CardBrand.discoverDiners; case 6 /* SQIPCardBrand.JCB */: return CardBrand.jcb; case 7 /* SQIPCardBrand.ChinaUnionPay */: return CardBrand.chinaUnionPay; case 8 /* SQIPCardBrand.SquareGiftCard */: return CardBrand.squareGiftCard; default: return CardBrand.other; } } function getCardType(type) { switch (type) { case CardType.credit: return 1 /* SQIPCardType.Credit */; case CardType.debit: return 2 /* SQIPCardType.Debit */; case CardType.unknown: return 0 /* SQIPCardType.Unknown */; default: throw new Error('Invalid CardType'); } } function toCardType(type) { switch (type) { case 1 /* SQIPCardType.Credit */: return CardType.credit; case 2 /* SQIPCardType.Debit */: return CardType.debit; case 0 /* SQIPCardType.Unknown */: return CardType.unknown; default: throw new Error('Invalid CardType'); } } function getCardPrepaidType(type) { switch (type) { case CardPrepaidType.notPrepaid: return 1 /* SQIPCardPrepaidType.NotPrepaid */; case CardPrepaidType.prepaid: return 2 /* SQIPCardPrepaidType.Prepaid */; case CardPrepaidType.unknown: return 0 /* SQIPCardPrepaidType.Unknown */; default: throw new Error('Invalid CardPrepaidType'); } } function toCardPrepaidType(type) { switch (type) { case 1 /* SQIPCardPrepaidType.NotPrepaid */: return CardPrepaidType.notPrepaid; case 2 /* SQIPCardPrepaidType.Prepaid */: return CardPrepaidType.prepaid; case 0 /* SQIPCardPrepaidType.Unknown */: return CardPrepaidType.unknown; default: throw new Error('Invalid CardPrepaidType'); } } export class Card { constructor(brand, lastFourDigits, expirationMonth, expirationYear, postalCode, type, prepaidType) { if (brand instanceof SQIPCard) { this._card = brand; } } static fromNative(card) { if (!card) { return null; } const ret = new Card(card); return ret; } get native() { return this._card; } get brand() { return toCardBrand(this._card.brand); } get type() { return toCardType(this._card.type); } get prepaidType() { return toCardPrepaidType(this._card.prepaidType); } get lastFourDigits() { return this._card.lastFourDigits; } get expirationMonth() { return this._card.expirationMonth; } get expirationYear() { return this._card.expirationYear; } get postalCode() { return this._card.postalCode; } toJSON() { return { brand: this.brand, type: this.type, prepaidType: this.prepaidType, lastFourDigits: this.lastFourDigits, expirationMonth: this.expirationMonth, expirationYear: this.expirationYear, postalCode: this.postalCode, }; } } export class CardDetails { get card() { if (!this._card) { this._card = Card.fromNative(this._cardDetails.card); } return this._card; } get nonce() { if (!this._nonce) { this._nonce = this._cardDetails.nonce; } return this._nonce; } static fromNative(cardDetails) { if (!cardDetails) { return null; } const ret = new CardDetails(); ret._cardDetails = cardDetails; return ret; } toJSON() { return { card: this.card, nonce: this.nonce, }; } } export class SquareInAppPayments extends SquareInAppPaymentsCommon { static init() { // noop } get squareApplicationID() { return SQIPInAppPaymentsSDK.squareApplicationID; } set squareApplicationID(appId) { SQIPInAppPaymentsSDK.squareApplicationID = appId; } get canUseApplePay() { return SQIPInAppPaymentsSDK.canUseApplePay; } startCardEntry(options) { const theme = this.createTheme(options?.themeConfig); const controller = NSCSQIPCardEntryViewController.alloc().initWithThemeIsGiftCard(theme, options?.isGiftCard ?? false); controller.onComplete = (cancelled, cardDetails) => { SquareInAppPayments.onComplete?.(cancelled, CardDetails.fromNative(cardDetails)); this.dismiss(); }; controller.onResponse = (cardDetails, complete) => { if (SquareInAppPayments.onResponse) { SquareInAppPayments.onResponse(CardDetails.fromNative(cardDetails), (error) => { if (error && typeof error === 'string') { complete(error); } else { complete(null); } }); } }; controller.collectPostalCode = options?.collectPostalCode ?? true; const navController = UINavigationController.alloc().initWithRootViewController(controller); const rootVC = this.getRootViewController(); if (rootVC) { rootVC.presentViewControllerAnimatedCompletion(navController, true, null); } else { console.error('No root view controller found to present Square Card Entry UI'); } } dismiss(animated = true) { const rootVC = this.getRootViewController(); if (!rootVC) { console.error('No root view controller found to dismiss Square Card Entry UI'); return; } const presentedVC = rootVC?.presentedViewController; if (presentedVC instanceof UINavigationController && presentedVC.viewControllers.firstObject instanceof SQIPCardEntryViewController) { presentedVC.dismissViewControllerAnimatedCompletion(animated, null); } else { console.warn('No card entry controller is currently presented.'); } } createTheme(config) { const theme = SQIPTheme.alloc().init(); if (!config || (config && !Object.keys(config)?.length)) { return theme; } if (config.font) theme.font = config.font; if (config.backgroundColor) theme.backgroundColor = config.backgroundColor; if (config.foregroundColor) theme.foregroundColor = config.foregroundColor; if (config.textColor) theme.textColor = config.textColor; if (config.placeholderTextColor) theme.placeholderTextColor = config.placeholderTextColor; if (config.tintColor) theme.tintColor = config.tintColor; if (config.messageColor) theme.messageColor = config.messageColor; if (config.errorColor) theme.errorColor = config.errorColor; if (config.saveButtonTitle) theme.saveButtonTitle = config.saveButtonTitle; if (config.saveButtonFont) theme.saveButtonFont = config.saveButtonFont; if (config.saveButtonTextColor) theme.saveButtonTextColor = config.saveButtonTextColor; if (config.keyboardAppearance !== undefined) theme.keyboardAppearance = config.keyboardAppearance; if (config.cancelButton) theme.cancelButton = config.cancelButton; return theme; } createApplePayRequest(merchantId, countryCode, currencyCode) { return PKPaymentRequest.squarePaymentRequestWithMerchantIdentifierCountryCodeCurrencyCode(merchantId, countryCode, currencyCode); } performApplePayNonceRequest(payment, callback) { const request = SQIPApplePayNonceRequest.alloc().initWithPayment(payment); request.performWithCompletionHandler((cardDetails, error) => { callback(cardDetails, error); }); } getRootViewController() { const keyWindow = UIApplication.sharedApplication.keyWindow; return keyWindow != null ? keyWindow.rootViewController : undefined; } } //# sourceMappingURL=index.ios.js.map