2.0.0 • Published 7 months ago

@meikotech/capacitor-square-pos v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@meikotech/capacitor-square-pos

Integrate with Square Payments SDK

Package VersionCapacitor Version
1.X6.X
2.X7.X

Install

Version 1.X is compatible with Capacitor 6.X Version 2.X is compatible with Capacitor 7.X

npm install @meikotech/capacitor-square-pos
npx cap sync

Usage

App Initalisation - App.ts (React example)

CapacitorSquare.initApp({
  applicationId: 'SQUARE-APP-ID',
})
  .then(() => {
    console.log('Square payment ready');
  })
  .catch((error) => {
    console.error(error.message);
  });

Payment flow

import { App } from '@capacitor/app';
import { CapacitorSquare } from '@meikotech/capacitor-square-pos';

// In Hook or component //

useEffect(() => {
  let appUrlListener, failedListener, successListener;

  const setupListeners = async () => {
    appUrlListener = App.addListener('appUrlOpen', (data) => {
      if (data.url.toLowerCase().startsWith('app-url-scheme://callback-url')) {
        CapacitorSquare.handleIosResponse({ url: data.url });
      }
    });

    failedListener = await CapacitorSquare.addListener('transactionFailed', handleFailedPayment);

    successListener = await CapacitorSquare.addListener('transactionComplete', (callback) => {
      handleSuccessPayment(callback);
    });
  };
  setupListeners();

  return () => {
    if (appUrlListener) appUrlListener.remove();
    if (failedListener) failedListener.remove();
    if (successListener) successListener.remove();
  };
}, [handleFailedPayment, handleSuccessPayment]);

const handleSquarePayment = (amount, orderId, squareLocationId) => {
  return new Promise((resolve, reject) => {
    CapacitorSquare.startTransaction({
      totalAmount: 100, // amount in pennies/cents
      currencyCode: 'CAD', // ISO currency code, must be support by square
      allowedPaymentMethods: ['CARD'], // Sqaure TendType: https://developer.squareup.com/docs/api/point-of-sale/android/com/squareup/sdk/pos/ChargeRequest.TenderType.html
      autoReturnTimeout: 4000, // The timeout to set in milliseconds, or AutoReturn.NoTimeout. If you specify a timeout, it must be between 3200 milliseconds and 10000 milliseconds.
      callbackUrl: 'app-url-scheme://callback-url', // see iOS setup
      note: 'Transaction note',
      locationId: 'SQ_location_id', // Use this to enforce payment for the correct business & location. Otherwise any logged in square account will work.
    })
      .then((result) => {
        console.log(result);
        resolve(result);
      })
      .catch((error) => {
        console.error(error);
        reject(error);
      });
  });
};

Note: autoReturnTimeout is only available on Android

Follow these setup steps from square to enable call back to your app: Square Documentation.

acornacorn-jsxajvansi-colorsansi-regexansi-stylesargparsearray-includesarray-unionarray.prototype.flatastral-regexat-least-nodebalanced-matchbrace-expansionbracescall-bindcallsiteschalkchevrotaincolor-convertcolor-namecoloretteconcat-mapcosmiconfigcross-spawndebugdeep-isdefine-propertiesdir-globdoctrineemoji-regexenquirererror-exes-abstractes-to-primitiveescape-string-regexpeslint-config-prettiereslint-import-resolver-nodeeslint-module-utilseslint-plugin-importeslint-scopeeslint-utilseslint-visitor-keysespreeesprimaesqueryesrecurseestraverseesutilsfast-deep-equalfast-globfast-json-stable-stringifyfast-levenshteinfastqfile-entry-cachefill-rangefind-upflat-cacheflattedfs-extrafs.realpathfunction-bindfunctional-red-black-treeget-intrinsicget-stdingithub-sluggerglobglob-parentglobalsglobbygraceful-fshashas-bigintshas-flaghas-symbolshosted-git-infoignoreimport-freshimurmurhashinflightinheritsis-arrayishis-bigintis-boolean-objectis-callableis-core-moduleis-date-objectis-extglobis-fullwidth-code-pointis-globis-negative-zerois-numberis-number-objectis-regexis-stringis-symbolisexejava-parserjs-tokensjs-yamljson-parse-better-errorsjson-parse-even-better-errorsjson-schema-traversejson-stable-stringify-without-jsonifyjson5jsonfilelevnlines-and-columnsload-json-filelocate-pathlodashlodash.clonedeeplodash.mergelodash.truncatelru-cachemerge2micromatchminimatchminimistmsnatural-comparenormalize-package-dataobject-inspectobject-keysobject.assignobject.valuesonceoptionatorp-limitp-locatep-tryparent-moduleparse-jsonpath-existspath-is-absolutepath-keypath-parsepath-typepicomatchpifypkg-dirpkg-upprelude-lsprogresspunycodequeue-microtaskread-pkgread-pkg-upregexp-to-astregexpprequire-from-stringresolveresolve-fromreusifyrun-parallelsemvershebang-commandshebang-regexsignal-exitslashslice-ansispdx-correctspdx-exceptionsspdx-expression-parsespdx-license-idssprintf-jsstring-widthstring.prototype.trimendstring.prototype.trimstartstrip-ansistrip-bomstrip-json-commentssupports-colortabletext-tableto-regex-rangetree-killtsconfig-pathstslibtsutilstype-checktype-festunbox-primitiveuniversalifyuntildifyuri-jsv8-compile-cachevalidate-npm-package-licensewhichwhich-boxed-primitiveword-wrapwrap-ansiwrappyyallistyaml
2.0.0

7 months ago

1.2.25

11 months ago

1.2.24

11 months ago

1.2.22

11 months ago

1.2.21

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago