зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-03 17:55:51 +03:00
add payment
Этот коммит содержится в:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
22
lib/yandex-checkout/response.rb
Обычный файл
22
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
|
||||
@@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YandexCheckout
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
|
||||
14
spec/fixtures/payment.json
поставляемый
Обычный файл
14
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"
|
||||
}
|
||||
29
spec/yandex-checkout/payment_spec.rb
Обычный файл
29
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
|
||||
@@ -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
|
||||
|
||||
Ссылка в новой задаче
Block a user