@nativescript/square-in-app-payments
Version:
Square In-App Payments SDK for NativeScript
328 lines • 12.4 kB
JavaScript
import { Application, Utils } from '@nativescript/core';
import { SquareInAppPaymentsCommon, CardBrand, CardType, CardPrepaidType } from './common';
export { CardBrand, CardType, CardPrepaidType };
var CheckoutActivity = /** @class */ (function (_super) {
__extends(CheckoutActivity, _super);
function CheckoutActivity() {
var _this = _super.call(this) || this;
return global.__native(_this);
}
CheckoutActivity.prototype.onCreate = function (savedInstanceState) {
var _this = this;
_super.prototype.onCreate.call(this, savedInstanceState);
var layoutId = this.getResources().getIdentifier('activity_checkout', 'layout', this.getPackageName());
var sheetView = this.getLayoutInflater().inflate(layoutId, null);
this.setContentView(sheetView);
var buttonId = this.getResources().getIdentifier('addCardButton', 'id', this.getPackageName());
var addCardButton = sheetView.findViewById(buttonId);
addCardButton.setOnClickListener(new android.view.View.OnClickListener({
onClick: function () {
sqip.CardEntry.startCardEntryActivity(_this, true, CheckoutActivity.DEFAULT_CARD_ENTRY_REQUEST_CODE);
},
}));
};
CheckoutActivity.prototype.onActivityResult = function (requestCode, resultCode, data) {
console.log('onActivityResult', requestCode, resultCode, data);
_super.prototype.onActivityResult.call(this, requestCode, resultCode, data);
sqip.CardEntry.handleActivityResult(data, new sqip.Callback({
onResult: function (result) {
console.log('CardEntryActivityResult:', result);
if (result.isSuccess()) {
var cardResult = result.getSuccessValue();
var card = cardResult.getCard();
var nonce = cardResult.getNonce();
console.log('[CardEntry] Success:', nonce);
}
else if (result.isCanceled()) {
console.log('[CardEntry] Canceled');
var context = Application.android.context;
android.widget.Toast.makeText(context, 'Canceled', android.widget.Toast.LENGTH_SHORT).show();
}
},
}));
};
CheckoutActivity.DEFAULT_CARD_ENTRY_REQUEST_CODE = 1;
return CheckoutActivity;
}(androidx.appcompat.app.AppCompatActivity));
export { CheckoutActivity };
var CardEntryBackgroundHandler = /** @class */ (function (_super) {
__extends(CardEntryBackgroundHandler, _super);
function CardEntryBackgroundHandler() {
var _this = _super.call(this) || this;
return global.__native(_this);
}
CardEntryBackgroundHandler.prototype.handleEnteredCardInBackground = function (cardDetails) {
try {
var nonce = cardDetails.getNonce();
console.log('[CardEntryBackgroundHandler] Received nonce:', nonce);
var response = this.simulateBackend(nonce);
if (response.isSuccessful()) {
return new sqip.CardEntryActivityCommand.Finish();
}
else {
return new sqip.CardEntryActivityCommand.ShowError(response.errorMessage);
}
}
catch (e) {
var context = Application.android.context;
var resources = context.getResources();
var msg = resources.getString(resources.getIdentifier('network_failure', 'string', context.getPackageName()));
return new sqip.CardEntryActivityCommand.ShowError(msg);
}
};
CardEntryBackgroundHandler.prototype.simulateBackend = function (nonce) {
return {
isSuccessful: function () { return true; }, // Simulate success
errorMessage: 'Payment failed on server.',
};
};
CardEntryBackgroundHandler.interfaces = [sqip.CardNonceBackgroundHandler];
return CardEntryBackgroundHandler;
}(java.lang.Object));
export { CardEntryBackgroundHandler };
function getCardBrand(brand) {
switch (brand) {
case CardBrand.visa:
return sqip.Card.Brand.VISA;
case CardBrand.mastercard:
return sqip.Card.Brand.MASTERCARD;
case CardBrand.americanExpress:
return sqip.Card.Brand.AMERICAN_EXPRESS;
case CardBrand.discover:
return sqip.Card.Brand.DISCOVER;
case CardBrand.discoverDiners:
return sqip.Card.Brand.DISCOVER_DINERS;
case CardBrand.jcb:
return sqip.Card.Brand.JCB;
case CardBrand.chinaUnionPay:
return sqip.Card.Brand.CHINA_UNION_PAY;
case CardBrand.squareGiftCard:
return sqip.Card.Brand.SQUARE_GIFT_CARD;
case CardBrand.other:
return sqip.Card.Brand.OTHER_BRAND;
default:
throw new Error('Invalid CardBrand');
}
}
function toCardBrand(brand) {
switch (brand) {
case sqip.Card.Brand.VISA:
return CardBrand.visa;
case sqip.Card.Brand.MASTERCARD:
return CardBrand.mastercard;
case sqip.Card.Brand.AMERICAN_EXPRESS:
return CardBrand.americanExpress;
case sqip.Card.Brand.DISCOVER:
return CardBrand.discover;
case sqip.Card.Brand.DISCOVER_DINERS:
return CardBrand.discoverDiners;
case sqip.Card.Brand.JCB:
return CardBrand.jcb;
case sqip.Card.Brand.CHINA_UNION_PAY:
return CardBrand.chinaUnionPay;
case sqip.Card.Brand.SQUARE_GIFT_CARD:
return CardBrand.squareGiftCard;
default:
return CardBrand.other;
}
}
function getCardType(type) {
switch (type) {
case CardType.credit:
return sqip.Card.Type.CREDIT;
case CardType.debit:
return sqip.Card.Type.DEBIT;
case CardType.unknown:
return sqip.Card.Type.UNKNOWN;
default:
throw new Error('Invalid CardType');
}
}
function toCardType(type) {
switch (type) {
case sqip.Card.Type.CREDIT:
return CardType.credit;
case sqip.Card.Type.DEBIT:
return CardType.debit;
case sqip.Card.Type.UNKNOWN:
return CardType.unknown;
default:
throw new Error('Invalid CardType');
}
}
function getCardPrepaidType(type) {
switch (type) {
case CardPrepaidType.notPrepaid:
return sqip.Card.PrepaidType.NOT_PREPAID;
case CardPrepaidType.prepaid:
return sqip.Card.PrepaidType.PREPAID;
case CardPrepaidType.unknown:
return sqip.Card.PrepaidType.UNKNOWN;
default:
throw new Error('Invalid CardPrepaidType');
}
}
function toCardPrepaidType(type) {
switch (type) {
case sqip.Card.PrepaidType.NOT_PREPAID:
return CardPrepaidType.notPrepaid;
case sqip.Card.PrepaidType.PREPAID:
return CardPrepaidType.prepaid;
case sqip.Card.PrepaidType.UNKNOWN:
return CardPrepaidType.unknown;
default:
throw new Error('Invalid CardPrepaidType');
}
}
export class Card {
constructor(brand, lastFourDigits, expirationMonth, expirationYear, postalCode, type, prepaidType) {
if (brand instanceof sqip.Card) {
this._card = brand;
}
else {
this._card = new sqip.Card(getCardBrand(brand), lastFourDigits, expirationMonth, expirationYear, postalCode, getCardType(type), getCardPrepaidType(prepaidType));
}
}
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.getBrand());
}
get type() {
return toCardType(this._card.getType());
}
get prepaidType() {
return toCardPrepaidType(this._card.getPrepaidType());
}
get lastFourDigits() {
return this._card.getLastFourDigits();
}
get expirationMonth() {
return this._card.getExpirationMonth();
}
get expirationYear() {
return this._card.getExpirationYear();
}
get postalCode() {
return this._card.getPostalCode();
}
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.getCard());
}
return this._card;
}
get nonce() {
if (!this._nonce) {
this._nonce = this._cardDetails.getNonce();
}
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 {
constructor() {
super();
}
static init() {
if (this._didInit) {
return;
}
const instance = io.github.triniwiz.launchpoint.squareiap.CardEntryBackgroundHandler.getShared();
const ref = new WeakRef(this);
instance.setOnResponse(new io.github.triniwiz.launchpoint.squareiap.CardEntryBackgroundHandler.Callback({
response(cardDetails, onComplete) {
const owner = ref.get();
if (owner && owner.onResponse) {
owner.onResponse(CardDetails.fromNative(cardDetails), (error) => {
if (typeof error === 'string') {
onComplete.onError(error);
}
else {
onComplete.onSuccess();
}
});
}
},
}));
sqip.CardEntry.setCardNonceBackgroundHandler(instance);
io.github.triniwiz.launchpoint.squareiap.SquareInAppPayments.setResultListener(new io.github.triniwiz.launchpoint.squareiap.SquareInAppPayments.Callback({
onSuccess(success, cancelled, card, nonce) {
const owner = ref.get();
if (owner && owner.onComplete) {
if (success) {
const cardDetails = new CardDetails();
cardDetails._card = Card.fromNative(card);
cardDetails._nonce = nonce;
owner.onComplete(false, cardDetails);
}
else if (cancelled) {
owner.onComplete(cancelled, null);
}
else {
owner.onComplete(false, null);
}
}
},
}));
Application.android.on(Application.AndroidApplication.activityResultEvent, (args) => {
io.github.triniwiz.launchpoint.squareiap.SquareInAppPayments.handleActivityResult(args.requestCode, args.intent);
});
this._didInit = true;
}
get squareApplicationID() {
return sqip.InAppPaymentsSdk.getSquareApplicationId();
}
set squareApplicationID(app_id) {
sqip.InAppPaymentsSdk.setSquareApplicationId(app_id);
}
startCardEntry(options) {
const activity = Utils.android.getCurrentActivity();
if (!activity) {
console.error('No active Android activity found');
return;
}
if (options?.isGiftCard) {
sqip.CardEntry.startGiftCardEntryActivity(activity, CheckoutActivity.DEFAULT_CARD_ENTRY_REQUEST_CODE);
}
else {
sqip.CardEntry.startCardEntryActivity(activity, options?.collectPostalCode ?? true, CheckoutActivity.DEFAULT_CARD_ENTRY_REQUEST_CODE);
}
}
}
SquareInAppPayments._didInit = false;
//# sourceMappingURL=index.android.js.map