diff --git a/lib/yandex-checkout.rb b/lib/yandex-checkout.rb index 74f1c13..31705c0 100644 --- a/lib/yandex-checkout.rb +++ b/lib/yandex-checkout.rb @@ -3,7 +3,12 @@ require 'evil/client' require 'pry' -require_relative 'yandex-checkout/version' -require_relative 'yandex-checkout/payment' -require_relative 'yandex-checkout/refund' -require_relative 'yandex-checkout/response' +require 'yandex-checkout/version' +require 'yandex-checkout/payment' +require 'yandex-checkout/refund' +require 'yandex-checkout/response' +require 'yandex-checkout/callable' +require 'yandex-checkout/optional' +require 'yandex-checkout/entity/payment' +# require 'yandex-checkout/entity/amount' +# require 'yandex-checkout/entity/payment_method' diff --git a/lib/yandex-checkout/callable.rb b/lib/yandex-checkout/callable.rb new file mode 100644 index 0000000..9893cb7 --- /dev/null +++ b/lib/yandex-checkout/callable.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module YandexCheckout + module Callable + def call(*args) + new(*args) + end + alias [] call + end +end diff --git a/lib/yandex-checkout/entity/amount.rb b/lib/yandex-checkout/entity/amount.rb new file mode 100644 index 0000000..f3442a7 --- /dev/null +++ b/lib/yandex-checkout/entity/amount.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module YandexCheckout + module Entity + class Amount + extend Dry::Initializer + extend YandexCheckout::Callable + include YandexCheckout::Optional + + option :value, proc(&:to_f), optional: true + option :currency, proc(&:to_s), optional: true + end + end +end diff --git a/lib/yandex-checkout/entity/card.rb b/lib/yandex-checkout/entity/card.rb new file mode 100644 index 0000000..7d5b3b4 --- /dev/null +++ b/lib/yandex-checkout/entity/card.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module YandexCheckout + module Entity + class Card + extend Dry::Initializer + extend YandexCheckout::Callable + include YandexCheckout::Optional + + option :first6 + option :last4 + option :expiry_month + option :expiry_year + option :card_type, proc(&:to_s) + option :source, proc(&:to_s), optional: true + end + end +end diff --git a/lib/yandex-checkout/entity/payment.rb b/lib/yandex-checkout/entity/payment.rb new file mode 100644 index 0000000..2f55a52 --- /dev/null +++ b/lib/yandex-checkout/entity/payment.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require_relative './amount' +require_relative './payment_method' + +module YandexCheckout + module Entity + class Payment < YandexCheckout::Response + option :paid + option :amount, Entity::Amount + option :created_at + option :expires_at, optional: true + option :description, proc(&:to_s), optional: true + option :metadata + option :payment_method, Entity::PaymentMethod + option :test + end + end +end diff --git a/lib/yandex-checkout/entity/payment_method.rb b/lib/yandex-checkout/entity/payment_method.rb new file mode 100644 index 0000000..b5e1c3e --- /dev/null +++ b/lib/yandex-checkout/entity/payment_method.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require_relative './card' + +module YandexCheckout + module Entity + class PaymentMethod + extend Dry::Initializer + extend YandexCheckout::Callable + include YandexCheckout::Optional + + option :type, proc(&:to_s) + option :id, proc(&:to_s) + option :saved + option :card, Entity::Card, optional: true + option :title, proc(&:to_s), optional: true + end + end +end diff --git a/lib/yandex-checkout/entity/refund.rb b/lib/yandex-checkout/entity/refund.rb new file mode 100644 index 0000000..8e9b8f9 --- /dev/null +++ b/lib/yandex-checkout/entity/refund.rb @@ -0,0 +1 @@ +# frozen_string_literal: true diff --git a/lib/yandex-checkout/optional.rb b/lib/yandex-checkout/optional.rb new file mode 100644 index 0000000..9351080 --- /dev/null +++ b/lib/yandex-checkout/optional.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module YandexCheckout + module Optional + private + + def initialize(opts) + super opts.each_with_object({}) { |(key, val), obj| obj[key.to_sym] = val } + end + + def __options__ + @__options__ ||= self.class.dry_initializer.attributes(self) + end + + def respond_to_missing?(name, *) + __options__.respond_to? name + end + + def method_missing(*args, &block) + respond_to_missing?(*args) ? __options__.send(*args, &block) : super + end + end +end diff --git a/lib/yandex-checkout/payment.rb b/lib/yandex-checkout/payment.rb index 4580c8f..6ce1645 100644 --- a/lib/yandex-checkout/payment.rb +++ b/lib/yandex-checkout/payment.rb @@ -8,41 +8,13 @@ module YandexCheckout path { 'https://payment.yandex.net/api/v3' } security { basic_auth shop_id, api_key } - # http_method { :get } - # scope :payments do - # operation :status do - # option :payment_id, proc(&:to_s) - - # http_method :get - # path { "payments/#{payment_id}" } - - # response 200 do |_status, _headers, body| - # p JSON.parse(*body) - # end - - # # Parses json response, wraps it into model with [#error] and raises - # # an exception where [ResponseError#response] contains the model instance - - # response(400, 422) do |(status, *)| - # raise "#{status}: Record invalid" - # end - - # response(404) do |(_status, *)| - # p 'Record Not Found' - # # raise "#{status}: Record not found" - # end - # end - operation :get_payment_info do option :payment_id, proc(&:to_s) http_method :get path { "payments/#{payment_id}" } - # response 200 do |_status, _headers, body| - # p JSON.parse(*body) - # end - response(200) { |*res| p Response.build(*res) } + response(200) { |*res| Entity::Payment.build(*res) } # Parses json response, wraps it into model with [#error] and raises # an exception where [ResponseError#response] contains the model instance diff --git a/lib/yandex-checkout/response.rb b/lib/yandex-checkout/response.rb index 51e8377..85c4134 100644 --- a/lib/yandex-checkout/response.rb +++ b/lib/yandex-checkout/response.rb @@ -10,7 +10,6 @@ module YandexCheckout class << self def build(*res) body = res.last - binding.pry new JSON.parse(body.first) end