Skip to content

Instantly share code, notes, and snippets.

@sime
Last active October 21, 2023 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sime/4b10ac2ddb24ea5457d6ee414e3750ac to your computer and use it in GitHub Desktop.
Save sime/4b10ac2ddb24ea5457d6ee414e3750ac to your computer and use it in GitHub Desktop.
Using the Ruby gem bitpay-sdk with BTCPay Server

Using the Ruby gem bitpay-sdk with BTCPay Server

Background

I haven't done Ruby in 5+ years and this reminder to myself how to kick start the bitpay-sdk gem to work with BTCPay Server.

Is this best practise?

No idea.

References

This guide is a butchered version of bitpay-sdk GUIDE.md

Assumptions

  • Working BTCPay Server
  • A store configured in BTCPay

Create Access Token

In BTCPay Server visit Stores > select Your Store > Access Token > Create a new token. Set Label as Ruby and leave PublicKey blank and press Request pairing. In Pairing permission choose Your Store in Pair to and press Approve.

Grab a the Server initiated pairing code and keep it handy.

Pair Ruby with your BTCPay Server Store

  1. Install gem
  2. Generate private key (keep this!)
  3. Pair and grab token (keep this!)

Generate private key

gem i bitpay-sdk

# OpenSSL v3 via https://github.com/bitpay/bitpay-ruby-keyutils/issues/3
ruby -rbitpay_sdk -e'puts OpenSSL::PKey::EC.generate("secp256k1").to_pem' > btcpay.pem

# OpenSSL v2
#ruby -rbitpay_sdk -e'puts BitPay::KeyUtils.generate_pem' > btcpay.pem

Pair

# pair.rb
require 'bitpay_sdk'

API_URI = 'YOUR_BTCPAY_INSTANCE'

client = BitPay::SDK::Client.new(
    api_uri: API_URI,
    pem: File.read('btcpay.pem'),
    debug: true # Optional
)

puts client.pair_client(pairingCode: 'YOUR_PAIRING_CODE')
$ ruby pair.rb

The output contains a token property. Capture this! If you loose it, you can refresh the 'Access Tokens' page to find it.

Create an invoice

# invoice.rb
require 'bitpay_sdk'

API_URI = 'YOUR_BTCPAY_INSTANCE'
RUBY_TOKEN = 'YOUR_RUBY_ACCESS_TOKEN'

client = BitPay::SDK::Client.new(
    api_uri: API_URI,
    pem: File.read('btcpay.pem'),
    tokens: {
      'merchant' => RUBY_TOKEN, # default facade for get_invoice
      'pos' => RUBY_TOKEN       # default facade for create_invoice
    },
    debug: true # Optional
)

puts client.create_invoice(price: 1, currency: 'EUR')
$ ruby invoice.rb

Get an invoice

Time for a challenge, try fetching the above created invoice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment