From 083375850419a7ab942eb64cf701fe64e61ca97d Mon Sep 17 00:00:00 2001 From: ZippyDev Date: Sun, 2 Aug 2020 17:10:15 -0600 Subject: [PATCH] add pull-payments endpoint with specs. --- README.md | 8 ++- lib/btcpay/client/api/base.rb | 1 + lib/btcpay/client/api/store_pull_payments.rb | 34 +++++++++++++ lib/btcpay/client/base.rb | 3 +- .../DELETE_delete/1_3_1.yml | 45 +++++++++++++++++ .../GET_all/1_1_1.yml | 47 ++++++++++++++++++ .../POST_create/1_2_1.yml | 49 +++++++++++++++++++ spec/client/api/store_pull_payments_spec.rb | 29 +++++++++++ spec/client/base_spec.rb | 1 + spec/factories/api/store_pull_payments.rb | 12 +++++ 10 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 lib/btcpay/client/api/store_pull_payments.rb create mode 100644 spec/cassettes/BtcPay_Client_Api_StorePullPayments/DELETE_delete/1_3_1.yml create mode 100644 spec/cassettes/BtcPay_Client_Api_StorePullPayments/GET_all/1_1_1.yml create mode 100644 spec/cassettes/BtcPay_Client_Api_StorePullPayments/POST_create/1_2_1.yml create mode 100644 spec/client/api/store_pull_payments_spec.rb create mode 100644 spec/factories/api/store_pull_payments.rb diff --git a/README.md b/README.md index e74b86a..01d5401 100644 --- a/README.md +++ b/README.md @@ -78,11 +78,17 @@ All endpoints are accessed via namespaced Api resource. Example: `client.users.c - ##### Payment Requests: 1. `GET #all(store_id)` -1. `POST #create(payload)` +1. `POST #create(store_id, payload)` 1. `GET #get(store_id, request_id)` 1. `DELETE #delete(store_id, request_id)` 1. `PUT #update(store_id, request_id, payload)` +- ##### Pull Payments: + +1. `GET #all(store_id)` +1. `POST #create(store_id, payload)` +1. `DELETE #delete(store_id, pull_payment_id)` + #### Users: 1. `GET #me` diff --git a/lib/btcpay/client/api/base.rb b/lib/btcpay/client/api/base.rb index 21d1bc9..606240e 100644 --- a/lib/btcpay/client/api/base.rb +++ b/lib/btcpay/client/api/base.rb @@ -10,6 +10,7 @@ module BtcPay require_relative './health' require_relative './lightning_node' require_relative './store_payment_requests' + require_relative './store_pull_payments' require_relative './users' end end diff --git a/lib/btcpay/client/api/store_pull_payments.rb b/lib/btcpay/client/api/store_pull_payments.rb new file mode 100644 index 0000000..484efc5 --- /dev/null +++ b/lib/btcpay/client/api/store_pull_payments.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module BtcPay + module Client + module Api + class StorePullPayments < Base + PATH = '/stores/:store_id/pull-payments' + + # @see https://docs.btcpayserver.org/API/Greenfield/v1/#tag/Pull-payments-(Management)/paths/~1api~1v1~1stores~1{storeId}~1pull-payments/get + def all(store_id, **opts) + client.get(store_path(store_id), options: opts) + end + + alias index all + + # @see https://docs.btcpayserver.org/API/Greenfield/v1/#operation/PaymentRequests_CreatePaymentRequest + def create(store_id, payload, **opts) + client.post(store_path(store_id), payload: payload, options: opts) + end + + # @see https://docs.btcpayserver.org/API/Greenfield/v1/#tag/Pull-payments-(Management)/paths/~1api~1v1~1stores~1{storeId}~1pull-payments~1{pullPaymentId}/delete + def delete(store_id, pull_payment_id, **opts) + client.delete(store_path(store_id, pull_payment_id), options: opts) + end + + protected + + def set_base_path + @base_path = PATH.dup + end + end + end + end +end diff --git a/lib/btcpay/client/base.rb b/lib/btcpay/client/base.rb index e656403..bb88e59 100644 --- a/lib/btcpay/client/base.rb +++ b/lib/btcpay/client/base.rb @@ -93,7 +93,8 @@ module BtcPay def store @store ||= OpenStruct.new( - payment_requests: Api::StorePaymentRequests.new(client: self) + payment_requests: Api::StorePaymentRequests.new(client: self), + pull_payments: Api::StorePullPayments.new(client: self) ) end diff --git a/spec/cassettes/BtcPay_Client_Api_StorePullPayments/DELETE_delete/1_3_1.yml b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/DELETE_delete/1_3_1.yml new file mode 100644 index 0000000..2b04c9a --- /dev/null +++ b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/DELETE_delete/1_3_1.yml @@ -0,0 +1,45 @@ +--- +http_interactions: +- request: + method: delete + uri: http://localhost:49392/api/v1/stores/EiGimqCJGwRGkCL8DbdeArVApkvHZdaZARofiSApKdpQ/pull-payments/3gedGdcqeiFNR5j4SKD6tcD4T1nq + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - application/json + User-Agent: + - btcpay_ruby/0.1.0 + Content-Type: + - application/json + Accept-Encoding: + - deflate, gzip + Authorization: + - token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68 + Host: + - localhost:49392 + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 02 Aug 2020 23:08:06 GMT + Server: + - Kestrel + Content-Length: + - '0' + Referrer-Policy: + - same-origin + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + body: + encoding: UTF-8 + string: '' + recorded_at: Sun, 02 Aug 2020 23:08:06 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/cassettes/BtcPay_Client_Api_StorePullPayments/GET_all/1_1_1.yml b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/GET_all/1_1_1.yml new file mode 100644 index 0000000..6e36744 --- /dev/null +++ b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/GET_all/1_1_1.yml @@ -0,0 +1,47 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:49392/api/v1/stores/EiGimqCJGwRGkCL8DbdeArVApkvHZdaZARofiSApKdpQ/pull-payments + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - application/json + User-Agent: + - btcpay_ruby/0.1.0 + Content-Type: + - application/json + Accept-Encoding: + - deflate, gzip + Authorization: + - token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68 + Host: + - localhost:49392 + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 02 Aug 2020 23:04:44 GMT + Content-Type: + - application/json; charset=utf-8 + Server: + - Kestrel + Content-Length: + - '2' + Referrer-Policy: + - same-origin + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + body: + encoding: UTF-8 + string: "[]" + recorded_at: Sun, 02 Aug 2020 23:04:45 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/cassettes/BtcPay_Client_Api_StorePullPayments/POST_create/1_2_1.yml b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/POST_create/1_2_1.yml new file mode 100644 index 0000000..6803eb0 --- /dev/null +++ b/spec/cassettes/BtcPay_Client_Api_StorePullPayments/POST_create/1_2_1.yml @@ -0,0 +1,49 @@ +--- +http_interactions: +- request: + method: post + uri: http://localhost:49392/api/v1/stores/EiGimqCJGwRGkCL8DbdeArVApkvHZdaZARofiSApKdpQ/pull-payments + body: + encoding: UTF-8 + string: '{"name":"btcpaygem-spec-f0b7d1d54176f6dc19e62ad8f74e5d6e","amount":0.7054181059655215,"currency":"BTC","paymentMethods":["BTC"]}' + headers: + Accept: + - application/json + User-Agent: + - btcpay_ruby/0.1.0 + Content-Type: + - application/json + Accept-Encoding: + - deflate, gzip + Authorization: + - token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68 + Content-Length: + - '128' + Host: + - localhost:49392 + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 02 Aug 2020 23:07:24 GMT + Content-Type: + - application/json; charset=utf-8 + Server: + - Kestrel + Content-Length: + - '290' + Referrer-Policy: + - same-origin + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + body: + encoding: UTF-8 + string: '{"startsAt":1596409645,"expiresAt":null,"id":"3gedGdcqeiFNR5j4SKD6tcD4T1nq","name":"btcpaygem-spec-f0b7d1d54176f6dc19e62ad8f74e5d6e","currency":"BTC","amount":"0.7054181059655215","period":null,"archived":false,"viewLink":"http://localhost:49392/pull-payments/3gedGdcqeiFNR5j4SKD6tcD4T1nq"}' + recorded_at: Sun, 02 Aug 2020 23:07:25 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/client/api/store_pull_payments_spec.rb b/spec/client/api/store_pull_payments_spec.rb new file mode 100644 index 0000000..a0ffa6e --- /dev/null +++ b/spec/client/api/store_pull_payments_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe BtcPay::Client::Api::StorePullPayments, :vcr do + let(:store_id) { 'EiGimqCJGwRGkCL8DbdeArVApkvHZdaZARofiSApKdpQ' } + let(:client) { build(:client) } + subject { described_class.new(client: client) } + + describe 'GET #all' do + let(:response) { subject.all(store_id) } + + it { expect(response).to be_success } + end + + describe 'POST #create' do + let(:payload) { build(:store_pull_payments_payload) } + let(:response) { subject.create(store_id, payload) } + + it { expect(response).to be_success } + end + + describe 'DELETE #delete' do + let(:request_id) { '3gedGdcqeiFNR5j4SKD6tcD4T1nq' } + let(:response) { subject.delete(store_id, request_id) } + + it { expect(response).to be_success } + end +end diff --git a/spec/client/base_spec.rb b/spec/client/base_spec.rb index 09dcdde..8e997c8 100644 --- a/spec/client/base_spec.rb +++ b/spec/client/base_spec.rb @@ -12,6 +12,7 @@ RSpec.describe BtcPay::Client::Base do it { expect(subject.health).to be_a(BtcPay::Client::Api::Health) } it { expect(subject.lightning.node).to be_a(BtcPay::Client::Api::LightningNode) } it { expect(subject.store.payment_requests).to be_a(BtcPay::Client::Api::StorePaymentRequests) } + it { expect(subject.store.pull_payments).to be_a(BtcPay::Client::Api::StorePullPayments) } it { expect(subject.users).to be_a(BtcPay::Client::Api::Users) } end diff --git a/spec/factories/api/store_pull_payments.rb b/spec/factories/api/store_pull_payments.rb new file mode 100644 index 0000000..acbecfb --- /dev/null +++ b/spec/factories/api/store_pull_payments.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :store_pull_payments_payload, class: Hash do + name { "btcpaygem-spec-#{SecureRandom.hex}" } + amount { SecureRandom.rand(0.0..1.0) } + currency { 'BTC' } + paymentMethods { %w[BTC] } + + initialize_with { attributes } + end +end