From aa28e7bc57dfd3486d2e74ff02b4569da2fa516f Mon Sep 17 00:00:00 2001 From: Andrey Paderin Date: Tue, 11 Jun 2019 01:05:39 +0300 Subject: [PATCH] add capture, cancel methods --- lib/yandex-checkout.rb | 1 + lib/yandex-checkout/entity/confirmation.rb | 16 ++++++ lib/yandex-checkout/entity/payment.rb | 5 +- lib/yandex-checkout/error.rb | 30 +++++++++++ lib/yandex-checkout/payment.rb | 61 +++++++++++++++------- 5 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 lib/yandex-checkout/entity/confirmation.rb create mode 100644 lib/yandex-checkout/error.rb diff --git a/lib/yandex-checkout.rb b/lib/yandex-checkout.rb index 31705c0..1c83db1 100644 --- a/lib/yandex-checkout.rb +++ b/lib/yandex-checkout.rb @@ -10,5 +10,6 @@ require 'yandex-checkout/response' require 'yandex-checkout/callable' require 'yandex-checkout/optional' require 'yandex-checkout/entity/payment' +require 'yandex-checkout/error' # require 'yandex-checkout/entity/amount' # require 'yandex-checkout/entity/payment_method' diff --git a/lib/yandex-checkout/entity/confirmation.rb b/lib/yandex-checkout/entity/confirmation.rb new file mode 100644 index 0000000..b8a0d8b --- /dev/null +++ b/lib/yandex-checkout/entity/confirmation.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module YandexCheckout + module Entity + class Confirmation + extend Dry::Initializer + extend YandexCheckout::Callable + include YandexCheckout::Optional + + option :type, proc(&:to_s), optional: true + option :confirmation_url, proc(&:to_s), optional: true + option :enforce, optional: true + option :return_url, proc(&:to_s), optional: true + end + end +end diff --git a/lib/yandex-checkout/entity/payment.rb b/lib/yandex-checkout/entity/payment.rb index 2f55a52..6dccc8e 100644 --- a/lib/yandex-checkout/entity/payment.rb +++ b/lib/yandex-checkout/entity/payment.rb @@ -2,6 +2,7 @@ require_relative './amount' require_relative './payment_method' +require_relative './confirmation' module YandexCheckout module Entity @@ -9,10 +10,12 @@ module YandexCheckout option :paid option :amount, Entity::Amount option :created_at + option :captured_at, proc(&:to_s), optional: true option :expires_at, optional: true option :description, proc(&:to_s), optional: true - option :metadata + option :metadata, optional: true option :payment_method, Entity::PaymentMethod + option :confirmation, Entity::Confirmation, optional: true option :test end end diff --git a/lib/yandex-checkout/error.rb b/lib/yandex-checkout/error.rb new file mode 100644 index 0000000..ef921e9 --- /dev/null +++ b/lib/yandex-checkout/error.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module YandexCheckout + class Error + extend Dry::Initializer + extend YandexCheckout::Callable + include YandexCheckout::Optional + + option :type, proc(&:to_s) + option :id, proc(&:to_s), optional: true + option :code, proc(&:to_s), optional: true + option :description, proc(&:to_s), optional: true + option :parameter, proc(&:to_s), optional: true + + def error? + type == 'error' + end + + class << self + def build(*res) + body = res.last + new JSON.parse(body.first) + end + + def new(opts) + super opts.each_with_object({}) { |(key, val), obj| obj[key.to_sym] = val } + end + end + end +end diff --git a/lib/yandex-checkout/payment.rb b/lib/yandex-checkout/payment.rb index 6ce1645..14908c8 100644 --- a/lib/yandex-checkout/payment.rb +++ b/lib/yandex-checkout/payment.rb @@ -5,28 +5,29 @@ module YandexCheckout option :shop_id, proc(&:to_s) option :api_key, proc(&:to_s) - path { 'https://payment.yandex.net/api/v3' } + path { 'https://payment.yandex.net/api/v3/payments' } security { basic_auth shop_id, api_key } operation :get_payment_info do option :payment_id, proc(&:to_s) http_method :get - path { "payments/#{payment_id}" } + path { "/#{payment_id}" } response(200) { |*res| Entity::Payment.build(*res) } + response(400, 404) { |*res| Error.build(*res) } # 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(400, 422) do |(status, *)| + # raise "#{status}: Record invalid" + # end - response(404) do |(_status, *)| - p 'Record Not Found' - # raise "#{status}: Record not found" - end + # response(404) do |(_status, *)| + # p 'Record Not Found' + # # raise "#{status}: Record not found" + # end end operation :create do @@ -35,29 +36,53 @@ module YandexCheckout http_method :post - path { 'payments' } + # path { 'payments' } # query { { params: params } } format 'json' headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10' body { payment } - response 200 do |_status, _headers, body| - p JSON.parse(*body) - end - + # response 200 do |_status, _headers, body| + # p JSON.parse(*body) + # end + response(200) { |*res| Entity::Payment.build(*res) } + response(400) { |*res| Error.build(*res) } # 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(400, 422) do |(status, *)| + # raise "#{status}: Record invalid" + # end end operation :capture do + option :payment_id, proc(&:to_s) + option :idempotency_key, optional: true + + http_method :post + + path { "/#{payment_id}/capture" } + + format 'json' + headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10' + + response(200) { |*res| Entity::Payment.build(*res) } + response(400) { |*res| Error.build(*res) } end operation :cancel do + option :payment_id, proc(&:to_s) + option :idempotency_key, optional: true + + http_method :post + + path { "/#{payment_id}/cancel" } + + format 'json' + headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10' + + response(200) { |*res| Entity::Payment.build(*res) } + response(400) { |*res| Error.build(*res) } end - # end end end