Integrating Cryptocurrency as a Payment Option: Javascript
Tags: javascript, payment, cryptocurrency
Published: 2021-10-05 20:52:32

Considering the extent to which cryptocurrency as a medium of payment has evolved, Soon enough your employers will start requesting to integrate multi-currency as a payment option in their platforms or they already did 😏. In this article, @tkings and I will be sharing a great solution that works for implementing cryptocurrency payment on javascript.


![Payment Process Flowchart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf457gzh6p8mjjxyq2bq.jpeg)

The diagram above shows the flow of how the implementation will serve the merchant and customer . The steps are outlined in the [previous article](https://dev.to/omolade11/integrating-cryptocurrency-as-a-payment-option-html-352i).


###Implementation

[CoinForBarter](https://coinforbarter.com) gives developers a seamless experience, and they support 11 currencies across multiple chains for customers. To proceed, you will need to [sign up](https://dashboard.coinforbarter.com/login) with them and [generate an API KEY](https://dashboard.coinforbarter.com/settings/api) from [CoinForBarter](https://coinforbarter.com). [CoinForBarter](https://coinforbarter.com) like Flutterwave or Stripe provides an implementation method for JavaScript.

An example of how you can implement cryptocurrency in your javascript is below:

```
<form>
<script src="https://checkout.coinforbarter.com/v1/script.js"></script>
<button type="button" onClick="makePayment()">Pay Now</button>
</form>
<script>
        function makePayment() {
            CoinForBarterCheckout({
                publicKey: "xxxxxxxxxxxxxxxxxxxxxxx",
                txRef: "RX1",
                amount: 10,
                currency: "BTC",
                redirectUrl: "http://example.com",
                currencies: [], //accept all currencies if you leave it empty
                meta: {
                    consumer_id: 23,
                },
                customer: "example@gmail.com",
                customerPhoneNumber: "+234xxxxxx",
                customerFullName: "John Doe",
                customizations: {
                    title: "My store",
                    description: "Payment for items in cart",
                    logo: "https://assets.example.com/logo.png",
                },
                onError: (data) => {
                    console.log(data);
                    alert(data.message)
                },
                onSuccess: (data) => {
                    console.log(data);
                }
            });
        }
    </script>
```

This is an [example implementation for php](https://github.com/coinforbarter/examples/tree/main/inline-implementation/php).


###Input Names and Description

* **publicKey**: You can get your publicKey from your CoinForBarter dashboard. It helps identify your transaction.

* **customer**: The email address of the customer to make that payment.

* **customerPhoneNumber**: This is the phone number of the customer. It is an optional field.

* **customerFullName**: This is the full name of the customer. It is an optional field.

* **txRef**: This is a random string to help you remember this transaction.

* **amount**: The amount to charge your customer.

* **currency**: The currency that you have set the amount in.

* **customizations**: An object to display the title and description to be displayed on the payment page.

* **redirectUrl**: A url to redirect the customer to when the transaction ends. This is optional.

* **currencies**: You can have multiple of these fields. It will be a list of the currencies you want to accept for this transaction. It is optional, and if left empty, the customer will pay in any supported currency.

* **onError**: A function to handle the data sent back if transactions fails or is cancelled. If provided, it overrides the redirectUrl.

* **onSuccess** : A function to handle the data sent back if transactions is successful. If provided, it overrides the redirectUrl.


Having inserted a user's public key (Tochukwu's) into the above JavaScript code, the 'Pay Now' button leads to this screen below on your website.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukdq4sx62563527rx4jl.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqmnz17s3txvhda8tfgv.png)


[CoinForBarter](https://coinforbarter.com) has a long list of supported currencies and [helps you get settled in your local bank account automatically](https://dashboard.coinforbarter.com/payouts/settings). The list of supported currencies can be viewed in the note section of the [previous article](https://dev.to/omolade11/integrating-cryptocurrency-as-a-payment-option-html-352i).
In this article, we learnt how to integrate cryptocurrency as a payment option with Javascript. @tkings and I wrote it. In the future, we will be writing on implementing it using React, React Native and API. We will appreciate your comments, and if you have any questions, do not hesitate to hit either [Kingsley](https://twitter.com/tkings_) or [me](https://twitter.com/OmoladeEkpeni) up on Twitter.