From 9fee7c413b99b852815fc01c1b71d92a1c1a2533 Mon Sep 17 00:00:00 2001 From: Andrey Paderin Date: Sun, 2 Jun 2019 21:15:17 +0300 Subject: [PATCH] add payment --- lib/yandex-checkout.rb | 8 +- lib/yandex-checkout/payment.rb | 108 +++++++++++++++++---------- lib/yandex-checkout/refund.rb | 46 ++++++++++++ lib/yandex-checkout/response.rb | 22 ++++++ lib/yandex-checkout/version.rb | 2 + spec/fixtures/payment.json | 14 ++++ spec/yandex-checkout/payment_spec.rb | 29 +++++++ spec/yandex-checkout_spec.rb | 6 +- 8 files changed, 188 insertions(+), 47 deletions(-) create mode 100644 lib/yandex-checkout/response.rb create mode 100644 spec/fixtures/payment.json create mode 100644 spec/yandex-checkout/payment_spec.rb diff --git a/lib/yandex-checkout.rb b/lib/yandex-checkout.rb index 56063e0..74f1c13 100644 --- a/lib/yandex-checkout.rb +++ b/lib/yandex-checkout.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true require 'evil/client' +require 'pry' -require 'yandex-checkout/version' -require 'yandex-checkout/payment' -require 'yandex-checkout/refund' +require_relative 'yandex-checkout/version' +require_relative 'yandex-checkout/payment' +require_relative 'yandex-checkout/refund' +require_relative 'yandex-checkout/response' diff --git a/lib/yandex-checkout/payment.rb b/lib/yandex-checkout/payment.rb index 4818247..4580c8f 100644 --- a/lib/yandex-checkout/payment.rb +++ b/lib/yandex-checkout/payment.rb @@ -9,53 +9,83 @@ module YandexCheckout security { basic_auth shop_id, api_key } # http_method { :get } - scope :payments do - operation :status do - option :payment_id, proc(&:to_s) + # scope :payments do + # operation :status do + # option :payment_id, proc(&:to_s) - http_method :get - path { "payments/#{payment_id}" } + # http_method :get + # path { "payments/#{payment_id}" } - response 200 do |_status, _headers, body| - p JSON.parse(*body) - end + # 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 + # # 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 :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) } + + # 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 - operation :create do - option :payment - option :idempotency_key, optional: true - - http_method :post - - 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 - - # 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 :create do + option :payment + option :idempotency_key, optional: true + + http_method :post + + 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 + + # 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 + end + + operation :capture do + end + + operation :cancel do + end + # end end end diff --git a/lib/yandex-checkout/refund.rb b/lib/yandex-checkout/refund.rb index 0487845..d249732 100644 --- a/lib/yandex-checkout/refund.rb +++ b/lib/yandex-checkout/refund.rb @@ -2,6 +2,52 @@ module YandexCheckout class Refund < Evil::Client + operation :get_refund_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 + + # 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 :create do + option :payment + option :idempotency_key, optional: true + + http_method :post + + 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 + + # 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 + end end end diff --git a/lib/yandex-checkout/response.rb b/lib/yandex-checkout/response.rb new file mode 100644 index 0000000..51e8377 --- /dev/null +++ b/lib/yandex-checkout/response.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module YandexCheckout + class Response + extend Dry::Initializer + option :id, proc(&:to_s) + option :status, proc(&:to_s), default: proc { nil } + option :test + + class << self + def build(*res) + body = res.last + binding.pry + 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/version.rb b/lib/yandex-checkout/version.rb index 84351e5..b069fc2 100644 --- a/lib/yandex-checkout/version.rb +++ b/lib/yandex-checkout/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module YandexCheckout VERSION = '0.1.0' end diff --git a/spec/fixtures/payment.json b/spec/fixtures/payment.json new file mode 100644 index 0000000..24e0010 --- /dev/null +++ b/spec/fixtures/payment.json @@ -0,0 +1,14 @@ +{ + "amount": { + "value": "10.00", + "currency": "RUB" + }, + "payment_method_data": { + "type": "bank_card" + }, + "confirmation": { + "type": "redirect", + "return_url": "https://www.merchant-website.com/return_url" + }, + "description": "Заказ №72" +} diff --git a/spec/yandex-checkout/payment_spec.rb b/spec/yandex-checkout/payment_spec.rb new file mode 100644 index 0000000..6100478 --- /dev/null +++ b/spec/yandex-checkout/payment_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +RSpec.describe YandexCheckout::Payment do + describe '#make_payment' do + let(:settings) { { shop_id: 'SHOP_ID', api_key: 'API_KEY' } } + let(:idempotence_key) { 123 } + let(:payment) { described_class.new(settings) } + let(:params) { { payment: File.read('spec/fixtures/payment.json') } } + let(:answer) { { result: 'accepted' } } + + before { stub_request(:any, //).to_return(body: answer.to_json) } + subject { payment.create(params) } + + context 'using ssl via POST:' do + let(:url) { 'https://payment.yandex.net/api/v3/payments' } + + it 'sends a request' do + # binding.pry + subject + expect(a_request(:post, url)).to have_been_made + end + + it 'returns success' do + expect(subject).to be_kind_of YandexCheckout::Response + expect(subject.result).to eq 'accepted' + end + end + end +end diff --git a/spec/yandex-checkout_spec.rb b/spec/yandex-checkout_spec.rb index fbc36d2..7a6ed85 100644 --- a/spec/yandex-checkout_spec.rb +++ b/spec/yandex-checkout_spec.rb @@ -1,9 +1,5 @@ RSpec.describe YandexCheckout do - it "has a version number" do + it 'has a version number' do expect(YandexCheckout::VERSION).not_to be nil end - - it "does something useful" do - expect(false).to eq(true) - end end