1.0.0 • Published 6 months ago

longswipe-react-sdk v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Longswipe React SDK

A React SDK for integrating with the Longswipe Merchant Integration API.

Installation

npm install longswipe-react-sdk
# or
yarn add longswipe-react-sdk

Components and Hooks

VoucherRedemption Component

A ready-to-use React component for voucher redemption with a clean UI.

import { VoucherRedemption } from 'longswipe-react-sdk';

function App() {
  const handleSuccess = (message: string) => {
    console.log('Success:', message);
  };

  const handleError = (error: string) => {
    console.error('Error:', error);
  };

  return (
    <VoucherRedemption
      apiKey="your_api_key_here"
      onSuccess={handleSuccess}
      onError={handleError}
    />
  );
}

Props

PropTypeRequiredDescription
apiKeystringYesYour Longswipe API key
onSuccess(message: string) => voidNoCallback function when voucher redemption succeeds
onError(error: string) => voidNoCallback function when an error occurs

useVoucherRedemption Hook

A custom hook for implementing your own voucher redemption UI.

import { useVoucherRedemption } from 'longswipe-react-sdk';

function CustomVoucherRedemption() {
  const {
    verifyVoucher,
    redeemVoucher,
    voucherDetails,
    loading,
    error,
    resetError,
    resetVoucherDetails
  } = useVoucherRedemption({
    apiKey: 'your_api_key_here'
  });

  const handleVerify = async (code: string) => {
    await verifyVoucher(code);
  };

  const handleRedeem = async (amount: number, pin: string) => {
    await redeemVoucher(amount, pin);
  };

  return (
    <div>
      {error && <div>{error}</div>}
      {loading && <div>Loading...</div>}
      {voucherDetails && (
        <div>
          <p>Amount: {voucherDetails.amount}</p>
          <p>Balance: {voucherDetails.balance}</p>
        </div>
      )}
      {/* Your custom UI implementation */}
    </div>
  );
}

Hook Return Values

ValueTypeDescription
verifyVoucher(code: string) => PromiseFunction to verify a voucher code
redeemVoucher(amount: number, pin: string) => PromiseFunction to redeem a verified voucher
voucherDetailsVoucher | nullDetails of the verified voucher
loadingbooleanLoading state for async operations
errorstring | nullError message if any operation fails
resetError() => voidFunction to clear the error state
resetVoucherDetails() => voidFunction to clear voucher details and state

API Usage

Initialize the SDK

import { LongswipeAPI } from 'longswipe-react-sdk';

const longswipe = new LongswipeAPI({
  apiKey: 'your_api_key_here',
  baseUrl: 'https://api.longswipe.com' // optional, defaults to this value
});

Available Methods

Create Invoice

Create a new invoice for a merchant.

const invoice = {
  blockchainNetworkId: "network_id",
  currencyId: "currency_id",
  dueDate: "2025-01-26",
  invoiceDate: "2025-01-26",
  invoiceItems: [
    {
      description: "Item description",
      quantity: 1,
      unitPrice: 100
    }
  ],
  merchantUserId: "merchant_id"
};

const response = await longswipe.createInvoice(invoice);

Fetch Admin Charges

Retrieve the current admin charges configuration.

const charges = await longswipe.fetchAdminCharges();

Fetch Invoices

Retrieve a paginated list of merchant invoices.

const invoices = await longswipe.fetchInvoices(1, 10, 0);

Fetch Supported Currencies

Get a list of supported currencies.

const currencies = await longswipe.fetchSupportedCurrencies();

Fetch Voucher Redemption Charges

Calculate charges for redeeming a voucher.

const charges = await longswipe.fetchVoucherRedemptionCharges({
  amount: 100,
  lockPin: "1234",
  voucherCode: "VOUCHER123"
});

Redeem Voucher

Redeem a voucher.

const result = await longswipe.redeemVoucher({
  amount: 100,
  lockPin: "1234",
  voucherCode: "VOUCHER123"
});

Update Admin Charges

Update the admin charges configuration.

const updated = await longswipe.updateAdminCharges({
  takeChargesFromWallet: true
});

Verify Voucher

Verify a voucher's validity and details.

const voucher = await longswipe.verifyVoucher({
  voucherCode: "VOUCHER123"
});

Development

  1. Clone the repository
  2. Install dependencies:
    npm install
  3. Start the demo:
    npm run demo
  4. Build the package:
    npm run build

Response Types

All API methods return a response with the following structure:

interface MerchantIntegrationResponse<T> {
  code: number;
  message: string;
  status: string;
  data?: T;
}

Error Handling

The SDK includes built-in error handling. All methods return a response object that includes error information when applicable:

try {
  const result = await longswipe.verifyVoucher({
    voucherCode: "INVALID_CODE"
  });
  
  if (result.status === 'error') {
    console.error('Error:', result.message);
    return;
  }
  
  // Handle successful response
  console.log('Voucher details:', result.data);
} catch (error) {
  console.error('Unexpected error:', error);
}

License

MIT

1.0.0

6 months ago