diff --git a/lib/yookassa/entity/card.rb b/lib/yookassa/entity/card.rb index 20904e4..27ec0f9 100644 --- a/lib/yookassa/entity/card.rb +++ b/lib/yookassa/entity/card.rb @@ -9,11 +9,11 @@ module Yookassa # First 6 digits of the card’s number (BIN). For payments with bank cards saved in YooMoney # and other services, the specified BIN might not correspond with the last4, expiry_year, expiry_month values. # For payments with bank cards saved in Apple Pay or Google Pay, the parameter contains Device Account Number. - attribute? :first6, Types::String + attribute? :first6, Types::Coercible::Integer # last4 [string, required] # Last 4 digits of the card's number. - attribute :last4, Types::String + attribute :last4, Types::Coercible::Integer # expiry_month [string, required] # Expiration date, month, MM. diff --git a/lib/yookassa/entity/collection.rb b/lib/yookassa/entity/collection.rb new file mode 100644 index 0000000..499f307 --- /dev/null +++ b/lib/yookassa/entity/collection.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative "./types" +require_relative "./payment" +require_relative "./receipt" +require_relative "./refund" + +module Yookassa + module Entity + class Collection < Dry::Struct + attribute :type, Types.Value("list") + attribute? :next_cursor, Types::String + end + + class PaymentCollection < Collection + attribute :items, Types::Array.of(Payment) + end + + class RefundCollection < Collection + attribute :items, Types::Array.of(Refund) + end + + class ReceiptCollection < Collection + attribute :items, Types::Array.of(Receipt) + end + end +end diff --git a/lib/yookassa/entity/payment_methods.rb b/lib/yookassa/entity/payment_methods.rb index 8e31396..4ee1c08 100644 --- a/lib/yookassa/entity/payment_methods.rb +++ b/lib/yookassa/entity/payment_methods.rb @@ -7,16 +7,8 @@ module Yookassa module Entity module PaymentMethod class Base < Dry::Struct - # id [string, required] - # Payment method ID. attribute :id, Types::String - - # saved [boolean, required] - # Saving payment methods allows conducting automatic recurring payments . attribute :saved, Types::Bool - - # title [string, optional] - # Payment method name. attribute? :title, Types::String end @@ -90,18 +82,18 @@ module Yookassa end end - PaymentMethods = PaymentMethod::BankCard | - PaymentMethod::Alfabank | - PaymentMethod::YooMoney | - PaymentMethod::Sberbank | - PaymentMethod::ApplePay | - PaymentMethod::Cash | - PaymentMethod::DirectCarrierBilling | - PaymentMethod::GooglePay | - PaymentMethod::Installments | - PaymentMethod::Qiwi | - PaymentMethod::Tinkoff | - PaymentMethod::Wechat | - PaymentMethod::Webmoney + PaymentMethods = PaymentMethod::BankCard | + PaymentMethod::Alfabank | + PaymentMethod::YooMoney | + PaymentMethod::Sberbank | + PaymentMethod::ApplePay | + PaymentMethod::Cash | + PaymentMethod::DirectCarrierBilling | + PaymentMethod::GooglePay | + PaymentMethod::Installments | + PaymentMethod::Qiwi | + PaymentMethod::Tinkoff | + PaymentMethod::Wechat | + PaymentMethod::Webmoney end end diff --git a/lib/yookassa/entity/receipt.rb b/lib/yookassa/entity/receipt.rb index ba91048..94db3cd 100644 --- a/lib/yookassa/entity/receipt.rb +++ b/lib/yookassa/entity/receipt.rb @@ -17,11 +17,11 @@ module Yookassa # Store's tax system. The parameter is only required if you use several tax systems. # Otherwise, the parameter is not specified. List of possible values: # https://yookassa.ru/en/developers/54fz/parameters-values#tax-systems - attribute? :tax_system_code, Types::Integer + attribute? :tax_system_code, Types::Coercible::Integer # User's phone number for sending the receipt. Set in the ITU-T E.164 format, for example, 79000000000. # !DEPRECATED PARAMETER: we recommend specifying the details in the receipt.customer.phone parameter. - attribute? :phone, Types::String + attribute? :phone, Types::Coercible::String # User's email address for sending the receipt. # !DEPRECATED PARAMETER: we recommend specifying the details in the receipt.customer.email parameter. diff --git a/lib/yookassa/entity/recipient.rb b/lib/yookassa/entity/recipient.rb index bc928c6..b8bdc39 100644 --- a/lib/yookassa/entity/recipient.rb +++ b/lib/yookassa/entity/recipient.rb @@ -5,8 +5,8 @@ require_relative "./types" module Yookassa module Entity class Recipient < Dry::Struct - attribute :account_id, Types::String - attribute :gateway_id, Types::String + attribute :account_id, Types::Coercible::Integer + attribute :gateway_id, Types::Coercible::Integer end end end diff --git a/lib/yookassa/entity/refund.rb b/lib/yookassa/entity/refund.rb index 5a81ec9..379ed81 100644 --- a/lib/yookassa/entity/refund.rb +++ b/lib/yookassa/entity/refund.rb @@ -35,7 +35,7 @@ module Yookassa # description [string, optional] # Reason behind the refund. - attribute :description, Types::String + attribute? :description, Types::String # sources [array, optional] # Information about money held for refunds: the amount to be held and the stores getting the refunds. diff --git a/lib/yookassa/payments.rb b/lib/yookassa/payments.rb index 47623ff..0792941 100644 --- a/lib/yookassa/payments.rb +++ b/lib/yookassa/payments.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "./entity/payment" +require_relative "./entity/collection" module Yookassa class Payments @@ -28,6 +29,11 @@ module Yookassa Entity::Payment.new(**data.merge(idempotency_key: idempotency_key)) end + def list(filters: {}) + data = api.get("payments", query: filters) + Entity::PaymentCollection.new(**data) + end + private attr_reader :api diff --git a/lib/yookassa/refunds.rb b/lib/yookassa/refunds.rb index 43869c0..db7c899 100644 --- a/lib/yookassa/refunds.rb +++ b/lib/yookassa/refunds.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "./entity/refund" +require_relative "./entity/collection" module Yookassa class Refunds @@ -18,6 +19,11 @@ module Yookassa Entity::Refund.new(**data.merge(idempotency_key: idempotency_key)) end + def list(filters: {}) + data = api.get("refunds", query: filters) + Entity::RefundCollection.new(**data) + end + private attr_reader :api diff --git a/spec/fixtures/payment_response.json b/spec/fixtures/payment_response.json index 662b38e..b64a844 100644 --- a/spec/fixtures/payment_response.json +++ b/spec/fixtures/payment_response.json @@ -19,8 +19,8 @@ "saved": false }, "recipient": { - "account_id": "54401", - "gateway_id": "289250" + "account_id": 54401, + "gateway_id": 289250 }, "refundable": false, "test": true diff --git a/spec/fixtures/payments_list_response.json b/spec/fixtures/payments_list_response.json new file mode 100644 index 0000000..f6694fa --- /dev/null +++ b/spec/fixtures/payments_list_response.json @@ -0,0 +1,42 @@ +{ + "type": "list", + "items": [ + { + "id": "22e12f66-000f-5000-8000-18db351245c7", + "status": "waiting_for_capture", + "paid": true, + "amount": { + "value": 2, + "currency": "RUB" + }, + "created_at": "2018-07-18T10:51:18.139Z", + "description": "Заказ №72", + "expires_at": "2018-07-25T10:52:00.233Z", + "metadata": { + + }, + "payment_method": { + "type": "bank_card", + "id": "22e12f66-000f-5000-8000-18db351245c7", + "saved": false, + "card": { + "first6": 555555, + "last4": 4444, + "expiry_month": 7, + "expiry_year": 2022, + "card_type": "MasterCard", + "issuer_country": "RU", + "issuer_name": "Sberbank" + }, + "title": "Bank card *4444" + }, + "recipient": { + "account_id": 100001, + "gateway_id": 1000001 + }, + "refundable": false, + "test": false + } + ], + "next_cursor": "37a5c87d-3984-51e8-a7f3-8de646d39ec15" +} diff --git a/spec/fixtures/refunds_list_response.json b/spec/fixtures/refunds_list_response.json new file mode 100644 index 0000000..4784b02 --- /dev/null +++ b/spec/fixtures/refunds_list_response.json @@ -0,0 +1,16 @@ +{ + "type": "list", + "items": [ + { + "id": "216749f7-0016-50be-b000-078d43a63ae4", + "status": "succeeded", + "amount": { + "value": 1, + "currency": "RUB" + }, + "created_at": "2017-10-04T19:27:51.407Z", + "payment_id": "216749da-000f-50be-b000-096747fad91e" + } + ], + "next_cursor": "416746f8-0016-50be-b000-078d43a4578" +} diff --git a/spec/yookassa/payments_spec.rb b/spec/yookassa/payments_spec.rb index 673f132..c032c76 100644 --- a/spec/yookassa/payments_spec.rb +++ b/spec/yookassa/payments_spec.rb @@ -98,4 +98,28 @@ RSpec.describe Yookassa::Payments do it_behaves_like "returns_payment_object" end + + describe "#list" do + let(:body) { File.read("spec/fixtures/payments_list_response.json") } + + let(:filters) do + { + limit: 20, + "created_at.gt": "2018-07-18T10:51:18.139Z" + } + end + + let(:url) { "https://api.yookassa.ru/v3/payments?limit=20&created_at.gt=2018-07-18T10:51:18.139Z" } + + subject { payment.list(filters: filters) } + + it "sends a request" do + subject + expect(a_request(:get, url)).to have_been_made + end + + it "returns a collection" do + expect(subject).to be_a Yookassa::Entity::PaymentCollection + end + end end diff --git a/spec/yookassa/refunds_spec.rb b/spec/yookassa/refunds_spec.rb index d075d65..68fdd9c 100644 --- a/spec/yookassa/refunds_spec.rb +++ b/spec/yookassa/refunds_spec.rb @@ -51,4 +51,28 @@ RSpec.describe Yookassa::Refunds do it_behaves_like "returns_refund_object" end + + describe "#list" do + let(:body) { File.read("spec/fixtures/refunds_list_response.json") } + + let(:filters) do + { + limit: 20, + "created_at.gt": "2018-07-18T10:51:18.139Z" + } + end + + let(:url) { "https://api.yookassa.ru/v3/refunds?limit=20&created_at.gt=2018-07-18T10:51:18.139Z" } + + subject { refund.list(filters: filters) } + + it "sends a request" do + subject + expect(a_request(:get, url)).to have_been_made + end + + it "returns a collection" do + expect(subject).to be_a Yookassa::Entity::RefundCollection + end + end end