diff --git a/README.md b/README.md index c7bbb91..a3320f9 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,19 @@ params = Cryptopay::CoinWithdrawalParams.new( force_commit: true ) +result = client.coin_withdrawals.create(params) +p result # => +# New amounts example +params = Cryptopay::CoinWithdrawalParams.new( + address: '2Mz3bcjSVHG8uQJpNjmCxp24VdTjwaqmFcJ', + amount: '100.0', + amount_currency: 'EUR', + received_currency: 'BTC', + amount_includes_processing_fee: true, + amount_includes_network_fee: true, + force_commit: true +) + result = client.coin_withdrawals.create(params) p result # => ``` @@ -402,9 +415,8 @@ p result # => ```ruby invoice_id = '7e274430-e20f-4321-8748-20824287ae44' -invoice_refund_params = Cryptopay::InvoiceRefundParams.new(address: '0xf3532c1fd002665ec54d46a50787e0c69c76cd44') -result = client.invoices.create_refund(invoice_id, invoice_refund_params) +result = client.invoices.create_refund(invoice_id) p result # => ``` diff --git a/lib/cryptopay/api/coin_withdrawals.rb b/lib/cryptopay/api/coin_withdrawals.rb index 8032d82..c939346 100644 --- a/lib/cryptopay/api/coin_withdrawals.rb +++ b/lib/cryptopay/api/coin_withdrawals.rb @@ -26,7 +26,7 @@ def commit(coin_withdrawal_id, _opts = {}) end # Create a withdrawal - # To create a withdrawal you need to use either `charged_amount`, `charged_amount_to_send` or `received_amount` parameters in your request body. + # To create a withdrawal you must provide either the legacy amount fields (`charged_amount`, `charged_amount_to_send`, `received_amount`) or the new amount fields (`amount`, `amount_currency`, `amount_includes_processing_fee`, `amount_includes_network_fee`). Mixing legacy and new amount fields in one request is not allowed. # @param coin_withdrawal_params [CoinWithdrawalParams] # @param [Hash] opts the optional parameters # @return [CoinWithdrawalResult] diff --git a/lib/cryptopay/api/invoices.rb b/lib/cryptopay/api/invoices.rb index 8d1e2bf..9d15fec 100644 --- a/lib/cryptopay/api/invoices.rb +++ b/lib/cryptopay/api/invoices.rb @@ -65,19 +65,19 @@ def create_recalculation(invoice_id, invoice_recalculation_params, _opts = {}) end # Create invoice refund - # This endpoint allows you to create invoice refunds. + # This endpoint allows you to create invoice refunds to your cryptocurrency account. # @param invoice_id [String] Invoice ID - # @param invoice_refund_params [InvoiceRefundParams] # @param [Hash] opts the optional parameters + # @option opts [Object] :body # @return [InvoiceRefundResult] - def create_refund(invoice_id, invoice_refund_params, _opts = {}) + def create_refund(invoice_id, opts = {}) path = '/api/invoices/{invoice_id}/refunds' path = path.sub('{invoice_id}', CGI.escape(invoice_id.to_s)) req = Request.new( method: :post, path: path, - body_params: invoice_refund_params + body_params: opts[:body] || {} ) connection.call(req, return_type: InvoiceRefundResult) diff --git a/lib/cryptopay/models/beneficiary.rb b/lib/cryptopay/models/beneficiary.rb index 5c39307..29a584e 100644 --- a/lib/cryptopay/models/beneficiary.rb +++ b/lib/cryptopay/models/beneficiary.rb @@ -40,7 +40,7 @@ def type @attributes[:type] end - # The registered name of the company for a `legal_person` or the full name for a `natural_person`. + # The registered name of the company for a `legal_person` or the full name for a `natural_person`. Note that only following symbols will be stored, regardless of the input value: letters of any language, digits `0-9`, symbols `&` `!` `,` `.` `-` and single spaces. Symbols `\\`, `/`, newlines and tabs will be replaced with spaces. Everything else will be removed, including double spaces and spaces in the end. def name @attributes[:name] end @@ -66,11 +66,6 @@ def invalid_properties properties.push('invalid value for "name", the character length must be smaller than or equal to 100.') end - pattern = Regexp.new(/^[a-zA-Z0-9\s-]+$/) - properties.push("invalid value for \"name\", must conform to the pattern #{pattern}.") if name !~ pattern - - properties.push('invalid value for "address", address cannot be nil.') if address.nil? - address&.invalid_properties&.each do |prop| properties.push("invalid value for \"address\": #{prop}") end diff --git a/lib/cryptopay/models/beneficiary_address.rb b/lib/cryptopay/models/beneficiary_address.rb index 1875a20..292628f 100644 --- a/lib/cryptopay/models/beneficiary_address.rb +++ b/lib/cryptopay/models/beneficiary_address.rb @@ -69,9 +69,7 @@ def post_code def invalid_properties properties = [] - properties.push('invalid value for "country", country cannot be nil.') if country.nil? - - if country.to_s.length > 2 + if !country.nil? && country.to_s.length > 2 properties.push('invalid value for "country", the character length must be smaller than or equal to 2.') end diff --git a/lib/cryptopay/models/coin_withdrawal_params.rb b/lib/cryptopay/models/coin_withdrawal_params.rb index f87e25e..3557ef4 100644 --- a/lib/cryptopay/models/coin_withdrawal_params.rb +++ b/lib/cryptopay/models/coin_withdrawal_params.rb @@ -20,7 +20,11 @@ class CoinWithdrawalParams 'network_fee_level': :network_fee_level, 'force_commit': :force_commit, 'travel_rule_compliant': :travel_rule_compliant, - 'beneficiary': :beneficiary + 'beneficiary': :beneficiary, + 'amount': :amount, + 'amount_currency': :amount_currency, + 'amount_includes_processing_fee': :amount_includes_processing_fee, + 'amount_includes_network_fee': :amount_includes_network_fee }, types: { 'address': :String, @@ -35,7 +39,11 @@ class CoinWithdrawalParams 'network_fee_level': :NetworkFeeLevel, 'force_commit': :Boolean, 'travel_rule_compliant': :Boolean, - 'beneficiary': :Beneficiary + 'beneficiary': :Beneficiary, + 'amount': :Decimal, + 'amount_currency': :String, + 'amount_includes_processing_fee': :Boolean, + 'amount_includes_network_fee': :Boolean }, nullables: %i[ charged_amount @@ -122,6 +130,26 @@ def beneficiary @attributes[:beneficiary] end + # Transaction amount for new calculation + def amount + @attributes[:amount] + end + + # An currency of the transaction amount + def amount_currency + @attributes[:amount_currency] + end + + # Whether the amount includes processing fee + def amount_includes_processing_fee + @attributes[:amount_includes_processing_fee] + end + + # Whether the amount includes network fee + def amount_includes_network_fee + @attributes[:amount_includes_network_fee] + end + # Show invalid properties with the reasons. Usually used together with valid? # @return Array for valid properties with the reasons def invalid_properties diff --git a/lib/cryptopay/require.rb b/lib/cryptopay/require.rb index 6c63557..cca8010 100644 --- a/lib/cryptopay/require.rb +++ b/lib/cryptopay/require.rb @@ -56,7 +56,6 @@ require 'cryptopay/models/invoice_recalculation_result' require 'cryptopay/models/invoice_refund' require 'cryptopay/models/invoice_refund_list_result' -require 'cryptopay/models/invoice_refund_params' require 'cryptopay/models/invoice_refund_result' require 'cryptopay/models/invoice_result' require 'cryptopay/models/invoice_status' diff --git a/lib/cryptopay/version.rb b/lib/cryptopay/version.rb index 73ac684..e0c3380 100644 --- a/lib/cryptopay/version.rb +++ b/lib/cryptopay/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Cryptopay - VERSION = '2.2.0' + VERSION = '3.0.0' end diff --git a/spec/cryptopay/api/coin_withdrawals_spec.rb b/spec/cryptopay/api/coin_withdrawals_spec.rb index eb0d942..3c9a81d 100644 --- a/spec/cryptopay/api/coin_withdrawals_spec.rb +++ b/spec/cryptopay/api/coin_withdrawals_spec.rb @@ -39,6 +39,19 @@ force_commit: true ) + result = client.coin_withdrawals.create(params) + p result # => + # New amounts example + params = Cryptopay::CoinWithdrawalParams.new( + address: '2Mz3bcjSVHG8uQJpNjmCxp24VdTjwaqmFcJ', + amount: '100.0', + amount_currency: 'EUR', + received_currency: 'BTC', + amount_includes_processing_fee: true, + amount_includes_network_fee: true, + force_commit: true + ) + result = client.coin_withdrawals.create(params) p result # => diff --git a/spec/cryptopay/api/invoices_spec.rb b/spec/cryptopay/api/invoices_spec.rb index 0712cc0..af7b561 100644 --- a/spec/cryptopay/api/invoices_spec.rb +++ b/spec/cryptopay/api/invoices_spec.rb @@ -56,9 +56,8 @@ describe '#create_refund' do it 'returns InvoiceRefundResult', :aggregate_failures, vcr: 'api/invoices/create_refund' do invoice_id = '7e274430-e20f-4321-8748-20824287ae44' - invoice_refund_params = Cryptopay::InvoiceRefundParams.new(address: '0xf3532c1fd002665ec54d46a50787e0c69c76cd44') - result = client.invoices.create_refund(invoice_id, invoice_refund_params) + result = client.invoices.create_refund(invoice_id) p result # => expect(result).to be_a(Cryptopay::InvoiceRefundResult) diff --git a/spec/support/cassettes/api/coin_withdrawals/create.yml b/spec/support/cassettes/api/coin_withdrawals/create.yml index 7a92ccb..7aa43f6 100644 --- a/spec/support/cassettes/api/coin_withdrawals/create.yml +++ b/spec/support/cassettes/api/coin_withdrawals/create.yml @@ -63,4 +63,67 @@ http_interactions: encoding: UTF-8 string: '{"data":{"id":"3e877d2f-1232-4ab6-868e-372e2b91afaa","custom_id":null,"customer_id":null,"address":"2Mz3bcjSVHG8uQJpNjmCxp24VdTjwaqmFcJ","network":"bitcoin","txid":null,"status":"pending","charged_amount":"100.0","charged_currency":"EUR","received_amount":"0.00184519","received_currency":"BTC","network_fee":"0.00000216","network_fee_level":"average","fee":"0.0","fee_currency":"BTC","exchange":{"pair":"BTCEUR","rate":"53590.0452","fee":"1.0","fee_currency":"EUR"},"risk":null,"created_at":"2021-10-18T07:09:29+00:00"}}' recorded_at: Mon, 18 Oct 2021 07:09:29 GMT +- request: + method: post + uri: https://business-sandbox.cryptopay.me/api/coin_withdrawals + body: + encoding: UTF-8 + string: '{"address":"2Mz3bcjSVHG8uQJpNjmCxp24VdTjwaqmFcJ","charged_currency":"EUR","received_currency":"BTC","amount":"100.0","amount_currency":"EUR","amount_includes_processing_fee":true,"amount_includes_network_fee":true,"force_commit":true}' + headers: + User-Agent: + - Cryptopay Ruby v0.1.0 + Accept: + - application/json + Date: + - Mon, 18 Oct 2021 07:09:28 GMT + Content-Type: + - application/json + Authorization: + - HMAC :B0UVFSms2R9wWqexFLMCP1huW/U= + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 201 + message: Created + headers: + Date: + - Mon, 18 Oct 2021 07:09:29 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '501' + Connection: + - keep-alive + Cf-Ray: + - 69fff101bc3184a4-LED + Strict-Transport-Security: + - max-age=15552000; includeSubDomains + Cf-Cache-Status: + - DYNAMIC + Content-Security-Policy: + - 'form-action ''self''; frame-ancestors ''self''; base-uri ''self''; default-src + ''none''; script-src ''self''; connect-src ''self''; img-src ''self'' https: + data:; style-src ''self'' ''unsafe-inline'' https:; font-src ''self''; object-src + ''none''; plugin-types application/pdf; child-src ''self''; frame-src ''self''; + media-src ''self''' + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + Vary: + - Accept-Encoding + Server: + - cloudflare + Alt-Svc: + - h3=":443"; ma=86400, h3-29=":443"; ma=86400, h3-28=":443"; ma=86400, h3-27=":443"; + ma=86400 + body: + encoding: UTF-8 + string: '{"data":{"id":"3e877d2f-1232-4ab6-868e-372e2b91afaa","custom_id":null,"customer_id":null,"address":"2Mz3bcjSVHG8uQJpNjmCxp24VdTjwaqmFcJ","network":"bitcoin","txid":null,"status":"pending","charged_amount":"100.0","charged_currency":"EUR","received_amount":"0.00184519","received_currency":"BTC","network_fee":"0.00000216","network_fee_level":"average","fee":"0.0","fee_currency":"BTC","exchange":{"pair":"BTCEUR","rate":"53590.0452","fee":"1.0","fee_currency":"EUR"},"risk":null,"created_at":"2021-10-18T07:09:29+00:00"}}' + recorded_at: Mon, 18 Oct 2021 07:09:29 GMT recorded_with: VCR 6.0.0