Sha256: 3d53c3e66f2b3ebba9ad54554563665e39fd5262128c9fce8b22606ad7c80183

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module SolidusBolt
  module BoltHelper
    PUBLIC_KEY_URL = 'https://sandbox.bolttk.com/public_key'
    TOKEN_URL = 'https://sandbox.bolttk.com/token'

    def tokenize_credit_card(credit_card_number:, cvv:)
      request_body = encrypt_request_body(credit_card_number, cvv)
      response = make_request(request_body)
      JSON.parse(decrypt_response_body(response))
    end

    def public_key
      @public_key ||= HTTParty.get(URI.parse(PUBLIC_KEY_URL)).body
    end

    def nonce
      @nonce ||= rand(10**24).to_s.rjust(24, '0')
    end

    def keys
      @keys ||= TweetNaCl.crypto_box_keypair
    end

    def make_request(encrypted_request_body)
      HTTParty.post(
        TOKEN_URL,
        {
          body: JSON.dump(encrypted_request_body),
          headers: {
            'Content-Type' => 'application/json'
          }
        }
      )
    end

    def encrypt_request_body(credit_card_number, cvv)
      message = { cc: credit_card_number, cvv: cvv }.to_json

      payload = TweetNaCl.crypto_box(
        message,
        nonce,
        Base64.decode64(public_key),
        keys[1]
      )

      {
        payload: Base64.encode64(payload),
        nonce: Base64.encode64(nonce),
        public_key: Base64.encode64(keys[0])
      }
    end

    def decrypt_response_body(response)
      parsed_response = JSON.parse(response.body)

      TweetNaCl.crypto_box_open(
        Base64.decode64(parsed_response['payload']),
        Base64.decode64(parsed_response['nonce']),
        Base64.decode64(public_key),
        keys[1]
      )
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
solidus_bolt-0.7.2 spec/support/bolt_helper.rb
solidus_bolt-0.7.1 spec/support/bolt_helper.rb
solidus_bolt-0.7.0 spec/support/bolt_helper.rb
solidus_bolt-0.6.0 spec/support/bolt_helper.rb
solidus_bolt-0.5.0 spec/support/bolt_helper.rb
solidus_bolt-0.4.0 spec/support/bolt_helper.rb
solidus_bolt-0.3.0 spec/support/bolt_helper.rb
solidus_bolt-0.2.0 spec/support/bolt_helper.rb
solidus_bolt-0.1.0 spec/support/bolt_helper.rb
solidus_bolt-0.0.1 spec/support/bolt_helper.rb