Skip to content

Commit

Permalink
[Bitpay] Include basic auth only when the api isn't v2 (#323)
Browse files Browse the repository at this point in the history
* - v2 api updates

* - Added basic auth to v1 condition test

* - Added header for tracking purposes
  • Loading branch information
Antonio Buedo authored and Pierre Nespo committed Jun 18, 2019
1 parent acb31d5 commit a1089df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/offsite_payments/integrations/bit_pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module OffsitePayments #:nodoc:
module Integrations #:nodoc:
module BitPay
API_V1_URL = 'https://bitpay.com/api/invoice'
API_V1_TOKEN_REGEX = /[0OIl]/
API_V2_TOKEN_REGEX = /^[^0OIl]{44,}$/
API_V2_URL = 'https://bitpay.com/invoices'

mattr_accessor :service_url
Expand All @@ -21,7 +21,7 @@ def self.return(query_string, options = {})
end

def self.v2_api_token?(api_token)
api_token.length >= 44 && !API_V1_TOKEN_REGEX.match(api_token)
API_V2_TOKEN_REGEX.match(api_token)
end

def self.invoicing_url(api_token)
Expand Down Expand Up @@ -84,7 +84,11 @@ def create_invoice
request = Net::HTTP::Post.new(uri.request_uri)
request.content_type = "application/json"
request.body = @fields.to_json
request.basic_auth @account, ''

unless v2_api_token?(@account)
request.add_field("x-bitpay-plugin-info", "BitPay_Shopify_Client_v2.0.1906")
request.basic_auth @account, ''
end

response = http.request(request)
JSON.parse(response.body)
Expand Down
3 changes: 2 additions & 1 deletion test/unit/integrations/bit_pay/bit_pay_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def test_calls_the_v1_api_url_when_the_token_is_v1
fullNotifications: "true",
transactionSpeed: 'high',
token: @token_v1,
}.to_json
}.to_json,
basic_auth: [@token_v1, ''],
).to_return(
status: 200,
body: { id: @invoice_id }.to_json
Expand Down

0 comments on commit a1089df

Please sign in to comment.