<img src="https://terminal-api.bitcoinsuisse.com/api/layout/logo/3a02fdeb-e9fd-4dff-810e-fe5cc81ad0c0" alt="logo" width="150"/>

# Bitcoin Suisse Pay plugin

[![npm version](https://badge.fury.io/js/@bitcoin-suisse%2Fpay.svg)](https://www.npmjs.com/package/@bitcoin-suisse/pay)


By implementing the @bitcoin-suisse/pay plugin and by having a Bitcoin Suisse merchant account, you will be able accept crypto currencies as online payment method for goods and services, alongside credit cards 💳 and other payment methods in your checkout process.

## Pre-requisites
1. BTCS Pay  merchant account. [Please sign up here](https://support.bitcoinsuisse.com/hc/en-us/requests/new?ticket_form_id=360003595459).
2. Merchant domiciled in Switzerland or Liechtenstein
3. Legal form GmbH or AG

## Get started

### 1. Installation

Run the following to install the package.

```cli
npm install @bitcoin-suisse/pay
```

### 2. Add script

Either add the script directly, preferably in `<head>` tag.

```html
<script type="text/javascript" src="~/node_modules/@bitcoin-suisse/pay/index.js"></script>
```

Or import the script into a larger codebase.

```javascript
import { CryptoPayments } from '@bitcoin-suisse/pay';
```

### 3. Generate auth-key
The format of the auth-key consists the *Terminal ID* + secret (found on the [Merchant site](https://merchant.bitcoinsuisse.com/settings) below `Customer Details -> API -> SharedSecret`)

**MacOS**  
Run `shasum -a 256 /path/to/file`

**Windows**  
Run `certutil -hashfile /path/to/file SHA256`

Example:  
TerminalID: `987654321`  
Secret: `000000001`  

Format: \<terminalID>\<secret>  
input.txt contains: `987654321000000001`  

`shasum -a 256 input.txt`  
SHA256/auth-key: `360d768442b399b2de3dded67681b3cca5eecde955c44c0b5db63353ec267ae6`

### 4. Instantiate CryptoPayments

At the end of your `<body>` tag, or directly from within a SPA, instantiate an instance of the CryptoPayments lib.

```javascript
/*
    Required:

    terminalId: Terminal ID
    auth: The auth-key enables the callback response as part of the paymentCallbackUrl if this is supplied
    paymentId: Adding this instead of amount and fromCurrency directs to specific payments request
    amount: Payment amount (not valid if paymentId is added)
    fromCurrency: Currency (abbriviation e.g. EUR, USD, CHF) (not valid if paymentId is added)
    -------------------------------------

    Optional:

    paymentCallbackUrl: Platform specific callback (avoid querstrings in URL)
    language: en (default), de, fr, it
    target: Append target to an HTML element (default: appended to body)
    referenceId: Adds a reference note to the payment request
*/
new CryptoPayments({
    terminalId: '123456789',
    auth: '0000...',
    paymentId: '0000000001',
    amount: 1337,
    fromCurrency: 'CHF',
    paymentCallbackUrl: 'https://www.example.com',
    language: 'en',
    target: document.getElementById('terminal-container'),
    referenceId: '12345-12345-12345',
})
    .create()
    .then((data) => {
        console.log('data:', data);
        // This is called when a payment has been successfully interacted with.
        // The terminal view will remove itself.

        // Payment states 👇
        // "crypto-payments-status-PAID":
        // "crypto-payments-status-PAIDOVER":
        // "crypto-payments-status-PAIDLATE":
        // "crypto-payments-status-PAIDPARTIAL":

        // Up to the client if they want to redirect to a certain success URL or update SPA accordingly.
    })
    .catch((errorData) => {
        console.log('data:', errorData);
        // This is called when a payment has an error
        // Terminal view will remove itself

        // Payment states 👇
        // "crypto-payments-status-CANCELED":
        // "crypto-payments-status-EXPIRED":

        // Up to the client if they want to redirect to a certain error URL or update SPA accordingly.
    });
```

## Callback response format

CallbackURL will get the following parameters appended (querystring)

```
?transactionid={payment.Key}
	&orderid={payment.OrderNumberRef}
	&state={state.Enumeration}
	&amount={Convert.ToString(payment.Amount)}
	&currency={payment.FromCurrencyName}
	&date={Date(yyyyMMdd) | ""}
	&time={Date("hhmm") | ""}
	&publickey={payment.CCAddress}
	&paymentType={payment.ToCurrencyName}
	&terminal={payment.TerminalKey}
	&hash={Sha256}
```

## Browser Support

- ES2015
- Commonjs
